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

OutputFcn in ode45

  1. Jul 25, 2011 #1

    I am using 'OutputFcn' to check integration values after each step. Druing my solution I want to stop integration after I reach y<=0. Problem I am acing is that, after each iteration solver returns me array of y which is correspond to a array of t rather than at each point of t. Thats why I am able to stop my integration but not at the point wherey<=0 but after some point. My code is as follows,

    function outfcn_test

    tspan = [0:.05:3];
    [t,y] = ode45(@myf,tspan,1,options);

    function yprime = myf(t,y)
    yprime = -y-5*exp(-t)*sin(5*t);

    function status = getvalues(t,y,done)
    if y>=0 status = 0;
    else status =1;

    I will appreciate any help in this regard. Thanks in advance.
  2. jcsd
  3. Jul 28, 2011 #2
    As far as I've been able to discern, during each integration step, an array of y values are calculated and passed to the OutputFcn after the integration step is complete. If OutputFcn returns status as 1, then the ode45 function stops and returns the [t,y] values. In your case, the ode45 function does 4 integration steps:

    1) the initial step
    2) where t=0.05
    3) where t starts at .1 and ends at .25 with .05 increments
    4) where t starts at .3 and ends at .6 with .05 increments

    The OutputFcn stops any further integration because other y values are below 0, but it returns all values of y corresponding with t values from 0 to where integration stopped, 0.6. You can trim the values returned by ode45, but you can't halt the integration if the condition y<=0 is reached in the middle of the integration step.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook