Heun’s Method

Heun’s method (the improved Euler method) solves y′ = f(x, y) numerically with a predictor-corrector step. First predict with an Euler step, ỹ = yₙ + h·f(xₙ, yₙ). Then correct by averaging the slopes at both ends: yₙ₊₁ = yₙ + (h/2)[f(xₙ, yₙ) + f(xₙ₊₁, ỹ)]. Its global error is O(h²).

Before this lesson: Euler’s Method, Trapezoidal Rule and Order of Convergence

The idea: average the slope at both ends

Euler’s method uses only the slope at the start of each step. If the solution curves upward, that slope is too small for the whole step, so Euler’s method falls below the curve and its error shrinks only in proportion to h.

Heun’s method fixes this with two stages. The predictor takes an ordinary Euler step to guess where the solution will be. The corrector measures the slope at that guess and then redoes the step with the average of the starting slope and the predicted end slope.

Averaging the two end slopes is the same idea as the trapezoidal rule for integrals. One extra function evaluation per step buys a whole order of accuracy: halving h cuts the error by about 4 instead of 2.

Graph of y = e to the x from x = 0 to x = 1.2. One Heun step with h = 1 for y prime = y starting at (0, 1): the Euler predictor line with slope 1 reaches (1, 2), the slope there is 2, and the averaged slope 1.5 gives the corrected point (1, 2.5). The exact value is e, about 2.718.
One Heun step for y′ = y: the predictor reaches 2, the corrector averages slopes 1 and 2 and lands at 2.5, much closer to e ≈ 2.718.

Heun’s method formula

For the initial value problem y′ = f(x, y), y(x₀) = y₀, with step size h and xₙ₊₁ = xₙ + h, each step of Heun’s method is

\begin{aligned} k_1 &= f(x_n, y_n) \\ \tilde{y}_{n+1} &= y_n + h k_1 \\ k_2 &= f(x_{n+1}, \tilde{y}_{n+1}) \\ y_{n+1} &= y_n + \frac{h}{2}\left(k_1 + k_2\right) \end{aligned}

The local error of one step is O(h³), and the global error at a fixed x is O(h²), so Heun’s method is a second-order method. It is the simplest two-stage Runge-Kutta method (RK2), and it costs two evaluations of f per step, compared with one for Euler’s method and four for RK4.

How to use Heun’s method

  1. Compute the slope k₁ = f(xₙ, yₙ) at the current point.
  2. Predict: ỹ = yₙ + h·k₁ (an Euler step).
  3. Compute the slope k₂ = f(xₙ₊₁, ỹ) at the predicted point.
  4. Correct: yₙ₊₁ = yₙ + (h/2)(k₁ + k₂).
  5. Repeat from the new point until you reach the target x. A table keeps the work organized.

Heun’s method examples

Example 1: Two steps by hand

Use Heun’s method with h = 0.1 to estimate y(0.2) for y′ = y − x, y(0) = 2. The exact solution is y = x + 1 + eˣ.

k_1 = f(0, 2) = 2 - 0 = 2

Slope at the start (x₀, y₀) = (0, 2).

\tilde{y}_1 = 2 + 0.1(2) = 2.2

Predictor: an Euler step.

k_2 = f(0.1, 2.2) = 2.2 - 0.1 = 2.1

Slope at the predicted point.

y_1 = 2 + 0.05(2 + 2.1) = 2 + 0.205 = 2.205

Corrector: h/2 = 0.05 times the sum of the slopes.

k_1 = f(0.1, 2.205) = 2.205 - 0.1 = 2.105

Second step: slope at (0.1, 2.205).

\tilde{y}_2 = 2.205 + 0.1(2.105) = 2.4155

Predictor.

k_2 = f(0.2, 2.4155) = 2.4155 - 0.2 = 2.2155

Slope at the predicted point.

y_2 = 2.205 + 0.05(2.105 + 2.2155) = 2.205 + 0.216025 = 2.421025

Corrector.

y(0.2) = 0.2 + 1 + e^{0.2} \approx 2.421403

Exact value, for comparison.

Answer: y(0.2) ≈ 2.421025, with error about 0.000378. Euler’s method with the same h gives 2.41, an error of about 0.0114, roughly 30 times larger.

