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
Click For Summary
SUMMARY

The forum discussion addresses the challenge of stopping the integration process in MATLAB's ode45 function when the output variable y becomes less than or equal to zero. The user employs the 'OutputFcn' option to monitor y values after each integration step. However, the integration cannot be halted precisely at the point where y reaches zero due to the nature of how ode45 processes and returns arrays of y corresponding to time t. The solution involves understanding that while the OutputFcn can stop further integration, it cannot interrupt the current integration step.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of numerical integration methods, specifically ode45
  • Knowledge of function handles and the OutputFcn option in MATLAB
  • Basic understanding of differential equations
NEXT STEPS
  • Explore MATLAB's documentation on 'OutputFcn' for ode45
  • Learn about alternative stopping criteria in numerical solvers
  • Investigate the use of event functions in MATLAB for more precise control
  • Study the behavior of arrays returned by ode45 and how to manipulate them
USEFUL FOR

Mathematics and engineering students, MATLAB users, and researchers working with numerical simulations who need to control integration processes effectively.

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
3K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 0 ·
Replies
0
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
7K