Numerical Methods: Euler to Runge–Kutta

Study Sheet

Numerical Methods: Euler to Runge–Kutta

Heun, RK4, error orders, and stability ceilings

Computing Solutions Honestly

Tip
The ladder of methods
ty0.5112Euler 2Heun 2.5eslope 2
One step h=1h = 1 on y′=yy' = y: Euler uses slope 11, Heun averages slopes 11 and 22 (faint) and lands nearer ee.

Euler: follow the tangent, yn+1=yn+hf(tn,yn)y_{n+1} = y_n + hf(t_n, y_n) — global error O(h)O(h). Improved Euler (Heun): predict with Euler, evaluate the slope at the prediction, use the average of the two slopes — O(h2)O(h^2): on y′=yy' = y, y(0)=1y(0)=1, h=1h = 1 it gives 2.52.5 where Euler gives 22 and the truth is e≈2.718e \approx 2.718.

RK4 blends four slope samples per step for O(h4)O(h^4) — the workhorse default. Order buys compounding accuracy: halving hh improves Euler 2×2\times but RK4 16×16\times.

Tip
Stability: the other failure mode

Accuracy is not the only concern. On decay y′=−λyy' = -\lambda y, Euler iterates yn+1=(1−λh)yny_{n+1} = (1 - \lambda h)y_n: if h>2λh > \tfrac{2}{\lambda} the multiplier exceeds 11 in size and the computed solution oscillates and grows while the true one dies quietly.

Stiff problems (mixing fast and slow decay) force explicit methods to tiny steps for stability alone — the reason implicit methods (backward Euler: yn+1=yn+hf(tn+1,yn+1)y_{n+1} = y_n + hf(t_{n+1}, y_{n+1}), stable for every hh) exist.

Example
Worked: watching the error order with your own eyes

Solve y′=yy' = y, y(0)=1y(0) = 1 to t=1t = 1 (truth: e≈2.71828e \approx 2.71828).

Step 1 — Euler with h=0.5h = 0.5: (1.5)2=2.25(1.5)^2 = 2.25, error 0.4680.468.

Step 2 — Euler with h=0.25h = 0.25: (1.25)4≈2.441(1.25)^4 \approx 2.441, error 0.2770.277 — roughly halved, as O(h)O(h) promises.

Step 3 — Heun with h=0.5h = 0.5: multiplier 1+h+h22=1.6251 + h + \tfrac{h^2}{2} = 1.625 per step, (1.625)2≈2.641(1.625)^2 \approx 2.641, error 0.0780.078.

Step 4 — Heun with h=0.25h = 0.25: multiplier 1.281251.28125 per step, (1.28125)4≈2.695(1.28125)^4 \approx 2.695, error ≈0.023\approx 0.023 — about 0.30.3 of the previous error, approaching a quarter as hh shrinks — O(h2)O(h^2) verified numerically. Running two step sizes and comparing is the standard self-check every real computation should include.

Side note
Significance: most equations are solved this way

Weather forecasts, spacecraft trajectories, protein folding, and car-crash simulations never see a closed-form solution — they are RK-style steppers running billions of steps. The stability theory of this topic is not academic: an unstable integrator once meant exploding simulations and, historically, real engineering failures.

Try it
Try it: one honest Euler step

For y′=t+yy' = t + y, y(0)=1y(0) = 1, take one Euler step and one Heun step with h=0.1h = 0.1, and compare both with the exact value y(0.1)=2e0.1−1.1y(0.1) = 2e^{0.1} - 1.1.

Show the answer

