Numerical Analysis 3rd Stage

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

University of Basrah - College of Engineering

Materials Engineering Department




  
EmadOAl-Fahad




Syllabus of Numerical Analysis

1. Roots of Equations
2. Solution of Linear Simultaneous Equations
3. Finite Difference, Interpolation and Extrapolation
4. Numerical Integration and Differentiation
5. Curve Fitting
6. Numerical Solution of Ordinary Differential Equations
7. Numerical Solution of Ordinary Partial Equations
8. Introduction to Finite Element Method.

1
1. Roots of Equations
Years ago, you learned to use the quadratic formula

−b ± b 2 − 4ac
x = (1)
2a
To solve

f ( x )= ax 2 + bx + c= 0 (2)
The values calculated with eq.(2) are called the "roots" of eq.(1). They represent the
vakues of x that make eq.(2) equal to zero.

Root of equation: The value of x that makes f(x)=0. The roots are sometimes
are called zeros of equation.
There are many other functions for which the root cannot be determined so easily
such as f(x) = e-x - x. For these cases the numerical methods efficient means to obtain
answer.

Before the advent of digital computers, there were several ways to solve for roots of
algebraic and transcendental equations. Such as Graphical technique method is to
plot the function and determine where it crosses the x-axis. This method is useful for
obtaining rough estimated of roots, this is limited because of their lack precision.

The other method is Trial and error. This method consists of guessing a value of x
and evaluating whether f(x) is zero. If not, another guess is made, and f(x) is again
evaluated to determine whether the new value provides a better estimate of the root.

Note: transcendental function is one that is nonalgebraic. These include


trigonometric, exponential, logarithmic, and other, less familiar, functions. Ex.

f (x ) =
ln x 2 − 1, or f (x ) =
e x + sin(3x )

1.1. The Bisection Method: (Bracketing methods)

One of the first numerical methods developed to find the root of a nonlinear equation
f(x)=0 was the bisection method (also called binary-search method).

In general, an equation f(x)=0, where f(x) is a real continuous function, has at least
one root between xl and xu if f(xl)f(xu) < 0.

2
Algorithm for the bisection method

The steps to apply the bisection method to find the root of the equation f(x) = 0 are

1. Choose xl and xu as two guesses for the root such that f(xl)f(xu) < 0 , or in other

words, f(x) changes sign between xl and xu.

2. Estimate the root, xm, is determined by :

x l + xu
xm =
2
3. Now check the following:

a) If f(xl)f(xm) < 0, then the root lies between xl and xm; then xl = xl and xu = xm.
b) If f(xl)f(xm) > 0, then the root lies between xm and xu; then xl = xm and xu = xu.
c) If f(xl)f(xu) = 0 ; then the root is xm. Stop the algorithm if this is true.
4. Find the new estimate of the root

x l + xu
xm =
2
Find the absolute relative approximate error as

x mnew − x mold
=∈a × 100
x mnew

where

x mnew = estimated root from present iteration

x mold = estimated root from previous iteration

5. Compare the absolute relative approximate error ∈a with the pre-specified


relative error tolerance ∈s . If ∈a >∈s , then go to Step 3, else stop the algorithm.

3
Ex.1

The floating ball has a specific gravity of 0.6 and has a radius of 5.5 cm. You are
asked to find the depth to which the ball is submerged when floating in water. The
equation that gives the depth x to which the ball is submerged under water is given
by:

x 3 − 0.165x 2 + 3.993 × 10−4 =


0

Use the bisection method of finding roots of equations to find the depth x to which
the ball is submerged under water. Find the absolute relative approximate error at the
end of each iteration.

Sol.

From the physics of the problem, the ball would be submerged between x = 0 and
x = 2R, where, R = radius of the ball

that is

0 ≤ x ≤ 2R
0 ≤ x ≤ 2(0.055)
0 ≤ x ≤ 0.11

Assume xl = 0 , xu = 0.11

Check if the function changes sign between xl and xu

f ( x l ) = f ( 0 ) = ( 0 ) − 0.165 ( 0 ) + 3.993 × 10−4 = 3.993 × 10−4


3 2

f (x u ) =
f ( 0.11) =
( 0.11) − 0.165 ( 0.11) + 3.993 × 10−4 =
−2.662 × 10−4
3 2

Hence
f ( x l ) f ( x u ) = f ( 0 ) f ( 0.11) = ( 3.99 × 10−4 )( −2.662 × 10−4 ) < 0

So there is at least one root between xl and xu, that is between 0 and 0.11.

Iteration 0

The estimate of the root is

4
x l + x u 0 + 0.11
=xm = = 0.055
2 2
f ( x m ) = f ( 0.055) = ( 0.055)3 − 0.165 ( 0.055)2 + 3.993 × 10−4 = 6.655 × 10−5
f (x l )f (x m ) = f ( 0 ) f ( 0.055) = ( 3.993 × 10−4 )( 6.655 × 10−5 ) > 0

Hence the root is bracketed between xm and xu. So, The new bracket is

xl = 0.055 , xu = 0.11

At this point the absolute relative approximate error cannot be calculated.

Iteration 1

The estimate of the root is

x l + x u 0.055 + 0.11
=xm = = 0.0825
2 2
f (x m ) =
f ( 0.0825) =
( 0.0825) − 0.165 ( 0.0825) + 3.993 × 10−4 =
−1.622 × 10−4
3 2

f ( x l ) f ( x m ) = f ( 0.055) f ( 0.0825) = ( 6.655 × 10 )( −1.622 × 10 ) < 0


−4 −4

Hence the root is bracketed between xl and xm. So, The new bracket is

xl = 0.055 , xu = 0.0825

The absolute relative approximate error ∈a at the end of iteration 1 is:

x mnew − x mold 0.0825 − 0.055


=∈a new
=× 100 = × 100 33.33%
xm 0.0825

Because the absolute relative approximate error is greater than 5%.

5
Iteration 2

The estimate of the root is

x l + x u 0.055 + 0.0825
=xm = = 0.06875
2 2
f (x m ) =
f ( 0.06875) =
( 0.06875) − 0.165 ( 0.06875) + 3.993 × 10−4 =
−5.563 × 10−5
3 2

f ( x l ) f ( x m ) = f ( 0.055) f ( 0.0825) = ( 6.655 × 10 )( −5.563 × 10 ) < 0


−5 −5

Hence the root is bracketed between xl and xm. So, The new bracket is

xl = 0.055 , xu = 0.06875

The absolute relative approximate error ∈a at the end of iteration 2 is:

x mnew − x mold 0.06875 − 0.0825


=∈a new
=× 100 = × 100 20%
xm 0.06875

Because the absolute relative approximate error is greater than 5%.

Table 1. Root of f(x) as function of number of iteration for bisection method.

N xl xu xm f(xl) f(xm) f(xl) f(xm) New Interval ∈a


(xl - xu) %

0 0 0.11 0.055 3.993×10-4 6.655×10-5 >0 0.055 - 0.11 -

1 0.055 0.11 0.0825 6.655×10-5 -1.622×10-4 <0 0.055 - 0.0825 33.3

2 0.055 0.0825 0.06875 6.655×10-5 -5.563×10-5 <0 0.055 - 0.06875 20

3 0.055 0.06875 0.06188 6.655×10-5 4.484×10-6 >0 0.0618 - 0.6875 11.11

4 0.06188 0.06875 0.06531 4.484×10-6 -2.593×10-5 <0 0.0618 - 0.0653 5.26

5 0.06188 0.06531 0.06359 4.484×10-6 -1.0804×10-5 <0 0.0618 - 0.0635 2.70

6 0.06188 0.06359 0.06273 4.484×10-6 -3.176×10-6 <0 0.0618 - 0.0627 1.37

7 0.06188 0.06273 0.0623 4.484×10-6 6.497×10-7 >0 0.0623 - 0.0627 0.69

8 0.0623 0.06273 0.06252 6.497×10-7 -1.265×10-6 <0 0.0623 - 0.0625 0.343

9 0.0623 0.06252 0.06241 6.497×10-7 -3.0768×10-7 <0 0.0623 - 0.0624 0.172

6
1.2. Iteration Method (Fixed point method) (open method)

We put x=g(x), assume initial value for xo then calculating next value as follows

x n +1 = g ( x n )

and continue in this iteration. The absolute relative approximate error as

x new − x old x i +1 − x i
=∈a =× 100 × 100
x new x i +1

Ex1. Solve the equation f ( x )= x 2 − 3x + 1= 0 using iteration method.

Since we know the solution =


x 1.5 ± 1.25 thus 2.618 and 0.3819

Sol. The equation f ( x )= x 2 − 3x + 1= 0 may be written

x = g1 ( x ) =
3
( x + 1) ,
1 2
⇒ x n +1 =
3
( x n + 1)
1 2

