5.4 Runge-Kutta Methods The Taylor methods outlined in the previous section have the desirable property of high-order local truncation error, but the disadvantage of requiring the computation and evaluation of the derivatives of f t , y . This is a complicated and time-consuming procedure for most problems, so the Taylor methods are seldom used in practice. Runge-Kutta methods have the high-order local truncation error of the Taylor methods while eliminating the need to compute and evaluate the derivatives of f t , y . Before presenting the ideas behind their derivation, we need to state Taylor’s Theorem in two variables. The proof of this result can be found in any standard book on advanced calculus (see, for example, [Fu, p.331]). Theorem 5.13 Suppose that f x, y and all its partial derivatives of order less than or equal to n 1are continuous on D t , y | a t b, c y d , and let t0 , y0 D . For every t, y D , there exists between t and t 0 and between y and y0 with f t , y Pn t , y Rn t , y , where f f Pn t , y f t0 , y0 t t0 t0 , y0 y y0 t0 , y0 t y 2 t t0 2 f 2 f t , y t t y y 0 0 0 t 0 , y0 0 t 2 t y 2 2 y y0 2 f t , y0 2 0 2 y 1 n n n f n j j t t0 y y0 n j j t0 , y0 t y n ! j 0 j and Rn t , y 1 n 1 n 1 n 1 f n 1 j j t t y y , . 0 0 j t n j y j n 1! j 0 ■ The function Pn t , y is called the n th Taylor polynomial in two variables for the function f about t0 , y0 , and Rn t , y is the remainder term 1 associated with Pn t , y . The first step in deriving a Runge-Kutta method is to determine values for a1 ,1 , and 1 with the property that a1 f t 1 , y 1 approximates T 2 t , y f t , y h f ' t, y , 2 with error no greater than O h 2 , the local truncation error for the Taylor method of order two. Since f ' t, y df f f t, y t, y t , y y ' t dt t y and y ' t f t , y , this implies T 2 t, y f t, y h f h f t, y t, y f t, y . 2 t 2 y (5.18) Expanding f t 1 , y 1 in its Taylor polynomial of degree on about t, y gives a1 f t 1 , y 1 a1 f t , y a11 f t , y t f a1 1 t , y a1 R1 t 1 , y 1 , y (5.19) where R1 t 1 , y 1 12 2 f 2 t 2 , 11 12 2 f 2 f , , , (5.20) ty 2 y 2 for some between t and t 1 and between y and y 1 . Matching the coefficients of f and its derivatives in Eqs. (5.18) and (5.19) gives the three equations f f t , y : a1 1; t, y : t and f t, y : y a11 h a11 ; 2 h f t, y . 2 The parameters a1 ,1 , and 1 are uniquely determined to be 2 a1 1, h 2 1 , and 1 h f t, y ; 2 so h h h h T 2 t , y f t , y f t , y R1 t , y f t , y , 2 2 2 2 and from Eq. (5.20), 2 2 h h2 2 f h h f R1 t , y f t , y , f t , y , 2 2 4 t y 2 8 t 2 2 f h2 f t, y , . 8 y 2 If all the second-order partial derivatives of f are bounded, then h h R1 t , y f t , y 2 2 is O h 2 , the order of the local truncation error of Taylor method of order two. As a consequence, using the new procedure in place of the Taylor method of order two might add error, but it does not increase the order of the error. The difference-equation method resulting from replacing T 2 t , y in Taylor’s method of order two by f t , h 2 , y h 2 f t , y is a specific Runge-Kutta method known as the Midpoint method. Midpoint method: w0 , h h wi 1 wi hf ti , wi f ti , wi , 2 2 for each i 0,1, , N 1. Since only three parameters are present in a1 f t 1 , y 1 and all are 2 needed in the match of T , we need a more complicated form to satisfy the condition required for any of the higher-order Taylor methods. The most appropriate four-parameter form for approximating T 3 h h2 t, y f t, y f ' t, y f "t, y 2 6 is a1 f t , y a2 f t 2 , y 2 f t , y ; (5.21) 3 and even with this, there is insufficient flexibility to match the term 2 h 2 f t, y f t , y , 6 y resulting from the expansion of h 2 6 f " t , y . Consequently, the best that can be obtained from using (5.21) are methods with O h 2 local truncation error. The fact that (5.21) has four parameters, however, gives a flexibility in their choice, so a number of O h 2 methods can be derived. One of the most important is the Modified Euler method, which corresponds 1 to choosing a1 a2 and 2 2 h and has the following 2 difference-equation form. Modified Euler Method: w0 , h wi 1 wi f ti , wi f ti 1 , wi hf ti , wi , 2 for each i 0,1, 2, , N 1, The other important O h 2 method is Heun’s method, which correspond to 1 3 2 a1 , a2 , and 2 2 h , and has the following difference-equation 4 4 3 form. Heun’s Method: w0 , h wi 1 wi f ti , wi 3 f 4 for each i 0,1, 2, 2 2 ti h, wi hf ti , wi , 3 3 , N 1. Both are classified as Runge-Kutta methods of order two, the order of their local truncation error. EXAMPLE 2 Suppose we apply the Runge-Kutta methods of order two to our usual example, y ' y t 2 1, 0 t 2, y 0 0.5, with N 10, h 0.2, ti 0.2i, and w0 0.5 in each case. The difference equations produced from the various formulas are 4 Midpoint method: wi 1 1.22wi 0.0088i 2 0.008i 0.218; Modified Euler method: wi 1 1.22wi 0.0088i 2 0.008i 0.216; Heun’s method: wi 1 1.22wi 0.0088i 2 0.008i 0.2173, for each i 0,1, ,9 . Table 5.5 lists the results of these calculations. For this problem, the Midpoint method is superior, followed by Heun’s method. ■ Table 5.5 (see p277) Although T 3 t , y can be approximated with error O h3 by an expression of the form f t 1 , y 1 f t 2 , y 2 f t , y , involving four parameters, the algebra involved in the determination of 1 , 1 , 2 , and 2 is quite involved and will not be presented. In fact, the Runge-Kutta method of order three resulting from this expression is not generally used. The most common Runge-Kutta method in use is of order four and, in difference-equation form, is given by the following. Runge-Kutta method of Order Four: w0 , k1 hf ti , wi , h 1 k2 hf ti , wi k1 , 2 2 h 1 k3 hf ti , wi k2 , 2 2 k4 hf ti 1 , wi k3 , wi 1 wi for each i 0,1, 1 k1 2k2 2k3 k4 , 6 , N 1 . This method has local truncation error O h 4 , provided the solution y t has five continuous derivatives. The reason for introducing the notation k1 , k2 , k3 , k4 into the method is to eliminate the need for successive nesting in the second variable of f t , y (see Exercise 17). Algorithm 5.2 implements the Runge-Kutta method of order four. 5 Runge-Kutta (Oreder Four) PURPOSE To approximate the solution of the initial-value problem y ' f t , y , a t b, y a , at N 1 equally INPUT spaced numbers in the interval a, b : endpoints a , b ; integer N ; initial condition , OUTPUT approximation wi to y ti at the Step 1 h b a N ; N 1 values of ti . t0 a ; w0 ; OUTPUT t0 , w0 . For i 0,1, Step 2 Step 3 , N 1 do Steps 3-5. K1 hf ti , wi ; K2 hf ti h 2, wi K1 2 ; K3 hf ti h 2, wi K2 2 ; K4 hf ti h, wi K3 . Step 4 wi 1 wi K1 2K2 2K3 K4 6 ; (Compute wi 1 .) ti 1 a (i 1)h . (compute ti 1 .) Step 5 Step6 OUTPUT ti 1 , wi 1 . STOP This produced Runge-Kutta method of order four (Algorithm 5.2) described in the following subroutine: SUBROUTINE RK4(N,A,B,ALPHA,T,W) C****************************************************************** C ALGORITHM 5.2 RUNGE-KUTTA (ORDER 4) C PURPOSE: TO APPROXIMATE THE SOLUTION TO THE INITIAL VALUE PROBLEM C Y' = F(T,Y), A <= T <= B, Y(A) = ALPHA, 6 C AT (N+1) EQUALLY SPACED NUMBERS IN THE INTERVAL [A,B]. C C INPUT: ENDPOINTS A,B; INITIAL CONDITION ALPHA; INTEGER N. C C OUTPUT: APPROXIMATION W TO Y AT THE (N+1) VALUES OF T. C-----------------------------------------------------------------C INTEGER N REAL A,B,ALPHA REAL T(0:10),W(0:10) REAL XK1,XK2,XK3,XK4,H INTEGER I EXTERNAL F C STEP 1 H = (B-A)/N T(0)=A W(0)=ALPHA C STEP 2 DO 110 I=0,N-1 STEP 3 USE XK1,XK2,XK3,XK4 FOR K(1),K(2),K(3),K(4) RESP. XK1 = H*F(T(I),W(I)) XK2 = H*F(T(I)+H/2,W(I)+XK1/2) XK3 = H*F(T(I)+H/2,W(I)+XK2/2) XK4 = H*F(T(I)+H,W(I)+XK3) STEP 4 COMPUTE W(I+1) W(I+1) = W(I) + (XK1+2*(XK2+XK3)+XK4)/6 COMPUTE T(I+1) T(I+1)=A+(I+1)*H STEP 5 CONTINUE STEP 6 RETURN END C C C C C C 110 C ■ EXAMPLE 3 (Section 5.4, p278) Using the Runge-Kutta method of order four to obtain approximations to the solution of the initial-value problem y ' y t 2 1, 0 t 2, y 0 0.5, with h 0.2, N 10, and ti 0.2i . 7 AL52.f is shown as follows: C****************************************************************** C Example 3 (Section 5.4) Using RUNGE-KUTTA (ORDER 4) to C approximate the solution of the initial-value problem C dy/dt=y-t^2+1, y(0)=0.5 C****************************************************************** C INTEGER N REAL A,B,ALPHA REAL T(0:10),W(0:10),Y(0:10) INTRINSIC EXP,ABS OPEN(UNIT=10,FILE='AL52.doc',STATUS='UNKNOWN') C *** Input initial values A = 0.0 B = 2.0 ALPHA = 0.5 N = 10 WRITE(10,*) ' T(i) W(i) Y(i) |W(i)-Y(i)|' WRITE(10,*) '---------------------------------------------' WRITE(*,*) ' T(i) W(i) Y(i) |W(i)-Y(i)|' WRITE(*,*) '---------------------------------------------' CALL RK4(N,A,B,ALPHA,T,W) DO 10 I = 0,10 C *** Exact solution Y(I) = (T(I)+1.0)*(T(I)+1.0)-0.5*EXP(T(I)) WRITE(10,99) T(I),W(I),Y(I),ABS(W(I)-Y(I)) WRITE(*,99) T(I),W(I),Y(I),ABS(W(I)-Y(I)) 10 CONTINUE STOP 99 FORMAT(1X,F5.1,3F12.7) END REAL FUNCTION F(T,Y) C==================================================================== C PURPOSE C Find the value of function f(t,y) = y-t^2+1 C-------------------------------------------------------------------C REAL T,Y F = Y-T*T+1.0 8 RETURN END h 0.2, N 10, and ti 0.2i gives the results and errors listed in Table 5.6. ■ Table 5.6 Exact Runge-Kutta Order Four ti yi y ti wi 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 0.5000000 0.8292986 1.2140877 1.6489406 2.1272295 2.6408591 3.1799415 3.7324000 4.2834838 4.8151763 5.3054720 0.5000000 0.8292933 1.2140762 1.6489220 2.1272027 2.6408227 3.1798942 3.7323401 4.2834095 4.8150857 5.3053630 Error yi wi 0 0.0000053 0.0000114 0.0000186 0.0000269 0.0000364 0.0000474 0.0000599 0.0000743 0.0000906 0.0001089 The main computation effort in applying the Runge-Kutta methods is the evaluation of f . In the second-order methods, the local truncation error is O h 2 , and the cost is two functional evaluation per step. The Runge-Kutta method of order four requires 4 evaluations per step, and the local truncation error is O h 4 . Butcher (see [But] for a summary) has established the relationship between the number of evaluations per step and the order of the local truncation error shown in Table 5.7. This table indicates why the methods of order less than five with smaller step size are used in preference to the higher-order methods using a larger step size. Table 5.7 Evaluations per 2 3 4 5 n 7 8 n 9 10 n step Best possible O h 2 O h3 O h 4 O h n 1 O h n 2 O h n 3 local Truncation error One measure of comparing the lower-order Runge-Kutta methods is described 9 as follows: The Runge-Kutta method of order four requires four evaluations per step, so it should give more accurate answers than Euler’s method with one-fourth the step size if it is to be superior. Similarly, if the Runge-Kutta method of order four is to be superior to the second-order Runge-Kutta methods, it should give more accuracy with step size h than 1 a second-order method with step size h ,because the fourth-order method 2 requires twice as many evaluations per step. An illustration of the superiority of the Runge-Kutta fourth-order method by this measure is shown in the following example. EXAMPLE 4 For the problem y ' y t 2 1, 0 t 2, y 0 0.5, Euler’s method with h 0.025 , the Midpoint method with h 0.05 , and the Runge-Kutta fourth-order method with h 0.1 are compared at the common mesh points of these methods 0.1, 0.2, 0.3, 0.4, and 0.5. Each of these techniques requires 20 functional evaluations to determine the values listed in Table 5.8 to approximate fourth-order ■ Table 5.8 ti 0.0 0.1 0.2 0.3 0.4 0.5 method Euler h 0.025 0.5000000 0.6554982 0.8253385 1.0089334 1.2056345 1.4147264 Exact 0.5000000 0.6574145 0.8292986 1.0150706 1.2140877 1.4256394 y 0.5 . In this example, the is Modified Euler h 0.05 0.5000000 0.6573085 0.8290778 1.0147254 1.2136079 1.4250141 clearly superior. Runge-Kutta Order Four h 0.1 0.5000000 0.6574144 0.8292983 1.0150701 1.2140869 1.4256384 Extercise set 5.4 Q4. Use the Modified Euler method to approximation the solutions to each if the following initial-value problems, give Fortran code and compare the results to the exact solutions: (b) y=1+ y t + y t , 1 t 3, y 1 0, with h 0.1; 2 Exact solution is y t t tan ln t . Q11. Repeat Q4 using the Runge-Kutta method of order four. 10