Lecture12 - Initial Value Problem Runge Kutta Methods

26
Numerical Solutions of Ordinary Differential Equations Lecture 12: Initial Value Problem: Runge- Kutta Methods MTH2212 – Computational Methods and Statistics

Transcript of Lecture12 - Initial Value Problem Runge Kutta Methods

MTH2212 Computational Methods and Statistics

Numerical Solutions of Ordinary Differential EquationsLecture 12: Initial Value Problem: Runge-Kutta Methods

ObjectivesIntroduction Runge-Kutta Methods

Second-order Runge-Kutta Heun Method Midpoint Method Ralstons Method Fourth-order Runge-Kutta

System of ODE

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

2

Introduction

A fundamental source of error in Eulers method is that the derivative at the beginning of the interval is assumed to apply across the entire interval. Improving the estimate of the slope will improve the solution.

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

3

Runge-Kutta Methods

Runge-Kutta (RK) methods achieve the accuracy of a Taylor series without requiring the calculation of higher derivatives. For a differential equation of the form: dy ! f ( x, y ) dx The estimate of the solution is given by

yi 1

yi J( xi , yi , h) h

where J( xi , yi , h) is the increment function which is the slope over the intervalDr. M. Hrairi

MTH2212 - Computational Methods and Statistics

4

Runge-Kutta Methods

The increment function can be written in general form as J ! a1k1 a 2 k 2 a3 k 3 ... a n k n where a1, a2, a3,, an are constants and k1, k2, k3,, kn are

k1 ! f ( xi , yi ) k 2 ! f ( xi p1 h, yi q11k1 h) k 3 ! f ( xi p 2 h, yi q 21k1 h q 22 k 2 h) . . . k n ! f ( xi p n 1 h, yi q n1,1 k1 h q n1, 2 k 2 h ... q n 1,n 1 k n1 h)Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

5

Runge-Kutta Methods

Where p1, p2,,pn-1 and q1, q2,,qn-1 are all constants n is order number of the RK method ks are recurrence functions. Because each k is a functional evaluation, this recurrence makes RK methods efficient for computer calculations. Various types of RK methods can be devised by employing different number of terms in the increment function as specified by n. Once n is chosen, values of as, ps, and qs are evaluated by setting general equation equal to terms in a Taylor series expansion. First-order RK method (n = 1) is Eulers method.MTH2212 - Computational Methods and Statistics 6

Dr. M. Hrairi

Second-order Runge-Kutta

The second-order version of Runge-Kutta methods is

yi 1 ! yi (a1k1 a2 k 2 ) hwhere

(i)

k1 ! f ( xi , yi ) k 2 ! f ( xi p1 h, yi q11k1 h)

Evaluate values for a1, a2, p1 and q11 by setting equation (i) equal to a Taylor series expansion to the second-order term (see Box 25.1 p.703 for details). Three equations are derived to solve four unknown constants

a1 a2 ! 1 a2 p1 ! 1 / 2 a2 q11 ! 1 / 2Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

7

Second-order Runge-Kutta

Since there are three equations with four unknowns, we must assume a value of one of the unknowns to determine the other three. Suppose that we specify a value for a2, then a1 ! 1 a 2

1 p1 ! q11 ! 2a 2 There are an infinite number of second-order RK methods

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics MTH2212

8

Second-order Runge-Kutta

Because we can choose an infinite number of values for a2, there are an infinite number of second-order RK methods. Every version would yield exactly the same results if the solution to ODE were quadratic, linear, or a constant. However, they yield different results if the solution is more complicated (typically the case). Three of the most commonly used RK second-order methods are: Heuns Method with a Single Corrector (a2=1/2) The Midpoint Method (a2=1) Raltsons Method (a2=2/3)MTH2212 - Computational Methods and Statistics MTH2212 9

Dr. M. Hrairi

Heun Method with a Single Corrector

a2 is set to a2=1/2

a1 ! 1 a 2 ! 1 / 2 p1 ! q11 ! 1 / 2a2Substituting into equation (i) 1 1 yi 1 ! yi ( k1 k 2 ) h 2 2 Where k1 ! f ( xi , yi )

