Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 45 ยท Numerical Methods

Integration, Differentiation and ODEs

Do calculus numerically in MATLAB and Octave: trapz, integral, diff, gradient, ode45 and stiff solvers like ode15s, events, with SciPy solve_ivp stand-ins.

Calculus on a computer works with numbers rather than formulas. Integration adds up area under a curve, differentiation measures slopes, and an ordinary differential equation (ODE) describes how a quantity changes over time, which you solve by stepping forward from a starting value. MATLAB and Octave have tools for all three: trapz and integral, diff and gradient, and the ODE solvers ode45 and ode15s.

MATLAB and Octave are not available in your browser here, so the MATLAB code is read-only. Each example has a runnable Python stand-in using NumPy and scipy.integrate, which implements the same methods (solve_ivp plays the role of ode45). Octave has trapz, quad, integral, diff, gradient and ode45.

Calculus with numbers

Integrate trapz for data, integral for functions.
Differentiate diff and gradient give slopes.
Solve ODEs ode45 steps through time.
1 / 2

Integrating samples with trapz

When you have measurements y at positions x, the trapezoid rule joins neighbouring points with straight lines and adds up the trapezoids' areas. trapz(x, y) does exactly that; cumtrapz gives the running total.

MATLAB
x = linspace(0, pi, 101);
y = sin(x);
trapz(x, y)                 % 1.99984 (exact answer: 2)
cumtrapz(x, y)              % area accumulated up to each point
trapz(y) * (x(2) - x(1))    % same result when the spacing is uniform

The error shrinks as the spacing shrinks (quartering for every halving of the step). With only 5 samples the answer would be noticeably off. For physical data, trapz is the natural tool: distance from speed samples, energy from power samples.

Python Area under sin with the trapezoid rule

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Integrating a function with integral

If you can evaluate the function anywhere, let the software choose the sample points. integral(f, a, b) uses adaptive quadrature: it adds points where the function varies quickly and stops when the estimated error is below a tolerance. The function handle must be vectorized (see Anonymous Functions and Function Handles), because integral passes it a vector of points at once.

MATLAB
f = @(x) exp(-x.^2);
integral(f, 0, Inf)         % 0.8862  (= sqrt(pi)/2)
integral(@(x) 1./x, 1, 10)  % 2.3026  (= log(10))
integral2(@(x,y) x.*y, 0, 1, 0, 2)   % double integral: 1

Forgetting the dot in x.^2 or 1./x is the typical error: you get a dimension or "matrix must be square" message. quad, quadgk and quadl are older relatives, still common in Octave code. If the function has a sharp spike or a singularity, split the interval at that point or pass 'Waypoints'.

Python Adaptive integration of function handles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Differences and slopes: diff and gradient

diff(y) returns the differences between neighbours, one element fewer than y. Dividing by the spacing gives a slope estimate at the midpoints:

MATLAB
x = linspace(0, 2*pi, 50);
y = sin(x);
dy = diff(y) ./ diff(x);              % 49 slopes, at midpoints
xm = (x(1:end-1) + x(2:end)) / 2;
dy2 = gradient(y, x);                 % 50 slopes, same positions as x

gradient uses central differences in the interior and one-sided differences at the two ends, so its result has the same length as the data. For a matrix, [gx, gy] = gradient(Z, dx, dy) gives the slope in both directions. Derivatives amplify noise: a tiny wiggle in the data becomes a big spike in the slope, so smooth the data first (see Cleaning and Preparing Data).

Python Numerical derivative of sine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

ODEs: ode45

An ODE has the form dy/dt = f(t, y): the rate of change depends on the time and on the current state. Together with a starting value y(t0), it determines the whole future. ode45 takes a function that returns the derivative, a time span and the starting state:

MATLAB
% exponential decay: dy/dt = -0.5*y, y(0) = 10
f = @(t, y) -0.5 * y;
[t, y] = ode45(f, [0 8], 10);
plot(t, y, 'o-', t, 10*exp(-0.5*t), '--')    % compare with the exact solution
legend('ode45', 'exact')

ode45 is an adaptive Runge-Kutta method of order 4 and 5: it picks its own step sizes (small where the solution changes quickly) and returns the time points it used. Ask for chosen times with tspan = 0:0.5:8; the solver still steps adaptively but interpolates the output. Control accuracy with odeset('RelTol', 1e-8, 'AbsTol', 1e-10).

Python Decay solved numerically, compared with the exact answer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Systems of ODEs

Higher-order equations and several coupled quantities become a system: the state y is a vector, and f returns a column vector of the same size. A second-order equation like a mass on a spring, x'' = -k*x - c*x', is rewritten with y(1) = x and y(2) = x':

MATLAB
k = 4; c = 0.4;
f = @(t, y) [ y(2);                      % x' = v
             -k*y(1) - c*y(2) ];         % v' = -k x - c v
[t, y] = ode45(f, [0 10], [1; 0]);       % start displaced by 1, at rest
plot(t, y(:,1))                          % column 1 is x(t)

The output y has one row per time point and one column per state variable. Be careful that f returns a column vector; a row vector produces an error about dimensions.

Python A damped spring as a two-variable system

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note the layout difference: ode45 gives one row per time point, while SciPy returns one row per state variable.

Stiff problems and events

Some systems contain a fast process and a slow one at once (a chemical reaction with a very fast step, for instance). Such stiff problems force ode45 to take tiny steps for stability, so it crawls or gives up. Use a stiff solver instead: ode15s or ode23s in MATLAB (SciPy: method="Radau" or "BDF"). If ode45 takes very many steps for a smooth-looking problem, try ode15s before anything else.

An event stops the integration when a condition is met, such as a ball hitting the ground. You write a function returning a value that crosses zero at the moment of interest and pass it through odeset('Events', @myEvent). The solver then also reports the event time te. In SciPy, you give events= a function with a terminal attribute.

Python A stiff problem and a falling ball with an event

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The stiff solver needs far fewer function evaluations for the same accuracy.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. When should you use trapz rather than integral?

    trapz works on measured samples. integral needs a vectorized function handle that it can evaluate wherever it likes.
  2. 2. Why must the handle passed to integral use x.^2 rather than x^2?

    integral evaluates the function on many points at once, so operators must act element by element.
  3. 3. diff(y) has how many elements if y has 50?

    Differences between neighbours give one element fewer. gradient, in contrast, returns the same length as y.
  4. 4. How do you turn a second-order ODE into a form ode45 accepts?

    Introduce v = x' so that x' = v and v' = (right-hand side). The state vector is [x; v].
  5. 5. ode45 takes a huge number of tiny steps on a smooth-looking problem. What is a likely cause and fix?

    Stiff problems force explicit solvers like ode45 to use tiny steps for stability. Implicit stiff solvers such as ode15s handle them efficiently.

Summary

  • trapz/cumtrapz integrate samples; integral integrates a vectorized function handle adaptively; integral2 handles double integrals.
  • diff shortens the data by one and gives midpoint slopes; gradient keeps the length. Differentiation amplifies noise.
  • [t, y] = ode45(f, tspan, y0) solves y' = f(t, y); for systems y is a vector and f returns a column.
  • Output has one row per time point; SciPy's solve_ivp returns one row per state, so transpose when translating.
  • Stiff problems need ode15s/ode23s; events stop the run at a condition via odeset('Events', ...).

Next, work with randomness and data summaries in Statistics and Random Numbers.