LeoMath

Introduction to differential equations

Numerical solutions

Euler and RK4: step size, error and the connection to Taylor expansion.

about 7 min

Start from a problem

Problem

The pendulum equation θ′′+gLsin⁡θ=0\theta''+\frac gL\sin\theta=0 has no elementary closed-form solution. Yet we still want to know what θ(t)\theta(t) looks like and to draw its curve.

We need a method that computes an approximate solution using only the four arithmetic operations, and we must be able to estimate its error and know when it breaks down.

Observe

Choose "Pendulum": θ′′+γθ′+gLsin⁡θ=0\theta''+\gamma\theta'+\frac gL\sin\theta=0. This equation has no elementary closed-form solution. Yet the experiment draws a curve: it is computed step by step. Increase the step hh: the red Euler curve drifts away from the blue RK4 curve and may even diverge. Switch back to "Exponential growth", which has an exact solution, and watch the end-point errors of the two methods as hh changes: halving hh roughly halves Euler's error and divides RK4's by about 16.

Interactive experimentNumerical ODE explorer
Equation
θ′′+γθ′+gLsin⁡θ=0\theta'' + \gamma\theta' + \tfrac{g}{L}\sin\theta = 0
Time series
Phase portrait
MethodNo closed-form solution for these parameters; numerical solutions only.
Conjecture

y′=f(t,y)y'=f(t,y) tells us the current slope. From (tn,yn)(t_n,y_n), walk a short step hh along that slope to get the next approximate point. Smaller steps are more accurate, but how the error shrinks with hh depends on how refined our "one step" is, and that should be computable with a Taylor expansion.

Definitions

Definition 4.1Euler's method

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

Definition 4.2Local truncation error and global error

The local truncation error is the difference between one step taken from the exact value y(tn)y(t_n) and y(tn+1)y(t_{n+1}). The global error is ∣yN−y(T)∣|y_N-y(T)| after N=T/hN=T/h steps. A method with global error O(hp)O(h^p) has order pp.

Derivation: the order of Euler's method

Proposition 4.1Euler's method is first order

Local truncation error O(h2)O(h^2), global error O(h)O(h).

Proof

Taylor-expand the exact solution: y(tn+h)=y(tn)+hy′(tn)+h22y′′(ξ)=y(tn)+hf(tn,y(tn))+O(h2)y(t_n+h)=y(t_n)+hy'(t_n)+\tfrac{h^2}2y''(\xi)=y(t_n)+hf(t_n,y(t_n))+O(h^2). An Euler step is exactly the first two terms, so the local error is O(h2)O(h^2).

Global error: let en=yn−y(tn)e_n=y_n-y(t_n), let ff be LL-Lipschitz in yy, and let the local error be at most Ch2Ch^2. Then ∣en+1∣≤∣en∣+h∣f(tn,yn)−f(tn,y(tn))∣+Ch2≤(1+hL)∣en∣+Ch2.|e_{n+1}|\le|e_n|+h|f(t_n,y_n)-f(t_n,y(t_n))|+Ch^2\le(1+hL)|e_n|+Ch^2. Iterating, ∣eN∣≤Ch2∑k=0N−1(1+hL)k≤Ch2⋅(1+hL)N−1hL≤ChL(eLT−1)|e_N|\le Ch^2\sum_{k=0}^{N-1}(1+hL)^k\le Ch^2\cdot\dfrac{(1+hL)^N-1}{hL}\le\dfrac{Ch}L\bigl(e^{LT}-1\bigr), using (1+hL)N≤ehLN=eLT(1+hL)^N\le e^{hLN}=e^{LT}. So the global error is O(h)O(h): NN steps each committing O(h2)O(h^2) accumulate to O(h)O(h).

Higher order: Runge–Kutta

Euler uses only the slope at the left end of each step. Better: sample several slopes inside the step and average them with weights.

