tags:

views:

118

answers:

2

I'm using Matlab to solve a differential equation. I want to force ode45 to take constant steps, so it always increments 0.01 on the T axis while solving the equation. How do I do this?

ode45 is consistently taking optimized, random steps, and I can't seem to work out how to make it take consistent, small steps of 0.01. Here is the code:

options= odeset('Reltol',0.001,'Stats','on');

%figure(1);
%clf;
init=[xo yo zo]';
tspan=[to tf];
%tspan = t0:0.01:tf;

[T,Y]=ode45(name,tspan,init,options);
+1  A: 

Based on the documentation for the ode functions, you should be able to do what you have indicated in the commented-out line:

tspan = to:0.01:tf;  %# Obtain solutions at specific times
[T,Y] = ode45(name,tspan,init,options);

EDIT:

With respect to the accuracy of solutions when fixed step sizes are used, refer to this excerpt from the above link:

Specifying tspan with more than two elements does not affect the internal time steps that the solver uses to traverse the interval from tspan(1) to tspan(end). All solvers in the ODE suite obtain output values by means of continuous extensions of the basic formulas. Although a solver does not necessarily step precisely to a time point specified in tspan, the solutions produced at the specified time points are of the same order of accuracy as the solutions computed at the internal time points.

So, even when you specify that you want the solution at specific time points, the solvers are still internally taking a number of adaptive steps between the time points that you indicate, coming close to the values at those fixed time points.

gnovice
You're right, the docs do suggest that each value can be specified, even though the article I link to suggests it cannot. Unfortunately I can't open my interpreter at the moment to try this out as I'm away from the University network (and licence server!)
Brendan
@Brenden: The documentation you link to doesn't actually appear to say you can't have values of the solution returned at specific time points for ODE45. Note the additional information I added above from the documentation.
gnovice
A: 

ode45 invariably uses adaptive step size, the documentation addresses this issue and recommends other solvers instead for fixed step size - see ode4 (fourth order Runge-Kutta) which is a fairly safe bet for solving most odes - at least according to Numerical Recipes

Brendan

related questions