Unit 10, lesson 58

Fourth-Order Runge-Kutta and Adaptive Steps

How the classical Runge-Kutta method takes a weighted average of slopes from four RK4 stages, why its global error is fourth order, and how embedded error estimates and absolute and relative tolerances adapt the step size.

  • Video 58
  • Euler and Runge-Kutta Methods

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, tt is the independent variable, y′=dy/dty' = dy/dt, the problem is y′=f(t,y)y' = f(t, y) with y(t0)=y0y(t_0) = y_0, and h>0h > 0 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 (tn,yn)(t_n, y_n) with four stage slopes, computed in this order,

k1=f(tn,yn),k2=f(tn+h2,yn+h2k1),k3=f(tn+h2,yn+h2k2),k4=f(tn+h,yn+hk3),\begin{aligned} k_1 &= f(t_n, y_n), \\[0.4em] k_2 &= f\bigl(t_n + \tfrac{h}{2}, y_n + \tfrac{h}{2} k_1\bigr), \\[0.4em] k_3 &= f\bigl(t_n + \tfrac{h}{2}, y_n + \tfrac{h}{2} k_2\bigr), \\[0.4em] k_4 &= f(t_n + h, y_n + h k_3), \end{aligned}

and then sets

kˉn=k1+2k2+2k3+k46,yn+1=yn+h kˉn.\begin{gathered} \bar k_n = \frac{k_1 + 2k_2 + 2k_3 + k_4}{6}, \\[0.4em] y_{n+1} = y_n + h\,\bar k_n. \end{gathered}

The stages k2k_2 and k3k_3 share the midpoint time tn+h/2t_n + h/2 but are read at the predicted states yn+h2k1y_n + \tfrac{h}{2}k_1 and yn+h2k2y_n + \tfrac{h}{2}k_2, which in general differ, and k4k_4 is read at the end of the step, at a state predicted with k3k_3. Every stage state starts from the same value yny_n. The predicted states are not consecutive solution points, and only yn+1y_{n+1} is kept. Each step costs four evaluations of ff.

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 yny_n with an average of the four slopes.

Example Take y′=yy' = y, y(0)=1y(0) = 1 and h=110h = \tfrac1{10}. On this equation the slope equals the state, so each slope is the predicted state of its stage,

k1=1,k2=1+120⋅1=2120,k3=1+120⋅2120=421400,k4=1+110⋅421400=44214000.\begin{aligned} k_1 &= 1, \\[0.4em] k_2 &= 1 + \tfrac1{20} \cdot 1 = \tfrac{21}{20}, \\[0.4em] k_3 &= 1 + \tfrac1{20} \cdot \tfrac{21}{20} = \tfrac{421}{400}, \\[0.4em] k_4 &= 1 + \tfrac1{10} \cdot \tfrac{421}{400} = \tfrac{4421}{4000}. \end{aligned}

Over the denominator 40004000, the sum k1+2k2+2k3+k4k_1 + 2k_2 + 2k_3 + k_4 has the parts 40004000, 84008400, 84208420 and 44214421, so it equals 252414000\tfrac{25241}{4000}. Multiplying by h/6=160h/6 = \tfrac1{60} and adding 11 gives

y1=265241240000≈1.10517083,y_1 = \frac{265241}{240000} \approx 1.10517083,

while e0.1≈1.10517092e^{0.1} \approx 1.10517092. After one step the error is about −8.5×10−8-8.5 \times 10^{-8}.

Weighted average of slopes

Definition The RK4 update moves from yny_n by hh times the weighted average

kˉn=k1+2k2+2k3+k46\bar k_n = \frac{k_1 + 2k_2 + 2k_3 + k_4}{6}

of its stage slopes, with the weights 16\tfrac16, 13\tfrac13, 13\tfrac13 and 16\tfrac16. The weights are positive and add up to 11, so a constant slope f=mf = m gives the exact change hmhm. Each kik_i is a slope, and hh multiplies the average once. Some books use the increments Ki=hkiK_i = hk_i instead, so that the update adds (K1+2K2+2K3+K4)/6(K_1 + 2K_2 + 2K_3 + K_4)/6 and the second stage is K2=hf(tn+h/2,yn+K1/2)K_2 = h f(t_n + h/2, y_n + K_1/2). Mixing the two conventions applies hh twice or not at all. When ff depends on tt alone, the two middle slopes are equal and the step is Simpson’s rule for the integral of ff over [tn,tn+1][t_n, t_{n+1}], with the weights 16\tfrac16, 46\tfrac46 and 16\tfrac16 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 hh turns it into the change in yy over the step.

Example For y′=t3y' = t^3 with y(0)=0y(0) = 0 and a single step h=1h = 1, the slopes are k1=0k_1 = 0, k2=k3=18k_2 = k_3 = \tfrac18 and k4=1k_4 = 1, so

y1=0+14+14+16=14.y_1 = \frac{0 + \tfrac14 + \tfrac14 + 1}{6} = \frac14.

This is the exact value y(1)=14y(1) = \tfrac14, because Simpson’s rule integrates cubics exactly. Equal weights of 14\tfrac14 would give 516\tfrac{5}{16} instead. On y′=yy' = y the same weights make each step multiply by the Taylor polynomial of ehe^h of degree four, as explained below.

Fourth-order global error

Definition Let ff have continuous partial derivatives through order four on an open set containing the graph of the exact solution over [t0,T][t_0, T], a closed interval inside its interval of existence. Then RK4 has local truncation error of order h5h^5, and there are constants CC and h0>0h_0 > 0 such that

∣yn−y(tn)∣≤Ch4\lvert y_n - y(t_n) \rvert \le C h^4

for every grid time tn≤Tt_n \le T whenever 0<h≤h00 < h \le h_0. In a typical problem, once hh is small, halving it divides the global error by about 1616. On y′=yy' = y one step multiplies by

R(h)=1+h+h22+h36+h424,\begin{aligned} R(h) &= 1 + h + \frac{h^2}{2} \\[0.4em] &\quad + \frac{h^3}{6} + \frac{h^4}{24}, \end{aligned}

the Taylor polynomial of ehe^h of degree four, so eh−R(h)=h5/120+O(h6)e^h - R(h) = h^5/120 + O(h^6). 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 tt and yy, and the classical stages and weights satisfy all of them. The order also describes small steps only. On y′=−ayy' = -ay with a>0a > 0, one step multiplies by R(−ah)R(-ah), which is positive for every hh, and the computed values decay exactly when ahah is below a boundary value β≈2.785\beta \approx 2.785.

In plain words Four well-placed slopes match the Taylor series of the solution through h4h^4, so each step misses only a term of size h5h^5 and the accumulated error falls like h4h^4. 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 y′=yy' = y and h=110h = \tfrac1{10}, the one-step error −8.47×10−8-8.47 \times 10^{-8} found above is close to −h5/120≈−8.33×10−8-h^5/120 \approx -8.33 \times 10^{-8}. For a nonlinear problem take y′=y2y' = y^2, y(0)=1y(0) = 1, whose solution 1/(1−t)1/(1 - t) gives y(12)=2y(\tfrac12) = 2. The signed RK4 errors at t=12t = \tfrac12 are below, where the ratio divides the error in the row above by the error in its own row.

hhSigned errorRatio
0.10.1−3.674×10−5-3.674 \times 10^{-5}
0.050.05−2.392×10−6-2.392 \times 10^{-6}15.415.4
0.0250.025−1.513×10−7-1.513 \times 10^{-7}15.815.8
0.01250.0125−9.484×10−9-9.484 \times 10^{-9}16.016.0

With h=0.1h = 0.1, which takes 55 steps and 2020 evaluations of ff, RK4 is already about eight times more accurate than Heun’s method with h=0.0125h = 0.0125, which takes 4040 steps and 8080 evaluations and leaves an error of about −3.07×10−4-3.07 \times 10^{-4}.

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 ycy_c by one step of size hh and yfy_f by two steps of size h/2h/2, both from (tn,yn)(t_n, y_n) to tn+1=tn+ht_{n+1} = t_n + h. If the coarse local error is about Ch5Ch^5, the fine result carries two errors of about C(h/2)5C(h/2)^5 each, so to leading order

