You are on page 1of 9

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods

Chapter 6 Ordinary Differential Equations

Reference
James F. Epperson (Revised Edition, 2007) - Chapter 6

Content
6.1 Introduction 6.2 Eulers Method 6.3 Mid-Point Method 6.4 Higher-Order Methods 6.5 Runge-Kutta Methods 6.6 Multistep Methods 6.7 System of ODEs

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods Chapter 6 Ordinary Differential Equations

6.1 Introduction Initial value problem (IVB) for ordinary differential equations (ODE). Say y = y (x ) . A first order ODE of y w.r.t. x is defined at an interval x [x0 , x n ] :
y = dy = f (x , y (x )), dx y (x 0 ) = y 0 .

(6.1)

For example: y' = f (x , y ) = 1 xy , y (0) = 0 or y' = f (x , y ) = 1 y , y (0) = 0 or y' = f (x , y ) = 1 x , y (0) = 0 . We want to find the unknown function y (x ) that solves the ODE (6.1) for all x > x0 and y (x 0 ) = y 0 .

where y (x 0 ) = y 0 is the initial value of y at the initial point x = x0 .

6.2 Eulers Method To derive Eulers formula we use Taylors Theorem. Divide the interval of interest into n subintervals: x0 , x1 = x0 + h,K , x n = x n 1 + h At any point xi +1 = xi + h :
y (x i + h ) = y (x i ) + hy i ' (x i ) + 1 2 h yi" ( ) 2 1 = y (x i ) + hf (x i , y (x i )) + h 2 y i " ( ) 2

for some between xi and x i + h . We write in the form that is suitable for programming:
y i +1 = y i + hf (x i , y i ) + 1 2 h y i " ( ) . 2

Dropping the residual, the Eulers formula is


y i +1 = y i + hf (x i , y i ) .

(6.2)

Residual,
R (x , h ) = 1 2 h y i " ( ) . 2

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods Chapter 6 Ordinary Differential Equations

Thus local truncation error ~ O h 2 Global truncation error ~ O(h )

( )

Example 6.1 (JE Chapter 6 Example 6.6, page 331) Given y' + y = 1, Solution: Step 1

y (0) = 0 for x [0,1] .

y' = f ( x , y ) = 1 y
From (6.2), the Eulers formula is
y i +1 = y i + hf (x i , y i ) = y i + h(1 y i )

Step 2 Choose step size h. Let us use h = 0.1 i.e. n = 10 . Step 3 Tabulate the results.
i +1

x i +1

y i +1

0 0+1 = 1 2 3 4 5 6 7 8 9 10

x0 = 0.0 y 0 = 0.0 Output from Eulers program y 1 = y 0 + h(1 y 0 ) = 0 + 0.1(1 0 ) x1 = x0 + h = 0.0 + 0.1

= 0.1000000 x2 = 0.2000000 x 3 = 0.3000000

= 0.1000000 y 2 = 0.1900000 y 3 = 0.2710000

x 4 = 0.4000000 x 5 = 0.5000000
x 6 = 0.6000000

y 4 = 0.3439000 y 5 = 0.4095100
y 6 = 0.4685590

x7 = 0.7000000 x 8 = 0.8000001
x 9 = 0.9000001 x10 = 1.0000001

y 7 = 0.5217031 y 8 = 0.5695328
y 9 = 0.6125795 y 10 = 0.6513216

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods Chapter 6 Ordinary Differential Equations

Exact solution:
y ' = 1 y

dy =0 1 y = dx 0 y0 ln y
y y 0 =0

=x

x 0

y = 1 e x y i = 1 e xi Error = En = max y (xi ) y i .


i n

Other variant of Eulers method is the backward Eulers method. If y' (x ) =


y (x ) y (x h ) 1 hy" ( ) then the backward Eulers method is h 2

y i +1 = y i + hf (x i +1 , y i +1 ) .

(6.3)

Equation (6.3) is an implicit method as opposed to (6.2) which is an explicit method. Solve (6.3) with Newtons method for nonlinear equations (Chapter 4). Eulers methods (forward or backward) are single-step methods. Algorithm 6.1 Given y 0 = initial value, x 0 = initial point, xn = final point, n = number of points.
h= x n x0 n

, for i = 0,1 2,L, (n 1)


y i +1 = y i + hf (x i , y i )

end

6.3 Mid-Point Method Main source of error in Eulers method is the usage of the derivative in the entire interval of interest. Mid-point or improved polygon method addresses this problem by using the derivative at the mid-point of the interval.

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods Chapter 6 Ordinary Differential Equations

Calculate y at mid-point:
y i + 1 = y i + f (x i , y i )
2

h . 2

(6.4)

Calculate the derivative at mid-point: y i+ 1 = f x i + 1 , y i + 1


2 2 2

)
)

(6.5)

Thus the mid-point formula: y i +1 = y i + f xi + 1 , y i + 1 h .


2 2

(6.6)

Example 6.2 Apply mid-point method to Example 6.1.

6.4 Higher-Order Methods Eulers method is straight forward to implement but not accurate. Alternatively use higher-order Taylor expansion. But this requires more function evaluation (work) per step. For example, second-order Taylor expansion: y (x + h ) y (x ) + hy (x , y ) + h2 y (x , y ) 2 h2 y (x ) + hf (x , y ) + f (x , y ) 2

requires two function evaluations ( f and f ) per step.

