non-zero threshold for event function

6 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by