Skip to article frontmatterSkip to article content

Runge–Kutta methods

We come now to one of the major and most-used types of methods for initial-value problems: Runge–Kutta (RK) methods.[1] They are one-step methods in the sense of (6.2.7), though they are not often written in that form. RK methods boost the accuracy past first order by evaluating the ODE function f(t,u)f(t,u) more than once per time step.

6.4.1A second-order method

Consider a series expansion of the exact solution to u=f(t,u)u'=f(t,u),

u^(ti+1)=u^(ti)+hu^(ti)+12h2u^(ti)+O(h3).\hat{u}(t_{i+1}) = \hat{u}(t_i) + h \hat{u}'(t_i) + \frac{1}{2}h^2 \hat{u}''(t_i) + O(h^3) .

If we replace u^\hat{u}' by ff and keep only the first two terms on the right-hand side, we would obtain the Euler method. To get more accuracy we will need to compute or estimate the third term as well. Note that

u^=f=dfdt=ft+fududt=ft+fuf,\hat{u}'' = f' = \frac{d f}{d t} = \frac{\partial f}{\partial t} + \frac{\partial f}{\partial u} \frac{d u}{d t} = f_t + f_u f,

where we have applied the multidimensional chain rule to the derivative, because both of the arguments of ff depend on tt. Using this expression in (6.4.1), we obtain

u^(ti+1)=u^(ti)+h[f(ti,u^(ti))+h2ft(ti,u^(ti))+h2f(ti,u^(ti))fu(ti,u^(ti))]+O(h3). \hat{u}(t_{i+1}) = \hat{u}(t_i) + h\left[f\bigl(t_i,\hat{u}(t_i)\bigr) + \frac{h}{2}f_t\bigl(t_i,\hat{u}(t_i)\bigr) + \frac{h}{2}f\bigl(t_i,\hat{u}(t_i)\bigr)\,f_u\bigl(t_i,\hat{u}(t_i)\bigr)\right] \\ + O(h^3).

We have no desire to calculate and then code those partial derivatives of ff directly; an approximate approximation is called for. Observe that

f(ti+α,u^(ti)+β)=f(ti,u^(ti))+αft(ti,u^(ti))+βfu(ti,u^(ti))+O(α2+αβ+β2). f\bigl(t_i+\alpha,\hat{u}(t_i)+\beta\bigr) = f\bigl(t_i,\hat{u}(t_i)\bigr) + \alpha f_t\bigl(t_i,\hat{u}(t_i)\bigr) + \beta f_u\bigl(t_i,\hat{u}(t_i)\bigr) + O\bigl(\alpha^2 + |\alpha\beta| + \beta^2\bigr).

Matching this expression to the term in brackets in (6.4.3), it seems natural to select α=h/2\alpha = h/2 and β=12hf(ti,u^(ti))\beta = \frac{1}{2}h f\bigl(t_i,\hat{u}(t_i)\bigr). Doing so, we find

u^(ti+1)=u^(ti)+h[f(ti+α,u^(ti)+β)]+O(hα2+hαβ+hβ2+h3). \hat{u}(t_{i+1}) = \hat{u}(t_i) + h\left[f\bigl(t_i+\alpha,\hat{u}(t_i)+\beta\bigr)\right] + O(h\alpha^2 + h|\alpha \beta| + h\beta^2 + h^3).

Truncation of the series here results in a new one-step method.

Thanks to the definitions above of α and β, the omitted terms are of size

O(hα2+hαβ+hβ2+h3)=O(h3). O(h\alpha^2 + h|\alpha \beta| + h\beta^2 + h^3) = O(h^3).

Therefore hτi+1=O(h3)h\tau_{i+1}=O(h^3), and the order of accuracy of improved Euler is two.

6.4.2Implementation

Runge–Kutta methods are called multistage methods. We can see why if we interpret (6.4.6) from the inside out. In the first stage, the method takes an Euler half-step to time ti+12ht_i+\frac{1}{2}h:

k1=hf(ti,ui),v=ui+12k1.\begin{split} k_1 &= h f(t_i,u_i), \\ v &= u_i + \tfrac{1}{2}k_1. \end{split}

The second stage employs an Euler-style strategy over the whole time step, but using the value from the first stage to get the slope:

k2=hf(ti+12h,v),ui+1=ui+k2.\begin{split} k_2 &= h f\left(t_i+\tfrac{1}{2}h,v\right),\\ {u}_{i+1} &= u_i + k_2. \end{split}

Our implementation of IE2 is shown in Function 6.4.1.

6.4.3More Runge–Kutta methods

While it is interesting to interpret IE2 as a pair of Euler-like steps, the Taylor series derivation is the only way to see that it will be more accurate than Euler, and it is also the path to deriving other methods. Moving to higher orders of accuracy requires introducing additional stages, each having free parameters so that more terms in the series may be matched. The amount of algebra grows rapidly in size and complexity, though there is a sophisticated theory for keeping track of it. We do not give the derivation details.

