Hi,
I understand you are trying to solve the particular ODE problem using ode45 using ODE Event Location. Also from the dummy event function I can understand that you want to detect the time when ode45 stops in term of u(1) (I am assuming that is displacement). So, the rope force S that is mentioned is dependent on u(1) as it’s a string so I suggest putting value parameter as u(1) only.
Here isterminal condition is critical as if you mention it as 1, the first-time value parameter reaches 0, ode45 will stop at that time moment. direction may be mentioned +1 if the event which signifies the value parameter increasing while going to zero. Same applies for direction value –1. So, Event Location serves as a stopping criterion for ode45 function.
Following Code may help:
g=9.81; mass=75; c=0.1; L=50; k=130;
T=50; %Time
Nstep=5000;
tspan=linspace(0,T,Nstep);
u0=[L 0]; % B.conditions
f=@(t,u) uprim(u,t,g,mass,k,c,L);
options = odeset('Events',@myEventsFcn);
[t,u]=ode45(f,tspan,u0,options);
plot(t,u(:,1)) % To see how the location varies with time.
function [value,isterminal,direction] = myEventsFcn(t,u)
value=u(1); %displacement at a given event is zero
isterminal=1; % Stop the ode45 at this moment
direction=1; % Direction which shows position increasing so that string goes into the relax mode
end
function f=uprim(u,t,g,mass,k,c,L)
f=[u(2); g - sign(u(2))*c*u(2)^2/mass - k*u(1)/mass]; % gravity - air-resistance - S
%f2=[u(2); g - sign(u(2))*c*u(2)^2/mass]; For when rope-force S == 0
end
For more Examples you may refer the following: