Heun’s method reaches second order with two slopes per step. How much more do four slopes buy, and how can a solver tell, without knowing the exact solution, whether a step was short enough? Throughout, is the independent variable, , the problem is with , and is the step size. This lesson computes the four stages of the classical Runge-Kutta method, explains its fourth-order error, and shows how an error estimate and a pair of tolerances adapt the step, together with what that estimate does not promise.
Key terms
Classical Runge-Kutta method
Definition The classical Runge-Kutta method, often called RK4, advances from with four stage slopes, computed in this order,
and then sets
The stages and share the midpoint time but are read at the predicted states and , which in general differ, and is read at the end of the step, at a state predicted with . Every stage state starts from the same value . The predicted states are not consecutive solution points, and only is kept. Each step costs four evaluations of .
In plain words Read the slope at the start and use it to predict the middle. Read the slope there and use it to predict the middle again. Read once more, use that newest slope to predict the end, and read the slope at the end. Then step from with an average of the four slopes.
Example Take , and . On this equation the slope equals the state, so each slope is the predicted state of its stage,
Over the denominator , the sum has the parts , , and , so it equals . Multiplying by and adding gives
while . After one step the error is about .
Weighted average of slopes
Definition The RK4 update moves from by times the weighted average
of its stage slopes, with the weights , , and . The weights are positive and add up to , so a constant slope gives the exact change . Each is a slope, and multiplies the average once. Some books use the increments instead, so that the update adds and the second stage is . Mixing the two conventions applies twice or not at all. When depends on alone, the two middle slopes are equal and the step is Simpson’s rule for the integral of over , with the weights , and at the start, the middle and the end.
In plain words The middle of the step counts twice as much as each end, as in Simpson’s rule. All four numbers being averaged are slopes, so the average is a slope too, and one multiplication by turns it into the change in over the step.
Example For with and a single step , the slopes are , and , so
This is the exact value , because Simpson’s rule integrates cubics exactly. Equal weights of would give instead. On the same weights make each step multiply by the Taylor polynomial of of degree four, as explained below.
Fourth-order global error
Definition Let have continuous partial derivatives through order four on an open set containing the graph of the exact solution over , a closed interval inside its interval of existence. Then RK4 has local truncation error of order , and there are constants and such that
for every grid time whenever . In a typical problem, once is small, halving it divides the global error by about . On one step multiplies by
the Taylor polynomial of of degree four, so . Matching this one equation is a check, not a proof. The general result also needs the order conditions for right sides that depend nonlinearly on and , and the classical stages and weights satisfy all of them. The order also describes small steps only. On with , one step multiplies by , which is positive for every , and the computed values decay exactly when is below a boundary value .
In plain words Four well-placed slopes match the Taylor series of the solution through , so each step misses only a term of size and the accumulated error falls like . Halving the step doubles the work and, for a smooth problem with a small enough step, makes the error about sixteen times smaller.
Example For and , the one-step error found above is close to . For a nonlinear problem take , , whose solution gives . The signed RK4 errors at are below, where the ratio divides the error in the row above by the error in its own row.
| Signed error | Ratio | |
|---|---|---|
With , which takes steps and evaluations of , RK4 is already about eight times more accurate than Heun’s method with , which takes steps and evaluations and leaves an error of about .
Embedded error estimate
Definition A solver estimates the local error of a step by computing a second approximation of different accuracy at the same end time. Step doubling does this with one method. With RK4, compute by one step of size and by two steps of size , both from to . If the coarse local error is about , the fine result carries two errors of about each, so to leading order
where here means the exact solution through . An embedded Runge-Kutta pair gets a similar estimate more cheaply. One set of stage slopes feeds two update formulas of different orders, usually and , and the difference of the two results estimates the local error of the lower-order one. Well-known pairs are the Fehlberg pair of orders and with six stages, and the Dormand-Prince pair of orders and , which has seven stages but reuses its last stage as the first stage of the next step. Every such estimate rests on the leading term of an error expansion, so it is an approximation and not a guaranteed bound.
In plain words Two answers for the same step tell you something by how much they disagree. The more accurate answer is much closer to the truth, so the gap between the two is roughly the error of the less accurate one.
Example Step doubling on from with gives and . The estimate is close to the actual . The simplest embedded pair uses Euler’s method and Heun’s method, which share the slope . On with they give and , and their difference estimates the local error of the Euler step.
Absolute and relative tolerances
Definition An absolute tolerance and a relative tolerance , not both zero, set the error scale of a step from to ,
and the step passes when its estimated local error satisfies . The absolute part controls the error while the solution is near , and the relative part measures the error against the size of the solution when it is large. Some solvers use in place of the sum, which changes the scale by at most a factor of . For a system each component gets its own scale, often with its own absolute tolerance in that component’s units, and the ratios are combined by a maximum or a root mean square. The tolerances control the estimated error of each step. They do not guarantee that the global error at the end is below the tolerance, because local errors add up over many steps and can grow as the solution evolves.
In plain words The relative tolerance asks for roughly a fixed number of correct digits in each step, and the absolute tolerance sets the size below which errors no longer matter. Whichever part is larger at the moment decides how small the step must be.
Example Take and . Where the solution has size , the scale is and the relative part decides. Where it has size , the scale is and the absolute part decides. With instead, the scale shrinks toward wherever the solution passes through , and the solver is forced toward tiny steps there.
Adaptive step size
Definition An adaptive solver changes from step to step to keep its estimated local error near the requested size. After a trial step of size it forms the error estimate and the scale , and computes the ratio . If the step is accepted, and otherwise it is rejected and repeated with a smaller step. In both cases the next trial step is
where is the lower of the two orders, since that formula’s local error scales like . The factor is a safety margin, and the new step is kept between fixed multiples of the old one, such as and . Many solvers keep the higher-order result as the accepted value, so the estimate is cautious for the value actually kept. Known jumps in or switches in the model belong at the ends of separate integration intervals, or are located as events. Steps that keep shrinking and repeated rejections are information about the problem, for example a nearby singularity or stiffness, and not something to hide by loosening the tolerances.
In plain words The solver takes long steps where the solution changes slowly and short steps where it changes quickly, guided by its own error estimate. A step that fails the test is thrown away and tried again with a shorter step.
Example Use the Euler and Heun pair on , , with both tolerances equal to and . A first try with gives and , so and the step is rejected. The new trial step is . With the two results are and , so , and . The step is accepted, the solver keeps at , and it proposes about for the next step.
Check yourself
- Take one RK4 step with for , , and compare the result with .
Show the answer
The stages are , then , then , and finally . The weighted sum is , so
That is about , while , so the signed error is about .
- A classmate codes RK4 with the stage states , and , where each is a slope, and keeps the update . What is wrong, and what does this code give for the problem of question 1?
Show the answer
The stage states use the increment convention, which needs , while the update uses the slope convention, so the step is missing from every prediction. On with the code gets , then the state and , then the state and , then the state and . The update gives , far from . With slopes, every prediction needs the factor , as in .
- Two RK4 steps of size for the problem of question 1 give at . Use step doubling with the result of question 1 to estimate the local error of this finer value. Would the step pass with ?
Show the answer
With and , the estimate is
for the correction . The actual correction is , so the estimate has the right sign and size. The scale is , and , so the step passes. Both results must end at the same time , since otherwise their difference would also contain the change of the exact solution between the two end times.