A generic ss-stage RK method takes the form

k1=hf(ti,ui),k2=hf(ti+c1h,ui+a11k1),k3=hf(ti+c2h,ui+a21k1+a22k2),ks=hf(ti+cs1h,ui+as1,1k1++as1,s1ks1),ui+1=ui+b1k1++bsks. \begin{split} k_1 &= h f(t_i,u_i),\\ k_2 &= h f(t_i+c_1h,u_i + a_{11}k_1),\\ k_3 &= h f(t_i+c_2h, u_i + a_{21}k_1 + a_{22}k_2),\\ &\vdots\\ k_s &= h f(t_i + c_{s-1}h, u_i + a_{s-1,1}k_1 + \cdots + a_{s-1,s-1}k_{s-1}),\\ \mathbf{u}_{i+1} &= u_i + b_1k_1 + \cdots + b_s k_s. \end{split}

This recipe is completely determined by the number of stages ss and the constants aija_{ij}, bjb_j, and cic_i. Often an RK method is presented as just a table of these numbers, as in

0c1a11c2a21a22cs1as1,1as1,s1b1b2bs1bs \begin{array}{r|ccccc} 0 & & & & & \\ c_1 & a_{11} & & &\\ c_2 & a_{21} & a_{22} & & &\\ \vdots & \vdots & & \ddots & &\\ c_{s-1} & a_{s-1,1} & \cdots & & a_{s-1,s-1}&\\[1mm] \hline \rule{0pt}{2.25ex} & b_1 & b_2 & \cdots & b_{s-1} & b_s \end{array}

For example, IE2 is given by

0121201 \begin{array}{r|cc} \rule{0pt}{2.75ex}0 & & \\ \rule{0pt}{2.75ex}\frac{1}{2} & \frac{1}{2} &\\[1mm] \hline \rule{0pt}{2.75ex}& 0 & 1 \end{array}

Here are two more two-stage, second-order methods, modified Euler and Heun’s method, respectively:

0111212023231434 \begin{array}{r|cc} \rule{0pt}{2.75ex}0 & & \\ \rule{0pt}{2.75ex}1 & 1 &\\[1mm] \hline \rule{0pt}{2.75ex}& \frac{1}{2} & \frac{1}{2} \end{array} \qquad \qquad \begin{array}{r|cc} \rule{0pt}{2.75ex} 0 & & \\ \rule{0pt}{2.75ex} \frac{2}{3} & \frac{2}{3} &\\[1mm] \hline \rule{0pt}{2.75ex} & \frac{1}{4} & \frac{3}{4} \end{array}

The most commonly used RK method, and perhaps the most popular IVP method of all, is the fourth-order one given by

0121212012100116131316 \begin{array}{r|cccc} \rule{0pt}{2.75ex}0 & & & & \\ \rule{0pt}{2.75ex}\frac{1}{2} & \frac{1}{2} & & &\\ \rule{0pt}{2.75ex}\frac{1}{2} & 0 & \frac{1}{2} & &\\ \rule{0pt}{2.75ex}1 & 0 & 0 & 1\\[1mm] \hline \rule{0pt}{2.75ex}& \frac{1}{6} & \frac{1}{3} & \frac{1}{3} & \frac{1}{6} \end{array}

This formula is often referred to as the fourth-order RK method, even though there are many others, and we refer to it as RK4. Written out, the recipe is as follows.

Our implementation is given in Function 6.4.2.

6.4.4Efficiency

As with rootfinding and integration, the usual point of view is that evaluations of ff are the only significant computations and are therefore to be minimized in number. One of the most important characteristics of a multistage method is that each stage requires an evaluation of ff; that is, a single time step of an ss-stage method requires ss evaluations of ff.

The error decreases geometrically as ss is incremented, so trading a stage for an increase in order is a good deal. But s=5s=5, 6, or 7 gives a maximal order of accuracy of s1s-1; this decreases to s2s-2 for s=8s=8 and s=9s=9, etc. Fourth order is considered adequate and the sweet spot for many applications.