Example 2: Checking that the error is O(h²)

Estimate y(1) for y′ = y, y(0) = 1, whose exact value is e ≈ 2.718282. For y′ = y, one Heun step multiplies y by a fixed factor.

k_1 = y_n,\quad \tilde{y} = y_n + h y_n = (1 + h)y_n

Slope and predictor for f(x, y) = y.

k_2 = (1 + h)y_n

The slope at the predicted point is the predicted value.

y_{n+1} = y_n + \tfrac{h}{2}\left(y_n + (1 + h)y_n\right) = \left(1 + h + \tfrac{h^2}{2}\right)y_n

Corrector, then factor out yₙ.

y(1) \approx \left(1 + h + \tfrac{h^2}{2}\right)^{1/h}

Take 1/h steps from y₀ = 1.

hHeun estimateHeun errorEuler estimateEuler error
0.51.625² = 2.6406250.0776571.5² = 2.250.468282
0.251.28125⁴ ≈ 2.6948560.0234261.25⁴ ≈ 2.4414060.276876
0.1251.1328125⁸ ≈ 2.7118410.0064411.125⁸ ≈ 2.5657850.152497
\dfrac{0.077657}{0.023426} \approx 3.31,\quad \dfrac{0.023426}{0.006441} \approx 3.64

Heun error ratios when h is halved.

\dfrac{0.468282}{0.276876} \approx 1.69,\quad \dfrac{0.276876}{0.152497} \approx 1.82

Euler error ratios when h is halved.

Answer: halving h cuts Heun’s error by a factor approaching 4 = 2², and Euler’s by a factor approaching 2. So Heun’s method has order 2 and Euler’s method has order 1. The factor 1 + h + h²/2 matches the Taylor series of eʰ through the h² term, which is another way to see the second order.

Example 3: A nonlinear equation

Use Heun’s method with h = 0.2 to estimate y(0.4) for y′ = 1 + y², y(0) = 0. The exact solution is y = tan x.

k_1 = f(0, 0) = 1 + 0^2 = 1

Slope at the start.

\tilde{y}_1 = 0 + 0.2(1) = 0.2

Predictor.

k_2 = f(0.2, 0.2) = 1 + 0.04 = 1.04

Slope at the predicted point.

y_1 = 0 + 0.1(1 + 1.04) = 0.204

Corrector with h/2 = 0.1.

k_1 = f(0.2, 0.204) = 1 + 0.041616 = 1.041616

Second step: 0.204² = 0.041616.

\tilde{y}_2 = 0.204 + 0.2(1.041616) = 0.204 + 0.2083232 = 0.4123232

Predictor.

k_2 = f(0.4, 0.4123232) = 1 + 0.1700104 = 1.1700104

0.4123232² ≈ 0.1700104.

y_2 = 0.204 + 0.1(1.041616 + 1.1700104) = 0.204 + 0.2211626 = 0.4251626

Corrector.

\tan 0.4 \approx 0.4227932

Exact value.

Answer: y(0.4) ≈ 0.42516, with error about 0.0024. The solution bends upward faster and faster (it blows up at x = π/2), so a fixed step size loses accuracy as y grows; a smaller h near the steep part would help.

Example 4: Predicting a fish population (word problem)

A lake is stocked with 100 fish. The population P (in fish) after t years follows the logistic model dP/dt = 0.5P(1 − P/1000). Use Heun’s method with h = 1 year to estimate the population after 2 years.

k_1 = 0.5(100)\left(1 - \tfrac{100}{1000}\right) = 50(0.9) = 45

Growth rate at t = 0 (see logistic differential equations).

\tilde{P}_1 = 100 + 1(45) = 145

Predictor.

k_2 = 0.5(145)(1 - 0.145) = 72.5(0.855) = 61.9875

Growth rate at the predicted point, t = 1.

P_1 = 100 + 0.5(45 + 61.9875) = 100 + 53.49375 = 153.49375

Corrector with h/2 = 0.5.

k_1 = 0.5(153.49375)(1 - 0.15349375) \approx 76.746875(0.84650625) \approx 64.96671