Euler uses the slope at the start, f(0,1)=0+1=1f(0, 1) = 0 + 1 = 1: y1=1+0.1⋅1=1.1y_1 = 1 + 0.1\cdot1 = 1.1. Heun uses that as a predictor, evaluates the slope there, f(0.1,1.1)=1.2f(0.1, 1.1) = 1.2, and averages: y1=1+0.05(1+1.2)=1.11y_1 = 1 + 0.05(1 + 1.2) = 1.11. The exact value is 2e0.1−1.1≈1.110342e^{0.1} - 1.1 \approx 1.11034. Euler's error is about 0.01030.0103, Heun's about 0.00030.0003 — some 3030 times smaller for one extra slope evaluation, because averaging the slopes at both ends of the step captures the curvature that the single tangent misses.

Euler's Method Step by Step

Tip
Follow the tangent, then re-aim

For y′=f(t,y)y' = f(t, y), y(t0)=y0y(t_0) = y_0 and step hh:

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

Each step replaces the solution curve by its tangent line over one step, then recomputes the slope at the new point. The same formula works for systems, with yy and ff vectors.

Why it approximates: Taylor's theorem gives y(t+h)=y(t)+hy′(t)+h22y′′(ξ)y(t + h) = y(t) + hy'(t) + \tfrac{h^2}{2}y''(\xi), and y′(t)=f(t,y(t))y'(t) = f(t, y(t)), so an Euler step is the Taylor expansion with the h2h^2 term dropped. The dropped term bends toward the concave side: where solutions curve upward, Euler lands below them, and where they curve downward, above. Laying the computation out as a table — tnt_n, yny_n, slope, next yy — keeps every step checkable.

Example
Worked: an Euler table
ty0.511.50.51Eulerexacty = t − 1
Euler's polygon with h=0.5h = 0.5 (dots) against the exact solution; both are drawn toward the line y=t−1y = t - 1.

Use Euler's method with h=0.5h = 0.5 on y′=t−yy' = t - y, y(0)=1y(0) = 1, up to t=2t = 2, and compare with the exact solution y=t−1+2e−ty = t - 1 + 2e^{-t}.

Each row uses the slope at the current point: yn+1=yn+0.5 (tn−yn)y_{n+1} = y_n + 0.5\,(t_n - y_n).

tnynslope tn−ynexacterror01−1100.50.500.71310.21310.50.50.73580.2361.50.750.750.94630.19621.1251.27070.146\begin{array}{c|c|c|c|c} t_n & y_n & \text{slope } t_n - y_n & \text{exact} & \text{error} \\ \hline 0 & 1 & -1 & 1 & 0 \\ 0.5 & 0.5 & 0 & 0.7131 & 0.213 \\ 1 & 0.5 & 0.5 & 0.7358 & 0.236 \\ 1.5 & 0.75 & 0.75 & 0.9463 & 0.196 \\ 2 & 1.125 & & 1.2707 & 0.146 \end{array}

Step 1 — the first step heads down with slope −1-1, to 0.50.5.

Step 2 — at (0.5,0.5)(0.5, 0.5) the slope is 00, so the second step is flat: Euler cannot see that the true solution, which turns around near t=0.69t = 0.69, is already rising again.

Step 3 — from there the slopes are positive and each step climbs.

Step 4 — read the errors: they grow while the solution bends sharply (the first second), then shrink, because this equation pulls every solution toward the line y=t−1y = t - 1 (the difference decays like e−te^{-t}), which also pulls the Euler values toward it. Whether errors accumulate or heal depends on the equation, not only on the method.

Example
Worked: Euler on an orbit
(1, 0)step 12
Euler's steps (dots) on the oscillator spiral outward from the true orbit, the unit circle (dashed).

Apply Euler with h=0.5h = 0.5 to the oscillator x′=yx' = y, y′=−xy' = -x from (x,y)=(1,0)(x, y) = (1, 0), whose exact solutions are circles.

One step: xn+1=xn+0.5 ynx_{n+1} = x_n + 0.5\,y_n, yn+1=yn−0.5 xny_{n+1} = y_n - 0.5\,x_n. From (1,0)(1, 0): (1,−0.5)(1, -0.5), then (0.75,−1)(0.75, -1), then (0.25,−1.375)(0.25, -1.375).

