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
This can be done just need to use a few more commands.
NEW:
options= odeset('Reltol',0.001,'Stats','on');
%figure(1);
%clf;
init=[xo yo zo]';
to=some number;
tf= some number;
nsteps= number of points you want function evaluated on.
tspan = linspace(t0,tf, nsteps);
[T,Y]=ode45(@function,tspan,init,options);
You can verify that it took the number points that you wanted using the command size(variable).
Well, you cannot ensure that it will ONLY calculate those steps, but you can ensure the maximum step size (and if you do it small enough you can are almost sure that you have all the desired times and take just those samples):
options= odeset('MaxStep',1);
[t,s] = ode45(@myode,tspan,[0;0],options);
to know more you can go to here:
Based on the documentation it doesn't appear that you can control the size of the steps taken internally by ode45
when solving the equation, but you can control the time points at which the output is generated. You can do this as indicated in your commented-out line:
tspan = to:0.01:tf; % Obtain solution at specific times
[T, Y] = ode45(name, tspan, init, options);
With respect to the accuracy of solutions when fixed step sizes are used, refer to this excerpt from the above link:
If
tspan
has more than two elements[t0,t1,t2,...,tf]
, then the solver returns the solution evaluated at the given points. However, the solver does not step precisely to each point specified intspan
. Instead, the solver uses its own internal steps to compute the solution, then evaluates the solution at the requested points intspan
. 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.
So, even when you specify that you want the solution at specific time points in the output, 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.
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
Your script does not indicate fixed-step size, It only shows that solution will be printed based on the time-step provided. Try check the solution structure of the ODE and you will realize that in fact, it uses a different timestep.
The best way to get it to use a fixed timestep is to ensure that both RelTol and AbsTol are large enough.
ODE45 (speak four-five not fortyfive) calculates the optimal stepsize and even goes back in time if the error is to large, check Runge-Kutta if you are interested. As a user you don't notice this since the output of ODE will be interpolated such that is suits the tspan vector. So if you set tspan to 0:1E-5:1 and you want to integrate a easy ODE such as dydt=-y ODE45 will make a few steps but the vektor you get out of the ode will be in time steps declared in tspan i.e. 1E-5. If you want to integrate the stiff equation dydt=1E2*(1-y)*y ODE45 will make large and very tiny steps but the output will be the same, for the later you should use ode15s because ODE45 can't handle stiff systems.
Hope this helps
cheers Marco