# odeEvent doesn't terminate the integration process?

4 views (last 30 days)
Oded Ben Tsedek on 15 Aug 2022
Edited: Torsten on 16 Aug 2022
Hello
I have a problem with odeEvents. I'm using logical expressions for the values.
The first value (double(theta_1~=theta_2)) starts of is 0, so the function skips it. But when the next time the value is zero, nothing happens.
Can someone help me understand what's wrong? I saw that it is possiable to use logical values for interupption if you put a double on it.
function [value,isterminal,direction]=events_impact(t,X)
% events_stick find whenever the system changes from stick event to slip
% and if the joint hip fell to the ground
global l
theta_1 = X(3);
theta_2 = X(4);
y = X(2);
% Height of the swing foot from the ground
%y_p = abs(theta_1-theta_2);
% Height of the hip joint from the ground
y_h = y+2*l*cos(theta_1);
value = [double(theta_1~=theta_2);double(y_h>0)];
isterminal = [1;1];
direction = [0;0];
end

Torsten on 15 Aug 2022
Edited: Torsten on 16 Aug 2022
The functions you can prescribe as "value" should be continous. Using logicals leads to step functions that are not appropriate here. I'm quite convinced that a case where logicals are used is not an official MATLAB example.
The first value (double(theta_1~=theta_2)) starts of is 0, so the function skips it. But when the next time the value is zero, nothing happens.
For that MATLAB could detect the next sign change, it had to exactly hit the t-value where theta1 == theta2. This is very improbable. And the sign of the expression also does not change (it remains non-negative).

Walter Roberson on 16 Aug 2022
The event functions are typically only executed at the end of a step, after the step has succeeded. The event functions are executed passing in whatever the current values are of time and the boundary conditions. MATLAB compares the sign() of the value output of the event function to the sign() of the previous iteration; if the sign() changes and the "direction" indicates it needs to be paid attention to, then MATLAB uses the value returned to try to guess how far back to project. It then proceeds with a search near the boundary trying to find the place where all of the values (that are being considered) are close enough to 0 within configured precision. It then emits an output at that location. If isterminal is marked for that condition, then integration is stopped.
MATLAB does not analyze the code in the event function and attempt to construct a proof that the condition could not be true between the previous and current conditions. So for example if the event function contains a line
value = x*besselj(2.1, x) == 7.83
then MATLAB will not attempt to use any kind of reasoning about the properties of the besselj function to prove that it is impossible for that condition to be true between the previous step and the current step. MATLAB just evaluates the event function numerically.
Now if you instead coded as
value = x*besselj(2.1, x) - 7.83
then if sign() of that changed then MATLAB can use a binary root finder to determine the x at which value becomes 0 (to within numeric precision)

R2021b

### Community Treasure Hunt

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

Start Hunting!