How to apply an ODE Solver to a while loop with non equivalent step size?

Hello there!
I've got this, probably very basic, problem but couldn't find any solution on the internet so far. I've got a set of ODEs, which I am solving with an ODE Solver (ode15s).
To solve the one main ODE, I need the current values of another equation. Therefore I implemented a main routine with function handles. After it got all the values, it proceeds to the next time step. This is realised with a for loop and fixed step size. The function handles take the values they need for the certain time step, solve the ODE, and give the value of the next time step back to the mother routine.
It looks in principal like this (without the code of the function handles):
t0 = 0;
tend = 2.5;
ntime = 5000;
dt = (tend-t0)/ntime;
t = t0 : dt : tend;
...then some parameters and the initial conditions are set, mostly as zero vectors with the size t:
R = zeros(1,size(t,2));
dRdt = zeros(1,size(t,2));
pg = zeros(1,size(t,2));
We = 1.375;
pv = 2.339;
...
R(1) = 1 ;
dRdt(1) = 0;
pinf (1) = p_inf_handle(0);
pg(1) = p_inf_handle(0)+ 2/(We*R(1)) - pv;
...
and then, the main loop looks basically like this:
for i=2:dt:size(t,2)
pg(i) = pg(i-1);
pinf(i) = p_inf_handle( t(i) );
delta_pg = 1;
tcurrent = t(i)
%solving the R values of step i
[R(i),dRdt(i)] = rpe_handler ...
( t(i) , dt , R(i-1) , dRdt(i-1) , pg(i) , ...
m_flux , dm_flux , rpe_parameters,pinf(i));
%solving the pg values of step i
pg(i) = pg(1) * Ng(1) / Ng(1) * ( R(1) / R(i) )^3;
end
My request is the following: I want to solve the same problem with a while loop and different step sizes in the mother routine. Is that possible or is that not possible for the ODE solvers? I know, that ode15s itself works with adaptive step size, but that doesnt matter in this case.
I left some parts of the code out, but I hope it is enough to clarify the problem.
Your help is very much appreciated!

 Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 2 Dez. 2018
Bearbeitet: Cris LaPierre am 2 Dez. 2018
You can solve odes with either a vector of time points, or with the start and stop times.
When passing in the start and stop time points, it will automatically determine the time steps. When you pass in a vector of time points, it interpolates the solution to the time points you passed in, providing a result for that time vector.
So it is possible to solve an ode passing in a time vector with irregular step sizes. It will return a result vector for those time points.

3 Kommentare

Okay thanks!
But how do I give the solver the start and stop times? And how does it determine the time steps? With the tolerance settings?
I'm sorry, I just never used ODE Solvers that way before.
Thank you again for your help!
This is enterred as the tspan input to the solver. You can check the synatx for ode15s in the documentation. You might be most interested in the section that specifically describes the time input, tspan.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by