If Choose x0 = 1
If Choose x0 = 3
g1 ( x n )
∈a
N xn
g1 ( x n )
N xn ∈a
0 1 0.667 50%
0 3 3.33 10%
1 0.667 0.481 39%
1 3.33 4.037 17.5%
2 0.481 0.411 17%
2 4.037 5.766 30%
3 0.411 0.39 5.3%
3 5.766 11.415 50%
4 0.39 0.38 2.6%
4 11.415 Divergence
Convergence

** To Find other roots may be rewritten above equation (divided by x)


1
f ( x )= x 2 − 3x + 1= 0 ⇒ x − 3 + = 0
x
1 1
g2 (x ) =
x = 3− , ⇒ x n +1 =
3−
x xn

7
If Choose x0 = 1 If Choose x0 = 3

g1 ( x n ) g1 ( x n )
N xn ∈a N xn ∈a

0 1 2 50% 0 3 2.667 12.5%


1 2 2.5 20% 1 2.667 2.625 1.6%
2 2.5 2.6 4% 2 2.625 2.619 0.22%
3 2.6 2.615 0.57% 3 2.619 2.618 0.04%
4 2.615 2.617 0.07% 4 2.618 2.618 0%
Convergence Convergence

y=x

x0 = 3

x0 = 1

* Convergence of fixed point method.

Then if g ′ ( x ) < 1 The iteration process converge for any x0

For above example

1 2 2
g 1 ( x )= ( x + 1) ⇒ g ′ ( x )= x
3 3
2 3
∴ g ′( x ) < 1 ⇒ x < 1 → x < to converges
3 2

8
1.3. Newton - Raphson Method

The Newton method is another iteration method for solving equation f(x) = 0, where
f(x) is assumed to have a continuous derivative f(x). The method commonly used
because of its simplicity and great speed.

f (x 0 )
′( x )
tan β f =
=
x 0 − x1
f (x 0 )
x= x0 −
f ′( x 0 )
1

f (x n )
x n= xn −
f ′( x n )
+1

Ex1. Solve the equation f ( x )= x 2 − 3x + 1= 0 using Newton Raphson method.

f ( x ) = x 2 − 3x + 1 ⇒ f ′ ( x ) = 2x − 3
f (x n ) x n2 − 3x n + 1 2x n2 − 3x n − x n2 + 3x n − 1
∴ x n +1 =x n − =x n − =
f ′( x n ) 2x n − 3 2x n − 3

x n2 − 1
∴ x n +1 =
2x n − 3
If Choose x0 = 1 If Choose x0 = 3

N xn xn+1 ∈a N xn xn+1 ∈a
0 1 0 - 0 3 2.667 12.5%
1 0 0.333 100% 1 2.667 2.619 1.8%
2 0.333 0.381 12% 2 2.619 2.618 0.04%
3 0.381 0.381 0% 3 2.618 2.618 0%
Convergence Convergence

H.W. Write a computer program to solve the following equation using the above
three methods: Take xl = 0 and xu = 3 For Bisection method.
x
e −x − =
0 where N is your number
N
9
2. Solution of linear simulation equation

a11x 1 + a12 x 2 + a13x 3 +  + a1n x n =


b1 (1)
a21x 1 + a22 x 2 + a23x 3 +  + a2 n x n =
b2 (2)
a31x 1 + a32 x 2 + a33x 3 +  + a3n x n =
b3 (3)
     
am 1x 1 + am 2 x 2 + am 3x 3 +  + amn x n =
bm (m )

Where:
m = No. of equations
n = No. of variables (unknowns)

i. If m=n, there is one exact solution.


ii. If m<n, there is infinite number of solutions.
iii. If m>n, there may be an approximate solution.
For n = m only

2.1 Gauss-Seidel Iteration Method


If take n =3 No. of equation
b1 − a12 x 2 − a13x 3
a11x 1 + a12 x 2 + a=
13x 3 b1 ⇒ =
x1
a11
b2 − a21x 1 − a23x 3
a21x 1 + a22 x 2 + a=
23x 3 b2 ⇒ =
x2
a22
b3 − a31x 1 − a32 x 2
a31x 1 + a32 x 2 + a33
= x 3 b3 ⇒ =
x3
a33
Algorithm for the iteration method

 n

b − a x  a .
 i ∑
1. Put x=
i
j =1
ij j
 ii
 j ≠i 
2. Assume values for xi , i = 2, 3, ... , n
3. Substitute in the equation to find new set of xi and continue till we get convergence

10
x ik +1 − x ik <∈ where ∈ rate of convergence (very small)

Note: To get convergence, the following condition is preferable


n
aii ≥ ∑ aij
j =1
j ≠i

Ex. Solve the set of equations

x 1 + 4x 2 − x 3 =
6  (1)
2x 1 − x 2 + x 3 =
3  (2)
− x 1 + 2x 2 + 4x 3 =
15  (3)

Swap eqs. (1) and (2)

2x 1 − x 2 + x 3 =
3  (1)
x 1 + 4x 2 − x 3 =
6  (2)
− x 1 + 2x 2 + 4x 3 =
15  (3)

n 0 1 2 - - -
Eqs.
3+ x 2 − x3
x1 = - 1.5 0.25 ........ 1 1
2
6 − x1 + x 3
x2 = 0 1.125 2.34 ........ 2 2
4
15 + x 1 − 2x 2
x3 = 0 3.625 2.64 ........ 3 3
4

Continue until the last two column are identical.

*Convergence at Max x ik +1 − x ik ≤ ∈

Max error ≤ ∈ Stop iteration

H.W. Write computer program to solve a set of equation by using Gauss-Seidel


Iteration.

11
2.2 Gauss Elimination Method
Forward Elimination and Back substitution

For n = m = 3 .The equations in matrix form as:

a11 a12 a13   x 1  b1 


a a a23   x 2  = b2 
 21 22    
a31 a32 a33   x 3  b3 

Ax = b A: Coefficient matrix

Step (1) Eliminate coefficient of x1 from equ. (2) & (3)

−a21
multiply equ.(1) by and add to equ. (2)
a11
−a31
multiply equ.(1) by and add to equ. (3)
a11

a11 a12 a13   x 1  b1 


 0 a ′ a ′   x  = b ′ 
23  2   2
 22

′ a33
 0 a32 ′   x 3  b3′ 

Step (2) Eliminate coefficient of x2 from equ. (3)


−a32
multiply equ.(2) by and add to equ. (3)

a22

a11 a12 a13   x 1  b1 


 0 a ′′ a ′′   x  = b ′′
23  2   2
 22

 0 0 a33′   x 3  b3′′

Step (3) Back substitution

b3′′
x3 =
′′
a33
b ′ − a′ x
x 2 = 2 22 3

a22
b1 − a12 x 2 − a13x 3
x1 =
a11

12
Ex. Solve the system by using Gauss elimination method.

8x 2 + 2 x 3 =
−7  (1)
3x 1 + 5x 2 + 2x 3 =
8  (2)
6 x 1 + 2 x 2 + 8x 3 =
26  (3)

Note.: Pivoting, select the equation with maximum x1 coefficient to eliminate the
coefficient in the other equations. (swap eqs. (1), (2) & (3))

6 x 1 + 2 x 2 + 8x 3 =
26  (1)
3x 1 + 5x 2 + 2x 3 =
8  (2)
8x 2 + 2 x 3 =
−7  (3)

−3
Step (1) Eliminate x1 from eq.(2), by multiply * (1) + eq .(2)
6
6 2 8   x 1   26 
 3 5 2   x  = 8 
  2  
0 8 2   x 3   −7 

Can be rewriting the Augmented matrix A

-3/6 *(1) 6 2 8 26 
=A =
A b  3 5 2 8  Pivot (6)

0 8 2 − 7 

6 2 8 26 
 0 4 −2 − 5 Swap (2) & (3) Because 8 > 4

0 8 2 − 7 

−4
Step (2) Elimination x2 from eq.(3) by multiply * (2) + eq .(3)
8
6 2 8 26  6 2 8 26 
Pivot (8)
0 8 2 − 7  ⇒ 0 8 2 − 7 
-4/8 *(2) 
0 4 −2 − 5  0 0 −3 − 3 / 2 

Rewriting in algebraic form

13
6 x 1 + 2 x 2 + 8x 3 =
26  (1)
8x 2 + 2 x 3 =
−7  (2)
− 3x 3 =
−3 / 2  (3)

Step (3) Back substitution,

1
x3 =
2
1
x 2 = ( −7 − 2x 3 ) =−1
8
1
x 1 = ( 26 − 2x 2 − 8x 3 ) = 4
6
Note: From smaller coefficient of equation can be multiply with factor and after the
solution divided it's on the same factor.

1000 * ( 0.001x 1 + 0.02x 2 + 0.0018x=


3 10−3 ) ⇒ x 1 + 20x 2 + 1.8x=
3 1

2.3 Gauss - Jordan Elimination Method


"Forward & Backward elimination"

Note: Pivoting, selecting the equation with maximum coefficient to eliminate the
coefficient in the other equation.

Step (1) Forward elimination the same as in Gauss elimination method.

Step (3) Backward elimination: eliminate all elements above the main diagonal.

Ex. Solve the set of equation using Gauss-Jordan Method

2x − y + z =3  (1)
x + 4y − z =
6  (2)
− x + 2 y + 4z =
15  (3)

Sol.

-1/2 *eq(1)  2 −1 1 3   2 −1 1 3 
1 4 −1 6  ⇒  0 4.5 − 1.5 4.5 
 
 −1 2 15  0 1.5 16.5
-1/2 *(1)+eq(3) 4 -1.5/4.5 *(2)+eq(3) 4.5

14
 2 −1 1 3 
 0 4.5 − 1.5 4.5

 0 0 5 15 

*Back elimination:

Step (3) eliminate x3 from eq.(1) & eq.(2)

−1
multiply eq.(3) by and add to eq. (1)
5
−1.5
multiply eq.(3) by and add to eq. (2)
5
-1/5 *(3)+eq(1)  2 −1 1 3   2 −1 0 0 
 0 4.5 − 1.5 4.5 ⇒  0 4.5 0 9 
-1.5/5 *(3)+eq(2) 
 0 0 5 15   0 0 5 15

1
Step (4) eliminate x2 from eq.(1) by multiply eq. (2) by and add to eq. (1)
4.5
1/4.5 *eq(2)+eq(1)  2 −1 0 0  2 0 0 2 
 0 4.5 0 9  ⇒  0 4.5 0 9 
 
 0 0 5 15  0 0 5 15

Step (5)
2 9 15
x= = 1 , y= = 2 , z= = 3
2 4.5 5

H.W. Write a computer program to solve N-simultaneous linear equation using Gauss
elimination and Gauss-Jordan method.

15
3. Finite Difference, Interpolation and Extrapolation

Finite Differences

Forward difference Backward difference Central difference

1. Forward differences:
f(x)

f2

f1
∆f 0 = f 1 − f 0
f0
∆f 1 = f 2 − f 1 h
 
x0 x1 x2 x
∆f n = f n +1 − f n "1st Forward difference"

∆ 2f 0 =
∆ ( ∆f 0 ) = (f 2 − f 1 ) − (f 1 − f 0 )
∆f 1 − ∆f 0 =
=f 2 − 2f 1 + f 0

∆ 2f n = f n + 2 − 2f n +1 + f n "2nd Forward difference"

∆ 3f n = ∆ ( ∆ 2f n ) = ∆f n + 2 − 2∆f n +1 + ∆f n
= ( f n + 3 − f n + 2 ) − 2 ( f n + 2 − f n +1 ) + ( f n +1 − f n )
∴∆ 3=
f n f n + 3 − 3f n + 2 + 3f n +1 − f n

Or ∆ 3f n = ∆ 2f n +1 − ∆ 2f n "3rd Forward difference"

∆ 4f n = ∆ 3f n +1 − ∆ 3f n

∆ k f n = ∆ k −1f n +1 − ∆ k −1f n (k = 1, 2, 3, .... )

Ex. Construct the forward difference table for the following data;

16
x 0 0.5 1 1.5 2.0
f(x) 1.2 1.8 3.1 4.6 7.6

n xn fn ∆f n ∆ 2f n ∆ 3f n ∆ 4f n
0 0 1.2 0.6 0.7 -0.5 1.8
1 0.5 1.8 1.3 0.2 1.3 -
2 1.0 3.1 1.5 1.5 - -
3 1.5 4.6 3.0 - - -
4 2.0 7.6 - - - -

2. Backward differences: "∇"

∇f 1 = f 1 − f 0

∇f n = f n − f n −1 " 1st Backward difference"

∇2f n = ∇f n − ∇f n −1 " 2nd Backward difference"

=( f n − f n −1 ) − ( f n −1 − f n − 2 )
=
f n − 2f n −1 + f n − 2

∇3f n = ∇2f n − ∇2f n −1 " 3rd Backward difference"

∇ k f n = ∇ k −1f n − ∇ k −1f n −1

Ex. Construct the backward table for the last data.

n xn fn ∇f n ∇2f n ∇3f n ∇4f n


0 0 1.2 - - - -
1 0.5 1.8 0.6 - - -
2 1.0 3.1 1.3 0.7 - -
3 1.5 4.6 1.5 0.2 -0.5 -
4 2.0 7.6 3.0 1.5 1.3 1.8

17
3. Central differences: "δ "
f2 −f0
δf1 = f(x)
2
Or f2

1 f1
δ f n= ( ∆ f n + ∇f n ) "1st Central"
f0
2
h h
= {( f n +1 − f n ) − ( f n − f n −1 )}
1
2 x0 x1 x2 x
1
∴δ f =
n ( f n +1 + f n −1 )
2
1
(δ f n )
δ 2f n δ=
= (δ f n +1 − δ f n −1 )
2
(δ f n ) (δ 2f n +1 − δ 2f n −1 )
1
δ 3f n δ=
= 2

δkf n
=
2
( δ f n +1 − δ k −1f n −1 )
1 k −1

H.W. Construct the central difference table for the data in previous example.

Interpolation and Extrapolation

* Linear interpolation
f(x)
f1 −f 0 f −f 0
=
x1 − x 0 x − x 0 f1

(f − f 0 ) h =( x − x 0 ) ∆f 0
f
x −x0
f =
f0 + ∆f 0
h
f0
x −x0
r=
h x0 x x1 x

f = f 0 + r ∆f 0 "linear" h = x1 - x0
= xn+1 - xn "step"

18
** Quadratic interpolation

r ( r − 1) 2
f = f 0 + r ∆f 0 + ∆ f0
2

***Newton Forward interpolation formula

r ( r − 1) 2 r ( r − 1)( r − 2 ) 3 r ( r − 1)( r − 2 )( r − 3) 4
f = f 0 + r ∆f 0 + ∆ f0 + ∆ f0 + ∆ f +
2! 3! 4!

***Newton Backward interpolation formula

r ( r − 1) 2 r ( r + 1)( r + 2 ) 3 r ( r + 1)( r + 2 )( r + 3) 4
f = f 0 + r ∇f 0 + ∇ f0 + ∇ f0 + ∇ f +
2! 3! 4!

Ex. For the same data in last example, find f( - 0.5), f(1.25), f(2.75) using linear and
quadratic and 4-term Newton formula.
x 0 0.5 1 1.5 2.0
f(x) 1.2 1.8 3.1 4.6 7.6
Sol.
f( - 0.5) = ? extrapolation
x −x0
linear : f = f 0 + r ∆f 0 r=
h
−0.5
x0 = 0 , h = 0.5 "step" h = xn+1 - xn r= = −1
0.5
f ( −0.5)= 1.2 + ( −1)( 0.6 )= 0.6

r ( r − 1) 2
Quadratic : f = f 0 + r ∆f 0 + ∆ f0
2
f ( −0.5)= 1.2 + ( −1)( 0.6 ) +
( −1)( −2 ) * 0.7= 1.3
2
4-term Newton Forward formula

r ( r − 1) 2 r ( r − 1)( r − 2 ) 3
f = f 0 + r ∆f 0 + ∆ f0 + ∆ f0
2! 3!

f ( −0.5) =1.2 + ( −1)( 0.6 ) +


( −1)( −2 ) ( −1)( −2 )( −3)
( 0.7 ) + ( −0.5) =1.8
2 6
19
f( 1.25) = ? Let x0 = 1.0 Note: Assume the x0 nearest to the desired value
x − x 0 1.25 − 1
linear : =r = = 0.5
h 0.5
f (1.25) = f 0 + r ∆f 0 = 3.1 + 0.5*1.5 = 3.85
Quadratic :
r ( r − 1) 2 0.5* ( −0.5)
f (1.25) = f 0 + r ∆f 0 + ∆ f 0 = 3.1 + 0.5*1.5 + *1.5 = 3.66
2 2

4-term Newton formula Let x0 = 0.5 *Because x0=1 not found ∆3f0
x − x 0 1.25 − 0.5
=r = = 1.5
h 0.5
r ( r − 1) 2 r ( r − 1)( r − 2 ) 3
f = f 0 + r ∆f 0 + ∆ f0 + ∆ f0
2! 3!
1.5* 0.5 1.5* 0.5* ( −0.5)
f (1.25) =
1.8 + 1.5*1.3 + * 0.2 + *1.3 =
3.74
2 6

