how to stop ode45 when one of states reach certain value

[x_dot] =derivative(t, x)
x is states [x(1).....x(4)] I need to stop the integration when x(3) reaches 0.1 when the integration stop record (t)

 Accepted Answer

Opt = odeset('Events', @myEvent);
[T, Y] = ode45(@YourFun, T, Y0, Opt);
function [value, isterminal, direction] = myEvent(T, Y)
value = (Y(3) == 0.1);
isterminal = 1; % Stop the integration
direction = 0;

7 Comments

It doesn't seem that your example should work given the limitation that the root-finder for the event relies on crossing zero...in fact I tried a similar example and matlab complained that "sign" is not defined for types logical
This works
function [value, isterminal, direction] = myEvent(T, Y)
value = (Y(3) == 0.1)-0.5;
isterminal = 1; % Stop the integration
direction = 0;
But I have another related question: is it possible to terminate upon meeting ALL conditions, not just ANY:
function [value, isterminal, direction] = myEvent(T, Y)
value = [(Y(3) == 0.1)-0.5; (x>100)-0.5];
isterminal = ???; % Stop the integration when the original condition is met AND x > 100
direction = [0;0];
The goal is to stop integration for a system approaching steady state by checking the magnitude of a derivative in the system; but it may be approached either from the top or the bottom, and that derivative is not monotonic with time so you can detect false positives, so I want to ensure "enough time" has passed before detecting.
"is it possible to terminate upon meeting ALL conditions, not just ANY"
tolerance = 1e-7;
value = (abs(y(3) - 0.1) < tolerance & abs(x - 100) < tolerance) -0.5;
hi all,
I have this problem, my function is this
function [value, isterminal, direction] = myEvent(t, X)
%err_att = [X(8) X(9) X(10)];
value = ( X(8) == 0.05 ) ; %X(9) X(10) );
isterminal = 1; % Stop the integration
direction = 0;
end
by I have this error :
SWITCH expression must be a scalar or a character vector.
Error in odeevents (line 31)
switch lower(eventFcn)
Error in ode113 (line 148)
odeevents(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);
Error in RLV_assetto (line 62)
[t,X]=ode113('eq_moto_assetto',tt,cond_in,Opt);
please can you help me ??
many many thank for your time and consideration
Please show the code for building Opt
What is T in this senario and how is it defined? Thanks
In the call
[T, Y] = ode45(@YourFun, T, Y0, Opt);
the input T is the timespan to integrate over. It is a vector that must have at least two elements, but may have more. If it has two elements then ode45() will decide by itself what times to output information at; if it has more than two elements then ode45() will output information at the times given in the vector.

Sign in to comment.

More Answers (2)

This is confusing. In matlab help it says: An event occurs when value(i) is equal to zero. All answers in this post make it value = 1 for the event to happen. Are the answers outdated somehow?

1 Comment

This is a valid concern.
value = (X(8) == 0.05 ) and (Y(3) == 0.1) would happen rarely, when the values were bit-for-bit identical to the representation of 0.05 and 0.1 . One bit different in the representation and the condition will not fire. Better is to write x(8) - 0.05 or 0.05 - x(8), and Y(3)-0.1 or 0.1-Y(3) -- zero crossings can be detected for those.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!