Step 1 — compute the radius: xn+12+yn+12=(xn+hyn)2+(yn−hxn)2=(1+h2)(xn2+yn2)x_{n+1}^2 + y_{n+1}^2 = (x_n + hy_n)^2 + (y_n - hx_n)^2 = (1 + h^2)(x_n^2 + y_n^2), because the cross terms 2hxnyn2hx_ny_n cancel.

Step 2 — so every step multiplies the radius by 1+h2=1.25≈1.118\sqrt{1 + h^2} = \sqrt{1.25} \approx 1.118.

Step 3 — after 1212 steps (t=6t = 6, just short of one period 2π2\pi) the radius is 1.256≈3.811.25^6 \approx 3.81 instead of 11.

Step 4 — the reason is geometric: each step moves along the tangent to the circle, and a tangent segment always ends outside the circle. For a model of a frictionless system this is a qualitative failure — Euler adds energy every step — and a smaller hh only slows the spiral, it never removes it.

Side note
Where it goes wrong: stepping past a blow-up

y′=y2y' = y^2, y(0)=1y(0) = 1 has exact solution y=11−ty = \tfrac{1}{1 - t}, which is infinite at t=1t = 1. Euler with h=0.25h = 0.25 knows nothing of this: 1→1.25→1.6406→2.3135→3.65171 \to 1.25 \to 1.6406 \to 2.3135 \to 3.6517, a perfectly finite value at t=1t = 1, and it will happily continue beyond. Already at t=0.75t = 0.75 it gives 2.312.31 against the true 44. A numerical method follows the formula it is given; checking where the exact solution can exist (here, only for t<1t < 1) is the user's job, and halving hh to see whether the numbers settle is the cheapest warning.

Try it
Try it: two steps by hand

Take two Euler steps with h=0.5h = 0.5 on y′=1−yy' = 1 - y, y(0)=0y(0) = 0, and compare y2y_2 with the exact y(1)=1−e−1y(1) = 1 - e^{-1}.

Show the answer

First step: slope f(0)=1−0=1f(0) = 1 - 0 = 1, so y1=0+0.5⋅1=0.5y_1 = 0 + 0.5\cdot1 = 0.5. Second step: slope 1−0.5=0.51 - 0.5 = 0.5, so y2=0.5+0.5⋅0.5=0.75y_2 = 0.5 + 0.5\cdot0.5 = 0.75. The exact value is 1−e−1≈0.6321 - e^{-1} \approx 0.632, so the error is about 0.1180.118. Euler overshoots here because the solution curves downward (it is concave, flattening toward 11), so the tangent lines lie above it.

Runge–Kutta Methods in Practice

Tip
The classical RK4 step
k1=f(tn,yn),k2=f ⁣(tn+h2, yn+h2k1),k3=f ⁣(tn+h2, yn+h2k2),k4=f(tn+h, yn+hk3),k_1 = f(t_n, y_n), \quad k_2 = f\!\left(t_n + \tfrac h2,\, y_n + \tfrac h2k_1\right), \quad k_3 = f\!\left(t_n + \tfrac h2,\, y_n + \tfrac h2k_2\right), \quad k_4 = f(t_n + h,\, y_n + hk_3),
yn+1=yn+h6 (k1+2k2+2k3+k4).y_{n+1} = y_n + \frac h6\,(k_1 + 2k_2 + 2k_3 + k_4).

Four slope evaluations per step; global error O(h4)O(h^4).

Why these weights: when ff depends on tt alone, the step is ∫tntn+hf dt\int_{t_n}^{t_n + h}f\,dt approximated by Simpson's rule, h6(f(tn)+4f(tn+h2)+f(tn+h))\tfrac h6\big(f(t_n) + 4f(t_n + \tfrac h2) + f(t_n + h)\big), with the midpoint weight 44 split between the two midpoint estimates k2k_2 and k3k_3. In general, the stages are arranged so that the step agrees with the Taylor expansion of the true solution through the h4h^4 term; the local error is O(h5)O(h^5), and the global error, after Th\tfrac Th steps, is O(h4)O(h^4).