y(tn+1)−yf≈yf−yc15,y(t_{n+1}) - y_f \approx \frac{y_f - y_c}{15},

where yy here means the exact solution through (tn,yn)(t_n, y_n). An embedded Runge-Kutta pair gets a similar estimate more cheaply. One set of stage slopes feeds two update formulas of different orders, usually qq and q+1q + 1, 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 44 and 55 with six stages, and the Dormand-Prince pair of orders 55 and 44, 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 y′=yy' = y from y(0)=1y(0) = 1 with h=12h = \tfrac12 gives yc=R(12)=1.6484375y_c = R(\tfrac12) = 1.6484375 and yf=R(14)2≈1.6486995y_f = R(\tfrac14)^2 \approx 1.6486995. The estimate (yf−yc)/15≈1.75×10−5(y_f - y_c)/15 \approx 1.75 \times 10^{-5} is close to the actual e0.5−yf≈2.18×10−5e^{0.5} - y_f \approx 2.18 \times 10^{-5}. The simplest embedded pair uses Euler’s method and Heun’s method, which share the slope k1k_1. On y′=yy' = y with h=0.2h = 0.2 they give 1.21.2 and 1.221.22, and their difference 0.020.02 estimates the local error e0.2−1.2≈0.0214e^{0.2} - 1.2 \approx 0.0214 of the Euler step.

Absolute and relative tolerances

Definition An absolute tolerance atol≥0\text{atol} \ge 0 and a relative tolerance rtol≥0\text{rtol} \ge 0, not both zero, set the error scale of a step from yny_n to yn+1y_{n+1},

s=atol+rtol⋅m,m=max⁡(∣yn∣,∣yn+1∣),\begin{gathered} s = \text{atol} + \text{rtol} \cdot m, \\[0.4em] m = \max\bigl(\lvert y_n \rvert, \lvert y_{n+1} \rvert\bigr), \end{gathered}

and the step passes when its estimated local error δ\delta satisfies ∣δ∣≤s\lvert \delta \rvert \le s. The absolute part controls the error while the solution is near 00, and the relative part measures the error against the size of the solution when it is large. Some solvers use max⁡(atol,rtol⋅m)\max(\text{atol}, \text{rtol} \cdot m) in place of the sum, which changes the scale by at most a factor of 22. 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 rtol=10−4\text{rtol} = 10^{-4} and atol=10−8\text{atol} = 10^{-8}. Where the solution has size 100100, the scale is 10−8+10−2≈10−210^{-8} + 10^{-2} \approx 10^{-2} and the relative part decides. Where it has size 10−610^{-6}, the scale is 10−8+10−10=1.01×10−810^{-8} + 10^{-10} = 1.01 \times 10^{-8} and the absolute part decides. With atol=0\text{atol} = 0 instead, the scale shrinks toward 00 wherever the solution passes through 00, and the solver is forced toward tiny steps there.

Adaptive step size

Definition An adaptive solver changes hh from step to step to keep its estimated local error near the requested size. After a trial step of size hh it forms the error estimate δ\delta and the scale ss, and computes the ratio ρ=∣δ∣/s\rho = \lvert \delta \rvert / s. If ρ≤1\rho \le 1 the step is accepted, and otherwise it is rejected and repeated with a smaller step. In both cases the next trial step is

hnew=0.9 h ρ−1/(q+1),h_{\text{new}} = 0.9\,h\,\rho^{-1/(q+1)},

where qq is the lower of the two orders, since that formula’s local error scales like hq+1h^{q+1}. The factor 0.90.9 is a safety margin, and the new step is kept between fixed multiples of the old one, such as 0.2h0.2h and 5h5h. Many solvers keep the higher-order result as the accepted value, so the estimate is cautious for the value actually kept. Known jumps in ff 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 y′=yy' = y, y(0)=1y(0) = 1, with both tolerances equal to 10−310^{-3} and q=1q = 1. A first try with h=0.2h = 0.2 gives δ=0.02\delta = 0.02 and s=10−3(1+1.22)=0.00222s = 10^{-3}(1 + 1.22) = 0.00222, so ρ≈9.0\rho \approx 9.0 and the step is rejected. The new trial step is 0.9⋅0.2⋅9.0−1/2≈0.060.9 \cdot 0.2 \cdot 9.0^{-1/2} \approx 0.06. With h=0.06h = 0.06 the two results are 1.061.06 and 1.06181.0618, so δ=0.0018\delta = 0.0018, s≈0.00206s \approx 0.00206 and ρ≈0.87\rho \approx 0.87. The step is accepted, the solver keeps y1=1.0618y_1 = 1.0618 at t=0.06t = 0.06, and it proposes about 0.0580.058 for the next step.