f( 2.7) = ? Let x0 = 2.0 Note: Using Backward Table because containing all terms
x − x 0 2.75 − 2
=r = = 1.5
h 0.5
linear : f ( 2.75) = f 0 + r ∇f 0 = 7.6 + 1.5* 3.0 = 12.1
Quadratic :
r ( r + 1) 2 1.5* 2.5
f ( 2.75) = f 0 + r ∇f 0 + ∇ f 0 = 7.6 + 1.5* 3.0 + *1.5 = 14.91
2 2
4-term Newton formula

r ( r + 1) 2 r ( r + 1)( r + 2 ) 3
f = f 0 + r ∇f 0 + ∇ f0 + ∇ f0
2! 3!
1.5* 2.5 1.5* 2.5* 3.5
f ( 2.75) =
7.6 + 1.5* 3.0 + *1.5 + *1.3 =
17.75
2 6
Note: Newton interpolation formula can be used only when the point are equally
spaced ( h = constant).
H.W. Write a computer program to interpolate f(x) using Newton forward and
backward formula using n-term.

20
Lagrange Interpolation formula
This method has less accuracy them Newton formula, but it has advantage of it
can be used for equally or non-equally spaced data.
n
f (x ) = ∑ N k ( x )f k
k =0

n (x − x ) n (x − x )
= ∏ x −x
= ∏ x −x
j j
N k (x )
( k j) j 0( k j)
=j 0=
j ≠k j ≠k

where, n: number of terms - 1


Ex. if take n = 2
2
f (x ) = ∑ N k ( x )f k = N 0 ( x )f 0 + N 1 ( x )f 1 + N 2 ( x )f 2
k =0

2 (x − x j ) (x − x 1) . (x − x 2 )
=N0 ∏ x −x
=
k =0 j =0 ( k j ) (x 0 − x 1) (x 0 − x 2 )
j ≠k

2 (x − x j ) (x − x 0 ) . (x − x 2 )
=N1 ∏=
k =1 j =0 ( x k − x j ) (x 1 − x 0 ) (x 1 − x 2 )
j ≠k

2 (x − x )
(x − x 0 ) . (x − x 1)
= ∏ x −x
=
j
N2
k =2 j =0 ( k j ) (x 2 − x 0 ) (x 2 − x 1 )
j ≠k

∴f ( x )
=
( x − x 1 ) . ( x − x 2 ) .f + ( x − x 0 ) . ( x − x 2 ) .f + ( x − x 0 ) . ( x − x 1 ) .f
(x 0 − x 1 ) (x 0 − x 2 ) 0 (x 1 − x 0 ) (x 1 − x 2 ) 1 (x 2 − x 0 ) (x 2 − x 1 ) 2

Ex. For the last example, find f(1.25) using 3-term Lagrange interpolation formula.
x0 x1 x2
x 0 0.5 1 1.5 2.0
x = 1.25
f(x) 1.2 1.8 3.1 4.6 7.6
n = No. of terms - 1 = 3 - 1 = 2 f0 f1 f2
2
f (x ) = ∑ N k ( x )f k = N 0 ( x )f 0 + N 1 ( x )f 1 + N 2 ( x )f 2
k =0

21
N0 =
( x − x 1 ) . ( x − x 2 ) = (1.25 − 1) . (1.25 − 1.5) = −0.125
k =0 ( x 0 − x 1 ) ( x 0 − x 2 ) ( 0.5 − 1) ( 0.5 − 1.5)
N1
(=x − x 0 ) ( x − x 2 ) (1.25 − 0.5) (1.25 − 1.5)
. = . 0.75
k =1 ( x 1 − x 0 ) ( x 1 − x 2 ) (1 − 0.5) (1 − 1.5)
N2
(=x − x 0 ) ( x − x 1 ) (1.25 − 0.5) (1.25 − 1)
. = . 0.375
k =2 ( x 2 − x 0 ) ( x 2 − x 1 ) (1.5 − 0.5) (1.5 − 1)
∴ f (1.25) =
−0.125*1.8 + 0.75* 3.1 + 0.375* 4.6 =
3.825

N.B. Using Lagrange interpolation up to term. (Max n=No. of point - 1)

H.W. Write a general computer program to read points (xn, fn) then construct the
difference table up ∆n-1f, then estimate y(x) for any x using 4-term Newton and
Lagrange interpolation.

4. Numerical Integration and Differentiation

4.1 Integration: f(x) fb

1. Trapezoidal Rule f3
b
f2
f1
∫f ( x )dx f0
a

n: No. of divisions (strips) A1 A2 A3 ...


b −a
∆x = a=x0 x1 x2 x3 xn-1=b
n ∆x x
b n
Total area = ∫ f ( x )dx = ∑ A i x1 = x0 + ∆x
i =1
a
= a + ∆x
f 0 +f1 f +f f xn 2−1=+xf1n+ ∆x
A1 = . ∆x , A 2 = 1 3 . ∆ x , A n = ... . ∆x
2 2 2

22
f f 
b

∫f ( x )dx =∆x  a + f 1 + f 2 + f 3 +  + b 
2 2
a

2. Simpson Rule
f(x) fb
Note: No. of division (strips) should be even
f4
f3
∆x f2
A=
1 ( f 0 + 4f 1 + f 2 ) f1
3 f0
∆x
A=2 ( f 2 + 4f 3 + f 4 ) A1
A2
3

∆x a=x0 x1 x2 x3 x4 xn-1=b x
A=n ( f n −3 + 4f n −2 + f n −1 ) ∆x
3
b
∆x
∫f ( x )dx= [f 0 + 4f 1 + 2f 2 + 4f 3 + 2f 4 +  + 2f n −3 + 4f n −2 + f n −1 ]
a 3
f 0= f a , f n= f b , x =
1 x 0 + ∆x , x 2= x 1 + ∆x 

2
Ex. Evaluate I = ∫ e − x dx using trapezoidal & Simpson methods. take h = 0.5.
2

b −a 2−0
Sol. No. of division = = = 4
h 0.5

f n = e −x n
2
n xn

0 0 1
1 0.5 0.778 Trapezoidal Simpson
value Value
2 1.0 0.367
3 1.5 0.105
4 2.0 0.018
0.8795 0.88

f f 
(1) Trapezoidal I t =∆x  0 + f 1 + f 2 + f 3 + 4 
2 2

23
1 0.018 
= 0.5  + 0.778 + 0.367 + 0.105 + = 0.8795
2 2 
∆x
(2) Simpson I=
s [ f 0 + 4f 1 + 2f 2 + 4f 3 + f 4 ]
3
0.5
= [1 + 4 * 0.778 + 2 * 0.367 + 4 * 0.105 + 0.018] =0.88
3

H.W Write computer program to evaluate the following internal using trapezoidal,
Simpson methods; Take any number of division

1
I = ∫x
2
sin 2 x 2dx
2π 0

4.2 Numerical Differentaition:


∆f ∆f
f ′ (=
x) =
∆x h
1 f −f
f ′ ( x 0 ) = ∆f 0 = 1 0
h h
1 2 1 3
f ′′ ( x 0 ) = 2
∆ f 0 ,  f ′′′ ( x 0 ) = ∆ f0
h h3

Using Newton interpolation formula


r ( r − 1) 2 r ( r − 1)( r − 2 ) 3
f ( x ) = f 0 + r ∆f 0 + ∆ f0 + ∆ f0 +
2! 3!
df df dr x −x0 dr 1
f ′( x ) = = . ,r = ,∴ =
dx dr dx h dx h
df 1 d  r ( r − 1) 2 r ( r − 1)( r − 2 ) 3 
= . f 0 + r ∆f 0 + 2! ∆ f 0 + ∆ f 0 + 
dx h dr  3! 
1 2r − 1 2 3r 2 − 6r + 2 3 
∴ f ′( x ) = ∆f + ∆ f + ∆ f + 
h  
0 0 0
2! 3! 

** If Using "Three-point formula" find f 0′, f 1′, and f 2′ .

1 2r − 1 2 
 f ′( x ) = ∆f + ∆ f 0 "Three points formula"
h 
0
2! 

24
(i) At x = x0 r=0
1 1 2  1
f ′( x 0 ) = f 0′ = ∆ f − ∆ f 0 ⇒ ∴ f 0′ = [ −3f 0 + 4f 1 − f 2 ]
h 
0
2  2h
x1 − x 0
(ii) At x = x=
1 , r = 1
h
1 1 2  1
f ′( x 1 ) = f 1′ = ∆ f + ∆ f 0 ⇒ ∴ f 1′ = [f 2 − f 0 ]
h 
0
2  2h
x 2 − x 0 2h
(iii) At x = x2 =
r = = 2
h h
1 3 2  1
f 2′= ∆ f + ∆ f 0 ⇒ ∴ f 2′= [f 0 − 4f 1 + 3f 2 ]
h 
0
2  2h
Ex. Consider f(x) = x4 for x0 = 0, x1 = 0.2, x3 = 0.4, x4 = 0.6, and x4 = 0.8. Calculate
f 0′, f 1′, and f 2′ using three point formula.

