5.4 Runge-Kutta Methods

Total Page:16

File Type:pdf, Size:1020Kb

5.4 Runge-Kutta Methods

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, y 0 ) D . For every (t, y) D ,

m y there exists x between t and t0 and between and y0 with

f( t, y) = Pn( t , y) + R n ( t , y) , where 轾 抖f f Pn ( t, y) = f( t0 , y 0) +犏( t - t 0) ( t 0 , y 0) +( y - y 0) ( t 0 , y 0 ) 臌 抖t y 2 (t- t ) 抖2f 2 f +0 (t, y) +( t - t)( y - y) ( t , y ) 2 抖t2 0 0 0 0 t y 0 0

2 ( y- y ) 2 f +0 (t, y ) +⋯ 2 y2 0 0

n n 轾1 骣n n- j j f +犏 琪 (t - t0) ( y - y 0) n- j j ( t 0, y 0 ) 臌n! j=0 桫j 抖 t y and

n+1 n+1 1 骣n +1 n+1 - j j f Rn ( t, y) =琪 ( t - t0) ( y - y 0 ) n- j j (x , m ) . ■ (n+1) ! j=0 桫 j 抖 t y

The function Pn ( t, y) is called the n th Taylor polynomial in two

1 variables for the function f about(t0, y 0 ) , and Rn ( t, y) is the remainder

term associated with Pn ( t, y) .

The first step in deriving a Runge-Kutta method is to determine values for a1,a 1 , and b1 with the property that a1 f( t+a 1, y + b 1 ) approximates h T(2) ( t, y) = f( t , y) + f '( t , y) , 2 with error no greater than O( h2 ) , the local truncation error for the Taylor method of order two. Since df抖 f f f'( t , y) =( t , y) =( t , y) + ( t , y)� y '( t) and y '( t) f( t , y) , dt抖 t y this implies h抖 f h f T(2) ( t, y) = f( t , y) +( t , y) + ( t , y) f( t , y) . 2抖t 2 y

Expanding f( t+a1, y + b 1 ) in its Taylor polynomial of degree on about

(t, y) gives

f a f( t+a, y + b) = a f( t , y) + a a ( t , y) 1 1 1 1 1 1 t f +ab( t, y) + a� R( t + a , y b ) , 1 1y 1 1 1 1 where a2抖2f 2 f b 2 2 f R( t+a, y + b) =1( x , m) + a b( x , m) + 1 ( x , m ) , 1 1 12抖t2 1 1 t 抖 y 2 y 2

m y for somex between t and t +a1 and between and y + b1 .

Matching the coefficients of f and its derivatives in Eqs. (5.18) and (5.19) gives the three equations f h f( t, y) : a= 1;( t , y) : a a = ; 1t 1 1 2 and

2 f h (t, y) : ab = f( t , y) . y 1 1 2

The parameters a1,a 1 , and b 1 are uniquely determined to be h h a=1,a = , and b = f( t , y) ; 1 12 1 2 so

(2) 骣h h 骣 h h T( t, y) = f琪 t + , y + f( t , y) - R1 琪 t + , y + f( t , y) , 桫2 2 桫 2 2 and from Eq. (5.20), 骣 h h h2抖 2 f h 2 2 f R1 琪 t+, y + f( t , y) =(x , m) + f( t , y) ( x , m ) 桫 2 2 8抖t2 4 t y 2 2 h2 f + ( f( t, y)) (x , m ) . 8 y2

If all the second-order partial derivatives of f are bounded, then

骣 h h R1 琪 t+, y + f( t , y) 桫 2 2 is O( h2 ) , 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 = a, 骣 h h wi+1 = w i + hf琪 t i +, w i + f( t i , w i ) , for each i = 0,1, , N - 1. 桫 2 2

Since only three parameters are present in a1 f( t+a 1, y + b 1 ) and all are needed in the match of T (2) , we need a more complicated form to satisfy the condition required for any of the higher-order Taylor methods.

3 The most appropriate four-parameter form for approximating

h h2 T(3) ( t, y) = f( t , y) + f '( t , y) + f "( t , y) 2 6 is

a1 f( t, y) + a 2 f( t +a 2 , y + d 2 f( t , y)) ; and even with this, there is insufficient flexibility to match the term

2 h2 轾 f 犏 (t, y) f( t , y) , 6 臌y resulting from the expansion of(h2 6) f "( t , y) . Consequently, the best that can be obtained from using (5.21) are methods with O( h2 ) local truncation error. The fact that (5.21) has four parameters, however, gives a flexibility in their choice, so a number of O( h2 ) methods can be derived. One of the most important is the Modified Euler method, 1 which corresponds to choosing a= a = and a= d = h and has the 1 2 2 2 2 following difference-equation form.

Modified Euler Method:

w0 = a, h wi+1= w i +轾 f( t i, w i) + f( t i + 1 , w i + hf( t i , w i )) , 2 臌 for eachi= 0,1,2, , N - 1,

The other important O( h2 ) method is Heun’s method, which correspond 1 3 2 to a=, a = , and a= d = h , and has the following difference- 14 2 4 2 2 3 equation form.

Heun’s Method:

