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
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.
Daniel
2019년 4월 19일
Walter Roberson
2019년 4월 19일
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.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Assembly에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!