6.4.5Exercises

  1. ✍ For each IVP, write out (possibly using a calculator) the first time step of the improved Euler method with h=0.2h=0.2.

    (a) u=2tu, 0t2, u(0)=2; u^(t)=2et2u' = -2t u, \ 0 \le t \le 2, \ u(0) = 2;\ \hat{u}(t) = 2e^{-t^2}

    (b) u=u+t, 0t1, u(0)=2; u^(t)=1t+3etu' = u + t, \ 0 \le t \le 1, \ u(0) = 2;\ \hat{u}(t) = -1-t+3e^t

    (c) (1+x3)uu=x2, 0x3, u(0)=1; u^(x)=[1+(2/3)ln(1+x3)]1/2(1+x^3)uu' = x^2,\ 0 \le x \le 3, \ u(0) = 1;\ \hat{u}(x) = [1+(2/3)\ln (1+x^3)]^{1/2}

  2. ✍ Use the modified Euler method to solve the problems in the preceding exercise.

  3. ⌨ Modify Function 6.4.2 to implement the modified Euler method. Test your function on the IVP in part (a) of Exercise 1 by solving with n=30,60,90,,300n=30,60,90,\ldots,300 and plotting the convergence of the error at the final time together with a line showing O(n2)O(n^{-2}).

  4. ✍ Use Heun’s method to solve the problems in Exercise 1 above.

  5. ⌨ Modify Function 6.4.2 to implement Heun’s method. Test your function on the IVP in part (a) of Exercise 1 by solving with n=30,60,90,,300n=30,60,90,\ldots,300 and plotting the convergence of the error at the final time together with a line showing O(n2)O(n^{-2}).

  6. ✍ Use RK4 to solve the problems in Exercise 1 above.

  7. ✍ Using (6.4.3) and (6.4.4), show that the modified Euler method has order of accuracy at least 2.

  8. ✍ Using (6.4.3) and (6.4.4), show that Heun’s method has order of accuracy at least 2.

  9. ⌨ For each IVP, compute the solution using Function 6.4.2. (i) Plot the solution for n=300n=300. (ii) For n=100,200,300,,1000n=100,200,300,\ldots,1000, compute the error at the final time and make a log-log convergence plot, including a reference line for fourth-order convergence.

    (a) u+9u=9t,0<t<2π,u(0)=1,u(0)=1;u^(t)=t+cos(3t)u''+ 9u = 9t, \: 0< t< 2\pi, \: u(0) = 1,\: u'(0) = 1; \: \hat{u}(t) = t+\cos (3t)

    (b) u+9u=sin(2t),0<t<2π,u(0)=2,u(0)=1u''+ 9u = \sin(2t), \: 0< t< 2\pi, \: u(0) = 2,\: u'(0) = 1;

    u^(t)=(1/5)sin(3t)+2cos(3t)+(1/5)sin(2t)\quad \hat{u}(t) = (1/5) \sin(3t) + 2 \cos (3t)+ (1/5) \sin (2t)

    (c) u9u=9t,0<t<1,u(0)=2,u(0)=1;u^(t)=e3t+e3ttu''- 9u = 9t, \: 0< t< 1, \: u(0) = 2,\: u'(0) = -1; \: \hat{u}(t) = e^{3t} + e^{-3t}-t

    (d) u+4u+4u=t,0<t<4,u(0)=1,u(0)=3/4;u^(t)=(3t+5/4)e2t+(t1)/4u''+ 4u'+ 4u = t, \: 0< t< 4, \: u(0) = 1,\: u'(0) = 3/4; \: \hat{u}(t) = (3t+5/4)e^{-2t} + (t-1)/4

    (e) x2y+5xy+4y=0,1<x<e2,y(1)=1,y(1)=1,y^(x)=x2(1+lnx)x^2 y'' +5xy' + 4y = 0,\: 1<x<e^2, \: y(1) = 1, \: y'(1) = -1, \: \hat{y}(x) = x^{-2}( 1 + \ln x)

    (f) 2x2y+3xyy=0,1<x<16,y(1)=4,y(1)=1,y^(x)=2(x1/2+x1)2 x^2 y'' +3xy' - y = 0,\: 1<x<16, \: y(1) = 4, \: y'(1) = -1, \: \hat{y}(x) = 2(x^{1/2} + x^{-1})

    (g) x2yxy+2y=0,1<x<eπ,y(1)=3,y(1)=4x^2 y'' -xy' + 2y = 0,\: 1<x<e^{\pi}, \: y(1) = 3, \: y'(1) = 4;

    y^(x)=x[3cos(lnx)+sin(lnx)]\quad \hat{y}(x) = x \left[ 3 \cos \left( \ln x \right)+\sin \left( \ln x \right) \right]

    (h) x2y+3xy+4y=0,eπ/12<x<eπ,y(eπ/12)=0,y(eπ/12)=6x^2 y'' + 3xy' + 4y = 0,\: e^{\pi/12} < x < e^{\pi}, \: y(e^{\pi/12}) = 0, \: y'(e^{\pi/12}) = -6;

    y^(x)=x1[3cos(3lnx)+sin(3lnx)]\quad \hat{y}(x) = x^{-1} \left[ 3 \cos \left( 3 \ln x \right)+\sin \left( 3 \ln x \right) \right]

  10. ⌨ Do Exercise 6.3.4, but using Function 6.4.2 instead of solve.

  11. ✍ Consider the problem u=cuu'=c u, u(0)=1u(0) = 1 for constant cc and t>0t>0.

    (a) Find an explicit formula in terms of hh and cc for ui+1/uiu_{i+1}/u_i in the modified Euler method.

    (b) Show that if ch=3ch=-3, then ui|u_i|\to\infty as ii\to\infty while the exact solution u^(t)\hat{u}(t) approaches zero as tt\to\infty.

Footnotes
  1. Americans tend to pronounce these German names as “run-ghuh kut-tah.”