Event function for ODE solver with binary values

조회 수: 2 (최근 30일)
Nicolas Mira Gebauer
Nicolas Mira Gebauer 2021년 9월 17일
댓글: Nicolas Mira Gebauer 2021년 9월 20일
Is there any reason why creating an event function which can only take values of 1 or 0 a bad idea? If so, why?
I ask because, as I understand it, the event function, by definition, should recognize when the variable "crosses" 0, but technically if the outputs of my function are only 1 or 0, then it would never "cross" zero. However, I have been doing this and so far I don't think I have found any issues, except one time where my example didn't work and I never understood why.
As an example, the event function that I am using is the following:
function [value,isterminal,direction] = zeroevents(t,x,D)
%The event function should also accept yp (or xp) for the ode15i solver
isterminal = 1;
direction = 0;
insideD = D(t,x);
value = 1-insideD;
end
Where D(t,x) is a piecewise function with the following structure:
function out1 = D(t,x)
if x >= something
out1 = 1.0;
else
out1 = 0.0;
end
I hope the question is clear enough.
Thanks

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 17일
편집: Walter Roberson 2021년 9월 17일
It is not illegal, but it makes it more difficult to find the boundary.
In the case of x < something resulting in a 0 that is then effectively negated to get a termination request, another way of writing that would be
value = something - x;
Exception: this might not produce exactly the same behaviour right at x == something. In such a case, either adding or subtracting eps(realmin) would change the behaviour, depending which side you want the case to follow.
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 9월 20일
Each condition being or'd together can be written as a separate condition. For example,
out1 = 0;
if ((z5 <= z4) | (0.0 < z3)) & (z6 == 0.0))
out1 = 1;
end
if (((z4 < z5) & (z6 == 1.0)) & (z3 <= 0.0)))
out1 = 1;
end
and then break them up into separate locations:
out1 = ((z5 <= z4) | (0.0 < z3)) & (z6 == 0.0));
out2 = (((z4 < z5) & (z6 == 1.0)) & (z3 <= 0.0)));
And break again,
out1a = (z5 <= z4) & (z6 == 0.0);
out1b = (0.0 < z3) & (z6 == 0.0);
out2 = (((z4 < z5) & (z6 == 1.0)) & (z3 <= 0.0)));
But before I analyze more... is this the place where a 1 indicates that you do want to terminate, or where a 1 indicates that you do not want to terminate ?
Also, is z6 a binary variable? ode*() cannot handle binary variables -- not unless they are constant for the duration of the call (i.e., you have parameterized them into the function, paramfun )
Nicolas Mira Gebauer
Nicolas Mira Gebauer 2021년 9월 20일
Indeed, out1 = 1 would mean that I want to terminate, that's why value in zeroevents(t,x,D) is 1 - insideD.
Also, z6 can only be whether 1 or 0, but it's included in the equations of the system as diff(z6(t),t) = 0. Thus, it will be constant until an event is detected (then I will update the value after the event, but before restarting the simulation).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by