Example
Worked: equal work, very different accuracy
digits123error 0.0485Eulererror 0.0093Heunerror 0.00058RK4
Correct digits (−log⁡10-\log_{10} of the error at t=1t = 1) for three methods using the same 88 slope evaluations.

Solve y′=t−yy' = t - y, y(0)=1y(0) = 1 to t=1t = 1 (exact y(1)=2e−1≈0.735759y(1) = 2e^{-1} \approx 0.735759) with exactly 88 slope evaluations each: Euler with h=18h = \tfrac18 (88 steps), Heun with h=14h = \tfrac14 (44 steps of 22), RK4 with h=12h = \tfrac12 (22 steps of 44).

RK4's first step, from (0,1)(0, 1): k1=−1k_1 = -1, k2=f(0.25,0.75)=−0.5k_2 = f(0.25, 0.75) = -0.5, k3=f(0.25,0.875)=−0.625k_3 = f(0.25, 0.875) = -0.625, k4=f(0.5,0.6875)=−0.1875k_4 = f(0.5, 0.6875) = -0.1875, so y1=1+0.56(−1−1−1.25−0.1875)=137192≈0.713542y_1 = 1 + \tfrac{0.5}{6}(-1 - 1 - 1.25 - 0.1875) = \tfrac{137}{192} \approx 0.713542.

Step 1 — RK4's second step, from (0.5,137192)(0.5, \tfrac{137}{192}), gives y(1)≈0.736342y(1) \approx 0.736342 (exactly 5428973728\tfrac{54289}{73728}): error 0.000580.00058.

Step 2 — Heun with h=14h = \tfrac14 gives 390625524288≈0.745058\tfrac{390625}{524288} \approx 0.745058: error 0.00930.0093.

Step 3 — Euler with h=18h = \tfrac18 gives 57648018388608≈0.687218\tfrac{5764801}{8388608} \approx 0.687218: error 0.0490.049.

Step 4 — at equal cost, each step up the ladder buys roughly one more correct digit here, and the gap widens as the work increases: halving hh divides the errors by about 22, 44 and 1616. This is why RK4, not a finer Euler grid, is the default.

Example
Worked: RK4 on the orbit
RK4Euler
The same 1212 steps of size 0.50.5: RK4's points (dots) stay on the unit circle; Euler's (faint) spiral out.

Repeat the oscillator x′=yx' = y, y′=−xy' = -x from (1,0)(1, 0) with RK4 and h=0.5h = 0.5. How much does the radius change per step?

For a linear system u′=Au\mathbf u' = A\mathbf u, all four stages are linear in u\mathbf u, and one RK4 step multiplies by I+hA+(hA)22+(hA)36+(hA)424I + hA + \tfrac{(hA)^2}{2} + \tfrac{(hA)^3}{6} + \tfrac{(hA)^4}{24} — the Taylor series of ehAe^{hA} cut after four terms.

Step 1 — for the rotation AA, the powers cycle (A2=−IA^2 = -I), so the step matrix is c I+s Ac\,I + s\,A with c=1−h22+h424c = 1 - \tfrac{h^2}{2} + \tfrac{h^4}{24} and s=h−h36s = h - \tfrac{h^3}{6}, and it scales every radius by c2+s2=1−h672+h8576\sqrt{c^2 + s^2} = \sqrt{1 - \tfrac{h^6}{72} + \tfrac{h^8}{576}}.

Step 2 — with h=0.5h = 0.5: c2+s2≈0.999790c^2 + s^2 \approx 0.999790, so the radius shrinks by the factor 0.9998950.999895 per step.

