Chapter 8. Numerical Solution of Ordinary Differential Equations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Chapter 8.

Numerical Solution of Ordinary Differential


Equations

8. 1. Initial- and boundary-value problems

For an n-th order ordinary differential equation, n conditions are required to solve the
equation.

Example-1: Consider the vibration of a spring with a mass m


The governing equation is

d 2x
m 2 = − Kx
dt K
2
0
d x x
2
= −K m
dt m
x
with the initial off balance displacement:

x(t = 0) = x0
and initial speed:
dx(t = 0)
v(t = 0) = = v0
dt
The problem is called an initial-value problem (which is associated with time t ).

99
Example-2: Now consider the deflection of the sea boat mast
The governing equation is

f EI

d 2 y M ( z) f
2
= = ( L − z )2 L
dz EI 2 EI
z

y
f: wind force, EI: the bending stiffness
The conditions (boundary conditions) at the built-in end are:

y ( z = 0) = 0

dy
( z = 0) = 0
dz

The problem is called a boundary-value problem (which is associated with the


edge of the space domain).

100
8.2. First-order ordinary differential equations

Euler’s method
Consider a first-order equation
d
= f ( x,  ) (1)
dx
with
 ( x0 ) =  0

 (b) = ?
d
( x = a)
dx
 (a)

x

a b
x
Taylor series

d (a ) d 2 (a) x 2
b = a + x + +
dx dx 2
ignoring the second and higher order terms, we have
d (a)
b = a + x = a + f [a,  (a )]x
dx
We use the tangent at point a to approximate the function/curve.
Now we divide the solution domain into a number of small grid cells.

101
 3 4
1
2
numerical solution
0
true solution
x x x x
x0 x1 x2 x3 x4 x
0 1 2 3 4
x1 = x0 + x, 1 = 0 + f ( x0 , 0 )x

x2 = x1 + x, 2 = 1 + f ( x1 , 1 )x

x3 = x2 + x, 3 = 2 + f ( x2 , 2 )x

x j +1 = x j + x,  j +1 =  j + f ( x j ,  j )x (2)

This is called Euler’s method. The accuracy of the Euler’s method is first -order.

Example: to solve the following equation


d
= x +  = f ( x,  )  (0) = 0
dx ,
we choose x = 0.2

X=0 0.2 0.4 0.6 0.8


x
j=0 1 2 3 4

From equation (2) we have

d
 j +1 =  j + f ( x j ,  j )  x =  j + 0.2 (x = x j )
dx
=  j + ( x j +  j )  0.2 = 0.2 x j + 1.2 j

102
The exact solution is  ( x) = e x − x − 1

 j +1 = 0.2 x j + 1.2 j

j xj j  j +1 exact  j error

0 0 0 0 0 0

1 0.2 0 0.04 0.021 0.021

2 0.4 0.04 0.128 0.092 0.052

3 0.6 0.1280 0.274 0.222 0.094


j x analytical error

0 0 0 0 0
1 0.2 0 0.021 0.021
2 0.4 0.04 0.092 0.052
3 0.6 0.1280 0.222 0.094
4 0.8 0.2736 0.4255 0.1519
5 1.0 0.4883 0.7183 0.2300

103
Improved Euler’s Method

d
to solve = f ( x,  )
dx
This is a two-step method:

(i) to compute the ‘predicted’ value of  j +1 using an intermediate step

 j +1 =  j + f ( x j ,  j )x (3)


f ( x j +1 ,  j +1 )

1
[ f ( x j ,  j ) + f ( x j +1 ,  j +1 )]
2
f ( x j , j )

xj x j +1 x

(ii) To compute the ‘corrected’ value of  j +1 using

1
 j +1 =  j + [ f ( x j ,  j ) + f ( x j +1 ,  j +1 )]x (4)
2
It can be shown that the improved Euler’s method is a second-order method
(error  O ( x 2 ) ).

104
Example: to solve the same problem:
d
= x + with  (0) = 0
dx
using the improved Euler’s method

from eq. (3)  j +1 =  j + f ( x j , j )x =  j + ( x j +  j )  0.2 = 0.2 x j + 1.2 j

1
from eq. (4)  j +1 =  j + [ f ( x j , j ) + f ( x j +1 ,  j +1 )]x
2

1
=  j + [( x j +  j ) + ( x j +1 +  j +1 )]  0.2
2
1
=  j + [( x j +  j ) + ( x j + 0.2 + 0.2 x j + 1.2 j )]  0.2
2
= 0.02 + 0.22x j + 1.22 j

 j = 0.02 + 0.22 x j −1 + 1.22 j −1

j x j exact error