n xn fn = x4n h = 0.2
1
0 0 0 f 0′ = [ −3f 0 + 4f 1 − f 2 ] =
2h
1 0.2 16×10-4
1
2 0.4 256×10-4 f=1
′ [f 2 − f 0=]
2h
3 0.6 1296×10-4 1
f=2
′ [f 0 − 4f 1 + 3f 2=]
4 0.8 4096×10-4 2h

H.W. find f 0′, f 1′, and f 2′ using four-point formula.

N.B. if h not constant using Lagrange interpolation to find differebtaition.

f (x )
n (x − x )
n

= ∏ x −x
j
N k ( x )f k , where N k ( x )
k 0=j 0
j ≠k
( k) j

25
5. Curve Fitting
In Curve Fitting we are given n points (pairs of numbers) (x1, y1),
(x2, y2),...,(xn, yn) and we want to determine a function f(x) approximately. The type
of function (for example, polynomials, exponential functions, sine and cosine
functions) may be suggested by the nature of the problem (the underlying physical
law, for instance), and in many cases a polynomial of a certain degree will be
appropriate.

f(x) y=f(x)

Using Least Square Method to find approximate function


5.1 Linear Fitting
y(x) = ax + b a, b : constants
f(x) y=f(x)
error = exact value - approx. value

e1 =[ y 1 − y ( x 1 )] = y 1 − (ax 1 + b )  or absolute


2 2

e 2 =[ y 2 − y ( x 2 )] = y 2 − (ax 2 + b ) 
2 2
e3
e2

 e1

e n =[ y n − y ( x n )] = y n − (ax n + b ) 
2 2

x
n
1
∴Total error =
E= ∑
2 i =1
e i2 n: No. of points

1 n
∑  y i − (ax i + b )  Should be minimum.
2
=
∴E
2 i =1

26
∂E ∂E
= 0,= 0
∂a ∂b
∂E 1 n

= ∑ 2.  y i − (ax i + b )  * (=
∂a 2 i =1 
−x i ) 0
n
∑  y i − (ax i + b )  x i =
0
i =1

y i x i ∑ (ax 2 + bx i )
n n
∑= i
divided both sides by n
=i 1=i 1

=
yx ax 2 + b x ...(1)

∂E 1 n
= ∑ 2.  y i − (ax i + b )=
∂b 2 i =1 
 * ( −1) 0

n n
∑yi
= ∑ (ax i + b ) divided both sides by n
=i 1=i 1

=
y ax + b ... (2)

=
yx ax 2 + b x ...(1)

Multiply eq.(2) by x and subtract from eq.(1);


yx − y x
∴a = , and b = y − ax
x 2 − ( x )2

Ex. Find the best line representing the following points (0, 1), (1, 2), (2, 4), (3, 8),
(4, 10) and calculate the approximate errors.
Sol. n = 5
i xi yi yixi x i2 y(xi)=axi+b ei ei 2
1 0 1 0 0 0.2 0.8 0.64
2 1 2 2 1 2.6 -0.6 0.36
3 2 4 8 4 5.0 -1 1
4 3 8 24 9 7.4 0.6 0.36
5 4 10 40 16 9.8 0.2 0.04

∑ 10 25 74 30 - - 2.4

Avg. 2 5 14.8 6

27
yx − y x 14.8 − ( 5)( 2 )
=a = = 2.4
x 2 − ( x )2 6 − ( 2)
2

5 ( 2.4 )( 2.) =
b =y − ax =− 0.2
∴ y= 2.4x + 0.2
5.2 Polynomial fitting
In General
y ( x ) = am x m + am −1x m −1 + am − 2 x m − 2 +  + a0

where m degree of polynomial


• For example m = 2
y ( x ) = a2 x 2 + a1x + a0
∂E
=0 ⇒ yx 2 = a2 x 4 + a1 x 3 + a0 x 2 (1)
∂a2
∂E
=0 ⇒ yx = a2 x 3 + a1 x 2 + a0 x ( 2 )
∂a1
∂E
= 0 ⇒ y = a2 x 2 + a1 x + a0  ( 3)
∂a0

Ex. Find the best second degree curve representing the following points (0, 1), (1, 3),
(2, 5), (3, 11), (4, 15).
Sol.
y ( x ) = a2 x 2 + a1x + a0 ;n=5
i x y x2 x3 x4 yx yx2
1 0 1 0 0 0 0 0
2 1 3 1 1 1 3 3
3 2 5 4 8 16 10 20
4 3 11 9 27 81 33 99
5 4 15 16 64 256 60 240

∑ 10 35 30 100 354 106 362

Avg. 2 7 6 20 70.8 21.2 72.4

28
yx 2 = a2 x 4 + a1 x 3 + a0 x 2 ⇒ 72.4 = 70.8 a2 + 20 a1 + 6 a0 (1)
yx = a2 x 3 + a1 x 2 + a0 x ⇒ 21.2 = 20 a2 + 6 a1 + 2 a0 ( 2 )
y = a2 x 2 + a1 x + a0 ⇒ 7= 6 a2 + 2 a1 + a0 ( 3)
Solve the above linear simultaneous equation using iteration or Gauss method to
find a2, a1, and a0

5.3 Exponential fitting, y = ae bx


Step (1) Take ln for both sides;
ln=y ln a + bx
Y = A + bx

Step (2) Perform linear fitting to evaluate b, A


Y x −Y x
b= , and A= Y − b x
x − (x )
2 2

∴a =eA
Ex. Find the best exponential function representing the following points (1, 4.7),
(2, 9), (3, 25), (4, 80) and calculate the approximate errors.
Sol. n = 4
y = ae bx ⇒ ln y = ln a + bx ⇒Y = A + bx
=
where Y ln= y , A ln a
i x y Y=lny Yx x2 y=aebx ei ei 2
1 1 4.7 1.548 1.548 1 4.437 0.263 0.07
2 2 9 2.197 4.394 4
3 3 25 3.219 9.387 9
4 4 80 4.382 17.53 15

∑ 10 118.4 11.346 32.859 30

Avg. 2.5 29.67 2.837 8.215 7.5

29
Y x −Y x 8.215 − 2.837 * 2.5
=b = = 0.8978
x 2 − ( x )2 7.5 − ( 2.5)
2

A =Y − b x =2.837 − 0.8978* 2.5 =0.5925


∴a = e A = 1.808
∴y =
1.808e 0.8978 x

5.4 Power fitting


y = ax b

Step (1) Take ln for both sides;


ln=y ln a + b ln x
Y = A + bX

Step (2) Perform linear fitting to evaluate b, A


Y X −Y X
b= , and A= Y − b X
X − (X )
2 2

∴a =eA
H.W. For the same points in the last example, find the best power function? and
calculate the approximation error. Ans. y = 3.614 x1.954

H.W. Write a computer program to read n-points then ask for the kind of fitting
required, then perform the procedure of fitting and evaluate the total error
generated.

30
6. Numerical Solution of ODE

6.1 First order DE.


We know that an ODE of the first order is of the form F(x, y, y')=0 and can often
be written in the explicit form y ′ = f ( x , y ) . An initial value problem for this
equation is of the form;
= (x , y ),
y ′ f= y (x 0 ) y 0
We shall discuss methods of computing numerical values of the solution. these
methods are step-by-step methods, that is, we start from the given y0=y(x0) and
process stepwise, computing approximate values of the solution of y(x) at the mesh
point.
x1 =
x 0 + h, x 2 =
x 0 + 2h , x 3 =
x 0 + 3h , 

Ex. y '− 2 y =x ⇒ y ' =f ( x , y ) =x + 2 y


(1) Euler Method f(x)

yn+1
Using Taylor series

yn
2 h
h
y n +1 =y n + hy n′ + y n′′ +
2! xn xn+1 x

For a small value of h, the highest powers h2, h3, .... are very small
y n +1 =y n + hy n′ =y n + hf ( x n , y n )
In the first step we compute
y=
1 y 0 + hf ( x 0 , y 0 )
In the second step we compute
y=
2 y 1 + hf ( x 1 , y 1 )
In general;

31
y n=
+1 y n + hf ( x n , y n ) n = 0, 1 , 2, ....

This is called the Euler method or Euler-Cauchy method.

Ex. Solve y '− 2( x + y ) =


0 for x from 0 to 1. given that y(0) = 1, use h = 0.1.
y=' f ( x , y=
) 2x + 2 y
Using Euler method y n=
+1 y n + hf ( x n , y n )
hfn
n xn yn yn+1
0.1(2xn+2yn)
0 0 1 + 0.2 = 1.2
1 0.1 1.2 0.26 1.46
2 0.2 1.46 0.332 1.792
3 0.3
4 0.4
5 0.5
6 0.6
7 0.7
8 0.8
9 0.9
10 1.0

