Razvan
Razvan

Reputation: 181

Setting up two events with ode45

Could someone please explain what am I doing wrong when setting up an ode45 event this way?

When I tried running the code it would reach line 6, however it would not stop computing the ode regardless that I set isTerminal=[1,1] on line 7, instead it continues until the end: when the second event z(3) reaches 0 at which point it reaches line 10 and depending on whether I set isTerminal=[1,1] or isTerminal=[0,1] then it stops the computation with isTerminal(1) as the triggered event and isTerminal(2) in the later case.

function [value, isTerminal, direction] = myEvent(t, z)
posObst=100;
dist = z(1)-posObst;

if dist == 0
   value = [dist; z(3)];
   isTerminal = [1; 1];
   direction = [0; -1];
else 
   value = [dist; z(3)];
   isTerminal = [0; 1]; 
   direction = [-1 ; -1];
end
end

My question is why doesn't the ode stop the computations once it reaches line 7? This is the most minimalistic example I could come up with. If there is anything else I should add please let me know.

Upvotes: 0

Views: 126

Answers (1)

Razvan
Razvan

Reputation: 181

I found what the problem was. It arises from the line

if dist==0

Should have defined a tolerance and then made the floating point comparison like so:

tol = 1e-4
if dist <= tol

Upvotes: 1

Related Questions