0 0 0 0 0
1 0.2 0.020 0.0214 0.0014
2 0.4 0.0884 0.0918 0.0034
3 0.6 0.2158 0.2221 0.0063
4 0.8 0.4153 0.4255 0.0102
5 1.0 0.7027 0.7183 0.0156

The errors are smaller than those derived from the original Euler’s
method.

105
8. 3. Linked (simultaneous) First-order Ordinary Differential Equations
We have studied methods for the numerical solution of equations of the form
dx
= f (t , x) with x(t = t0 ) = x0
dt
It is also possible to have a set of linked first-order equations
dx
= f (t , x, y ) , x(t = t0 ) = x0 (5, a)
dt

dy
= g (t , x, y ), y (t = t0 ) = y0 (5, b)
dt
For example,

dx
= x − y 2 + xt
dt
dy
= 2 x 2 + xy − t
dt

These two equations must be solved simultaneously, or side by side.


Euler’s method for the solution of equations (5.a) and (5.b) takes the form:

x j +1 = x j + tf (t , x j , y j ) (6. a)

y j +1 = y j + tg (t , x j , y j ) (6. b)

The improved Euler’s method then becomes:

xj +1 = x j + tf (t , x j , y j ) (7. a)

y j +1 = y j + tg (t , x j , y j ) (7. b)

t (8. a)
x j +1 = x j + [ f (t j , x j , y j ) + f (t j +1 , xj +1 , y j +1 )]
2
t (8. b)
y j +1 = y j + [ g (t j , x j , y j ) + g (t j +1 , xj +1 , y j +1 )]
2

106
Example: to find the value of x(1.4) which satisfies the following problem:
dx
= x − y 2 + xt , x(t = 1) = 0.5
dt

dy
= 2 x 2 + xy − t , y (t = 1) = 1.2
dt

Solution:

using Euler’s method with t = 0.1

j=0 1 2 3 4

0.1 0.1 0.1 0.1


t
t=1.0 1.1 1.2 1.3 1.4

From Eqs (6.a) and (6.b)

x j +1 = x j + tf (t j , x j , y j ) = x j + 0.1 ( x j − y 2j + x j t j )

y j +1 = y j + tg (t j , x j , y j ) = y + 0.1 (2 x 2 + x y − t )
j j j j j

For j=0 1 (i.e., t=1.0 1.1)

x1 = x0 + 0.1 ( x0 − y02 + x0t0 )

= 0.5 + 0.1 (0.5 − 1.2 2 + 0.5  1.0) = 0.4560

y1 = y0 + 0.1 (2 x02 + x0 y0 − t0 )

= 1.2 + 0.1 (2  0.52 + 0.5  1.2 − 1.0) = 1.210

107
For j=1 2 (i.e., t=1.1 to 1.2)

x2 = x1 + 0.1 ( x1 − y12 + x1t1 )

= 0.4560 + 0.1 (0.4560 − 1.212 + 0.4560 1.1) = 0.4054

y2 = y1 + 0.1 (2 x12 + x1 y1 − t1 )

= 1.21 + 0.1 (2  0.4560 2 + 0.4560  1.21 − 1.1) = 1.1968

……
x4 = 0.2980

Please complete the calculations !!


Also please use the improved Euler’s method to solve this problem !

The method can be used to solve a set of n simultaneous first-order


ordinary differential equations:

dy1
= f1 ( x, y1 , y2 ,, yn ) y1 ( x = x0 ) = y1,0
dx

dy2 y2 ( x = x0 ) = y2,0
= f 2 ( x, y1 , y2 ,, yn )
dx

…………… …………..

dyn
= f n ( x, y1 , y2 ,, yn ) yn ( x = x0 ) = yn,0
dx

108
8. 4. Numerical Solution of Higher-order Equations
consider
d 2x dx 1
2
+ x 2t − xt 2 = t 2 (9)
dt dt 2

dx
x(0) = 1.2 , (t = 0) = 0.8
dt
If we introduce an additional variable

dx d 2 x d dx dy
y= 2
= ( )=
dt dt dt dt dt

Then we have

dx
=y , x(0) = 1.2
dt

dy 1
+ x 2ty − xt 2 = t 2 , y (0) = 0.8
dt 2
Thus the original single second-order differential equation is replaced by a pair of
linked first-order differential equations. (one 2nd order equation = two 1st order
equations).

This process can be extended to higher-order equations.

(i.e. one nth-order equation = n 1st-order equations)

109
Example:
Find the value of x(0.2) satisfying the initial-value problem

d 3x d 2x dx
3
+ xt 2 + t − t 2 x = 0
dt dt dt
dx d 2x
x(t = 0) = 1 , (t = 0) = 0.5 (t = 0) = −0.2
dt , dt 2