Second step: rate at t = 1.

\tilde{P}_2 = 153.49375 + 64.96671 = 218.46046

Predictor.

k_2 = 0.5(218.46046)(1 - 0.21846046) \approx 109.23023(0.78153954) \approx 85.36774

Rate at the predicted point, t = 2.

P_2 = 153.49375 + 0.5(64.96671 + 85.36774) \approx 153.49375 + 75.16723 = 228.66098

Corrector.

Answer: about 229 fish after 2 years. The exact solution P = 1000/(1 + 9e^(−0.5t)) gives about 232, so the estimate is within 1.5%. Euler’s method with h = 1 gives only 145 + 61.9875 ≈ 207 fish.

Example 5: A second-order equation as a system

The spring equation u″ = −u with u(0) = 1 and u′(0) = 0 has exact solution u = cos t. Write it as a system of first-order equations and take one Heun step with h = 0.5.

u' = v,\quad v' = -u,\quad (u_0, v_0) = (1, 0)

Let v = u′; then v′ = u″ = −u.

\mathbf{k}_1 = (v_0, -u_0) = (0, -1)

Slope vector at the start.

(\tilde{u}, \tilde{v}) = (1 + 0.5(0),\ 0 + 0.5(-1)) = (1, -0.5)

Predictor for both variables at once.

\mathbf{k}_2 = (\tilde{v}, -\tilde{u}) = (-0.5, -1)

Slope vector at the predicted point.

u_1 = 1 + 0.25(0 + (-0.5)) = 1 - 0.125 = 0.875

Corrector for u with h/2 = 0.25.

v_1 = 0 + 0.25(-1 + (-1)) = -0.5

Corrector for v.

(\cos 0.5,\ -\sin 0.5) \approx (0.877583, -0.479426)

Exact values.

Answer: u(0.5) ≈ 0.875 and u′(0.5) ≈ −0.5, compared with the exact 0.877583 and −0.479426. For a system, every stage is done for all variables together: predict every variable before computing any k₂.

Common mistakes

MistakeExample of the errorFix
Evaluating k₂ at the old xUsing f(0, 2.2) = 2.2 instead of f(0.1, 2.2) = 2.1 in Example 1The predicted point is (xₙ₊₁, ỹ): advance x by h too.
Using h instead of h/2 in the correctory₁ = 2 + 0.1(2 + 2.1) = 2.41The corrector averages two slopes: multiply their sum by h/2.
Reporting the predictor as the answerGiving ỹ₁ = 2.2 as y(0.1)The predictor is only a trial value; the corrected yₙ₊₁ is the estimate.
Correcting from the predicted valuey₁ = 2.2 + 0.05(2 + 2.1)Start the corrector from yₙ, not from ỹ.
Updating one variable of a system before the othersUsing u₁ to compute the predictor for vCompute all of k₁, then all predictors, then all of k₂.

Related methods

Euler’s method and RK4

Euler’s method is first order and uses one slope per step. Heun’s method is second order with two slopes, and the classical Runge-Kutta method (RK4) is fourth order with four slopes. For the same total number of function evaluations, the higher-order method is usually far more accurate on smooth problems.

The midpoint method

The midpoint method is another RK2 method: take a half Euler step to y + (h/2)k₁, find the slope k₂ there at x + h/2, and set yₙ₊₁ = yₙ + h·k₂. It is also second order, and for linear equations with constant coefficients it gives the same results as Heun’s method.

Heun’s method and the trapezoidal rule

When f depends only on x, the equation y′ = f(x) is just an integral, and Heun’s step yₙ₊₁ = yₙ + (h/2)[f(xₙ) + f(xₙ₊₁)] is exactly the trapezoidal rule. The implicit trapezoidal method uses yₙ₊₁ itself inside f; Heun’s method replaces that unknown value with the Euler predictor so no equation has to be solved.

Predictor-corrector methods

Heun’s method is the simplest predictor-corrector method. Multistep methods such as Adams-Bashforth (predictor) and Adams-Moulton (corrector) use the same pattern with several earlier points; see Multistep Methods. The gap between the corrected and predicted values also gives a rough estimate of the local error, which adaptive solvers use to choose h.

