DifferentialEquations
Chapter 8 · Section 8.6

8.6 Numerical Methods III: Runge–Kutta

Heun's method (Section 8.5) improved on Euler's by sampling the slope twice per step instead of once, and the reward was a jump from first to second order. A natural question follows: what if we sample the slope at even more places inside the step and combine the samples cleverly? Around 1900 the German mathematicians Carl Runge and Wilhelm Kutta answered it. The family of methods that carries their names contains the most widely used general-purpose ODE solver of all time, the classical fourth-order Runge–Kutta method, usually called simply RK4.

As before, we solve

on the grid , writing for the exact value and for its approximation.

The classical RK4 formulas

Definition 8.6.1 (Classical fourth-order Runge–Kutta method)

Given at , compute four slopes

and then step with their weighted average:

Each step costs four evaluations of . Notice the structure: every slope after the first is evaluated at a point reached by moving from with the previous slope. The method is explicit: each line uses only quantities already computed.

Another common notation

Many books and programs put the factor inside the stages: , , and so on, with . This is the same method; only the bookkeeping differs (). In this course the are always slopes.

What the four slopes mean

It helps to read the formulas as a small exploration of the direction field across one step.

  • : slope at the start. This is the Euler slope at .
  • : first look at the midpoint. Follow for half a step to the point and read the slope there.
  • : second, better look at the midpoint. The point used for was only an Euler guess. Go back to the start, follow the improved slope for half a step, and read the slope at this new midpoint estimate.
  • : slope at the end. Follow for a full step to and read the slope there.
  • Weighted average. The end slopes get weight each and the two midpoint slopes weight each; the weights add up to , hence the .

Figure 8.6.1 shows the four samples for , , with the large step . Here , , , , and

only below . With the same step, Euler gives and Heun gives .

Figure 8.6.1. The four slopes of one RK4 step for , , . Dotted rays show how each sample point is reached from the start. The weighted step (orange) ends at , almost on the exact curve.

Why the weights 1, 2, 2, 1?

Look at the special case where depends on only. Then the -arguments play no role, , and the RK4 step becomes

The bracket is Simpson's rule for , using the two ends and the midpoint. So RK4 is to Simpson's rule what Heun's method is to the trapezoid rule. Splitting the midpoint weight into between two different midpoint estimates is what lets the method handle a slope that also depends on .

Method in a nutshell: one RK4 step
  1. .
  2. .
  3. (note: , not ).
  4. (full step, slope ).
  5. ; set and repeat.
Common mistakes

(1) Using instead of in or . (2) Building from again instead of from . (3) Forgetting to advance in , , . (4) Dividing by instead of . A quick check: if is a constant , all four slopes equal and the step must be .

Worked examples

We return to the test problem of Section 8.5,

As before, we carry full precision and round only what we display.

Example 8.6.1

Use RK4 with to approximate the solution of , , at , and compare with the exact values.

Solution
  1. First step in full. , , .
  2. Combine. The exact value is : the error is only , compared with for Heun and for Euler with the same .
  3. All steps.
    0.01.0000001.0000001.0900001.0990001.1798001.218593
    0.21.2185931.1785931.2464531.2532391.3092411.468167
    0.41.4681671.3081671.3489841.3530661.3787801.737869
    0.61.7378691.3778691.3856561.3864341.3751562.014442
    0.82.0144421.3744421.3418861.3386311.2821682.281697
  4. Errors.
    exactRK4error
    0.21.2185971.218593
    0.41.4681751.468167
    0.61.7378811.737869
    0.82.0144592.014442
    1.02.2817182.281697
    With only five steps, RK4 is correct to about five significant digits on the whole interval.

The solver below shows every of every step. Start with the preset (Example 8.6.1), then try : the error at should drop by a factor close to . You can also enter the problem of Example 8.6.2.

Example 8.6.2 (A nonlinear equation)

Take one RK4 step with for , . Compare with the exact solution.

Solution
  1. Exact solution. Separate the variables: , so . From , , and . Thus .
  2. The slopes. With :
  3. Combine. The error is about . Note that : Euler's method would have predicted no change at all, .

How accurate is RK4?

The analysis follows the pattern of Section 8.5. We expand the RK4 step and the exact solution in powers of and compare. For Heun's method they agreed up to . For RK4 they agree up to . The algebra is long, because every must be expanded with the two-variable Taylor formula up to fourth order, so we state the result and then check it on a case where the computation is short.

Theorem 8.6.1 (Order of the classical Runge–Kutta method)

Suppose has continuous partial derivatives up to order four near the solution curve, so that the solution has a continuous fifth derivative on . Then RK4 has local truncation error and its global error satisfies

with a constant independent of . RK4 is a fourth-order method: halving divides the error by about .

A check on the test equation

