# I want to do a stop condition in ode45 that he demands dx=0

2 views (last 30 days)
shir hartman on 17 Aug 2022
Commented: shir hartman on 20 Aug 2022
this is my code And I don't understand why it doesn't work :(
AnonFun=@(t,x)(5*x-1*x^2);
Opt=odeset("Events",@myEvent);
[t,x]=ode45(AnonFun,[0,5],1,Opt);
plot(t,x)
function [value, isterminal, direction] = myEvent(t,x)
value = diff(x)==0;
isterminal = 1; % Stop the integration
direction = -1;
end
Im trying to get this : Torsten on 17 Aug 2022
Edited: Torsten on 17 Aug 2022
AnonFun=@(t,x)(5*x-1*x^2);
Opt=odeset("Events",@(t,x)myEvent(t,x,AnonFun));
[t,x]=ode45(AnonFun,[0,5],1,Opt);
plot(t,x) function [value, isterminal, direction] = myEvent(t,x,AnonFun)
value = AnonFun(t,x)-1.0e-4;
isterminal = 1; % Stop the integration
direction = -1;
end
shir hartman on 20 Aug 2022
thanks!!!

### More Answers (1)

Walter Roberson on 17 Aug 2022
You need to carry around one more level of derivatives.
AnonFun = @(t, x) [x(2); 5-2*x(1)];
Opt = odeset("Events", @myEvent);
[t, x] = ode45(AnonFun, [0,5], [1, 5], Opt);
plot(t, x(:,1)) function [value, isterminal, direction] = myEvent(t, x)
value = x(2);
isterminal = 1; % Stop the integration
direction = -1;
end
##### 1 CommentShowHide None
shir hartman on 17 Aug 2022
Hi, first of all thanks.
But what I wanted to get was the orange graph (which is the function) - simply that it will stop calculating from the moment the derivative resets (when the function is constant) 