Hi everybody,(adsbygoogle = window.adsbygoogle || []).push({});

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:

%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

%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

**Physics Forums | Science Articles, Homework Help, Discussion**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Matlab Help:Stop ode45 with OutpuFcn

Can you offer guidance or do you also need help?

Draft saved
Draft deleted

Loading...

Similar Threads for Matlab Help Stop |
---|

MATLAB MATLAB: forces acting on an object |

MATLAB Matlab histogram |

MATLAB Sovling Bernoulli's differential equation in matlab? |

MATLAB MATLab: Not enough inputs for nlinfit |

MATLAB Help needed to fix a matlab code |

**Physics Forums | Science Articles, Homework Help, Discussion**