using Euler’s method with step size t = 0.1

Solution
Since this is a third-order equation, we need to introduce two new variables:
dx
y=
dt
dy d 2 x
z= =
dt dt 2
Then the equation is transformed into:

dx
= y = f1 (t , x, y, z ) , x(0) = 1
dt
dy
= z = f 2 (t , x, y, z ) , y (0) = 0.5
dt
dz
= − xtz − ty + t 2 x = f 3 (t , x, y, z ), z (0) = −0.2
dt
Applying Euler’s method

x1 = x0 + tf1 (t0 , x0 , y0 , z0 ) = 1 + 0.1 0.5 = 1.05


y1 = y0 + tf 2 (t0 , x0 , y0 , z0 ) = 0.5 + 0.1 (−0.2) = 0.48
z1 = z0 + tf 3 (t0 , x0 , y0 , z0 ) = −0.2 + 0.1 (0) = −0.2

x2 = x(0.2) = x1 + tf1 (t1 , x1 , y1 , z1 ) = x1 + t  y1


= 1.05 + 0.1 0.48 = 1.098

110
8.5. Boundary-value Problems

The ‘shooting method’


Suppose we wish to solve

d 2x t  dy t
− (1 − ) x = t  dt = (1 − )x + t
2
 5
dt 5  dx
 =y
 dt

with x(1) = 2 , x(3) = −1

If dx / dt (t = 1) = x ' (1) is known, it is an initial value problem. The shooting


method is a form of trial and error in which the boundary-value problem is replaced
by an initial-value problem.

x
3
2
1
0 t
1 2 3
-1

111
Main steps

To assume an initial value of x ' (0) = dx / dt(t = t0 ) (slope)

To solve this initial value problem

To compare the solution at the other boundary

To adjust the value of x’ 0

To repeat the process until agreement is achieved


at the other boundary

112
Finite difference method

Consider

d 2x t
2
− (1 − ) x = t (1)
dt 5

with x(1) = 2 , x(3) = −1

Main steps:

(1) To divide the physical region

t =1 t =3
t t
t
j=0 1 2 j −1 j j +1 N

(2) To replace the derivative terms by finite differences

d 2x d dx = ( x j +1 − x j − x j − x j −1 ) / t x j −1 − 2 x j + x j +1
= ( ) = (2)
dt 2
dt dt t t t 2

Substituting (2) into (1) we get

x j −1 − 2 x j + x j +1 tj (3)
− (1 − )x j = t j
t 2 5
or
'
x j −1 − [2 + t 2 (1 − t j / 5)]x j + x j +1 = t j t 2 (3 )

If we set t = 0.5 , we have 3 internal points

113
1.5 2.0 2.5 3.0
t =1

j=0 1 2 3 4

From eq. (3) x j −1 − [2 + t 2 (1 − t j / 5)]x j + x j +1 = t j t 2 we have

at j=1, t=1.5

x0 − [2 + 0.52 (1 − 1.5 / 5)]x1 + x2 = 1.5  0.52

j=2, t=2.0

x1 − [2 + 0.52 (1 − 2.0 / 5)]x2 + x3 = 2.0  0.52

j=3, t=2.5

x2 − [2 + 0.52 (1 − 2.5 / 5)]x3 + x4 = 2.5  0.52

Rewriting the above equations

− 2.175 x1 + x2 = 0.375 − x0

x1 − 2.15 x2 + x3 = 0.5

x2 − 2.125 x3 = 0.625 − x 4

Applying the boundary conditions:

x0 = 2 and x4 = −1

114
− 2.175 x1 + x2 = −1.625 (a)

x1 − 2.15 x2 + x3 = 0.5 (b)

(c)
x2 − 2.125 x3 = 1.625

Elimination: (a) + (b)  2.175

0 + (1 − 4.676) x2 + 2.175 x3 = −1.625 + 1.0875

− 3.676 x2 + 2.175 x3 = −0.5375


(b)’

(b) , + (c)  3.676

0 + (2.175 − 7.8115) x3 = −0.5375 + 5.9735

− 5.6365 x3 = 5.436 (c)’

Now, we have:

− 2.175 x1 + x2 = −1.625 (a)

− 3.676 x2 + 2.175 x3 = −0.5375 (b)’

− 5.6365 x3 = 5.436 (c)’

115
Backward substitution:

From (c)’ x3 = 5.436 /( −5.6365) = −0.964

From (b)’ − 3.676 x2 + 2.175  (−0.964) = −0.5375

x2 = −0.424

From (a) − 2.175 x1 + (−0.424) = −1.625

x1 = 0.552

116

You might also like