How to Stop Integration in ode45 When y<=0 is Reached?

  • Context: Undergrad 
  • Thread starter Thread starter oby7842
  • Start date Start date
  • Tags Tags
    Ode45
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 7K views
oby7842
Messages
9
Reaction score
0
Hii,

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];
options=odeset('OutputFcn',@getvalues);
[t,y] = ode45(@myf,tspan,1,options);
plot(t,y)

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;
end


I will appreciate any help in this regard. Thanks in advance.
 
Physics news on Phys.org
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.