(2) Modified Euler Method (Heun's Mehtod)


Euler’s method is generally much too inaccurate. For a large and small h the
computation becomes prohibitive.
The improved Euler method we compute two values;
Step (1): Predictor
K n = hf ( x n , y n )
L n = hf ( x n + h , y n + K n )

32
Step (2): Corrector
1
y n +1 =
yn + ( K n + Ln )
2
Hence the improved Euler method is a predictor–corrector method: In each
step we predict a value and then we correct it.
Ex. Solve the least example using modified Euler method.
y=' f ( x , y=
) 2x + 2 y , y(0) = 1, h = 0.1

=K n hf ( x n , y n ) = 0.1( 2x n + 2 y n )
L=
n hf ( x n + h , y n + K n )= 0.1  2 ( x n + 0.1) + 2 ( y n + K n ) 
1
y n + ( K n + Ln )
y n +1 =
2
K=
0 0.1( 2 * 0 + 2 *1=
) 0.2
L0 0.1  2 ( 0 + 0.1) + 2 (1 + 0.2=
= )  0.26
1
1 + ( 0.2 + 0.26 ) =
y1 = 1.23
2

n xn yn Kn Ln yn+1
0 0 1 0.2 0.26 1.23
1 0.1 1.23 0.266 0.3392 1.533
2 0.2 1.533
3 0.3
4 0.4
5 0.5
6 0.6
7 0.7
8 0.8
9 0.9
10 1.0

33
(3) Runge - Kutta Method
A method of great practical importance and much greater accuracy than that of
the improved Euler method is the classical Runge–Kutta method of fourth order,
which we call briefly the Runge–Kutta method.
The following fourth order Runge-Kutta algorithm, with xn = x0 +nh.
Step (1): Calculate
A n = hf ( x n , y n )
 h A 
B n = hf  x n + , y n + n 
 2 2 
 h B 
C n = hf  x n + , y n + n 
 2 2 
D n = hf ( x n + h , y n + C n )

Step (2): Calculate


1
y n +1 =y n + ( A n + 2 B n + 2C n + D n )
6
Ex. Use the fourth order Runge-Kutta algorithm with a step size h = 0.2 to sole initial
value problem
dy
= + 2 y sin 3x=
with y (0) 1
dx

in the interval 0 ≤ x ≤ 1 .
Sol.
∴ y=′ f ( x , y=
) sin 3x − 2 y

=A n 0.2 * ( sin 3x n − 2 y n )
  A 
=B n 0.2 sin 3 ( x n + 0.1) − 2  y n + n 
  2 
  B 
=C n 0.2 sin 3 ( x n + 0.1) − 2  y n + n 
  2 
D n 0.2 [sin 3( x n + 0.2) − 2( y n + C n )]
=

34
0.2 * ( sin 3* 0 − 2 *1) =
A0 = −0.4
  −0.4  
0.2 sin 3 ( 0 + 0.1) − 2  1 +
B0 =  = −0.2609
  2  
  −0.2609  
0.2 sin 3 ( 0 + 0.1) − 2  1 +
C0 =  = −0.28872
  2 
0.2 [sin 3(0 + 0.2) − 2(1 − 0.28872)] =
D0 = −0.17158
1
∴ y 1 =1 + ( −0.4 ) + 2 ( −0.2609 ) + 2 ( −0.28872 ) + ( −0.17158)  =0.72153
6

n xn yn An Bn Cn Dn yn+1
0 0 1 -0.4 -0.261 -0.288 -0.171 0.721
1 0.2 0.721 -0.175 -0.097 -0.112 -0.057 0.613
2 0.4 0.613 -0.058 -0.034 -0.038 -0.0348 0.573
3 0.6 0.573
4 0.8
5 1.0

H.W. Write a computer program to solve and first order differential equation using
Euler, modified Euler and Runge-Kutta methods.

6.2 Second Order Ordinary Differential Equation:


We know that an ODE of the second order is of the form F(x, y, y',y")=0 and can
often be written in the explicit form;
y ′′ f ( x , y , y ′) =
with y ( x 0 ) y=
0 and y ′ ( x 0 ) y 0′
(1) Euler Method
Using Taylor series

35
h2 h3
y n +1 =y n + hy n′ + y n′′ + y n′′′ + 
2! 3!
h2
y n +1 =y n + hy n′ + y n′′
2!

The Euler method we compute two steps;


h2
Step (1) y n +1 =y n + hy n′ + f ( x n , y n , y n′ )
2

Step (2) y n′=


+1 y n′ + hf ( x n , y n , y n′ )

Ex. Solve y ′′ − 4 y ′ − 2xy = =


0 given that y ( 0 ) 1,=
y ′ ( 0 ) 1 for x from 0 to 1
using h = 0.1.
Sol.
y ′′ f ( x , y , =
= y ′) 2xy + 4 y ′
y n + 0.1y n′ + 0.005 ( 2x n y n + 4 y n′ )
y n +1 =
y n′ + 0.1( 2x n y n + 4 y n′ )
y n′ +1 =
Calculate
1 + 0.1*1 + 0.005 ( 2 * 0 *1 + 4 *1) =
y1 = 1.12
1 + 0.1( 2 * 0 *1 + 4 *1) =
y 1′ = 1.4
 

n xn yn y'n f(xn,yn,y'n) yn+1 y'n+1


0 0 1 1 4 1.12 1.4
1 0.1 1.12 1.4 5.824 1.289 1.982
2 0.2 1.289 1.982
3 0.3
4 0.4
5 0.5
6 0.6
7 0.7

36
8 0.8
9 0.9
10 1.0

(2) Runge-Kutta-Nyström Method (RKN Method)


RKN methods are direct extensions of RK methods (Runge–Kutta methods) to
second-order ODEs y ′′ = f ( x , y , y ′) , as given by Nyström (1925). The best known
of these uses the following formulas;
Step (1): Calculate
A n = hf ( x n , y n , y n′ )

 h K A   A 
B n = hf  x n + , y n + , y n′ + n  where =
K h  y n′ + n 
 2 2 2   4 

 h K B 
C n = hf  x n + , y n + , y n′ + n 
 2 2 2 

D n = hf ( x n + h , y n + L , y n′ + C n ) where =L h ( y n′ + C n )

Step (2): Calculate


 1 
y n +1 = y n + h  y n′ + ( A n + B n + C n ) 
 6 
Step (3): Calculate
1
y n′ +1 =y n′ + ( A n + 2 B n + 2C n + D n )
6
Ex. Resolve the last example using RKN method, take h = 0.2.
f ( x , y , y ′) =
y ′′ = 2xy + 4 y ′ , y ( 0) =
y ′ ( 0) =
1
=A n 0.2 ( 2x n y n + 4 y n′ )

  K  A   A 
B n 0.2  2 ( x n + 0.1)  y n +  + 4  y n′ + n   where
= = K 0.2  y n′ + n 
  2  2   4 

37
  K   Bn 
C n 0.2  2 ( x n + 0.1)  y n +
=  + 4  y n′ + 
  2   2  

 Cn 
D n 0.2 ( 2 ( x n + 0.2 )( y n + L ) + 4 ( y n′ + C n ) )
= where =L 0.2  y n′ + 
 2 

A 0 0.2 [ 2 * 0 *1 + 4=
= *1] 0.8  0.8 
K = 0.2  1 +  = 0.24
 4 

  0.24   0.8  
=
B 0.2  2 ( 0 + 0.1)  1 +  + 4 1 + = 1.165
2  
n
  2  

  0.24   1.165  
=
C n 0.2  2 ( 0 + 0.1)  1 +  + 4 1 + =  1.311
  2   2 

 1.311 
L =0.2  1 +  =0.311
 2 

=
D n 0.2  2 ( 0 + 0.2 )(1 + 0.331) + 4 (1 + 1.311=
)  1.971
 1 
1 + 0.2  1 + ( 0.8 + 1.165 + 1.311)  =
y1 = 1.309
 6 
1
1 + ( 0.8 + 2 *1.165 + 2 *1.311 + 1.971) =
y 1′ = 2.287
6

n xn yn y'n An Bn Cn Dn yn+1 y'n+1


0 0 1 1 0.8 1.165 1.311 1.971 1.309 2.287
1 0.2 1.309 2.287
2 0.4
3 0.6
4 0.8
5 1.0