Step 3 — after the same 1212 steps where Euler reached radius 3.813.81, RK4's radius is 0.998740.99874: a drift of about 0.13%0.13\% instead of 281%281\%, for four times the work per step.

Step 4 — note the sign: RK4 loses a trace of energy while Euler gains it. Neither conserves it exactly; long simulations of orbits use methods designed to (symplectic methods).

Side note
Where it goes wrong: averaging the four slopes equally

A natural simplification is yn+1=yn+h4(k1+k2+k3+k4)y_{n+1} = y_n + \tfrac h4(k_1 + k_2 + k_3 + k_4). On y′=yy' = y, y(0)=1y(0) = 1, h=1h = 1: k1=1k_1 = 1, k2=1.5k_2 = 1.5, k3=1.75k_3 = 1.75, k4=2.75k_4 = 2.75. The correct weights give 1+16(1+3+3.5+2.75)=2.7083‾1 + \tfrac16(1 + 3 + 3.5 + 2.75) = 2.708\overline3 (error 0.00990.0099); the plain average gives 1+14⋅7=2.751 + \tfrac14\cdot 7 = 2.75 (error 0.0320.032). Worse than the numbers suggest: expanding in hh, the plain average matches the true h3h^3 coefficient 16\tfrac16 with 316\tfrac3{16} instead, so it is only a second-order method — four evaluations for Heun's accuracy.

Try it
Try it: one RK4 step on a decay

Take one RK4 step with h=0.5h = 0.5 on y′=−2yy' = -2y, y(0)=1y(0) = 1, and compare with the exact e−1e^{-1}.

Show the answer

k1=−2(1)=−2k_1 = -2(1) = -2. k2=−2(1+0.25⋅(−2))=−2(0.5)=−1k_2 = -2\left(1 + 0.25\cdot(-2)\right) = -2(0.5) = -1. k3=−2(1+0.25⋅(−1))=−2(0.75)=−1.5k_3 = -2\left(1 + 0.25\cdot(-1)\right) = -2(0.75) = -1.5. k4=−2(1+0.5⋅(−1.5))=−2(0.25)=−0.5k_4 = -2\left(1 + 0.5\cdot(-1.5)\right) = -2(0.25) = -0.5. Then y1=1+0.56(−2−2−3−0.5)=1−7.512=0.375y_1 = 1 + \tfrac{0.5}{6}\left(-2 - 2 - 3 - 0.5\right) = 1 - \tfrac{7.5}{12} = 0.375. The exact value is e−1≈0.3679e^{-1} \approx 0.3679: error 0.0070.007 after one large step. (Euler with the same step would give 1−0.5⋅2=01 - 0.5\cdot2 = 0, error 0.370.37.) In general, RK4 on y′=λyy' = \lambda y multiplies by 1+z+z22+z36+z4241 + z + \tfrac{z^2}{2} + \tfrac{z^3}{6} + \tfrac{z^4}{24} with z=λh=−1z = \lambda h = -1, which is 0.3750.375.

Proofs & Why It Matters

Tip
Proof: Euler's global error is O(h)

One step from the true solution commits local error y(t+h)−y(t)−hy′(t)=h22y′′(ξ)y(t + h) - y(t) - hy'(t) = \tfrac{h^2}{2}y''(\xi) — the first Taylor term the tangent line drops.

Reaching a fixed time TT takes Th\tfrac Th steps; local errors accumulate (amplified at most by a bounded factor elte^{\emph{lt}} from the Lipschitz constant), totalling Th⋅O(h2)=O(h)\tfrac Th \cdot O(h^2) = O(h). The same bookkeeping with a sharper local error O(hp+1)O(h^{p+1}) yields global order pp — which is exactly how Heun (p=2p = 2) and RK4 (p=4p = 4) earn their labels. ■\blacksquare

Tip
Proof: Heun's extra order