w0 = a, h 轾 骣 2 2 wi+1 = w i +犏 f( t i, w i) + 3 f琪 t i + h , w i + hf( t i , w i ) , 4臌 桫 3 3 for eachi= 0,1,2, , N - 1. Both are classified as Runge-Kutta methods of order two, the order of their local truncation error.

4 EXAMPLE 2 Suppose we apply the Runge-Kutta methods of order two to our usual example, y'= y - t2 + 1, 0# t 2, y ( 0) = 0.5,

with N=10, h = 0.2, ti = 0.2 i , and w0 = 0.5 in each case. The difference equations produced from the various formulas are

2 Midpoint method: wi+1 =1.22 w i - 0.0088 i - 0.008 i + 0.218;

2 Modified Euler method: wi+1 =1.22 w i - 0.0088 i - 0.008 i + 0.216;

2 Heun’s method: wi+1 =1.22 w i - 0.0088 i - 0.008 i + 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 errorO( h3 ) by an expression of the form

f( t+a1, y + d 1 f( t + a 2 , y + d 2 f( t , y))) , involving four parameters, the algebra involved in the determination ofa1, d 1 , a 2 , and d 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 = a,

k1 = hf( ti, w i ) , 骣 h 1 k2= hf琪 ti +, w i + k 1 , 桫 2 2 骣 h 1 k3= hf琪 ti +, w i + k 2 , 桫 2 2

k4= hf( ti+ 1, w i + k 3 ) , 1 w= w +( k +2 k + 2 k + k ) , i+1 i 6 1 2 3 4

5 for each i=0,1, , N - 1. This method has local truncation error O( h4 )

, provided the solution y( t) has five continuous derivatives. The

reason for introducing the notation k1, k 2 , k 3 , k 4 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.

Runge-Kutta (Oreder Four) PURPOSE To approximate the solution of the initial-value problem y'= f( t , y) , a# t b , y( a) = a , at ( N +1) equally spaced numbers in the interval [a, b] :

INPUT endpoints a, b ; integer N ; initial condition a ,

OUTPUT approximation wi to y( ti ) at the ( N +1) values of ti .

Step 1 h=( b - a) N ;

t0 = a ;

w0 = a ;

OUTPUT (t0, w 0 ) .

Step 2 For i=0,1, , N - 1 do Steps 3-5.

Step 3 K1 = hf( ti, w i ) ;

K2= hf( ti + h2, w i + K 1 2) ;

K3= hf( ti + h2, w i + K 2 2) ;

K4= hf( ti + h, w i + K 3 ) .

Step 4 wi+1= w i +( K 1 +2 K 2 + 2 K 3 + K 4 ) 6 ; (Compute wi+1 .)

ti+1 = a +( i + 1) h . (compute ti+1 .)

6 Step 5 OUTPUT(ti+1, w i + 1 ) . Step6 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, 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 C STEP 3 C 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) C STEP 4 C COMPUTE W(I+1) W(I+1) = W(I) + (XK1+2*(XK2+XK3)+XK4)/6 C COMPUTE T(I+1) T(I+1)=A+(I+1)*H C STEP 5

7 110 CONTINUE C STEP 6 RETURN END ■ 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 - t2 + 1, 0# t 2, y ( 0) = 0.5,

with h=0.2, N = 10, and ti = 0.2 i . 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))

8 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 RETURN END

h=0.2, N = 10, and ti = 0.2 i gives the results and errors listed in Table 5.6. ■ Table 5.6 Exact Runge-Kutta Error t i Order Four yi= y( t i ) yi- w i

wi 0.0 0.5000000 0.5000000 0 0.2 0.8292986 0.8292933 0.0000053 0.4 1.2140877 1.2140762 0.0000114 0.6 1.6489406 1.6489220 0.0000186 0.8 2.1272295 2.1272027 0.0000269 1.0 2.6408591 2.6408227 0.0000364 1.2 3.1799415 3.1798942 0.0000474 1.4 3.7324000 3.7323401 0.0000599 1.6 4.2834838 4.2834095 0.0000743 1.8 4.8151763 4.8150857 0.0000906 2.0 5.3054720 5.3053630 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( h2 ) , 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( h4 ) . Butcher (see

9 [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 h2 O h3 O h4 O hn-1 O hn-2 O hn-3 local ( ) ( ) ( ) ( ) ( ) ( ) Truncation error

One measure of comparing the lower-order Runge-Kutta methods is described 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 1 size h than a second-order method with step size h ,because the 2 fourth-order method 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 - t2 + 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.1are 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 y (0.5) . In this example, the fourth-order method is clearly superior. ■ Table 5.8 Exact Euler Modified Runge-Kutta t i h = 0.025 Euler Order Four h = 0.05 h = 0.1 0.0 0.5000000 0.5000000 0.5000000 0.5000000 0.1 0.6574145 0.6554982 0.6573085 0.6574144

10 0.2 0.8292986 0.8253385 0.8290778 0.8292983 0.3 1.0150706 1.0089334 1.0147254 1.0150701 0.4 1.2140877 1.2056345 1.2136079 1.2140869 0.5 1.4256394 1.4147264 1.4250141 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)2 , 1# t 3, y( 1) = 0, with h = 0.1;

Exact solution is y( t) = t tan( ln t) . Q11. Repeat Q4 using the Runge-Kutta method of order four.

11

Recommended publications