Integration & ODEs
Calculus problems that have no neat formula are solved numerically. MATLAB offers functions for integrating sampled data (trapz, cumtrapz), integrating a function (integral), and solving ordinary differential equations (ode45 and friends).
Integrating sampled data: trapz
When you only have measurements, trapz(x, y) approximates the area under the curve by joining the points with straight lines and adding up the trapezoids:
t = 0:10:60; % seconds
v = [0 8 15 21 24 26 27]; % speed in m/s
distance = trapz(t, v);
fprintf('distance travelled: %.0f m\n', distance);
fprintf('average speed: %.2f m/s\n', distance / (t(end) - t(1)));distance travelled: 1075 m
average speed: 17.92 m/s
Integrating speed over time gives distance. With evenly spaced data you can also write trapz(y) * dx.
cumtrapz returns the running integral, the area from the start up to each point:
t = 0:10:60;
v = [0 8 15 21 24 26 27];
s = cumtrapz(t, v);
fprintf('%2d s: %5.0f m\n', [t; s]); 0 s: 0 m
10 s: 40 m
20 s: 155 m
30 s: 335 m
40 s: 560 m
50 s: 810 m
60 s: 1075 m
Integrating a function: integral
If you can write the integrand as a function, integral(f, a, b) computes the definite integral adaptively to high accuracy. The function must accept a vector of x-values, so use element-wise operators:
f = @(x) x.^2;
fprintf('integral of x^2 on [0, 3]: %.6f (exact 9)\n', integral(f, 0, 3));
g = @(x) exp(-x.^2);
fprintf('integral of exp(-x^2) on [-Inf, Inf]: %.6f\n', integral(g, -Inf, Inf));
fprintf('sqrt(pi) = %.6f\n', sqrt(pi));
fprintf('integral of sin on [0, pi]: %.6f\n', integral(@sin, 0, pi));integral of x^2 on [0, 3]: 9.000000 (exact 9)
integral of exp(-x^2) on [-Inf, Inf]: 1.772454
sqrt(pi) = 1.772454
integral of sin on [0, pi]: 2.000000
integral handles infinite limits too. (Older code uses quad, which is now discouraged; integral2 and integral3 do double and triple integrals.)
Ordinary differential equations
An ODE describes how something changes: dy/dt = f(t, y). Given a starting value y(t₀), a solver steps forward in time to find y(t). ode45 is the general-purpose solver (an adaptive Runge–Kutta method). You give it:
- a function handle
f(t, y)returning dy/dt, - the time span
[t0 tEnd], or a vector of times at which you want the solution, - the initial value
y0.
Example: exponential decay dy/dt = −0.5·y with y(0) = 4. The exact solution is y = 4·e^(−0.5t), so we can check the answer:
f = @(t, y) -0.5 * y;
opts = odeset('RelTol', 1e-8, 'AbsTol', 1e-10);
[t, y] = ode45(f, [0 1 2 3 4], 4, opts);
exact = 4 * exp(-0.5 * t);
fprintf('t = %d: y = %.6f, exact %.6f\n', [t'; y'; exact']);t = 0: y = 4.000000, exact 4.000000
t = 1: y = 2.426123, exact 2.426123
t = 2: y = 1.471518, exact 1.471518
t = 3: y = 0.892521, exact 0.892521
t = 4: y = 0.541341, exact 0.541341
Passing a vector of times makes ode45 report the solution exactly at those times. With just [0 4] it returns every internal step it took, and the number of steps can differ between MATLAB and Octave. Tight tolerances (odeset) make the results agree to many digits; the defaults (RelTol 1e-3) are fine for plots but less accurate.
Systems of ODEs
Higher-order equations are rewritten as systems of first-order equations, with y as a column vector. A mass on a spring with damping, x'' = −4x − 0.4x', becomes y₁ = x (position), y₂ = x' (velocity):
spring = @(t, y) [y(2); -4 * y(1) - 0.4 * y(2)];
opts = odeset('RelTol', 1e-8, 'AbsTol', 1e-10);
tq = 0:1:5;
[t, Y] = ode45(spring, tq, [1; 0], opts);
fprintf('t = %d s: position %7.4f, velocity %7.4f\n', [t'; Y']);t = 0 s: position 1.0000, velocity 0.0000
t = 1 s: position -0.2581, velocity -1.5032
t = 2 s: position -0.4983, velocity 1.0018
t = 3 s: position 0.5051, velocity 0.3400
t = 4 s: position -0.0026, velocity -0.8981
t = 5 s: position -0.3369, velocity 0.3707
Each row of Y is the state [position velocity] at one time. To see the damped oscillation you would plot it with plot(t, Y(:, 1)) on a finer time grid.
Choosing a solver
| Solver | Use for |
|---|---|
ode45 |
Most problems: try it first |
ode23 |
Lower accuracy, sometimes faster |
ode15s |
Stiff problems (very different time scales, e.g. chemical kinetics) |
If ode45 takes forever or tiny steps, the problem is probably stiff: switch to ode15s.
Test yourself
Which function integrates measured data points (x, y)?
integraltrapzode45polyint
What must the function passed to ode45 return?
- The solution y(t)
- The derivative dy/dt at (t, y)
- The time step
- The integral of y
How do you solve a second-order ODE with ode45?
- It can't be done
- Rewrite it as a system of first-order equations
- Call ode45 twice
- Use integral instead