← Blog

article

Notes on Numerical Integration of ODEs

Explicit Euler versus fourth-order Runge-Kutta on a test equation, and why accuracy and stability are separate questions.

Mathematical Foundations3 min read

Most simulations of physical systems reduce to the same task: advance an ordinary differential equation y˙=f(t,y)\dot{y} = f(t, y) in small time steps of size hh. The choice of method decides how much accuracy a given step size buys, and whether the result stays bounded at all. These notes compare two standard methods on the simplest useful test problem.

The test equation

Consider y˙=λy\dot{y} = \lambda y with y(0)=1y(0) = 1, whose exact solution is y(t)=eλty(t) = e^{\lambda t}. Setting λ=−1\lambda = -1 gives a decaying signal that is easy to check against.

Explicit Euler takes one slope evaluation per step:

yn+1=yn+h f(tn,yn).y_{n+1} = y_n + h\,f(t_n, y_n).

On the test equation this becomes yn+1=(1+hλ) yny_{n+1} = (1 + h\lambda)\,y_n. The error made in one step is of order h2h^2, and accumulated over a fixed time interval the global error is of order hh.

Classical Runge-Kutta (RK4) evaluates the slope four times per step:

k1=f(tn,yn),k2=f(tn+h2, yn+h2k1),k3=f(tn+h2, yn+h2k2),k4=f(tn+h, yn+h k3),\begin{aligned} k_1 &= f(t_n, y_n), \\ k_2 &= f\bigl(t_n + \tfrac{h}{2},\, y_n + \tfrac{h}{2}k_1\bigr), \\ k_3 &= f\bigl(t_n + \tfrac{h}{2},\, y_n + \tfrac{h}{2}k_2\bigr), \\ k_4 &= f\bigl(t_n + h,\, y_n + h\,k_3\bigr), \end{aligned}

and combines them into the next value:

yn+1=yn+h6 (k1+2k2+2k3+k4).y_{n+1} = y_n + \frac{h}{6}\,(k_1 + 2k_2 + 2k_3 + k_4).

Its global error is of order h4h^4.

Measuring the order

import math

def euler(f, t, y, h, n):
    for _ in range(n):
        y += h * f(t, y)
        t += h
    return y

def rk4(f, t, y, h, n):
    for _ in range(n):
        k1 = f(t, y)
        k2 = f(t + h / 2, y + h * k1 / 2)
        k3 = f(t + h / 2, y + h * k2 / 2)
        k4 = f(t + h, y + h * k3)
        y += h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
        t += h
    return y

f = lambda t, y: -y
exact = math.exp(-1.0)
for h in (0.1, 0.05, 0.025):
    n = round(1.0 / h)
    print(h, abs(euler(f, 0.0, 1.0, h, n) - exact), abs(rk4(f, 0.0, 1.0, h, n) - exact))

Absolute error at t=1t = 1, from running this script:

Step size hhEulerRK4
0.11.92×10−21.92 \times 10^{-2}3.33×10−73.33 \times 10^{-7}
0.059.39×10−39.39 \times 10^{-3}2.00×10−82.00 \times 10^{-8}
0.0254.65×10−34.65 \times 10^{-3}1.22×10−91.22 \times 10^{-9}

Halving hh roughly halves the Euler error and divides the RK4 error by about sixteen, consistent with first-order and fourth-order convergence.

Accuracy is not stability

For Euler on the test equation with real λ<0\lambda < 0, the numerical solution decays only if ∣1+hλ∣≤1|1 + h\lambda| \le 1, which means h≤2/∣λ∣h \le 2/|\lambda|. With λ=−50\lambda = -50 and h=0.05h = 0.05 the factor is 1−2.5=−1.51 - 2.5 = -1.5, so the computed solution grows in magnitude and flips sign at every step, even though the true solution decays quickly.

Choosing a step size by asking how accurate the answer should be is only half the job. For fast decaying dynamics the step size may be limited by stability instead.

RK4 has a larger stability region than Euler, but it is also bounded. On the negative real axis it reaches about −2.79-2.79, so the step hλ=−2.5h\lambda = -2.5 that makes Euler blow up is stable for RK4.

Stability regions of explicit Euler and classical RK4In the complex h-lambda plane, explicit Euler is stable inside the disc of radius 1 centred at minus 1. The RK4 region is larger: it reaches about minus 2.79 on the real axis and about plus and minus 2.83 j on the imaginary axis. The point h-lambda equal to minus 2.5, from lambda equal to minus 50 and h equal to 0.05, lies outside the Euler disc but inside the RK4 region.-3-2-1-3j-2j-1j1j2j3jRe hλIm hλEulerRK4hλ = −2.5
Stability regions in the complex hλ-plane. Euler is stable inside the dashed disc |1 + hλ| ≤ 1; RK4 inside the shaded region. The point hλ = −2.5 (λ = −50, h = 0.05) is outside the first and inside the second.

Stiff problems, where fast and slow dynamics coexist, usually call for implicit methods, which are outside the scope of these notes.

← Back to all articles

Search

Arrow keys move between results. Enter opens the first result.