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

  • Thread starter Thread starter oby7842
  • Start date Start date
  • Tags Tags
    Ode45
Click For Summary
The discussion focuses on using the 'OutputFcn' in ode45 to stop integration when y reaches 0 or below. The user encounters an issue where the solver returns an array of y values corresponding to an array of t values, making it difficult to stop integration precisely at y<=0. It is explained that while the OutputFcn can halt further integration, it cannot stop mid-step, which results in returning all y values up to the last completed integration step. Users can trim the results post-integration but cannot prevent the solver from completing the current step. The challenge lies in the inherent behavior of ode45 regarding integration steps and output handling.
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 12 ·
Replies
12
Views
33K