Heun's update is y+h2[f(y)+f(y+hf(y))]y + \tfrac h2\left[f(y) + f(y + hf(y))\right]. Expand the second slope: f(y+hf(y))=f+hff′+O(h2)f(y + hf(y)) = f + hff' + O(h^2), so the update is y+hf+h22ff′+O(h3)y + hf + \tfrac{h^2}{2}ff' + O(h^3).

The true solution expands as y+hy′+h22y′′+O(h3)y + hy' + \tfrac{h^2}{2}y'' + O(h^3), and y′′=ddtf(y)=f′y′=ff′y'' = \tfrac{d}{dt}f(y) = f'y' = ff' — the h2h^2 terms match. Local error drops to O(h3)O(h^3), hence global O(h2)O(h^2): averaging the two slopes secretly reproduces the second Taylor coefficient. ■\blacksquare

Going Deeper: Worked Problems

Example
Worked: RK4 by hand, one step

Apply one RK4 step with h=0.2h = 0.2 to y′=y−ty' = y - t, y(0)=2y(0) = 2 (exact solution y=t+1+ety = t + 1 + e^t).

Step 1 — the four slopes: k1=f(0,2)=2k_1 = f(0, 2) = 2; k2=f(0.1,2+0.1⋅2)=f(0.1,2.2)=2.1k_2 = f(0.1, 2 + 0.1\cdot2) = f(0.1, 2.2) = 2.1; k3=f(0.1,2+0.1⋅2.1)=f(0.1,2.21)=2.11k_3 = f(0.1, 2 + 0.1\cdot2.1) = f(0.1, 2.21) = 2.11; k4=f(0.2,2+0.2⋅2.11)=f(0.2,2.422)=2.222k_4 = f(0.2, 2 + 0.2\cdot2.11) = f(0.2, 2.422) = 2.222.

Step 2 — weighted average: y1=2+0.26(2+2⋅2.1+2⋅2.11+2.222)=2+0.26(12.642)=2.4214y_1 = 2 + \dfrac{0.2}{6}(2 + 2\cdot2.1 + 2\cdot2.11 + 2.222) = 2 + \dfrac{0.2}{6}(12.642) = 2.4214.

Step 3 — exact: y(0.2)=1.2+e0.2=2.42140…y(0.2) = 1.2 + e^{0.2} = 2.42140\ldots — RK4 agrees to about five significant figures in one step; Euler would have given 2.42.4 (error 0.020.02).

Step 4 — the pattern: slopes at start, two midpoint estimates, and end, weighted 1:2:2:11:2:2:1 — Simpson's rule in disguise, which is why the order is four.

Example
Worked: catching instability before it bites
ty0.10.2−4−224h = 0.05h = 0.03
Euler on y′=−50yy' = -50y: with h=0.05h = 0.05 (dots, dashed) the values alternate and grow; with h=0.03h = 0.03 (open dots) they alternate and die, as e−50te^{-50t} (solid) does.

For y′=−50yy' = -50y (a stiff decay), find the largest Euler step that stays stable, and compare with backward Euler.

Step 1 — Euler: yn+1=(1−50h)yny_{n+1} = (1 - 50h)y_n; stability needs ∣1−50h∣<1|1 - 50h| < 1, i.e. h<0.04h < 0.04. With h=0.05h = 0.05 the multiplier is −1.5-1.5: the numbers alternate sign and grow 50%50\% per step — nonsense for a decaying solution.

Step 2 — backward Euler: yn+1=yn−50hyn+1y_{n+1} = y_n - 50hy_{n+1}, so yn+1=yn1+50hy_{n+1} = \dfrac{y_n}{1 + 50h}; the multiplier is below 11 for every h>0h > 0 — unconditionally stable.

Step 3 — the trade: backward Euler costs an algebraic solve per step (trivial here, a Newton iteration in general) but lets the step size follow accuracy instead of stability.

Step 4 — this is why "stiff" is a word every simulation engineer knows: the fast-decaying mode is boring but still dictates explicit step sizes.