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

조회 수: 14 (최근 30일)
shir hartman
shir hartman 2022년 8월 17일
댓글: shir hartman 2022년 8월 20일
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
Torsten 2022년 8월 17일
편집: Torsten 2022년 8월 17일
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
  댓글 수: 3
Torsten
Torsten 2022년 8월 17일
There is no sign-change in dx/dt from positive to negative. So I changed the code above to stop when
dx/dt < 1e-4.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 8월 17일
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
shir hartman
shir hartman 2022년 8월 17일
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)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by