Practice problems

  1. Use one step of Heun’s method with h = 0.2 to estimate y(0.2) for y′ = 3 − y, y(0) = 1. The exact value is 3 − 2e^(−0.2).
  2. Use Heun’s method with h = 0.5 to estimate y(1) for y′ = xy, y(0) = 1. Compare with the exact value e^0.5.
  3. Show that when f depends only on x, one Heun step is the trapezoidal rule for the integral of f from xₙ to xₙ₊₁.
  4. Heun’s method has error 0.0004 at x = 1 with h = 0.1. Estimate the error with h = 0.05.
  5. For y′ = y, y(0) = 1, compare Heun’s method with h = 0.2 and Euler’s method with h = 0.1 for y(0.4). Both use four evaluations of f.
  6. Show that for y′ = λy, one Heun step multiplies y by 1 + hλ + (hλ)²/2. What happens for λ = −10 and h = 0.3?
  7. A tank drains so that its water depth h (in meters) satisfies dh/dt = −0.6√h, with h(0) = 9 and t in minutes. Use one Heun step of 1 minute to estimate the depth at t = 1. The exact depth is 7.29 m.
  8. Take one Heun step with step size 0.1 for the system u′ = v, v′ = −4u, u(0) = 0, v(0) = 2. The exact solution is u = sin 2t, v = 2cos 2t.
  9. In Example 1, find the difference between the corrected value y₁ and the predicted value ỹ₁. What does it estimate?
  10. Use one step of the midpoint method with h = 0.2 for y′ = 3 − y, y(0) = 1, and compare with Problem 1.
Show answers

1.

k_1 = 3 - 1 = 2,\quad \tilde{y}_1 = 1 + 0.2(2) = 1.4

Slope at the start and the predictor.

k_2 = 3 - 1.4 = 1.6

Slope at (0.2, 1.4).

y_1 = 1 + 0.1(2 + 1.6) = 1 + 0.36 = 1.36

Corrector with h/2 = 0.1.

3 - 2e^{-0.2} \approx 1.362538

Exact value.

Answer: y(0.2) ≈ 1.36, error about 0.0025

2.

k_1 = 0(1) = 0,\quad \tilde{y}_1 = 1 + 0.5(0) = 1

Step 1: slope and predictor.

k_2 = 0.5(1) = 0.5,\quad y_1 = 1 + 0.25(0 + 0.5) = 1.125

Slope at (0.5, 1), then the corrector.

k_1 = 0.5(1.125) = 0.5625,\quad \tilde{y}_2 = 1.125 + 0.5(0.5625) = 1.40625

Step 2: slope and predictor.

k_2 = 1(1.40625) = 1.40625

Slope at (1, 1.40625).

y_2 = 1.125 + 0.25(0.5625 + 1.40625) = 1.125 + 0.4921875 = 1.6171875

Corrector.

Answer: y(1) ≈ 1.6172; the exact value e^0.5 ≈ 1.6487, so the error is about 0.032

3.

k_1 = f(x_n),\quad k_2 = f(x_{n+1})

f ignores y, so the predictor does not affect k₂.

y_{n+1} - y_n = \dfrac{h}{2}\left[f(x_n) + f(x_{n+1})\right]

Corrector, rearranged.

Answer: the right side is the trapezoidal rule for ∫ f(x) dx from xₙ to xₙ₊₁, and the left side is that integral’s estimate, so the two methods agree

4.

\text{error} \approx 0.0004 \cdot \left(\tfrac{1}{2}\right)^2 = 0.0001

Second order: halving h divides the error by about 2² = 4.

Answer: about 0.0001

5.

\text{Heun factor} = 1 + 0.2 + \tfrac{0.04}{2} = 1.22,\quad 1.22^2 = 1.4884

Two Heun steps (Example 2’s formula).

\text{Euler factor} = 1.1,\quad 1.1^4 = 1.4641

Four Euler steps.

e^{0.4} \approx 1.491825

Exact value.

Answer: Heun’s error is about 0.0034 and Euler’s is about 0.0277, so Heun’s method is about 8 times more accurate for the same work

6.

