# 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

-