k 2 ! f ( xi h, yi k1 h) k1 is the slope at the beginning of the interval and k2 is the slope at the end of the interval.MTH2212 - Computational Methods and Statistics 10

Dr. M. Hrairi

The Midpoint Method

a2 is set to a2=1 a1 ! 0

1 p1 ! q11 ! 2 Substituting into equation (i)

yi 1Where

yi k 2 h

k1 ! f ( xi , yi ) 1 1 k 2 ! f ( xi h, yi k1h) 2 2

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

11

Ralstons Method

a2 is set to a2=2/3

a1 ! 1 / 3 p1 ! q11 ! 3 / 4

Substituting into equation (i)

yi 1Where

y i ( k1 k 2 ) h 3 3

k1 ! f ( xi , yi ) 3 3 k 2 ! f ( xi h, yi k1h) 4 4

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

12

Second-order Runge-Kutta

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

13

Fourth-order Runge-Kutta

This is the most popular of the RK methods 1 yi 1 ! yi (k1 2k 2 2k 3 k 4 ) h 6

Where

k1 ! f ( xi , y i ) 1 1 k 2 ! f ( xi h, y i k1 h) 2 2 1 1 k 3 ! f ( x i h, y i k 2 h ) 2 2 k 4 ! f ( x i h, y i k 3 h )Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

14

Example 1Integrate using fourth-order Runge-Kutta method: dy 4e0.8 x 0.5 y dx Using h = 0.5 with y(0) = 2 from x = 0 to 0.5.

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

15

Example 1 - SolutionCompute the slope at the beginning of the interval: k ! f ( xi , yi ) ! f (0, ) ! 4e 0.8( 0) 0.5( ) ! 3

Use this slope to compute the value of y and slope at midpoint y(0.25 ) ! 2 3(0.25 ) ! 2.75 k 2 ! f (0.25, .75 ) ! 4e 0.8(0.25) 0.5( .75 ) ! .510611

Use this slope to calculate another value of y and slope at the midpoint y( .2 ) ! 2 3. 1 611 ( .2 ) ! 2.8776 3k3 ! f 0.25 2.877653 ) ! 4e 0.8 0.25) 0.5 2.877653 ! 3.446785

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

16

Example 1 - SolutionUse the this slope to compute the slope at the end of the interval:y(0.5) ! .071785 (0.5) ! .72 92 k 4 ! f (0.5,3.723392 ) ! 4e 0.8( 0.5) 0.5(3.72 92 ) ! 4.10560

Evaluate the final prediction at the end of the interval1 (k1 2k 2 2k 3 k 4 ) h i 6 1 y (0.5) ! 2 (3 2 v 3.510611 2 v 3.446785 4.105603) 0.5 6 ! 3.751669i 1 !

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics MTH2212

17

System of Equations

Many practical problems in engineering require the solution of a system of simultaneous ODE rather than a single equationdy 1 dx dy 2 dx . . . dy n dx f1 ( x, y1 , y2 ,..., yn ) f 2 ( x, y1 , y2 ,..., yn )

f n ( x, y1 , y2 ,..., yn )

Solution requires that n initial conditions be known at the starting value x.MTH2212 - Computational Methods and Statistics 18

Dr. M. Hrairi

Procedure for Solution Using RK Methods1.

Compute the slopes for all variables at the beginning of the interval (set of k1s). Use these slopes to make predictions of the dependent variable at the midpoint of the interval (y1, y2,). Use the midpoint values to compute a first set of slopes at the midpoint of the interval (set of k2s) The new set of slopes k2 are used to make another set of midpoint predictions (y1, y2,). Use the latest set of midpoint predictions (y1, y2,) to compute the second set of midpoint slopes (set of k3s)MTH2212 - Computational Methods and Statistics 19

2.

3.

Dr. M. Hrairi

Procedure for Solution Using RK Methods4.

