ode45

Solve a system of non-stiff differential equations.

Syntax

[t,y] = ode45(@func,tin,y0)

[t,y] = ode45(@func,tin,y0,options)

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

Inputs

func
The system of equations to solve.
tin
The vector of times (or other domain variable) at which to report the solution. If the vector has two elements, then the solver operates in single step mode and determine the appropriate intermediate steps.
y0
The vector of initial conditions.
options
A struct containing options settings specified via odeset.
The default relative and absolute tolerances are 1.0e-3 and 1.0e-6, respectively.

Outputs

t
The times at which the solution is computed.
y
The solution matrix, with the solution at each time stored by row.

Example

Find the location in a mass spring damper system:

function dy = MSD(t,y,m,k,c)
  % y = [x, dx/dt]
  dy = [0, 0];
  dy(1) = y(2);
  dy(2) = -y(1)*(k/m) - y(2)*(c/m);
end

v = 1.5;  % initial velocity
m = 1.6;  % mass
k = 1.25; % spring constant
c = 1.1;  % damping constant

handle = @(t,y) MSD(t,y,m,k,c);
t = [0:0.2:12]; % time vector
yi = [0, v];
[t,y] = ode45(handle,t,yi);
x = y(:,1)'
x = [Matrix] 1 x 61
0.00000  0.27883  0.51372  0.70348  0.84837  0.94996  1.01082  1.03443  1.02489
0.98672  0.92472  0.84376  0.74863  0.64392  0.53391  0.42248  0.31307  0.20859
0.11146  0.02356  -0.05373  -0.11953  -0.17340  -0.21529  -0.24554  -0.26475
-0.27378  -0.27368  -0.26562  -0.25086  -0.23069  -0.20640  -0.17922  -0.15031
-0.12073  -0.09144  -0.06325  -0.03684  -0.01275  0.00862  0.02699  0.04222  0.05426
0.06317  0.06910  0.07226  0.07290  0.07134  0.06790  0.06293  0.05676  0.04974
0.04216  0.03433  0.02651  0.01892  0.01176  0.00517  -0.00072  -0.00584  -0.01012

Comments

ode45 solves the system with an adaptive Runge-Kutta algorithm the from the Sundials ARKODE library.

To pass additional parameters to a function argument, use an anonymous function.

The odeset options and defaults are as follows.
  • RelTol: 1.0e-3
  • AbsTol: 1.0e-6