Discussion:
ODE45 time step size
(too old to reply)
Alyson Turri
2006-04-12 00:09:38 UTC
Permalink
I would like to determine the time step size that Matlab uses at each
time tn (delta t = t(n) - t(n-1)). Is there a way to do this using
the ode45 solver? I know that there are some fixed step solvers on
the Matlab website but I would prefer to use the more robust ode45.
I would like to perform a calculation using this delta t time step
size at each time step t(n). Thanks in advance.
Darren
2006-04-12 01:02:37 UTC
Permalink
Post by Alyson Turri
I would like to determine the time step size that Matlab uses at each
time tn (delta t = t(n) - t(n-1)). Is there a way to do this using
the ode45 solver? I know that there are some fixed step solvers on
the Matlab website but I would prefer to use the more robust ode45.
I would like to perform a calculation using this delta t time step
size at each time step t(n). Thanks in advance.
The ode45 solver can output the time vector used:

[t,y] = ode45(... etc

and then you had it anyway:

dt = t(2:end)-t(1:end-1);

Hope this helps,

Darren
Alyson Turri
2006-04-12 01:28:06 UTC
Permalink
Thanks for the feedback. I think that I need to provide a little
more explain for this problem. I am using the ode45 function to pass
the time interval and the initial vector y0 to a second function
(called odefun in the Matlab help files). Within odefun, I perform
an operation on the y vector and save the changes to a new vector
ydot. The y vector is updated using the ydot vector and the time
step size. This is a short example of what I am trying to do:

Main Program:

y0=[10, 20];
t0=0;
tmax=2;
[t,y]= ode45('odefuntest',[t0 tmax],y0);

Odefun Function:

function ydot = odefuntest(t,y);
ydot= zeros(size(y));
r=sqrt(y(1)^2+y(2)^2);
ydot(1)=y(1)/r;
ydot(2)=y(2)/r;

I would like to add a line of code similar to this within odefun:

timestep = t(n) - t(n-1);
ydot(2) = y(2)/r - timestep*10;

I need to find the time step that Matlab uses at the point t(n)
because it will become part of the solution for the next timestep
t(n+1). I cannot perform this operation after the entire t and y
vectors are calculated. Thanks again for the help.

Alyson
Post by Alyson Turri
Post by Alyson Turri
I would like to determine the time step size that Matlab uses
at
Post by Alyson Turri
Post by Alyson Turri
each
time tn (delta t = t(n) - t(n-1)). Is there a way to do this
using
Post by Alyson Turri
the ode45 solver? I know that there are some fixed step
solvers
Post by Alyson Turri
on
Post by Alyson Turri
the Matlab website but I would prefer to use the more robust
ode45.
Post by Alyson Turri
I would like to perform a calculation using this delta t time
step
Post by Alyson Turri
size at each time step t(n). Thanks in advance.
[t,y] = ode45(... etc
dt = t(2:end)-t(1:end-1);
Hope this helps,
Darren
Felloweskraft
2006-04-12 15:37:20 UTC
Permalink
Alyson,

I believe ode45 returns a structure for the solution to the
differential equation that's supposed to be subsequently passed to
the function deval. For instance if you want to view the values of
the solution at a fixed time spacing or at one point in time. In any
event, I don't think the time step from ode45 is what you want. Try
to reformulate the problem in terms of only the continuous first
derivatives.

F
Post by Alyson Turri
Thanks for the feedback. I think that I need to provide a little
more explain for this problem. I am using the ode45 function to pass
the time interval and the initial vector y0 to a second function
(called odefun in the Matlab help files). Within odefun, I perform
an operation on the y vector and save the changes to a new vector
ydot. The y vector is updated using the ydot vector and the time
y0=[10, 20];
t0=0;
tmax=2;
[t,y]= ode45('odefuntest',[t0 tmax],y0);
function ydot = odefuntest(t,y);
ydot= zeros(size(y));
r=sqrt(y(1)^2+y(2)^2);
ydot(1)=y(1)/r;
ydot(2)=y(2)/r;
timestep = t(n) - t(n-1);
ydot(2) = y(2)/r - timestep*10;
I need to find the time step that Matlab uses at the point t(n)
because it will become part of the solution for the next timestep
t(n+1). I cannot perform this operation after the entire t and y
vectors are calculated. Thanks again for the help.
Alyson
Post by Alyson Turri
Post by Alyson Turri
I would like to determine the time step size that Matlab
uses
Post by Alyson Turri
at
Post by Alyson Turri
Post by Alyson Turri
each
time tn (delta t = t(n) - t(n-1)). Is there a way to do
this
Post by Alyson Turri
Post by Alyson Turri
using
Post by Alyson Turri
the ode45 solver? I know that there are some fixed step
solvers
Post by Alyson Turri
on
Post by Alyson Turri
the Matlab website but I would prefer to use the more
robust
Post by Alyson Turri
Post by Alyson Turri
ode45.
Post by Alyson Turri
I would like to perform a calculation using this delta t
time
Post by Alyson Turri
Post by Alyson Turri
step
Post by Alyson Turri
size at each time step t(n). Thanks in advance.
[t,y] = ode45(... etc
dt = t(2:end)-t(1:end-1);
Hope this helps,
Darren
Darren
2006-04-12 23:24:12 UTC
Permalink
I agree that you really want to give ode45 a continuous function,
like F said, but if you really! need dt then maybe you could use a
persistent or global variable, like

function dydt = odefun(t,y)

dt = t-t_old

global t_old = t

etc, etc, ...

I haven't actually tried this... just a thought

Darren
Post by Felloweskraft
Alyson,
I believe ode45 returns a structure for the solution to the
differential equation that's supposed to be subsequently passed to
the function deval. For instance if you want to view the values of
the solution at a fixed time spacing or at one point in time. In any
event, I don't think the time step from ode45 is what you want. Try
to reformulate the problem in terms of only the continuous first
derivatives.
F
Post by Alyson Turri
Thanks for the feedback. I think that I need to provide a
little
Post by Felloweskraft
Post by Alyson Turri
more explain for this problem. I am using the ode45 function
to
Post by Felloweskraft
Post by Alyson Turri
pass
the time interval and the initial vector y0 to a second
function
Post by Felloweskraft
Post by Alyson Turri
(called odefun in the Matlab help files). Within odefun, I
perform
Post by Alyson Turri
an operation on the y vector and save the changes to a new
vector
Post by Felloweskraft
Post by Alyson Turri
ydot. The y vector is updated using the ydot vector and the
time
Post by Felloweskraft
Post by Alyson Turri
y0=[10, 20];
t0=0;
tmax=2;
[t,y]= ode45('odefuntest',[t0 tmax],y0);
function ydot = odefuntest(t,y);
ydot= zeros(size(y));
r=sqrt(y(1)^2+y(2)^2);
ydot(1)=y(1)/r;
ydot(2)=y(2)/r;
I would like to add a line of code similar to this within
timestep = t(n) - t(n-1);
ydot(2) = y(2)/r - timestep*10;
I need to find the time step that Matlab uses at the point t(n)
because it will become part of the solution for the next
timestep
Post by Felloweskraft
Post by Alyson Turri
t(n+1). I cannot perform this operation after the entire t and y
vectors are calculated. Thanks again for the help.
Alyson
Post by Alyson Turri
Post by Alyson Turri
I would like to determine the time step size that
Matlab
Post by Felloweskraft
uses
Post by Alyson Turri
at
Post by Alyson Turri
Post by Alyson Turri
each
time tn (delta t = t(n) - t(n-1)). Is there a way to
do
Post by Felloweskraft
this
Post by Alyson Turri
Post by Alyson Turri
using
Post by Alyson Turri
the ode45 solver? I know that there are some fixed
step
Post by Felloweskraft
Post by Alyson Turri
solvers
Post by Alyson Turri
on
Post by Alyson Turri
the Matlab website but I would prefer to use the more
robust
Post by Alyson Turri
Post by Alyson Turri
ode45.
Post by Alyson Turri
I would like to perform a calculation using this delta
t
Post by Felloweskraft
time
Post by Alyson Turri
Post by Alyson Turri
step
Post by Alyson Turri
size at each time step t(n). Thanks in advance.
[t,y] = ode45(... etc
dt = t(2:end)-t(1:end-1);
Hope this helps,
Darren
Gustavo Sandri Heidner
2016-10-31 01:24:03 UTC
Permalink
Post by Alyson Turri
I would like to determine the time step size that Matlab uses at each
time tn (delta t = t(n) - t(n-1)). Is there a way to do this using
the ode45 solver? I know that there are some fixed step solvers on
the Matlab website but I would prefer to use the more robust ode45.
I would like to perform a calculation using this delta t time step
size at each time step t(n). Thanks in advance.
This question is REALLY old, but I've come across it and it seems that the answer to what you wanted was on the actual ode45 function itself. The documentation says that if you provide the [t0 tf] vector, than the resulting [t] output will be in terms of each integration cycle as performed by the function. On the other hand, if you provide a vector with all the steps [t0,t1,t2,...,tf], then the output [t] will be in terms of the steps provided, which are an approximation of the integration cycle, although the system still does the calculations with it's own integration steps. The approximation to the steps are still accurate.

From the documentation (https://www.mathworks.com/help/matlab/ref/ode45.html#inputarg_tspan):

The solver imposes the initial conditions, y0, at tspan(1), then integrates from tspan(1) to tspan(end):

If tspan has two elements, [t0 tf], then the solver returns the solution evaluated at each internal integration step within the interval.
If tspan contains more than two elements [t0,t1,t2,...,tf], then the solver returns the solution evaluated at the given points. This does not affect the internal steps that the solver uses to traverse from tspan(1) to tspan(end). Thus, the solver does not necessarily step precisely to each point specified in tspan. However, the solutions produced at the specified points are of the same order of accuracy as the solutions computed at each internal step.
Specifying several intermediate points has little effect on the efficiency of computation, but for large systems it can affect memory management.
The solution obtained by the solver might be different depending on whether you specify tspan as a two-element vector or as a vector with intermediate points. If tspan contains several intermediate points, then they give an indication of the scale for the problem, which can affect the size of the initial step taken by the solver.

Example: [1 10]

Example: [1 3 5 7 9 10]

Loading...