non-zero threshold for event function

조회 수: 4 (최근 30일)
9times6
9times6 2019년 9월 10일
댓글: Star Strider 2019년 9월 10일
I have written the following event function and I want to detect an event which is non-zero. Therefore, I am using the logical operator since event function will only work for value=0. In this code, y is 4x1 vector.
function [value,isterminal,direction] = myevent12d1(t,y)
% Locate the time when height passes through zero in a decreasing direction
% and stop integration.
z=y(1)<y(3);
value = z; % detect height = 0
isterminal = 1; % stop the integration
direction = -1; % negative
However, upon running the code, I am getting the following error message:
Undefined function 'sign' for input arguments of type 'logical'.
Error in odezero (line 46)
indzc = find((sign(vL) ~= sign(vR)) & (direction .* (vR - vL) >= 0));
Error in ode45 (line 352)
[te,ye,ie,valt,stop] = ...
Error in onedof2dof (line 19)
[t1,y1,te1,ye1,ie1]=ode45(@(t,y) sp121(t,y,k1,k2,m1,m2),[tstart tfinal],y0,options1);
Any help in this regard would be appreciated.

채택된 답변

Star Strider
Star Strider 2019년 9월 10일
편집: Star Strider 2019년 9월 10일
I am not certain what you are doing. If you want to evaluate ‘z’ as numeric rather than logical, just do an arithmetic operation on it, here that being uplus (unary plus):
y = 1:4; % Create ‘y’
z1 = y(1)<y(3) % Logical Result
z2 = +(y(1)<y(3)) % Double Result
Note the parentheses, so the logical operation is done first, then the arithmetic operation.
Using the ‘z2’ approach will at least avoid the problem with logical variables.
EDIT —
Also:
z3 = y(3) - y(1);
Note that the documentation on ODE Event Location states: ‘Event functions take an expression that you specify, and detect an event when that expression is equal to zero.’ So a variable in your ODE evalutation does not itself have to be zero, it simply needs for the ‘value’ output to be zero to trigger the event.
  댓글 수: 2
9times6
9times6 2019년 9월 10일
Thanks a lot, Star Strider! It worked :)
Star Strider
Star Strider 2019년 9월 10일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by