Event detection fails in matlab

5 次查看(过去 30 天)
9times6
9times6 2021-6-29
I am attempting to solve an ode using event detection. I need matlab to choose either option 1 or option 2 based on two initial conditions. Lets say if y0(1)~=y0(2); then it should solve option 2 and if y0(1)=y0(2) matlab should solve option 1. . However, whatever the initial condition, Matlab always enters the first ode function.
function [value,isterminal,direction] = myeventsys1(t,y) %option 1
z=double((y(1)-y(3))==0);
value=z;
isterminal = 1;
direction = [];
function [value,isterminal,direction] = myeventsys2(t,y) % option 2
z=double((y(1)-y(3))~=0);
value=z;
isterminal = 1;
direction = [];
For example: If y(1)=1 and y(3)=2. In this case Matlab enters the ode45 function corresponding to option 1 because it appears first in the program. Then it checks the condition: z=double((y(1)-y(3))==0); This condition being false, z takes value 0 and MatLab should terminate the integration right there (since value=0 implies event has occured) and enter the next ode45 function in the code. However, MatLab gets stuck in the first ode45 and then randomly jumps to next. Can someone please help to clarify what is happening here?
Thanks in advance!!

回答(1 个)

Walter Roberson
Walter Roberson 2021-6-29
Events that are initially true are not detected until they have been false at least once.
  4 个评论
Dipak Panigrahy
Dipak Panigrahy 2021-6-30
Can it check y(1)> y(2)? here y refers to displacement of two mass. i need to check y(1)>y(2) to check event occurs. in this case, can i use event detection algorithm?
Walter Roberson
Walter Roberson 2021-6-30
It can check y(1)>y(2), but if that happens to be true right at the start, then it will not terminate integration -- not unless at some point in the future y(1)>y(2) becomes false.
Imagine that you are programming in a bouncing ball. The ball has to reverse direction each time the height becomes 0, so you program in an event function. But suppose the ball starts at height 0 (but with an upward velocity, such as if it were kicked from below.) If event functions fired immediately at the initial conditions then ode*() would see that the height was 0 and would think it was time to terminate integration and let the velocity be reversed. And then you submit that configuration and the height is still 0 but the velocity is aimed downward, and ode45() would see height 0 and decide it needed to terminate the integration and reverse velocity... and round and round it goes, never getting anying done. Whereas if you ignore stopping because of the termination condition on the very first event, you give an opportunity for something to happen.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by