Event not triggering when condition met with ODE45
Show older comments
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
Walter Roberson
on 19 Apr 2019
Edited: Walter Roberson
on 19 Apr 2019
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.
Daniel
on 19 Apr 2019
Walter Roberson
on 19 Apr 2019
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.
Answers (0)
Categories
Find more on Assembly 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!