If I give only one step by tspan = 0:0.01:0.01; then it takes many smaller steps automatically between 0 and 0.01 How can I force it to take just one step?
MATLAB: How to give ode45 just 1 time step
differential equationsMATLABode45structures
Related Solutions
The ability to use a fixed-step solver is not built into MATLAB 8.0 (R2012b).
The ordinary differential equation solver functions provided with MATLAB employ a variety of variable-step methods. ODE23 is based on the Runge Kutta (2,3)integration method, and ODE45 is based on the Runge Kutta (4,5) integration method. ODE113 is a variable-order Adams-Bashforth-Moulton PECE solver. For a complete listing of the various solvers and their methods, see the documentation.
The MATLAB ODE solvers utilize these methods by taking a step, estimating the error at this step, checking to see if the value is greater than or less than the tolerance, and altering the step size accordingly. These integration methods do not lend themselves to a fixed step size. Using an algorithm that uses a fixed step size is dangerous since you can miss points where your signal frequency is greater than the solver frequency. Using a variable step ensures that a large step size is used for low frequencies and a small step size is used for high frequencies. The ODE solvers within MATLAB are optimized for a variable step, run faster with a variable step size, and clearly the results are more accurate.
There are now fixed time step solvers available:
ODE1 - A first-order Euler method
ODE2 - A second-order Euler method
ODE3 - A third-order Runge-Kutta method
ODE4 - A fourth-order Runge-Kutta method
ODE5 - A fifth-order Runge-Kutta method
ODETest
These are included in the attached zip file. After saving the files into a folder located on the MATLAB path, these solvers can be used with the following syntax:
y = ode4(odefun,tspan,y0);
The integration proceeds by steps, taken to the values specified in tspan. The time values must be in order, either increasing or decreasing. Note that the step size (the distance between consecutive elements of tspan) does not have to be uniform. If the step size is uniform, you might want to use LINSPACE.
For example,
tspan = linspace(t0,tf,nsteps); % t0 = 0; tf = 10, nsteps = 100;
Since these files do not ship with MATLAB, these solvers are not officially supported.
ODE23 and ODE45 are MATLAB's ordinary differential equation solver functions. ODE23 is based on the integration method, Runge Kutta23, and ODE45 is based on the integration method, Runge Kutta45. The way that ODE23 and ODE45 utilize these methods is by selecting a point, taking the derivative of the function at that point, checking to see if the value is greater than or less than the tolerance, and altering the step size accordingly. These integration methods do not lend themselves to a fixed step size. Using an algorithm that uses a fixed step size is dangerous since you may miss points where your signal's frequency is greater than the solver's frequency. Using a variable step ensures that a large step size is used for low frequencies and a small step size is used for high frequencies. ODE23/ODE45 are optimized for a variable step, run faster with a variable step size, and clearly the results are more accurate. If you wish to obtain only those values at a certain fixed increment, do the following:
- Use ODE23/ODE45 to solve the differential equation.
- Use INTERP1 to extract only the desired points.
For example:
% the fixed step vector for desired
% output:
t0 = 0:.01:10; [t,y] = ode23('filename',0,10);y0 = interp1(t0,t,y);
Now, t0 and y0 are the outputs at a fixed interval.
Note that, as of MATLAB 5, you can also obtain solutions at specific time points by specifying tspan as a vector of the desired times. The time values must be in order, either increasing or decreasing.
For example:
tspan = 0:.01:10;[t,y] = ode23('filename',tspan);
Best Answer