Explicit one-step methods for ODEs: Euler, Heun and Runge–Kutta
Euler's method with its global error bound, the second-order Heun and midpoint methods, classical RK4, and how to measure the order of a method by step halving.
§15.1Estimated reading time: 16 min
Most differential equations met in applications have no closed-form solution, so the solution is computed step by step on a grid. This unit develops the explicit one-step methods and answers the questions a laboratory report must answer: how large is the error, how does it depend on the step size h, and what does a higher-order method cost? It extends the Euler experiments in lab 3 and complements Implicit Euler and the meaning of stability, which treats stiff decay; here we stay with explicit methods and accuracy.
The initial-value problem and Euler's method
We solve y′=f(t,y), y(t0)=y0 on [t0,T], with grid tn=t0+nh and h=(T−t0)/N.
Definition 15.1.1
(One-step method, Euler's method, local and global error)
A one-step method computes yn+1 from yn alone; it is explicit if yn+1 is given by a formula without solving an equation. Euler's method is
yn+1=yn+hf(tn,yn).
Let y(t) denote the exact solution. The global error is en=y(tn)−yn. The error made in a single step when the starting value is exact is the local error.
Local error of Euler. If y∈C2, Taylor's theorem gives y(tn+1)=y(tn)+hy′(tn)+2h2y′′(ξn) for some ξn∈(tn,tn+1), and y′(tn)=f(tn,y(tn)). So the local error is 2h2y′′(ξn), of order h2.
Theorem 15.1.2
(Global error of Euler's method)
Suppose f is Lipschitz in y with constant L>0, that is ∣f(t,u)−f(t,v)∣≤L∣u−v∣ for all relevant t,u,v, and the solution satisfies ∣y′′∣≤M on [t0,T]. Then for y0 exact,
∣en∣≤2LhM(eL(tn−t0)−1).
Proof
(A recursion for the error, then a geometric sum)
Subtract Euler's formula from the Taylor expansion:
so ∣en+1∣≤(1+hL)∣en∣+2h2M. With e0=0, induction gives ∣en∣≤2h2Mk=0∑n−1(1+hL)k=2h2M⋅hL(1+hL)n−1=2LhM((1+hL)n−1). Since 1+hL≤ehL, (1+hL)n≤enhL=eL(tn−t0).
Proof X-ray
Where each hypothesis enters the recursion
The Lipschitz condition is used once, in the bracket: it turns the difference of slopes at the exact and the computed point into at most L∣en∣, which is why the previous error is multiplied by 1+hL rather than by something uncontrolled. The bound ∣y′′∣≤M is used once, to cap the fresh local error by h2M/2 at every step. The key move is to unfold the one-line recursion ∣en+1∣≤(1+hL)∣en∣+h2M/2 into a geometric sum; the sum has about 1/h terms of size h2, so the total is of order h rather than h2, and this is the exact place where one power of h is lost. The last inequality 1+hL≤ehL trades the sharp but awkward (1+hL)n for a quantity that does not depend on n and h separately, which is why the factor eL(tn−t0) appears and can be large for long intervals. Exactness of y0 makes e0=0; with an initial error the bound would carry an extra eL(tn−t0)∣e0∣ term.
Thus the global error is of order h: the local error is O(h2) but the accumulation of N∼1/h steps loses one power, and Euler's method is first order.
Second-order methods
Definition 15.1.3
(Order of a method)
A method has order p if its global error is O(hp) for smooth f.
Two classical second-order methods use two evaluations of f per step.
If f is twice continuously differentiable, the local error of Heun's method and of the midpoint method is O(h3). A local error O(h3) accumulates to a global error O(h2).
Proof
(Matching the Taylor expansion through order two)
Write f,ft,fy for values at (tn,yn) with yn=y(tn). Since y′′=ft+ffy, Taylor's theorem gives y(tn+1)=yn+hf+2h2(ft+ffy)+O(h3). For Heun's method, k2=f(tn+h,yn+hf)=f+hft+hffy+O(h2), so yn+1=yn+hf+2h2(ft+ffy)+O(h3). The two expansions agree up to O(h3). The same computation works for the midpoint method, where k2=f+2hft+2hffy+O(h2) and the factor h in front of k2 restores the same 2h2 terms.
On the test equation y′=λy, with z=hλ, Heun and midpoint both give yn+1=(1+z+2z2)yn, the Taylor polynomial of ez of degree 2, which explains the second-order accuracy.
It is a standard result that this method has local error O(h5) and global error O(h4). For y′=λy we can verify the matching with the exponential directly. With z=hλ and yn=y: k1=λy, k2=λ(1+2z)y, k3=λ(1+2z+4z2)y, k4=λ(1+z+2z2+4z3)y. Then k1+2k2+2k3+k4=λy(6+3z+z2+4z3) and
yn+1=(1+z+2z2+6z3+24z4)yn,
the degree-4 Taylor polynomial of ez.
RK4 needs four evaluations of f per step, Heun two and Euler one. Cost is measured in evaluations of f, and the right comparison is accuracy at equal cost, not at equal h.
Measuring the order experimentally
If E(h)≈Chp then E(h)/E(h/2)≈2p, so p≈log2E(h/2)E(h). The approximation is only valid for h small enough that the leading term dominates, and requires the exact solution (or a much finer reference solution) to compute E.
Worked examples
Worked example 15.1.5
(All three methods on y′=y)
Take y′=y, y(0)=1, T=1. The exact value is y(1)=e≈2.71828. For y′=λy each method multiplies by a fixed factor per step. With h=21, so z=21 and two steps:
With h=41 (four steps): Euler 1.254=2.441406, error 0.27688; Heun factor 1.28125, y4=2.694856, error 0.02343; RK4 error 0.0000719. Error ratios from h=21 to h=41 are 1.69, 3.31 and 13.0, approaching 2, 4 and 16 as h→0, that is orders 1, 2 and 4.
Worked example 15.1.6
(One step of two methods on a non-autonomous equation)
Let y′=y−t2+1, y(0)=21, whose exact solution is y=(t+1)2−21et, so y(21)=2.25−21e1/2=1.425639. Take h=21.
Euler: y1=21+21(21−0+1)=1.25. Error 0.1756.
Heun: k1=f(0,21)=1.5. The predictor point is (21,21+21⋅1.5)=(21,1.25), so k2=1.25−41+1=2. Then y1=21+41(1.5+2)=1.375. Error 0.0506.
A Heun loop in MATLAB, traced on this data (first pass: k1=1.5, k2=2, y=1.375):
f = @(t,y) y - t.^2 + 1;
h = 0.5; N = 1; t = 0; y = 0.5;
for n = 1:N
k1 = f(t, y);
k2 = f(t + h, y + h*k1);
y = y + h/2*(k1 + k2);
t = t + h;
end
Worked example 15.1.7
(The Euler error bound at a given step)
For y′=y on [0,1]: L=1 and ∣y′′∣=et≤e=:M, so ∣en∣≤2he(e−1)≈2.335h at t=1. For h=0.1 this is 0.2335, and the actual Euler error is e−1.110=2.71828−2.59374=0.1245. The bound holds but is pessimistic by a factor of about two. For h=21 the bound is e(e−1)/4≈1.168 against the actual error 0.468 of the first worked example, a factor of about 2.5.
Common mistakes and subtle points
Common mistake
(Quoting the local exponent as the order)
A method with local error O(hp+1) has global error O(hp). Euler's local error is 2h2y′′, but Euler is first order; calling it second order because of the h2 misreads the accumulation of N∼1/h steps.
Common mistake
(Comparing errors at different times)
Halving h in a table of errors must use the same exact solution and the same end point T; comparing errors at different times mixes in the growth factor eLt. In the first worked example the ratios 1.69, 3.31, 13.0 are all taken at t=1.
Common mistake
(Using the wrong slope in an RK4 stage)
RK4 uses k2 and k3 both at the midpoint time tn+2h, but k2 is evaluated at yn+2hk1 and k3 at yn+2hk2, while k4 uses the full step hk3. Mixing up which slope feeds which stage is the commonest hand-computation error.
The Euler bound requires f Lipschitz in y and a bounded second derivative of the exact solution; it can fail near singularities, and the bound itself is typically far from sharp.
Counterexample mode
A higher-order method need not be better at a large step
Claim: at every step size, a method of higher order gives the smaller error. Take y′=−10y, y(0)=1, one step of size h=21, so z=hλ=−5 and the exact value is e−5≈0.0067. Euler has factor 1+z=−4, Heun 1+z+2z2=8.5, and RK4 1−5+12.5−6125+24625≈13.71. All three are far from 0.0067, and the errors grow with the order. With h=0.05, z=−0.5, the factors are 0.5, 0.625 and 0.60677 against e−0.5≈0.60653, and now the order ranking is the expected one. Order describes the limit h→0; it says nothing about accuracy or stability at a given large h.
Exercises
Quick check
(For y′=−2y, y(0)=1, h=41, compute y2≈y(21) by Euler and by Heun, and compare with e−1.)
Use the per-step factors 1+z and 1+z+2z2 with z=−21.
Solution · Guided solution
Euler: factor 1+hλ=1−21=21, so y2=41. Heun: factor 1+z+2z2 with z=−21 is 1−21+81=85, so y2=6425=0.390625. The exact value is e−1=0.367879; the errors are 0.1179 (Euler) and 0.0227 (Heun).
Quick check
(Show that Heun's method applied to y′=λy with real λ<0 gives ∣yn+1∣<∣yn∣ for every yn=0 exactly when −2<hλ<0.)
Complete the square in 1+z+2z2.
Solution · Guided solution
With z=hλ real, the factor is 1+z+2z2=21((z+1)2+1)≥21>0. Therefore it is less than 1 iff (z+1)2<1, i.e. −2<z<0. For z≤−2 or z≥0 it is at least 1. So ∣yn+1∣<∣yn∣ iff −2<hλ<0.
Quick check
(Euler's method with step sizes h, h/2, h/4 gives errors 0.1200, 0.0610, 0.0307 at the same time. Estimate the order.)
Use p≈log2(E(h)/E(h/2)) for each consecutive pair.
Solution · Answer
log2(0.1200/0.0610)=log21.967≈0.976 and log2(0.0610/0.0307)=log21.987≈0.991. Both are close to 1, consistent with first order.
Quick check
(Show that the midpoint method applied to y′=λy has the same update factor as Heun's method.)
Compute k2 in each method with z=hλ.
Solution · Guided solution
With z=hλ: for the midpoint method k1=λy, k2=λ(y+2hλy)=λ(1+2z)y, so yn+1=y+hλ(1+2z)y=(1+z+2z2)y. For Heun, k2=λ(y+hλy)=λ(1+z)y, so yn+1=y+2hλy(2+z)=(1+z+2z2)y. The factors coincide.
Quick check
(For y′=y, y(0)=1 on [0,1], compute the Euler bound for h=21 and compare it with the actual error of the first worked example.)
Use L=1, M=e and tn−t0=1.
Solution · Guided solution
∣e2∣≤2(1/2)e(e−1)=4e(e−1)≈1.168. The actual error is 0.468, so the bound holds and overestimates by a factor of about 2.5.