ODE event: Is there a counter ?

26 次查看(过去 30 天)
Hi, Is there a way to terminate after an event occurs a certain number of times? I am thinking of using a counter in the ODE event function. But I am not sure how to pass it as an argument to the event function.
Thank you,
Sid

采纳的回答

Siddhartha Harsha Ommi
I am marking my naive approach commented above as the answer for now.

更多回答(1 个)

Kiran Felix Robert
Kiran Felix Robert 2021-3-22
Hi Siddhartha,
You don’t have to pass the counter as an argument to the event function, you can define the count variable as a global variable (persistent) and increment/decrement it, while not terminating the integration, until the control arrives at a count to terminate the integration.
Refer the answer here (syntax example) and ODE Event Location documentation.
The following snippet can be used as an example, for 10 events.
function [value,isterminal,direction] = Eventsfcn(t,y)
persistent count;
if isempty(count)
count = 10;
end
if count > 1
% Your code here
count = count - 1;
isterminal = 0; % Do not stop the integration
else
% Your Code here
isterminal = 1; % Stop the integration
end
  2 个评论
Siddhartha Harsha Ommi
Hi Kiran, But this seems to only keep track of the number of times the Eventsfcn() is called. Am I wrong ?
My question was different. Probably same as the question in the link you have provided. So to make it more clear, if the Eventsfcn() has possibility to track multiple events (say 1] the solution itself reaching a value, 2] the slope and 3] the curvature of the solution vanishing approaching from either positive or negative values), how do I track the number of times one of those events (or each of the events) has occured and eventually stop the integration after a particular limit-count. Below I have written the base code without this feature. Any help is appreaciated.
function [value,isterminal,direction] = Eventsfcn(t,y)
value = [(y(1)-0.5); y(2); y(3)]; % for solution=0.5; slope = 0.0; curvature = 0.0;
isterminal = [0; 0; 0]; % do not terminate in any such event
direction = [0; 0; 0] % such events could occur from either directions
% Now I need a count on let's say how many times the curvature, y(3), has
% vanished approaching from either side. And then stop integration when
% this happens 10 times.
end
Thank you,
Sid
Siddhartha Harsha Ommi
编辑:Siddhartha Harsha Ommi 2021-3-28
To update, I have tried the following naive approach which seemed to work for simpler cases that involve less significant digits. Also, I am not sure how to adapt this to also look at the particular direction from which the event occurs. Probably need to take inspiration from the source code.
function [value,isterminal,direction] = Eventsfcn(t,y)
value = [(y(1)-0.5); y(2); y(3)]; % for solution=0.5; slope = 0.0; curvature = 0.0;
direction = [0; 0; 0] % such events could occur from either directions
persistent cnt;
if isempty(cnt)
cnt = 0;
end
if y(2)==0 % vanishing curvature. Could be other criteria aswell.
cnt = cnt + 1;
end
if cnt > 9 % if vanishes 10 times change the terminal
isterminal = [0; 0; 1];
else
isterminal = [0; 0; 0]; % default terminal
end
end
However, in my case I am working with a higher number of significant digits and this seemed to be working but only errattically.
Sid

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by