ode45
Default Runge-Kutta solver for nonstiff ordinary differential equations.
Category: Mathematics > Numerical Integration and Differential Equations
Syntax
[t, y] = ode45(odefun, tspan, y0)
Integrates y' = odefun(t, y) over tspan from y0.
[t, y] = ode45(odefun, tspan, y0, options)
Also applies options from odeset (tolerances, events, output functions).
sol = ode45(___)
Returns a structure for use with deval.
[t, y, te, ye, ie] = ode45(___)
Also returns event times, states and indices when an event function is set.
Description
ode45 uses the Dormand-Prince pair of fourth- and fifth-order formulas: the difference between the two estimates controls the step size. It is a good first attempt for most problems, as long as they are not stiff.
Higher-order equations are written as first-order systems: for x'' = -x, use y = [x; x'] and odefun = @(t,y) [y(2); -y(1)]. If the integration needs huge numbers of tiny steps, the problem is probably stiff and ode15s is the right tool.
Input arguments
odefun- Handle
dydt = odefun(t, y)returning a column vector the size ofy. tspan[t0 tf], or a vector of increasing (or decreasing) times to report.y0- Vector of initial values.
options- Structure from
odeset. Default tolerances areRelTol = 1e-3andAbsTol = 1e-6.
Output arguments
t- Column vector of time points.
y- Array with one row per time and one column per component.
Example
Harmonic oscillator
[t, y] = ode45(@(t, y) [y(2); -y(1)], [0 pi/2], [0; 1]);
fprintf('%.3f %.3f\n', y(end, 1), y(end, 2))
1.000 -0.000