Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab Help:Stop ode45 with OutpuFcn

  1. Sep 26, 2009 #1
    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:

    %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
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted



Similar Discussions: Matlab Help:Stop ode45 with OutpuFcn
  1. Ode45 help in MATLAB (Replies: 0)

Loading...