For ( constant) put . The slopes are

Add them with weights :

Multiply by (remember ) and add :

The exact solution satisfies , and . RK4 reproduces the series exactly through and first misses at : a local error of order , as the theorem says. For comparison, Euler's multiplier is and Heun's is (Exercise 4 of Section 8.5).

The Runge–Kutta family

Euler, Heun, the midpoint method and RK4 are all explicit Runge–Kutta methods: each step evaluates at a few trial points built from earlier slopes and takes a weighted average. With slope evaluations ("stages") one can reach order for . Beyond that the pattern breaks: order needs at least six stages. This is one reason why four stages and fourth order are such a popular compromise.

A fair comparison: Euler, Heun and RK4

Counting steps is unfair to Euler, since an RK4 step does four times as much work. The honest measure of cost is the number of evaluations of , because in real problems that is where the computing time goes. So let us give every method the same budget.

Example 8.6.3 (Equal cost)

For , , allow each method exactly evaluations of to reach . Which method is most accurate?

Solution
  1. Step sizes. Euler uses evaluation per step, so steps: . Heun uses per step, so steps: . RK4 uses per step, so steps: .
  2. Results at (exact ):
    method at error
    Euler0.052.2640370.017681
    Heun0.12.2777570.003961
    RK40.22.2816970.000021
  3. Conclusion. For the same work, RK4 is about times more accurate than Euler and about times more accurate than Heun. The extra effort per step pays off many times over.
Figure 8.6.2. Errors along for the three methods of Example 8.6.3, each using 20 slope evaluations. Note the logarithmic vertical axis: RK4 is almost three orders of magnitude better than Euler.

The advantage grows as we ask for more accuracy. The table lists the error at for several step sizes; the last column shows how much the RK4 error shrinks each time is halved.

EulerHeunRK4RK4 ratio
0.2–
0.115.95
0.0516.02
0.02516.02
Figure 8.6.3. Error at against on logarithmic axes. The three methods follow lines of slope , and : the slope of each line is the order of the method.

Estimating the error without the exact solution

In practice we do not know the exact solution, so how can we judge the accuracy of an RK4 result? A simple and reliable trick is step doubling: compute the same value twice, with steps and .

Call the results and , and the true value . Since the method is fourth order,

Subtracting the second relation from the first eliminates the unknown :

Example 8.6.4 (Step doubling)

RK4 for , gives at with , and with . Estimate the error of the better value, pretending we do not know the exact solution.

Solution
  1. Estimate.
  2. Check. The true error is . The estimate is excellent. Adaptive solvers in engineering software use ideas of exactly this kind to choose automatically.
Example 8.6.5 (Computing with RK4)

Since , the solution of , , satisfies . Use RK4 with to estimate .

Solution
  1. First step (). Since does not involve , :
  2. Second step ().
  3. Result. , compared with . Four slope evaluations at five distinct points give five correct significant digits. (This is Simpson's rule with two panels.)

Summary

  • RK4 samples the slope at the start, twice at the midpoint, and at the end of each step, and averages with weights .
  • It is fourth order: global error , local error . Halving divides the error by about .
  • For it reduces to Simpson's rule.
  • At equal cost (same number of -evaluations) it is far more accurate than Euler or Heun, which is why it is the default choice for smooth problems.
  • Step doubling gives a practical error estimate: .

Exercises

  1. Take one RK4 step with for , , listing . Compare with the exact value and with the Heun result of Exercise 1 in Section 8.5.
    Answer , , , ; (error , versus for Heun).
  2. Use RK4 with to approximate for , . Compare with .
    Answer , ; .
  3. Use RK4 with to approximate for , . (Compare with the Heun result of Exercise 3, Section 8.5, and with the accurate value .)
    Answer , (Heun gave ).
  4. For the draining tank , of Example 8.5.3, take one RK4 step with .
    Answer , , , ; (exact ).
  5. Show directly from Definition 8.6.1 that RK4 gives the exact answer, for any , when (a cubic in alone).
    Answer Here , so the step is Simpson's rule on , which integrates cubics exactly. Hence exactly.
  6. For the RK4 multiplier is with . Evaluate and compare with . Do the same for Euler's multiplier .
    Answer , (difference ); Euler gives (difference ).
  7. Use RK4 with on , , to estimate .
    Answer , ; .
  8. In Example 8.6.1 the RK4 error at was with . Using the fourth-order law, estimate the step size needed for an error below . How many evaluations of is that?
    Answer , so ; e.g. (40 steps, 160 evaluations), which gives .
  9. For , , RK4 gives at with and with . Estimate the error of the second value by step doubling and compare with the true error.
    Answer ; the true error is .
  10. A population model reads , (in thousands, in years). Use RK4 with to estimate , and compare with the exact logistic solution .
    Answer , ; exact .