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
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.
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 .
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 .
- .
- .
- (note: , not ).
- (full step, slope ).
- ; set and repeat.
(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.
Use RK4 with to approximate the solution of , , at , and compare with the exact values.
Solution
- First step in full. , , .
- Combine. The exact value is : the error is only , compared with for Heun and for Euler with the same .
- All steps.
0.0 1.000000 1.000000 1.090000 1.099000 1.179800 1.218593 0.2 1.218593 1.178593 1.246453 1.253239 1.309241 1.468167 0.4 1.468167 1.308167 1.348984 1.353066 1.378780 1.737869 0.6 1.737869 1.377869 1.385656 1.386434 1.375156 2.014442 0.8 2.014442 1.374442 1.341886 1.338631 1.282168 2.281697 - Errors.
With only five steps, RK4 is correct to about five significant digits on the whole interval.exact RK4 error 0.2 1.218597 1.218593 0.4 1.468175 1.468167 0.6 1.737881 1.737869 0.8 2.014459 2.014442 1.0 2.281718 2.281697
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.
Take one RK4 step with for , . Compare with the exact solution.
Solution
- Exact solution. Separate the variables: , so . From , , and . Thus .
- The slopes. With :
- 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.
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).
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.
For , , allow each method exactly evaluations of to reach . Which method is most accurate?
Solution
- Step sizes. Euler uses evaluation per step, so steps: . Heun uses per step, so steps: . RK4 uses per step, so steps: .
- Results at (exact ):
method at error Euler 0.05 2.264037 0.017681 Heun 0.1 2.277757 0.003961 RK4 0.2 2.281697 0.000021 - 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.
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.
| Euler | Heun | RK4 | RK4 ratio | |
|---|---|---|---|---|
| 0.2 | – | |||
| 0.1 | 15.95 | |||
| 0.05 | 16.02 | |||
| 0.025 | 16.02 |
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 :
RK4 for , gives at with , and with . Estimate the error of the better value, pretending we do not know the exact solution.
Solution
- Estimate.
- Check. The true error is . The estimate is excellent. Adaptive solvers in engineering software use ideas of exactly this kind to choose automatically.
Since , the solution of , , satisfies . Use RK4 with to estimate .
Solution
- First step (). Since does not involve , :
- Second step ().
- 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
- 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). - Use RK4 with to approximate for , . Compare with .
Answer
, ; . - 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 ). - For the draining tank , of Example 8.5.3, take one RK4 step with .
Answer
, , , ; (exact ). - 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. - For the RK4 multiplier is with . Evaluate and compare with . Do the same for Euler's multiplier .
Answer
, (difference ); Euler gives (difference ). - Use RK4 with on , , to estimate .
Answer
, ; . - 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 . - 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 . - A population model reads , (in thousands, in years). Use RK4 with to estimate , and compare with the exact logistic solution .
Answer
, ; exact .