# MATLAB Matlab Help:Stop ode45 with OutpuFcn

#### stevenumber5

Hi everybody,
I'm trying to solve this first-order ODEs system:and I'd like to stop the solution when y, i.e u(3), becomes negative:

Matlab:
%system
function rhs=particle(t,u)
global k
rhs=[u(2);
-k*u(1)*sqrt(u(2)*u(2)+u(4)*u(4));
u(4);
-9.81-k*abs(u(4))*sqrt(u(2)*u(2)+u(4)*u(4))];

where u(1)=x,u(2)=dx/dt,u(3)=y,u(4)=dy/dt
I use ode45 as solver, and in order to stop it the option OutputFcn.
It stops after few values, when y is already positive.
Here is the script

Matlab:
%ode script

global k

t0=0;tend=10;

k=0.065;

a=20*pi/180;

u0=[0 20*cos(a) 1.5 20sin(a)];

options = odeset('OutputFcn',@output);

[time,result]=ode45('particle',[t0,tend],u0,options);

%output function

function status = output(t,y,status)

[val,pos]=min(y)

status=(val>=0)

if status==1

tend=t(pos);

end

Thank you all
Best Regards

Last edited by a moderator:
Related Math Software Workshop News on Phys.org

"Matlab Help:Stop ode45 with OutpuFcn"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving