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?

  • 6
    $\begingroup$ You can attach your own monitoring function to ode45, have you consulted the documentation for the routine? $\endgroup$ Feb 26, 2013 at 14:42

2 Answers 2


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;

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?

  • $\begingroup$ This is overly complicated when ode45 already has this functionality implemented, as Aron's comment states. $\endgroup$ Jun 5, 2013 at 20:37
  • $\begingroup$ Here is a link with what I think are more details on the built-in. mathworks.com/help/matlab/math/… $\endgroup$ Jun 5, 2013 at 20:48
  • $\begingroup$ 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". reproducibleresearch.net/index.php/Main_Page cran.r-project.org/web/views/ReproducibleResearch.html $\endgroup$ Jun 9, 2013 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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