# setting a condition on MATLAB ode45 output

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?

• 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));
if(isempty(IE))
fprintf('Threshold not exceeded for tMax = %g\n',tMax);
else
fprintf('Threshold exceeded for tMax = %g\n',tMax);
end
end

end

function [value,isterminal,direction] = myEvent(~,y)
threshold  = 10;
r          = double(abs(y) > threshold);
value      = r;
isterminal = r;
direction  = 0;
end


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?

• 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. mathworks.com/help/matlab/math/… – 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". reproducibleresearch.net/index.php/Main_Page cran.r-project.org/web/views/ReproducibleResearch.html – EngrStudent Jun 9 '13 at 17:10