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

  • Thread starter oby7842
  • Start date
  • Tags
    Ode45
In summary, the problem the individual is facing is that the OutputFcn in their code is returning an array of y values corresponding to an array of t values, making it difficult to stop the integration at the exact point where y<=0. They are seeking help in finding a solution to this issue.
  • #1
oby7842
11
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
  • #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.
 

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

What is the purpose of using OutputFcn in ode45?

The OutputFcn in ode45 is used to specify a function that is called at every integration step of the solver. This function can be used to monitor the progress of the solver, record the values of the solution at specified time points, or terminate the integration based on certain conditions.

How do you specify an OutputFcn in ode45?

The OutputFcn can be specified as an additional input argument when calling the ode45 function. It should be a function handle to the desired output function. The function handle should have the format "output = outputFcn(t,y,flag)", where t is the current time, y is the current solution, and flag is an integer that indicates the type of the current call (initialization, integration, or termination).

What is the syntax for creating an OutputFcn in ode45?

The syntax for creating an OutputFcn in ode45 is as follows:
function output = outputFcn(t,y,flag)
%insert code to be executed
end
The function should return a value for the output variable, which can be used to record the desired values or terminate the integration.

Can multiple OutputFcns be used in ode45?

Yes, multiple OutputFcns can be used in ode45 by specifying them as cell arrays. For example:
ode45(@odeFunction, tspan, y0, 'OutputFcn', {@outputFcn1, @outputFcn2})
In this case, both output functions will be called at every integration step in the order specified in the cell array.

What are some common uses of OutputFcn in ode45?

Some common uses of OutputFcn in ode45 include monitoring the progress of the solver, recording the values of the solution at specific time points, plotting the solution as it is being integrated, and terminating the integration based on certain conditions (such as reaching a certain time or a specific value in the solution).

Similar threads

Replies
5
Views
2K
Replies
8
Views
2K
Replies
2
Views
5K
Replies
1
Views
2K
Replies
4
Views
2K
Replies
2
Views
7K
Replies
5
Views
9K
Replies
2
Views
1K
Replies
5
Views
10K
Back
Top