H.W. Write a computer program to solve any second order differential equation
using Euler and Runge-Kutta Nyström methods.

38
7. Numerical Solution of Partial Differential Equation
The PDEs serve as models for elliptic, parabolic, and hyperbolic. For example,
the Laplace equation is a representative example of an elliptic type of PDE, and so
forth.
A PDE is called quasilinear if it is linear in the highest derivatives. Hence a
second-order quasilinear PDE in two independent variables x, y is of the form;
Note:

∂u ∂ 2u
=ux = , u xx
F ( x , y ,u ,u x ,u y )
au xx + 2bu xy + cu yy = ∂x ∂x 2
∂u ∂ 2u
=uy = , u yy
∂y ∂y 2
∂ 2u
u is the unknown function. This equation is said to be of; u xy =
∂x ∂y

Elliptic type if ac − b 2 > 0 (example: Laplace equation)


Parabolic type if ac − b 2 =
0 (example: heat equation
Hyperbolic type if ac − b 2 < 0 (example: wave equation)

7.1 Solution of 2-D Laplace Equation;


∇2u = u xx + u yy = 0

To obtain methods of numerical solution. we replace the partial derivatives by


corresponding difference quotients, as follows. By the Taylor formula,
h2 h3
u ( x + h , y )= u ( x , y ) + hu x ( x , y ) + u xx ( x , y ) + u xxx ( x , y ) +  (1)
2 6
2
h h3
u ( x − h , y )= u ( x , y ) − hu x ( x , y ) + u xx ( x , y ) − u xxx ( x , y ) +  (2)
2 6

39
Subtract Eq.(2) from Eq.(1), neglect terms in h2, h3, ...,

u x (x , y ) ≈
1
2h
[u ( x + h , y ) − u ( x − h , y )] ⇒ u x=
j ,i
1
2h
(u i +1, j − u i −1, j )
Similarly

u y (x , y ) ≈
1
2k
[u ( x , y + k ) − u ( x , y − k )] ⇒ u=
y j ,i
1
2k
(u i , j +1 − u i , j +1 )

(x,y+k)
i,j+1

k k

h h h h
(x-h,y) (x+h,y)
(x,y) i-1,j i,j i+1,j

k k

i,j-1
(x,y-k)

To obtain second derivative. Adding Eq.(1) and Eq.(2);


1
u xx ( x , y ) ≈ [u ( x + h , y ) − 2u ( x , y ) + u ( x − h , y )]
h2
1
u xx = u i +1, j − 2u i , j + u i −1, j  (4)
i,j
h2 
Similarly

1
u yy ( x , y ) ≈ [u ( x + k , y ) − 2u ( x , y ) + u ( x − k , y )]
k2
1
u xx = u i , j +1 − 2u i , j + u i , j −1  (5)
i,j
k2
Substitute Eqs. (4) &(5) into Laplace Equation, get;

40
1 1
u − 2u + u 
 k 2 u i , j +1 − 2u i , j + u i , j −1  =
+ 0
h2 
i +1, j i , j i −1, j

* If h = k
u i +1, j + u i −1, j + u i , j +1 + u i , j −1 − 4u i , j =
0
u i +1, j + u i −1, j + u i , j +1 + u i , j −1
∴u i , j =
4
Note:

i, j+1  1 
k   (Called Stencil, pattern,
1 −4 1 molecule, or star)
h
i-1, j i+1, j  
i, j
 1 

i, j-1

Can be rewritten the Laplace Equation in stencil as;

 1 
 
1 −4 1 u =0
 
 1 

Ex. The four sides of a square plate of side 12 cm made of homogeneous material are
kept at constant temperature 0 ºC and 100 ºC as shown figure. Using a grid mesh
4 cm. Find the (steady state) temperature at the mesh point.

y
0ºC 1,3 3,2
0,3 3,3
12

0,2 1,2 2,2 3,2


100ºC 100ºC
R
0,1 2,1 3,1
1,1

0 x 0,0 1,0 2,0 3,0


100ºC 12

Grid and mesh points


Given Problem
41
Sol. The equations for 2-D in steady state as;
∇2u = u xx + u yy = 0
Consider the mesh points;
h=k=4
4u i , j = u i +1, j + u i −1, j + u i , j +1 + u i , j −1
* Boundary Condition
u=
1,0 u=
2,0 100 0C
u=
1,3 u=
2,3 0 oC

u=
0,1 u=
0,1 100 0C
u=
3,1 u=
3,2 100 oC

At point (1,1)

4u1,1 = u 2,1 + u 0,1 + u1,2 + u1,0 ⇒ 4u1,1 = u 2,1 + 100 + u1,2 + 100

4u1,1 − u 2,1 − u1,2 =


200 (1)
At point (2,1)

4u 2,1 = u 3,1 + u1,1 + u 2,2 + u 2,0 ⇒ 4u 2,1 = 100 + u1,1 + u 2,2 + 100

4u 2,1 − u1,1 − u 2,2 =


200 (2)
At point (1,2)

4u1,2= u 2,2 + u 0,2 + u1,3 + u1,1 ⇒ 4u1,2= u 2,2 + 100 + 0 + u1,1

4u1,2 − u 2,2 − u1,1 =


100 (3)
At point (2,2)

4u 2,2 = u 3,2 + u1,2 + u 2,3 + u 2,1 ⇒ 4u 2,2 = 100 + u1,2 + 0 + u 2,1

4u 2,2 − u1,2 − u 2,1 =


100 (4)
Solving Eqs. (1), (2), (3), and (4) by Iteration method or Gauss-Jordan method to
finding the interior point u1,1 ,u 2,1 , u1,2 , and u 22

42
Rearrangement above equations

(1) If using Gauss-Jordan elimination method (Practical for small system of equation)
as;

−4u11 + u 21 + u12 =
−200 −4 1 1 0 −200
u11 − 4u 21 + u 22 =
−200 1 −4 0 1 −200
u11 − 4u12 + u 22 =
−100 1 0 −4 1 −100
u 21 + u12 − 4u 22 =
−100 0 1 1 −4 −100

(2) If using Iteration method (Practical for large system of equation) as;

u11 = 0.25u 21 + 0.25u12 + 50


u 21 =0.25u11 + 0.25u 22 + 50
u12 = 0.25u11 + 0.25u 22 + 25
u 22 = 0.25u 21 + 0.25u12 + 25

Ex. Find the temperature distribution for the following plate.

y
0ºC 1,3 3,2
0,3 3,3
12

0,2 1,2 2,2 3,2


100ºC 100ºC
R
0,1 2,1 3,1
1,1

0 x 0,0 1,0 2,0 3,0


12
∴uy = 0
1,-1 2,-1
Given Problem
Grid and mesh points
To find the temperature at isolation side

u1,1 − u1,0
uy = = 0 ⇒ u1,1 = u1,0 ...(a )
1,0 2k
u −u
uy = 2,1 2,0 = 0 ⇒ u 2,1 = u 2,0 ...(b )
2,0 2k

43
At point (1,0)
4u1,0 = u 2,0 + u 0,0 + u1,1 + u1,−1 ⇒ 4u1,0 = u 2,0 + 100 + u1,1 + u1,1
4u1,0 − u 2,0 − 2u1,1 =
100 (1)
At point (2,0)
4u 2,0 = u 3,0 + u1,0 + u 2,1 + u 2,−1 ⇒ 4u 2,0 = 100 + u1,0 + u 2,1 + u 2,−1
4u 2,0 − u1,0 − 2u 2,1 =
100 (2)
The other points same the above example

H.W. Complete the other points and solution using Gauss-Seidel Method.

H.W. Write a computer programming to solve 2-D Laplace equation in case of


Dirichlet or Neumann Conditions.

7.2 Solution of Parabolic Equation; (1-D Diffuse equaton)


In this section we explain the numerical solution of the prototype of parabolic
equation, the one dimensional heat equation;
1
ut = u xx (1)
α
 m2 
x : space 0 ≤ x ≤ L , t :time t ≥0 α : thermal diffusivity  
 s 
The initial condition u ( x ,0) = f ( x ) (given) and boundary condition at x=0 and x =
L for all t ≥ 0 . Using ∆x= h , ∆=
t k
u i , j +1 − u i , j
ut i,j
= Forward difference (a)
k
1
u xx = u i +1, j − 2u i , j + u i −1, j 
i,j
h2  (b)
Subs. above Eqs. (a) & (b) in Eq.(1);
1 1
u i , j +1 − u i = 
,j  u i +1, j − 2u i , j + u i −1, j 
αk h2 
αk
u i= u i +1, j − 2u i , j + u i −1, j  + u i , j
h2 
, j +1

αk  2α k 
=
u i , j +1 u
2  i +1, j
+ u i −1, j  +  1 − 2 u i , j
h  h 

44
(1 − 2r )u i , j + r u i +1, j + u i −1, j 
u i , j +1 =

αk
where r =
h2
i, j+1

t k
....

....
h
i-1, j i, j i+1, j
0,3 3,2 j=3
1,3 3,3

u at x =0 0,2 1,2 2,2 3,2 j=2 u at x = L


0,1 j=1
1,1 2,1 3,1
k
h  −1 
0,0 x  
i=1 i=2 i=3 ....  
r (1 − 2r ) r 

u at t =0
Initial condition
Figure; stencil for Four Points
Figure; Grid and mesh points

N. B. For convergence and stability of explicit method is the condition;

1 αk 1
r≤ ⇒ 2

2 h 2
2
h
∴k ≤ time step

45
Ex. A bar is initially at 100 ºC, it's ends are attached to a constant temperature body
at 0 ºC. Find the temperature distribution assuming α = 0.1, Take ∆x=h=0.25.

Sol. If h= 0.25 and α = 0.25

The condition for stability is;


0 ºC 100 ºC 0 ºC
αk 1 h 2
≤ ⇒ ∴ k ≤
h2 2 2α

....

....
....
2
0.25
k ≤ → k ≤ 0.3125
2 * 0.1 4,3
0,3 1,3 3,2 3,3
Take k = 0.25
0,2 1,2 2,2 3,2 4,2
αk 0.1* 0.25
=
r = = 0.4
h2 0.252 0,1
1,1 2,1 3,1 4,1
t k
h
x 0,0
(1 − 2r )u i , j + r u i +1, j + u i −1, j 
u i , j +1 =
1,0 2,0 3,0 4,0

* Boundary conditions Figure; Grid and mesh points

At x = 0 u 0,1= u 0,2= u 0,3= ...= u 0, j= 0


i, j+1
At x = L u 4,1= u 4,2= u 4,3= ...= u 4, j= 0

* Initial conditions At t = 0 i-1, j i, j i+1, j

u1,0= u 2,0= u 3,0= ...= u i ,0= 100

Compute the other points using the above formula

At t = 0.25, j =1

u=
1,1  0.2 *100 + 0.4 (100 + 0=
0.2u1,0 + 0.4 u 2,0 + u 0,0= ) 60
u 2,1= 0.2u 2,0 + 0.4 u 3,0 + u1,0 = 0.2 *100 + 0.4 (100 + 100 )= 100
u 3,1= 0.2u 3,0 + 0.4 u 4,0 + u 2,0 = 0.2 *100 + 0.4(0 + 100)= 60

At t = 0.5, j =2

46
 0.2 * 60 + 0.4 (100 + 0=
= 0.2u1,1 + 0.4 u 2,1 + u 0,1=
u 2,1 ) 52
u 2,2= 0.2u 2,1 + 0.4 u 3,1 + u1,1 = 0.2 *100 + 0.4 ( 60 + 60=
) 68
u 3,2 = 0.2u 3,1 + 0.4 u 4,1 + u 2,1 = 0.2 * 60 + 0.4(0 + 100)= 52

At t = 0.75, j = 3

H.W. Complete the other points

continue the procedure for until the steady state condition that is not change the
value of u at increase the time t.

H.W. Write a computer programming to solve 1-D heat equation.

7.3 Solution of Hyperbolic Equation; (1-D Wave equaton)


In this section we explain the numerical solution of the prototype of hyperbolic
equation, the one dimensional wave equation;
1
utt = u xx
c2 (1)
m 
x : space 0 ≤ x ≤ L , t :time t ≥0 c : speed  
s 

Initial displacement u ( x ,0) = f ( x ) (Given)

Initial velocity ut ( x ,0) = v ( x ) (Given)


boundary condition u=
(0,t ) u=
( L ,t ) 0
Using ∆x= h , ∆=
t k
1
u xx = u i +1, j − 2u i , j + u i −1, j 
i,j
h2  (a)
1
utt = u i , j +1 − 2u i , j + u i , j −1  (b)
i,j
k2
Subs. above Eqs. (a) & (b) in Eq.(1);
1 1
u
2  i , j +1 1
− 2u i , j + u i , j −= u i +1, j − 2u i , j + u i −1, j 
2
c k h2 

47
c 2k 2
u= u i +1, j − 2u i , j + u i −1, j  + 2u i , j + u i , j −1
h2 
i , j +1

2(1 − r 2 )u i , j + r 2 (u i +1, j + u i −1, j ) − u i , j −1


u i , j +1 =

ck
where r =
h

i, j+1

k
h
t i-1, j i, j i+1, j
....

....
i, j-1
0,3 3,2 j=3
1,3 3,3

0,2 1,2 2,2 3,2 j=2

0,1 j=1
1,1 2,1 3,1  −1 
k  2 
t =0
h
r 2 (1 − r 2 ) r2
0,0 1,0 3,0 .... x
2,0
 
 −1 
1,-1 2,-1 3,-1

Figure; stencil for five Points


Figure; Grid and mesh points

* The condition for convergence and stability of the solution;

(1 − r ) ≥ 0
2
⇒ r ≤1 ⇒
ck
h
≤ 1 ∴k ≤
h
c

At initial velocity v(x) given (At t = 0 , j = 0)

u i ,0 − u i ,−1
v (=
x i ) u=
t i ,0 Using Backward difference
k

∴u i ,−1 =u i ,0 − kv ( x i )

48
Ex. For the string shown, find the motion using ∆x = 0.25, speed c = 1 m/s.
L=1
2 0.25 ≤ x ≤ 0.75
v (x ) = 
0 otherwise
f(x)
1

Sol. h = ∆x = 0.25
0,3 1,3 3,2 3,3
h
r ≤ 1 ⇒ ∴k ≤
c 0,2 1,2 2,2 3,2
k ≤ 0.25 ⇒ take k =
0.2
ck 1* 0.2 0,1
=
r = = 0.8 1,1 2,1 3,1
h 0.25 k
h
t =0
r 2 = 0.64 0,0 1,0 2,0 3,0 4,0

2(1 − r 2 )u i , j + r 2 (u i +1, j + u i −1, j ) − u i , j −1


u i , j +1 = 1,-1 2,-1 3,-1

u i , j +1= 0.72u i , j + 0.64 (u i +1, j + u i −1, j ) − u i , j −1 Figure; Grid and mesh points

* Boundary conditions;

u=
0,0 u=
0,1 u=
0,2 u=
0,3 = 0
......
u=
4,0 u=
4,1 u=
4,2 u=
4,3 = 0
......

At t = 0 , j = 0

Initial conditions (I.C.)

u i ,0 = f ( ih ) Note: Some time the function


f(x) given as; f(x)= sin πx
= ( 0.25) 0.5
u1,0 f =
= ( 0.5) 1
u 2,0 f =
= ( 0.75) 0.5
u 3,0 f =

49
u i=
, −1 u i ,0 − kv ( ih )
u1,0 − kv ( 0.25) =
u1,−1 = 0.5 − 0.2 * 2 =
0.1
u 2,0 − kv ( 0.5) =
u 2,−1 = 1 − 0.2 * 2 =
0.6
u 3,0 − kv ( 0.75) =
u 3,−1 = 0.5 − 0.2 * 2 =
0.1

u1,1 0.72u1,0 + 0.64 (u 2,0 + u 0,0 ) − u=


= 1, −1 0.72 * 0.5 + 0.64(1 + 0) −=
0.1 0.9
u=
2,1 0.72u 2,0 + 0.64 (u 3,0 + u1,0 ) − u 2,=
−1 0.72 *1 + 0.64 ( 0.5 + 0.5) − 0.6
= 0.76
u 3,1 0.72u 3,0 + 0.64 (u 4,0 + u 2,0 ) − u=
= 3, −1 0.72 * 0.5 + 0.64 ( 0 + 1) −=
0.1 0.9

At t = 0.2, j =2

u=
1,2 0.72u1,1 + 0.64 (u 2,1 + u 0,1 ) − =
u1,0 0.72 * 0.9 + 0.64(0.76 + 0) −=
0.5 0.6344
u 2,2 0.72u 2,1 + 0.64 (u 3,1 + u1,1 ) −=
= u 2,0 0.72 * 0.76 + 0.64 ( 0.9 + 0.9=
) − 1 0.6992
u=
3,2 0.72u 3,1 + 0.64 (u 4,1 + u 2,1 ) − u=
3,0 0.72 * 0.9 + 0.64 ( 0 + 0.76 ) − 0.5
= 0.6344

At t = 0.4, j = 3

Note: Can be calculate half points and using the symmetry to obtained the
others points

H.W. Complete the other points

H.W. Write a computer programming to solution the hyperbolic equation.

50

You might also like