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

Daniel
Daniel 2019년 4월 19일
편집: Daniel 2019년 4월 19일
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.
Walter Roberson
Walter Roberson 2019년 4월 19일
편집: Walter Roberson 2019년 4월 19일
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 2019년 4월 19일
편집: Daniel 2019년 4월 19일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

제품

릴리스

R2018b

질문:

2019년 4월 18일

편집:

2019년 4월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by