Is there a way to terminate integration when multiple events are triggered simultaneoulsy?

Multiple events can be included in an ODE event function, but it seems there is only capability to detect and act on each event individually. I need a way to be able to terminate integration when multiple events are all triggered simultaneoulsy. E.g., when multiple functions are negative at the same time. Is there any way to do this?

 Accepted Answer

Adjust the isTerminal flag as all(TheExpressions < 0) or as appropriate.

7 Comments

Ok so for this event function,
function [position,isterminal,direction] = appleEventsFcn(t,y)
position = [y(1); y(2)]; % The value that we want to be zero
isterminal = [1; 1]; % Halt integration
direction = [0; 0]; % The zero can be approached from either direction
end
if I wanted to make the integration terminate only when y(1) and y(2) are negative, then the code should be...
function [position,isterminal,direction] = appleEventsFcn(t,y)
position = [y(1); y(2)]; % The value that we want to be zero
isterminal = [all(position < 0); all(position < 0)]; % Halt integration
direction = [0; 0]; % The zero can be approached from either direction
end
?
I doubt that integration will ever be halted with this setting.
It's impossible that y(1) or y(2) equals 0 (which means that an event occurs) and that at the same time both y(1) and y(2) are < 0.
I think I see your point. Would adjusting it so that it checks if all other y's are negative except for the y that the event is occuring for?
function [position,isterminal,direction] = appleEventsFcn(t,y)
position = [y(1); y(2)]; % The value that we want to be zero
isterminal = [y(2) < 0; y(1) < 0]; % Halt integration
direction = [-1; -1]; % The zero can be approached from either direction
end
Note that I also changed the direction to make it clearer that I want the integration to stop the instant that both y's are negative.
The solver will stop if
((y(1) = 0 and y(1) comes from the positive region) and y(2) < 0)
or
((y(2) = 0 and y(2) comes from the positive region) and y(1) < 0).
If this is what you want, you can try.
This seems to be working as expected. Thanks
Thank you for your help Torsten, I was very lost! I had it flipped, but we good now.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!