Definition 4.3Classical fourth-order Runge–Kutta (RK4)
k1=f(tn, yn),k2=f ⁣(tn+h2, yn+h2k1),k3=f ⁣(tn+h2, yn+h2k2),k4=f ⁣(tn+h, yn+hk3),yn+1=yn+h6 (k1+2k2+2k3+k4).\begin{aligned} k_1&=f(t_n,\,y_n),\\ k_2&=f\!\left(t_n+\tfrac h2,\ y_n+\tfrac h2k_1\right),\\ k_3&=f\!\left(t_n+\tfrac h2,\ y_n+\tfrac h2k_2\right),\\ k_4&=f\!\left(t_n+h,\ y_n+hk_3\right),\\ y_{n+1}&=y_n+\frac h6\,(k_1+2k_2+2k_3+k_4). \end{aligned}
Theorem 4.2RK4 is fourth order

Local truncation error O(h5)O(h^5), global error O(h4)O(h^4).

Proof

The full proof expands k1,…,k4k_1,\dots,k_4 to order h4h^4 and compares term by term with the expansion of y(tn+h)y(t_n+h); it is long. We prove a special case that carries the idea: f=f(t)f=f(t) independent of yy. Then k1=f(tn)k_1=f(t_n), k2=k3=f(tn+h2)k_2=k_3=f(t_n+\tfrac h2), k4=f(tn+h)k_4=f(t_n+h), and yn+1−yn=h6[f(tn)+4f(tn+h2)+f(tn+h)],y_{n+1}-y_n=\frac h6\Bigl[f(t_n)+4f\bigl(t_n+\tfrac h2\bigr)+f(t_n+h)\Bigr], which is Simpson's rule, while y(tn+1)−y(tn)=∫tntn+1fy(t_{n+1})-y(t_n)=\int_{t_n}^{t_{n+1}}f. Simpson's rule is exact for cubics with error −h52880f(4)(ξ)-\frac{h^5}{2880}f^{(4)}(\xi), i.e. O(h5)O(h^5). The general case has the same conclusion with a messier expansion. The global O(h4)O(h^4) follows from the local error by the same accumulation argument as for Euler.

This is where "halve hh, divide the error by 16" comes from: (h/2)4=h4/16(h/2)^4=h^4/16.

Theorem: why large steps diverge

Proposition 4.3Stability of Euler's method

For y′=λyy'=\lambda y with λ<0\lambda<0, Euler gives yn=(1+hλ)ny0y_n=(1+h\lambda)^ny_0. The true solution decays to 00; the numerical one decays iff ∣1+hλ∣<1|1+h\lambda|<1, i.e. h<2∣λ∣h<\dfrac2{|\lambda|}.

Proof

Substitute: yn+1=yn+hλyn=(1+hλ)yny_{n+1}=y_n+h\lambda y_n=(1+h\lambda)y_n.

Beyond h=2/∣λ∣h=2/|\lambda| the factor (1+hλ)n(1+h\lambda)^n oscillates and grows, bearing no relation to the true solution. This explains the divergence of the Euler curve at large steps. Such "stiff" problems call for implicit methods, a topic of Numerical analysis.

Common mistake

Halving the step only halves Euler's error; making hh tiny does not compensate for a low-order method, it just adds work and rounding error. And with hh too large Euler diverges. Accuracy (order) and stability (a ceiling on hh) are separate concerns; both matter.

Application

ApplicationEquations without closed-form solutions

The pendulum, the three-body problem, the Lorenz system, almost every realistic physical model: numerical solution is the only option. The pendulum curve in the experiment is RK4 with step hh and four function evaluations per step. The number of steps is N=T/hN=T/h; trading accuracy against work is the daily business of scientific computing.

Remark

Numerical methods are not "approximate mathematics". They have theorems of their own: order, convergence and stability are precise, provable statements. Every letter in Euler's global bound ChL(eLT−1)\dfrac{Ch}L(e^{LT}-1) has a meaning, and it honestly tells you the error grows exponentially with TT: the further the forecast, the less reliable.

Exercises

01
Apply Euler's method with h=0.5h=0.5 to y′=y, y(0)=1y'=y,\ y(0)=1 for two steps. What approximation of y(1)y(1) results?
02
RK4 has global error O(h4)O(h^4). Halving the step size divides the error by roughly