Use the values of k3s to make predictions at the end of the interval (y1, y2,). Use the predictions at the end of the interval to compute the endpoint slopes (set of k4s) Combine the ks into a set of increment functions and brought back to the beginning to make the final prediction of (y1, y2,).

5.

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

20

Example 2Solve the following set of differential equations using the 4th order RK method, assuming that at x = 0, y1 = 4 and y2 = 6. Integrate to x = 2 with a step size of 0.5dy 1 ! 0.5 y dx dy 2 ! 4 0.3 y 0.1y 2 1 dx dy ! y ! f ( x, y , y ) dx dy ! y y ! f ( x, y , y ) dx

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

21

Example 2 - Solution1.

Compute the slopes for all variables at the beginning of the interval (set of k1s).k1,1 ! f1 (0,4, ) ! 0.5(4) ! 2 k1, 2 ! f 2 (0,4, ) ! 4 0.3( ) 0.1(4) ! 1.8

2.

Use set of k1s to make predictions of (y1, y2,) at the midpoint of the interval. Use these (y1, y2,) to compute a first set of slopes at the midpoint of the interval (set of k2s)h y1 ( .25) ! y1 ( ) k1,1 ! 4 ( )( . 5) ! .5 2 h y2 ( . 5) ! y2 ( ) k , 2 ! 6 ( .8)( . 5) ! 6.45

k2 ,1 ! f1 (0.25,3.5, .45) ! 0.5(3.5) ! 1.75 k2 , 2 ! f 2 (0.25,3.5, .45) ! 4 0.3( .45) 0.1(3.5) ! 1.715Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics MTH2212

22

Example 2 - SolutionUse the set of k2s to make another set of midpoint predictions (y1, y2,). Use the latest set of (y1, y2,) to compute the second set of midpoint slopes (set of k3s)y1 (0.25) ! y1 (0) k2,1 0.5 h ! 4 (1.75)( ) ! 3.5 25 2 2 0.5 h y2 (0.25) ! y2 (0) k2, 2 ! (1.715)( ) ! .42 75 2 2

3.

k3,1 ! f1 (0.25,3.5 25, .42875) ! 0.5(3.5 25) ! 1.78125 k3, 2 ! f 2 (0.25,3.5 25, .42875) ! 4 0.3( .42875) 0.1(3.5 25) ! 1.715125

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

23

Example 2 - Solution4.

Use the values of k3s to make predictions at the end of the interval (y1, y2,). Use the predictions at the end of the interval to compute the endpoint slopes (set of k4s)y1 (0.5) ! y1 (0) k3,1h ! 4 (1.78125)(0.5) ! 3.109375 y2 ( .5) ! y2 ( ) k3, 2 h ! 6 (1.715125)( .5) ! 6.85756

k4,1 ! f1 (0.5,3.109375,6.857563) ! 0.5(3.109375) ! 1.554688 k4, 2 ! f 2 (0.5,3.109375,6.857563) ! 4 0.3(6.857563) 0.1(3.109375) ! 1.6317945.

Combine the ks into a set of increment functions and brought back to the beginning to make the final prediction of (y1, y2,).1 y1 (0.5) ! 4 [( 2) 2(1.75) 2(1.78125) (1.554688)] 0.5 ! 3.115234 6 1 y2 (0.5) ! 6 [(1.8) 2(1.715) 2(1.715125) (1.631794)] 0.5 ! 6.857670 6MTH2212 - Computational Methods and Statistics

Dr. M. Hrairi

24

Example 2 - SolutionRepeating the procedure for the remaining steps gives the following results x 0.0 0.5 1.0 1.5 2.0Dr. M. Hrairi

y1 4 3.115234 2.426171 1.889523 1.471577

y2 6 6.857670 7.632106 8.326886 8.94686525

MTH2212 - Computational Methods and Statistics

Assignment # 5

Computational Methods

22.13, 25.4, 25.8, 25.9, 28.33 Check with Dr Faiz

Statistics

Dr. M. Hrairi

MTH2212 - Computational Methods and Statistics

26