Event not triggering when condition met with ODE45

I am trying to make my ode45 stop solving once a certain condition is met using an event function. I can see that my condition is met many times through the process, yet the integration continues (I did try seeing if it should be the negation of my stated condition, but it's still the same so not sure there). The condition is that g should be equal to some value I've called condition, where it stops once this occurs. Are there any errors in this code that I'm not seeing?
%% Solver
trebuchetparameters % load parameters
Opt = odeset('Events',@myEvent);
[t, y] = ode45(@trebuchet,[0 tspan], y0, Opt);
theta = y(:, 1); % theta
theta_dot = y(:,2); % theta'
phi = y(:, 3); % phi
phi_dot = y(:, 4); % phi'
function [yprime] = trebuchet(t, y)
trebuchetparameters % load parameters
trebuchetvariableshorthand % bunch of variables
yprime=zeros(4,1);
yprime(1) = y(2);
yprime(3) = y(4);
yprime(2) = iota_1;
yprime(4) = iota_1.*iota_2+iota_3;
end
function [value, isterminal, direction] = myEvent(t, y)
trebuchetparameters, trebuchetvariableshorthand, trebuchetevent % parameters, variables, condition
value = abs(g - condition) < tolerance
isterminal = 1;
direction = 0;
end

5 Comments

Daniel
Daniel on 19 Apr 2019
Edited: Daniel on 19 Apr 2019
If needed, here attached are the files of variables that I call in the code. The code I pasted is in trebuchetode.m. I haven't been able to make much more progress on this problem.
With the files you have provided (which reverses the < tolerence to > tolerance ), g is never anywhere near the condition value and condition gets further and further away, so the event is never triggered.
I didn't notice that was changed, apologies. In any case, even if I have some statement like "value = 0 == 1" or "g < condition" (or whatever should constantly trigger an event, not sure if it's when the statement is true or false), my integration persists. Any ideas there?
You are using direction = 0, which specifies that the condition should trigger on a crossing of 0 in either direction. With your g always having the same relationship to condition within tolerance, your logical test is always returning the same value, either 0 or 1 (depending on the > or < that you use), and that never crosses 0: it is either a constant 1 or a constant 0.
Daniel
Daniel on 19 Apr 2019
Edited: Daniel on 19 Apr 2019
Oh okay, I didn't realize that what was how that worked. I was able to alter my condition and fix it with that realization in mind, thank you very much; I appreciate your time.

Sign in to comment.

Answers (0)

Products

Release

R2018b

Asked:

on 18 Apr 2019

Edited:

on 19 Apr 2019

Community Treasure Hunt

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

Start Hunting!