How to introduce a constraint and stop condition in ode45 ?

조회 수: 38 (최근 30일)
Cola
Cola 2022년 6월 8일
편집: Cola 2023년 7월 20일
There are two questions. The first question is how to introduce a constraint in ode45 for a variable.
For example, the constraint of y(2) is 0<=y(2)<=2 in the following code,and how to introduce it?
The second question is how to introduce a stop condition in ode45?
For example, how to stop the integration of ode45 at two conditions when y(1)<-1 and y(2)<-2 in the following code?
Please give me help. Thanks.
y10=2;
y20=0;
Y0=[y10,y20];
[t, Y] = ode45(@fun,[0:0.1:20], Y0);
plot(t,Y(:,1),'-o',t,Y(:,2),'-o')
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2')
function Dy=fun(t,y)
Dy=zeros(2,1);
Dy(1)=y(2);
Dy(2)=(1-y(1)^2)*y(2)-y(1)
end
  댓글 수: 2
Jan
Jan 2022년 6월 8일
Note: [] is Matlab's operator for concatenations. [0:0.1:20] concates the vector 0:0.1:20 with nothing, so tzhe square brackets are a waste of time only.
Cola
Cola 2022년 6월 9일
You have my gratitude.

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

채택된 답변

Torsten
Torsten 2022년 6월 8일
편집: Torsten 2022년 6월 8일
First question:
You can't prescribe a constraint for a solution variable of an ODE. The ODE variable results automatically from the ODE equation, so you cannot influence it: either it respects your constraint or not.
Second question:
Use the "event" facility of the ODE integrators:
  댓글 수: 1
Cola
Cola 2022년 6월 9일
편집: Cola 2023년 7월 20일
Thank you so much. The second question is solved by the ''event''. The first question is just what you said, but I still want to try it.

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

추가 답변 (1개)

Jan
Jan 2022년 6월 8일
Both is done by an event function. There you can check the values of the current trajectory and stop the integration. If the values should be limited only, you restart the integration from the current location setting the corresponding derivative to 0.
  댓글 수: 1
Cola
Cola 2022년 6월 9일
Thank you for your prompt response. The second question is solved, and I am trying to introduce a constraint by an event function for the first question .

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

카테고리

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