6.5 Runge-Kutta Methods Single-step methods. Widely used ODE solver. Expand Taylor series to higher orders: y (x + h ) = y (t ) + hy' (x ) + Second order method: y (x + h ) y (x ) + hy (x ) + h2 y (x ) . 2 h2 h3 y" (x ) + y (x ) + ... 2 6

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods Chapter 6 Ordinary Differential Equations

or y i +1 = y i + hy i + h2 y i . 2

To evaluate y , we can use chain rule y = f (x , y ) + f (x , y )y x y = fx (x , y ) + fy (x , y )f (x , y ) = f x + fy y where fx = f f , fy = , y = f . y x

(*)

Expand y = f (x , y ) in Taylors series, f (x + h, y + hf ) = f + hf x + hfy f + O h 2 fx + fy f =

f (x + h, y + hf ) f (x , y ) . h = y

( )

(**)

Hence (*) = (**),

y i +1 = y i + hf (xi , y i ) +

h 2 f (xi + h, y i + hf (x i , y i )) f (x i , y i ) 2 h h = y i + hf (xi , y i ) + [f (x i + h, y i + hf (x i , y i )) f (x i , y i )] 2 h = y i + [f (x i , y i ) + f (x i + h, y i + hf (x i , y i ))] 2 h = y i + (K1 + K 2 ) 2

(6.7)

where K1 = f (xi , y i ) and K 2 = f (xi + h, y i + hf (xi , y i )) . Equation (6.7) Runge-Kutta Order 2 (RK2) or Heuns method.

Example 6.3 (MH Chapter 9 Example 9.12, page 405) Given f = y' = 2xy 2 , y (0) = 1, x [0,0.5]. Use h = 0.25 . Solution: Tabulate the results.

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods Chapter 6 Ordinary Differential Equations

xi
x0 = 0 0.25 0.50

K1
0 -0.4395

K2
-0.5000 -0.6850

y i +1
y0 = 1 0.9375 0.7969

Analytic 0.9412 0.8000

Error 0.0380 0.0041

0 1 2 Exact solution: y (x ) =

1 1+ x 2

y (0.25) = 0.9412 and y (0.5) = 0.8 .


We write without proof the formula for Runge-Kutta Order 4 (RK4). y i +1 = y i + h (K1 + 2K 2 + 2K 3 + K 4 ) 6

where K 1 = f (x i , y i ) h h K 2 = f x i + , y i + K1 2 2 h h K 3 = f xi + , y i + K 2 2 2 K 4 = f (xi + h, y i + hK 3 ) (6.8)

Example 6.4 Try RK4 in Example 6.1.

6.6 Multistep Methods Euler, RK are single-step methods. In the multistep methods, we need information more than the previous point to estimate the solution at the next point. Strategy: 1. Use single step methods to generate approximation at enough points to begin multistep method. 2. A good guess is suggested by an explicit method. 3. Then an implicit method used to solve y k +1 Hence explicit and implicit pair is called predictor corrector pair: a) Adam-Bashforth Predictor (explicit fourth-order) h y i +1 = y i + 55 y i' 59 y i' 1 + 37 y i' 2 9 y i' 3 . 24 b) Adam-Moulton Corrector (implicit fourth-order)

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods Chapter 6 Ordinary Differential Equations

y i +1 = y i +

h 9 y i' +1 + 19 y i' 5 y i' 1 + y i' 2 . 24

6.7 System of ODEs

Appilcation of Eulers method (Equation (6.2)) for solving a system of ODEs. Say we have two ODEs:
dy = f1 (x , y , z ) dx dz = f2 ( x , y , z ) dx

(6.9)

(6.10)

for x [a, b] , intial conditions: y (a ) = y 1 z(a ) = z1 and with y and z as the dependent variables and x as the independent variables. Using Eulers method (Equation (6.2)), the recursion relations to solve (6.9) and (6.10) simultaneously are:

xi +1 = xi + h
y i +1 = y i + hf1 (x i , y i , zi ) zi +1 = zi + hf2 (x i , y i , zi )

(6.11) (6.12) (6.13)

Example 6.5 (JE Chapter 6 Exercise 1(a), page 379) Given h = 1 4 . y 1 = 4 y 1 + y 2 , y 1 (0 ) = 1 y 2 = y 1 4 y 2 , y 2 (0 ) = 1 Solution: Step 1 According to Equations (6.12) and (6.13), y 1 = y and y 2 = z . The recursion relations are:

xi +1 = xi + h
y 1,i +1 = y 1,i + h y 1 (x i , y 1,i , y 2 ,i )

SEMESTER 1 2011/2012

SMES2105 Numerical and Computational Methods Chapter 6 Ordinary Differential Equations

y 2 ,i +1 = y 2 ,i + hy 2 (x i , y 1,i , y 2 ,i )

Step 2

i =0
x1 = x0 + h = 0 + 0.25 = 0.25
y 1,1 = y 1,0 + h y 1 (x 0 , y 1,0 , y 2 ,0 ) = 1 + 0 .25 y 1 (0,1,1) = 1 + 0 .25 ( 4 1) = 1 1.25 = 0 .25

y 2 ,1 = y 2 ,0 + h y 2 (x 0 , y 1,0 , y 2 ,0 ) = 1 + 0 .25 (0,1,1) = 1 + 0 .25 (1 + 4 ) = 1 + 1 .25 = 0 .25

Step 3 Tabulate the results.

(Last page of Chapter 6)

You might also like