Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to solve a system of differential equations using ode45 , for (many) parameters in MATLAB to investigate the behavior of solutions with coefficients of the equation .I want to find the parameters for which the solution absolute value doesn't get larger than some given number . The script has a for loop and solves the system for given parameters one after another(in some limited time span (independent variable is time)) .

Now,my question is that "how can I set a condition on ode45 to "detect" automatically if the solution for some parameter gets larger than that (given) value", so I can automatically find those parameters for which the solution gets larger than the given value, by running the for loop once?

share|improve this question
You can attach your own monitoring function to ode45, have you consulted the documentation for the routine? – Aron Ahmadia Feb 26 '13 at 14:42

As described in the documentation, one can set the 'Event' property using odeset. The following (in an m-file named ode45EventTest.m) provides a barebones example of your use case, with the trivial ODE $\frac{dx}{dt} = 1, x(0)=0$:

function ode45EventTest
for tMax = [2, 20]
    [~,~,~,~,IE] = ode45(@(t,y) 1, [0 tMax],0,odeset('Event',@myEvent));
        fprintf('Threshold not exceeded for tMax = %g\n',tMax);
        fprintf('Threshold exceeded for tMax = %g\n',tMax); 


function [value,isterminal,direction] = myEvent(~,y)
threshold  = 10;
r          = double(abs(y) > threshold);
value      = r;
isterminal = r;
direction  = 0;
share|improve this answer

I would put a switch in the target function, and allow it to write to a file the parameters of interest then switch the output falsely such that the ode45 truncates the run, possibly with a NaN or other.

Do you need some pseudocode? If so could you provide pseudocode that has some of your info structure and a derivative function that you find useful?

share|improve this answer
This is overly complicated when ode45 already has this functionality implemented, as Aron's comment states. – Godric Seer Jun 5 '13 at 20:37
Here is a link with what I think are more details on the built-in.… – EngrStudent Jun 5 '13 at 20:48
Although it is nice to know the built-in, it is a little bit of a trap. The company I work for is a JMP-using house, so my MatLab doesn't translate. If I use a specialized switch built into a built-in then it is hard to make code that others can find useful. Why make one-use code ten thousand times? As I get older I find my sense of accomplishment from such things decreases and I agree more with the ideas of "reproducible research". – EngrStudent Jun 9 '13 at 17:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.