k_1 = \lambda y_n,\quad \tilde{y} = (1 + h\lambda)y_n,\quad k_2 = \lambda(1 + h\lambda)y_n

Apply each stage to f = λy.

y_{n+1} = y_n + \tfrac{h}{2}\left(\lambda y_n + \lambda(1 + h\lambda)y_n\right) = \left(1 + h\lambda + \tfrac{(h\lambda)^2}{2}\right)y_n

Corrector, then factor.

1 + (-3) + \tfrac{(-3)^2}{2} = 1 - 3 + 4.5 = 2.5

Substitute hλ = −3.

Answer: each step multiplies y by 2.5, so the numerical solution grows without bound although the true solution e^(−10x) decays; h is too large for stability (see stiff differential equations)

7.

k_1 = -0.6\sqrt{9} = -1.8,\quad \tilde{h} = 9 + 1(-1.8) = 7.2

Rate at t = 0 and the predictor.

k_2 = -0.6\sqrt{7.2} \approx -0.6(2.683282) \approx -1.609969

Rate at the predicted depth.

h_1 = 9 + 0.5(-1.8 - 1.609969) = 9 - 1.704984 = 7.295016

Corrector with h/2 = 0.5.

Answer: about 7.295 m, only 0.005 m above the exact 7.29 m

8.

\mathbf{k}_1 = (v_0, -4u_0) = (2, 0),\quad (\tilde{u}, \tilde{v}) = (0.2, 2)

Slopes at the start and the predictor.

\mathbf{k}_2 = (\tilde{v}, -4\tilde{u}) = (2, -0.8)

Slopes at the predicted point.

u_1 = 0 + 0.05(2 + 2) = 0.2,\quad v_1 = 2 + 0.05(0 - 0.8) = 1.96

Corrector with h/2 = 0.05.

Answer: (u, v) ≈ (0.2, 1.96); exact (sin 0.2, 2cos 0.2) ≈ (0.198669, 1.960133)

9.

y_1 - \tilde{y}_1 = 2.205 - 2.2 = 0.005

Corrected minus predicted.

Answer: 0.005, an estimate of the local error of the Euler predictor (the true Euler error is 2.205171 − 2.2 ≈ 0.0052); adaptive solvers use differences like this to choose h

10.

k_1 = 2,\quad y_{1/2} = 1 + 0.1(2) = 1.2

Half Euler step to the midpoint.

k_2 = f(0.1, 1.2) = 3 - 1.2 = 1.8

Slope at the midpoint.

y_1 = 1 + 0.2(1.8) = 1.36

Full step with the midpoint slope.

Answer: 1.36, the same as Heun’s method, as expected for a linear equation with constant coefficients

Heun’s method FAQ

What is Heun’s method?

Heun’s method is a numerical method for y′ = f(x, y) that predicts with an Euler step and then corrects by averaging the slopes at the start and at the predicted end of the step. It is also called the improved Euler method or the explicit trapezoidal method.

Is Heun’s method the same as the improved Euler method?

Yes. Most textbooks use the two names for the same predictor-corrector formula. It is also a second-order Runge-Kutta method (RK2); the midpoint method is a different RK2 method.

What is the order of accuracy of Heun’s method?

Heun’s method is second order: the error at a fixed x is proportional to h², so halving the step size cuts the error by about 4. Each single step has local error proportional to h³.

Why is Heun’s method better than Euler’s method?

It uses the average of two slopes instead of only the starting slope, which cancels Euler’s first-order error. For one extra function evaluation per step, it is usually far more accurate than Euler’s method with the same total work.

Summary

  • Heun’s method predicts ỹ = yₙ + h·f(xₙ, yₙ) and corrects yₙ₊₁ = yₙ + (h/2)[f(xₙ, yₙ) + f(xₙ₊₁, ỹ)].
  • It is also called the improved Euler method and is a second-order Runge-Kutta method.
  • Its global error is O(h²): halving h divides the error by about 4.
  • It needs two evaluations of f per step and works the same way for systems, one stage at a time.
  • When f depends only on x, it reduces to the trapezoidal rule.

Previous lesson: Newton’s Method for Systems

Next lesson: Multistep Methods

Back to the Numerical Methods Index