Check yourself

  1. Take one RK4 step with h=14h = \tfrac14 for y′=−2yy' = -2y, y(0)=1y(0) = 1, and compare the result with e−1/2e^{-1/2}.
Show the answer

The stages are k1=−2k_1 = -2, then k2=−2(1+18(−2))=−32k_2 = -2\bigl(1 + \tfrac18(-2)\bigr) = -\tfrac32, then k3=−2(1+18⋅(−32))=−138k_3 = -2\bigl(1 + \tfrac18 \cdot (-\tfrac32)\bigr) = -\tfrac{13}8, and finally k4=−2(1+14⋅(−138))=−1916k_4 = -2\bigl(1 + \tfrac14 \cdot (-\tfrac{13}8)\bigr) = -\tfrac{19}{16}. The weighted sum is k1+2k2+2k3+k4=−15116k_1 + 2k_2 + 2k_3 + k_4 = -\tfrac{151}{16}, so

y1=1+124(−15116)=233384.y_1 = 1 + \tfrac{1}{24}\bigl(-\tfrac{151}{16}\bigr) = \tfrac{233}{384}.

That is about 0.6067710.606771, while e−1/2≈0.606531e^{-1/2} \approx 0.606531, so the signed error is about 2.4×10−42.4 \times 10^{-4}.

  1. A classmate codes RK4 with the stage states yn+k1/2y_n + k_1/2, yn+k2/2y_n + k_2/2 and yn+k3y_n + k_3, where each kik_i is a slope, and keeps the update yn+h6(k1+2k2+2k3+k4)y_n + \tfrac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4). 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 Ki=hkiK_i = hk_i, while the update uses the slope convention, so the step hh is missing from every prediction. On y′=−2yy' = -2y with yn=1y_n = 1 the code gets k1=−2k_1 = -2, then the state 1−1=01 - 1 = 0 and k2=0k_2 = 0, then the state 11 and k3=−2k_3 = -2, then the state 1−2=−11 - 2 = -1 and k4=2k_4 = 2. The update gives 1+124(−2+0−4+2)=56≈0.8331 + \tfrac1{24}(-2 + 0 - 4 + 2) = \tfrac56 \approx 0.833, far from 0.60650.6065. With slopes, every prediction needs the factor hh, as in yn+h2k1y_n + \tfrac{h}{2}k_1.

  1. Two RK4 steps of size 18\tfrac18 for the problem of question 1 give 0.606542830.60654283 at t=12t = \tfrac12. Use step doubling with the result of question 1 to estimate the local error of this finer value. Would the step pass with atol=rtol=10−5\text{atol} = \text{rtol} = 10^{-5}?
Show the answer

With yc=233384≈0.60677083y_c = \tfrac{233}{384} \approx 0.60677083 and yf=0.60654283y_f = 0.60654283, the estimate is

yf−yc15≈−1.52×10−5\frac{y_f - y_c}{15} \approx -1.52 \times 10^{-5}

for the correction y(12)−yfy(\tfrac12) - y_f. The actual correction is e−1/2−0.60654283≈−1.22×10−5e^{-1/2} - 0.60654283 \approx -1.22 \times 10^{-5}, so the estimate has the right sign and size. The scale is s=10−5+10−5⋅max⁡(1,0.6065)=2×10−5s = 10^{-5} + 10^{-5} \cdot \max(1, 0.6065) = 2 \times 10^{-5}, and 1.52×10−5≤2×10−51.52 \times 10^{-5} \le 2 \times 10^{-5}, so the step passes. Both results must end at the same time t=12t = \tfrac12, since otherwise their difference would also contain the change of the exact solution between the two end times.

How this lesson connects