Clear Filters
Clear Filters

non-zero threshold for event function

3 views (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 10 Sep 2019
Edited: Star Strider on 10 Sep 2019
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 Comments
9times6
9times6 on 10 Sep 2019
Thanks a lot, Star Strider! It worked :)

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!