100% found this document useful (3 votes)
9K views83 pages

Numerical Analysis M.SC PDF

The document provides an introduction to numerical analysis. It discusses the differences between analytical and numerical solutions. Numerical analysis aims to approximate solutions to problems using arithmetic operations like addition and multiplication, whereas analytical solutions use functions and calculus. The document also covers types of errors in numerical procedures like truncation error and round-off error. It provides examples of solving systems of linear equations using Gaussian elimination and calculating relative errors. Numerical analysis is important as it allows solving problems using computer programs and commercial software packages.

Uploaded by

Zaidoon Mohsin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
9K views83 pages

Numerical Analysis M.SC PDF

The document provides an introduction to numerical analysis. It discusses the differences between analytical and numerical solutions. Numerical analysis aims to approximate solutions to problems using arithmetic operations like addition and multiplication, whereas analytical solutions use functions and calculus. The document also covers types of errors in numerical procedures like truncation error and round-off error. It provides examples of solving systems of linear equations using Gaussian elimination and calculating relative errors. Numerical analysis is important as it allows solving problems using computer programs and commercial software packages.

Uploaded by

Zaidoon Mohsin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Introduction to Numerical Analysis

Analysis versus Numerical Analysis


The word analysis in mathematics usually means who to solve a problem through
equations. The solving procedures may include algebra, calculus, differential
equations, or the like.

Numerical analysis is similar in that problems solved, but the only procedures that
are used are arithmetic: add, subtract, multiply, divide and compare.

Differences between analytical solutions and numerical solutions:

1) An analytical solution is usually given in terms of mathematical functions. The


behavior and properties of the function are often apparent. However, a numerical
solution is always an approximation. It can be plotted to show some of the
behavior of the solution.

2) An analytical solution is not always meaningful by itself.

Example: 3 as one of the roots of x 3 − x 2 − 3x + 3 = 0 .

3) While the numerical solution is an approximation, it can usually be evaluated as


accurate as we need. Actually, evaluating an analytic solution numerically is
subject to the same errors.

Computers and Numerical Analysis

Numerical Methods+Programs Computers


Numerical Analysis

• As you will learn enough about many numerical methods, you will be able to
write programs to implement them.
• Programs can be written in any computer language. In this course all programs
will be written in Matlab environment.
• Actually, writing programs is not always necessary. Numerical analysis is so
important that extensive commercial software packages are available.

Numerical Analysis / M. Sc. -1- Written by Assoc. Prof.


Introduction to Numerical Analysis Dr. Zaidoon M. Shakoor
Types of Equations
The equations is divided into three main categories such as in below figure:-

Kinds of Errors in Numerical Procedures

The total error comprises of:

1) Model Error: due to the mismatch between the physical situation and the
mathematical model.

2) Data Error: due to the measurements of doubtful accuracy.

3) Human Error: due to human blunders.

4) Propagated Error: the error in the succeeding steps of a process due to an


occurrence of an earlier error.

5) Truncation Error: the notion of truncation error usually refers to errors introduced
when a more complicated mathematical expression is “replaced” with a more
elementary formula. This formula itself may only be approximated to the true values,
thus would not produce exact answers.

Numerical Analysis / M. Sc. -2- Written by Assoc. Prof.


Introduction to Numerical Analysis Dr. Zaidoon M. Shakoor
Example:
Truncation of an infinite series to a finite series to a finite number of terms leads to
the truncation error. For example, the Taylor series of exponential function
x 2 x3 xn
ex = 1+ x + + + ... +
2! 3! n!
If only four terms of the series are used, then
x2 x3
e ≈ 1+ x +
x
+
2! 3!
12 13
e ≈ 1+1+ +
1
= 2.66667
2! 3!

The truncation error would be the unused terms of the Taylor series, which then are
x4 x5 14 15
Et = + + = + + ≅ 0.0516152
4! 5! 4! 5!

Check a few Taylor series approximations of the number ex, for x = 1, n = 2, 3 and 4.
Given that e1 = 2.718281.

Approximation Percent relative


Order of n Absolute error
for ex error
2 2.500000 0.218281 8.030111%
3 2.666667 0.051614 1.898774%
4 2.708333 0.00995 0.365967%

6) Round-Off Error: A round-off error, also called rounding error, is the


difference between the calculated approximation of a number and its exact
mathematical value due to rounding

Example:

Numbers such as π, e, or 3 cannot be expressed by a fixed number of decimal

places. Therefore they cannot be represented exactly by the computer.


Consider the number π. It is irrational, i.e. it has infinitely many digits after the
period: π = 3.1415926535897932384626433832795.....
The round-off error computer representation of the number π depends on how many
digits are left out.

Numerical Analysis / M. Sc. -3- Written by Assoc. Prof.


Introduction to Numerical Analysis Dr. Zaidoon M. Shakoor
Let the true value for π is 3.141593.

Number of digits Approximation Percent relative


Absolute error
(Decimal digit) for π error
1 3.1 0.041593 1.3239%
2 3.14 0.001593 0.0507%
3 3.142 0.000407 0.0130%

1.5 Errors in Numerical Procedures


There are two common ways to express the size of the error in a computed result:
absolute error and relative error.

• Absolute error = | true value – approximate value |, which is usually used when the
magnitude of the true value is small.

| true value - approximate value |


• Relative error = , which is a desirable one.
| true value |

While
true value − approximate value
Percent relative error, ε t = × 100%
true value

Numerical Analysis / M. Sc. -4- Written by Assoc. Prof.


Introduction to Numerical Analysis Dr. Zaidoon M. Shakoor
Chapter 1: Solution System of Linear Equations

1.1 Linear Equation


y = mx is an equation, in which variable y is expressed in terms of x and the constant
m, is called Linear Equation. In Linear Equation exponents of the variable is always
‘one’.

1.2 Linear Equation in n variables:


a1 x1 + a2 x2 + a3 x3 + .... + an xn = b
Where x1 , x2 , x3 ,..., xn are variables and
a1 , a2 , a13 ,..., an and b are constants.

1.3 System of Linear Equations:


A Linear System of m linear equations and n unknowns is:
a11 x1 + a12 x2 + a13 x3 + .... + a1n xn = b1
a21 x1 + a22 x2 + a23 x3 + .... + a2 n xn = b2
a31 x1 + a32 x2 + a33 x3 + .... + a3n xn = b3
.......................................................
........................................................
am1 x1 + am 2 x2 + am3 x3 + .... + amn xn = bm
Where x1 , x2 , x3 ,..., xn are variables or unknowns and a’s and b’s are constants.

1.4 Augmented Matrix


System of linear equations:
a11 x1 + a12 x2 + a13 x31 = b1
a21 x1 + a22 x2 + a23 x31 = b2
a31 x1 + a32 x2 + a33 x31 = b3
Can be written in the form of matrices product
 a11 a12 a13   x1   b1 
a
 21 a22 a23   x2  = b2 
 a31 a32 a33   x3  b3 
Or we may write it in the form AX=b,
 a11 a12 a13   x1   b1 
Where A= a21 a22 a23  , X =  x2  , b = b2 
 a31 a32 a33   x3  b3 
 a11 a12 a13 b1 
Augmented matrix is [A : b] = a21 a22 a23 b2 
 a31 a32 a33 b3 
Numerical Analysis / M. Sc. -5- Written by Assoc. Prof.
Chap. 1 Dr. Zaidoon M. Shakoor
Example 1.1:
Write the matrix and augmented form of the system of linear equations
3x – y + 6z = 6
x+y+z=2
2x + y+4z = 3
Solution: Matrix form of the system is
 3 − 1 6   x  6 
1 1 1   y  =  2 
    
2 1 4  z  3
 3 − 1 6 6
Augmented form is [A : b] = 1 1 1 2 
2 1 4 3

1.5 Direct Methods for Solving System of Linear Equations


1.5.1 Gaussian Elimination.
Gaussian elimination is a general method of finding possible solutions to a linear
system of equations.
Gaussian Elimination Method
Step 1. By using elementary row operations
 a11 a12 a13 b1  1 A12 A13 B1 
a
 21 a22 a23 b2  → 0 1 A23 B2 
 a31 a32 a33 b3  0 0 1 B3 
Step 2. Find solution by back – substitutions.

Example 1.2:
Solve the system of linear equations by Gaussion- Elimination method
 x1 + x2 + x3 = 3

 2 x1 − x2 − 2 x3 = 6
4 x + 2 x + 3x =
 1 2 3 7

Solution:
Step 1.
Augmented matrix is
1 1 1 3 R2 = r2 − 2r1
  R3 = r3 − 4r1
 2 − 1 − 2 6 
4 2 3 7 

Numerical Analysis / M. Sc. -6- Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
1 1 1 3
  R3 = 3r3 − 2r1
0 − 3 − 4 0 
0 − 2 − 1 − 5

1 1 1 3 
  R2 = −r2
 0 − 3 − 4 0 
r3
0 0 5 − 15 R3 =
5
1 1 1 3 
 
 0 3 4 0 
0 0 1 − 3
Equivalent system of equations form is:

Step 2. Back Substitution


x1 + x2 + x3 = 3 x3 = −3
3 x2 + 4 x3 = 0 ⇒ x2 = −4 x3 / 3 = 12 / 3 = 4
x3 = −3 x1 = 3 − x2 − x3 = 3 − 4 + 3 = 2
Solutions are x1 = 2, x2 = 4, x3 = −3

Example 1.3:
For the below figure calculate the values of the unknown flow rates F 1 , F 2 and F 3 by
using Gaussion- Elimination method

. F1=? F2=?
99% Benzene 5% Benzene
1% Toluene 92% Toluene
3 % Xylene
Tower 1

Tower 2

F=1000 kg/hr

40% Benzene
40% Toluene
20% Xylene
F3=?
10% Toluene
90% Xylene

Component material balance gives these three equations of three variables


F1 + F2 + F3 = 1000
0.99 F1 + 0.05 F2 + 0 F3 = 400
0.01F1 + 0.92 F2 + 0.1F3 = 400
Numerical Analysis / M. Sc. -7- Written by Assoc. Prof.
Chap. 1 Dr. Zaidoon M. Shakoor
Augmented matrix is
 1 1 1 1000 
0.99 0.05 0 400  R 2 =r 2 – (0.99/1)×r 1
 
 0.01 0.92 0.1 400 R 3 =r 3 – (0.01/1)×r 1
1 1 1 1000 
0 − 0.94 − 0.99 - 590
  R 3 =r 3 -(0.91/(-0.94))r 2
0 0.91 0.09 390 
1 1 1 1000 
0 − 0.94
 − 0.99 - 590  R 2 =r 2 /(-0.94)
0 0 - 0.8684 - 181.17  R 3 =r 3 /( -0.8684)
1 1 1 1000 
0 1 1.0532 627.6596
 
0 0 1 208.6253 

Equivalent system of equations form is:


F 1 + F 2 + F 3 = 1000
F 2 +1.0532F 3 = 627.6596
F 3 = 208.6253

Step 2. Back Substitution


F 3 = 208.6253
F 2 = 627.6596-1.0532F 3 =627.6596-1.0532×208.6253 =407.9354
F 1 =1000- F 2 -F 3 =1000 -208.6253 - 407.9354= 383.4393

1.5.2 Gauss - Jorden Elimination Method

Gauss - Jorden Method


By using elementary row operations
 a11 a12 a13 b1  1 0 0 B1 
a
 21 a22 a23 b2  → 0 1 0 B2 

 a31 a32 a33 b3  0 0 1 B3 

Example 1.4:
Solve the system of linear equations by Gauss-Jorden elimination method
x 1 + x 2 + 2x 3 = 8
- x 1 - 2x 2 + 3x 3 = 1
3x 1 -7x 2 + 4x 3 = 10

Numerical Analysis / M. Sc. -8- Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
Solution:
Augmented matrix is
1 1 2 8
− 1 − 2 3 1  R 2 = r 2 +r 1
 
 3 − 7 4 10 R 3 = r 3 -3r 1

1 1 2 8 
0 − 1
 5 9  R 2 =-r 2
0 − 10 − 2 - 14 R 3 =r 3 -10r 2
1 1 2 8 
0 1 − 5
 - 9  R 3 =-r 3 /52
0 0 − 52 - 104

1 1 2 8 R 1 =r 1 -2r 3
0 1 − 5 − 9  R 2 =r 2 +5r 3
 
0 0 1 2 
1 1 0 4
0 1 0 1  R 1 =r 1 -r 2
 
0 0 1 2
1 0 0 3
0 1 0 1 
 
0 0 1 2
Equivalent system of equations form is:
x1 = 3
x2 = 1
x3 = 2
is the solution of the system.

Example 1.5:
Total and component material balance on a system of distillation columns gives the
flowing equations:-
F1 + F2 + F 3 + F 4 = 1690
0.4F 1 + 0.15F 2 + 0.25F 3 + 0.2F 4 = 412.5
0.25F 1 + 0.8F 2 + 0.3F 3 + 0.45F 4 = 701
0.08F 1 + 0.05F 2 + 0.45F 3 + 0.3F 4 = 487.3
Use Gauss - Jorden method to compute the four un-known's in above equations:-

Numerical Analysis / M. Sc. -9- Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
Solution:
Augmented matrix is
 1 1 1 1 1690 
 0.4 0.15 0.25 0.2 412.5 R 2 = r 2 -(0.4/1)r 1
 
0.25 0.8 0.3 0.45 701  R 3 =r 3 -(0.25/1)r 1
  R 4 =r 4 -(0.08/1)r 1
0.08 0.05 0.45 0.3 487.3
1 1 1 1 1690 
0 − 0.25 − 0.15 − 0.2 − 263.5
 
0 0.55 0.05 0.2 278.5  R 3 =r 3 -(0.55/(-0.25))r 2
  R 4 =r 4 -((-0.03)/(-0.025))r 2
0 − 0.03 0.37 0.22 352.1 
1 1 1 1 1690 
0 − 0.25 − 0.15 − 0.2 − 263.5
 
0 0 − 0.28 − 0.24 − 301.2
  R 4 =r 4 -((0.0388)/(-0.028))r 3
0 0 0.388 0.244 383.72 
1 1 1 1 1690 
0 − 0.25 − 0.15 − 0.2 − 263.5 
 R 2 = r 2 /(-0.25)
0 0 − 0.28 − 0.24 − 301.2  R 3 =r 3 /(-0.028)
 
0 0 0 − 0.08857 − 33.657 R 4 =r 4 /(-0.0887)
1 1 1 1 1690  R 1 = r 1 -r 4
0 1 0.6 0.8 1054  R 2 =r 2 -(0.8/1)r 4

0 0 1 0.85714 1075.74 R 3 =r 3 -(0.85714/1)r 4
 
0 0 0 1 380 
1 1 1 0 1310 R 1 = r 1 -r 3
0 1 0.6 0 750  R 2 =r 2 -((-0.6)/1)r 3

0 0 1 0 750 
 
0 0 0 1 380 
1 1 0 0 560
0 1 0 0 300 R 1 =r 1 -r 2

0 0 1 0 750
 
0 0 0 1 380
1 0 0 0 260 Equivalent system of equations form:
0 1 0 0 300  F 1 = 260 , F 2 = 300 , F 3 = 750 and

0 0 1 0 750 F 4 = 380 is the solution of the system.
 
0 0 0 1 380 

Numerical Analysis / M. Sc. - 10 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
1.6 In-direct (iterative) Methods for Solving System of Linear Equations
The sufficient condition for solving systems of linear equations using iterative
methods is:
a1 ≥ b1 + c1
b2 ≥ a2 + c2
c3 ≥ a3 + b3
The absolute value of the diagonal element in each row of the coefficient matrix must
be greater than the sum of the absolute values of the off-diagonal elements in the
same row.

1.6.1 Jacobi Method


Let the given equation be
a2 x + b1 y + c1 z = d1
a2 x + b2 y + c2 z = d 2
a3 x + b3 y + c3 z = d 3
If the given system of equation is diagonally dominant then
x (i +1) =
1
a1
(
d1 − b1 y (i ) − c1 z (i ) )
y (i +1) =
1
b2
(
d 2 − a2 x (i ) − c2 z (i ) )
z (i +1) =
1
c3
(
d 3 − a3 x (i ) − b3 y (i ) )

Example 1.6:
Use the Jacobi iteration method to obtain the solution of the following equations:
6x 1 -2 x 2 + x 3 = 11
x 1 +2x 2 -5x 3 = -1
-2x 1 +7 x 2 +2x 3 = 5

Solution
Step 1: Re-write the equations such that each equation has the unknown with largest
coefficient on the left hand side:
6x 1 = 11+2 x 2 -x 3
7x 2 = 5+2x 1 -2x 3
5x 3 =1+x 1 +2x 2
2 x2 − x3 + 11
x1 =
6

Numerical Analysis / M. Sc. - 11 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
2 x1 − 2 x3 + 5
x2 =
7
x + 2 x2 + 1
x3 = 1
5
Step 2: Assume the initial guesses x1 = x2 = x3 = 0 then calculate x1 , x2 and x3 :
0 0 0 1 1 1

2( x20 ) − ( x30 ) + 11 2(0) − (0) + 11


x11 = = =1.833
6 6
2( x10 ) − 2( x30 ) + 5 2(0) − 2(0) + 5
x12 = = = 0.714
7 7
( x10 ) + 2( x20 ) + 1 (0) + 2(0) + 1
x31 = = = 0.200
5 5
Step 3: Use the values obtained in the first iteration, to calculate the values for the 2nd
iteration:
2( x12 ) − ( x3`1 ) + 11 2(0.714) − (0.200) + 11
x12 = = = 2.038
6 6
2( x11 ) − 2( x31 ) + 5 2(1.833) − 2(0.200) + 5
x22 = = =1.181
7 7
( x11 ) + 2( x12 ) + 1 (1.833) + 2(0.714) + 1
x32 = = = 0.852
5 5
and so on for the next iterations so that the next values are calculated using the
current values:
2( x2i ) − ( x3i ) + 11
x1i +1 =
6
2( x1i ) − 2( x3i ) + 5
x2i +1 =
7
( x1i ) + 2( x2i ) + 1
x3i +1 =
5
The results for 9 iterations are:
Unknowns
Iter. x1 x2 x3
1 1.833 0.714 0.200
2 2.038 1.181 0.852
3 2.085 1.053 1.080
4 2.004 1.001 1.038
. . . .
. . . .
9 2.000 1.000 1.000

Numerical Analysis / M. Sc. - 12 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
Example 1.7:
Solve the equations by Jacobi method
20x 1 + x 2 – 2x 3 = 17
3x 1 + 20x 2 – x 3 = –18
2x 1 – 3x 2 + 20x 3 = 25
Solution
Rewrite the given equation in the form:
1
x1i +1 = (17 − x2i + 3x3i )
20
1
x2i +1 = (−18 − 3x1i + x3i )
20
1
x3i +1 = (25 − 2 x1i + 3x2i )
20
Using x=
0
1
x=
0
2
x 30 = 0, we obtain
17
x=
1
1
= 0.85
20
−18
x12 = = −0.90
20
25
x=
1
3
= 1.25
20
Putting these values on the right of equations to obtain
(17 − x12 − 2x=3)
1
x=
2
1
1
1.02
20
1
(
x 22 = −18 − 3x11 + x13 =
20
) −0.965

x=3
2 1
20
( 25 − 2x11 + 3x12 =) 1.1515
These and further iterates are listed in the table below:
i x1i x2i x3i
0 0 0 0
1 0.85 –0.90 1.25
2 1.02 –0.965 1.1515
3 1.0134 –0.9954 1.0032
4 1.0009 –1.0018 0.9993
5 1.0000 –1.0002 0.9996
6 1.0000 –1.0000 1.0000

The values in 5th and 6th iterations being practically the same, we can stop. Hence the
solutions are:
x 1 = 1, x 2 = –1 and x 3 = 1

Numerical Analysis / M. Sc. - 13 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
1.6.2 Gauss-Seidel Method
If the given system of equation is diagonally dominant then
x (i +1) =
1
a1
(
d1 − b1 y (i ) − c1 z (i ) )
y (i +1) =
1
b2
(
d 2 − a2 x (i +1) − c2 z (i ) )
z (i +1) =
1
c3
(
d 3 − a3 x (i +1) − b3 y (i +1) )

Example 1.8:
Use the Gauss-Seidel method to obtain the solution of the following equations:
6x 1 -2 x 2 + x 3 = 11 (1)
x 1 +2 x 2 -5x 3 = -1 (2)
-2x 1 +7 x 2 +2x 3 = 5 (3)
Solution
Step 1: Re-write the equations such that each equation has the unknown with largest
`coefficient on the left hand side:
2 x − x + 11
x1 = 2 3 from eq. (1)
6
2 x − 2 x3 + 5
x2 = 1 from eq. (3)
7
x + 2 x2 + 1
x3 = 1 from eq. (2)
5
Step 2: Assume the initial guesses x20 = x30 = 0 , then calculate x11 :
2( x20 ) − ( x30 ) + 11 2(0) − (0) + 11
x11 = = =1.833
6 6
Use the updated value x11 = 1.833 and x30 = 0 to calculate x12
2( x11 ) − 2( x30 ) + 5 2(1.833) − 2(0) + 5
x12 = = =1.238
7 7
Similarly, use x11 = 1.833 and x12 = 1.238 to calculate x31
( x11 ) + 2( x12 ) + 1 (1.833) + 2(1.238) + 1
x31 = = =1.062
5 5

Step 3: Repeat the same procedure for the 2nd iteration


2( x12 ) − ( x31 ) + 11 2(1.238) − (1.062) + 11
x12 = = = 2.069
6 6
2( x12 ) − 2( x31 ) + 5 2(2.069) − 2(1.062) + 5
x22 = = =1.002
7 7

Numerical Analysis / M. Sc. - 14 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
( x12 ) + 2( x22 ) + 1 (2.069) + 2(1.002) + 1
x32 = = =1.015
5 5
and so on for the next iterations so that the next values are calculated using the
current values:
2( xi2 ) − ( x3i ) + 11
x1i +1 =
6
2( x1i +1 ) − 2( x3i ) + 5
xi2+1 =
7
( x1i +1 ) + 2( xi2+1 ) + 1
x3i +1 =
5
and continue the above iterative procedure until [(x k )i+1 - (x k )i]/ (x k )i+1 < Є for i=1,2 and
3.
The procedure yields the exact solution after 5 iterations only:
Unknown
Iter. x1 x2 x3
1 1.833 1.238 1.062
2 2.069 1.002 1.015
3 1.998 0.995 0.998
4 1.999 1.000 1.000
5 2.000 1.000 1.000

Example 1.9:
For the below figure calculate the values of the unknown flow rates F 1 , F 2 and F 3 by
using Gauss-Seidel Method
. F1=? F2=?
99% Benzene 5% Benzene
1% Toluene 92% Toluene
3 % Xylene
Tower 1

Tower 2

F=1000 kg/hr

40% Benzene
40% Toluene
20% Xylene
F3=?
10% Toluene
90% Xylene
Component material balance gives these three equations of three variables
0.99 F1 + 0.05 F2 + 0 F3 = 400
0.01F1 + 0.92 F2 + 0.1F3 = 400
0 F1 + 0.03F2 + 0.9 F3 = 200

Numerical Analysis / M. Sc. - 15 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
Re-arranging the above equations
F1 = (400 − 0.05F2 ) / 0.99
F2 = (400 − 0.01F1 − 0.1F3 ) / 0.92
F3 = (200 − 0.03F2 ) / 0.9
Starting with F1=F2=F3=1000/3
Iteration F1 F2 F3
333.3333 333.3333 333.3333
1.0000 387.2054 394.3420 209.0775
2.0000 384.1241 407.8815 208.6262
3.0000 383.4403 407.9380 208.6243
4.0000 383.4375 407.9383 208.6243
5.0000 383.4375 407.9383 208.6243

A Matlab program for solving the above equations using Gauss-Seidel method is
listed in Table 1.1
Table (1.1) Matlab code and results for solution example (1.9)
F1=333.33; F2=333.33; F3=333.33
for i=1:4
F1=(400-0.05*F2)/0.99;
Matlab F2=(400-0.01*F1-0.1*F3)/0.92;
Code F3=(200-0.03*F2)/0.9;
disp([ i, F1, F2, F3])
end
1.0000 387.2056 394.3423 209.0775
2.0000 384.1241 407.8815 208.6262
Results 3.0000 383.4403 407.9380 208.6243
4.0000 383.4375 407.9383 208.6243

1.7 Solving Linear Simultaneous Equations Using Gaussian Elimination Matlab


Command
There are a number of common situations in chemical engineering where systems
of linear equations appear. There are at least three ways in MATLAB to solve these
systems of equations. One of these methods is using matrix algebra commands (Also
called matrix inverse or Gaussian Elimination method).
Consider a set of n equations in which the unknowns are x 1 , x 2 ... x n .
The system of equations given above can be expressed in the matrix form as.
 a 11 a 12 a 13 : a 1n   x 1   b1 
a
 21 a 22 a 23 : :   x 2   b 2 
  
 : : : : : x 3  = b3 
     Or AX = b
 : : : : :  :   : 
a n1 a n2 a n3 : a nn   x n  b n 
Numerical Analysis / M. Sc. - 16 - Written by Assoc. Prof.
Chap. 1 Dr. Zaidoon M. Shakoor
Where:
 a 11 a 12 a 13 : a 1n 
 b1   x1 
a
 21 a 22 a 23 : a 2n  b  x 
 2  2
A= : : : : :  b = b3  X = x 3 
     
 : : : : : 
:   : 
a n1 a n2 a n3 : a nn  b n   x n 

To determine the variables contained in the column vector 'x', complete the
following steps.
(a) Create the coefficient matrix 'A'. Remember to include zeroes where an equation
does not contain a variable.
(b) Create the right-hand-side column vector 'b' containing the constant terms from
the equation. This must be a column vector, not a row.
(c) Calculate the values in the 'x' vector by left dividing 'b' by 'A', by typing x = A\b.
Note: this is different from x = b/A.

Example 1.10
Xylene, styrene, toluene and benzene are to be separated with the array of distillation
columns that is shown below. Write a program to calculate the amount of the streams
D, B, D1, B1, D2 and B2 also to calculate the composition of streams D and B.

Note: Solve the system of equations by using the matrix inverse method.
Solution
By making material balances on individual components on the overall

Numerical Analysis / M. Sc. - 17 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
separation train yield the equation set
Xylene: 0.07D1+ 0.18B1+ 0.15D2+ 0.24B2= 0.15× 70
Styrene: 0.04D1+ 0.24B1+ 0.10D2+ 0.65B2= 0.25× 70
Toluene: 0.54D1+ 0.42B1+ 0.54D2+ 0.10B2= 0.40× 70
Benzene: 0.35D1+ 0.16B1+ 0.21D2+ 0.01B2= 0.20× 70
Overall material balances and individual component balances on column 2 can be
used to determine the molar flow rate and mole fractions from the equation of stream
D.
Molar Flow Rates: D = D1 + B1
Xylene: XDxD = 0.07D1 + 0.18B1
Styrene: XDsD = 0.04D1 + 0.24B1
Toluene: XDtD = 0.54D1 + 0.42B1
Benzene: XDbD = 0.35D1 + 0.16B1
Where:
XDx = mole fraction of Xylene.
XDs = mole fraction of Styrene.
XDt = mole fraction of Toluene.
XDb =mole fraction of Benzene.
Similarly, overall balances and individual component balances on column 3 can be
used to determine the molar flow rate and mole fractions of stream B from the
equation set.
Molar Flow Rates: B = D2 + B2
Xylene: XBxB = 0.15D2 + 0.24B2
Styrene: XBsB = 0.10D2 + 0.65B2
Toluene: XBtB = 0.54D2 + 0.10B2
Benzene: XBbB = 0.21D2 + 0.01B2
Where: F, D, B, D1, B1, D2 and B2 are the molar flow rates in mol/min.
A Matlab program for solving the above equations using Gaussian-Elimination
method is listed in Table 1.2
Table (1.2) Matlab code and results for solution example (1.10)
A=[0.07, 0.18, 0.15, 0.24; 0.04, 0.24, 0.10, 0.65; 0.54, 0.42, 0.54, 0.1;0.35,
0.16, 0.21, 0.01];
B=[0.15*70; 0.25*70; 0.4*70; 0.2*70];
X=A\B;
D1=X(1),B1=X(2),D2=X(3),B2=X(4)
Matlab D=D1+B1
Code B=D2+B2
XDx=(0.07*D1+0.18*B1)/D
XDs=(0.04*D1+0.24*B1)/D
XDt=(0.54*D1+0.42*B1)/D
XDb=(0.35*D1+0.16*B1)/D
Numerical Analysis / M. Sc. - 18 - Written by Assoc. Prof.
Chap. 1 Dr. Zaidoon M. Shakoor
XBx=(0.15*D2+0.24*B2)/B
XBs=(0.1*D2+0.65*B2)/B
XBt=(0.54*D2+0.1*B2)/B
XBb=(0.21*D2+0.01*B2)/B
D1 =
26.2500
B1 =
17.5000
D2 =
8.7500
B2 =
17.5000
D=
43.7500
B=
26.2500
XDx =
0.1140
Results XDs =
0.1200
XDt =
0.4920
XDb =
0.2740
XBx =
0.2100
XBs =
0.4667
XBt =
0.2467
XBb =
0.0767

Example 1.11
Balance the following chemical equation:
x 1 P 2 I 4 + x 2 P 4 + x 3 H2 O → x 4 PH 4 I + x 5 H 3 PO 4
R R R R R R R R R R R R R R R R R R R R R R R

USolution:
P balance: 2x 1 + 4x 2 =x 4 + x 5 R R R R R R R R

I balance: 4x 1 =x 4 + x 5 R R R R R R

H balance: 2x 3 =4x 4 +3x 5 R R R R R R

O balance: x 3 =4x 5 R R R R

Re-write these as homogeneous equations, each having zero on its right hand
side:

Numerical Analysis / M. Sc. - 19 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
2x 1 + 4x 2 - x 4 - x 5 = 0
4x 1 - x 4 - x=0
2x 3 - 4x 4 - 3x 5 =0
x 3 - 4x 5 = 0
At this point, there are four equations in five unknowns. To complete the system,
we define an auxiliary equation by arbitrarily choosing a value for one of the
coefficients:
x1= 1
We can easily solve the above equations to balance this reaction using MATLAB
such in table 1.3

Table (1.3) Matlab code and results for solution example (1.11)
A = [2 4 0 -1 -1
4 0 0 -1 0
0 0 2 -4 -3
Matlab
0 0 1 0 -4
Code
1 0 0 0 0];
B= [0;0;0;0;1];
X = A\B
X=
1.0000
Results 1.3000
12.8000
4.0000
3.2000
This does not yield integral coefficients, but multiplying by 10 will do the trick:
The balanced equation will be:
10 P 2 I 4 + 13 P 4 + 128 H 2 O → 40 PH4 I + 32 H 3 PO 4

Example 1:12
Steady state mass balances on a flash tank.
Consider an isothermal flash tank:
Vi(y)
Fi(Z)
Isothermal
flash tank Li(x)

This unit takes a pressurized liquid, three-component feed stream and exposes it to
a low-pressure vessel maintained under isothermal conditions. The net result is that

Numerical Analysis / M. Sc. - 20 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
some of the fluid is vaporized, while some fluid remains liquid. The compositions of
the liquid and vapor phase are determined by the combined analysis of mass balances
and Raoult’s Law for vapor-liquid equilibrium. The temperature in the flash tank is T
= 298 K and the pressure in the tank is P= 101 kPa. Raoult’s Law states that the
product of the liquid mole fraction of component i and the vapor pressure of
component i is equal to the partial pressure of component i in the vapor phase:
xi Pi vap = yi P
Use the following data for the temperature given above
PAVap =0.6 bar at 298 K
PBVap =1.0 bar at 298 K
PCVap =2.9 bar at 298 K
F=100 mol/hr V=44.738 mol/hr L=F-V mol/hr
ZA=0.4 y A =? x A =?
ZB=0.3 yB =? x B =?
ZC=0.3 yC =? x C =?
Using MATLAB solve for steady state values of the unknowns.

Solution:
Then you have six unknowns, the compositions of the liquid stream and the
composition of the vapor stream and you have eight available equations, which are.
A mole balance: Lx A +Vy A =Fz A
B mole balance: Lx B +VyB =Fz B (not used dependent)
C mole balance: Lx C +VyC =Fz C (not used dependent)
Liquid mole fraction constraint x A +x B +x C =1
Vapor mole fraction constraint y A +yB +y C =1
A equilibrium constraint x A PAVap -y A P=0
B equilibrium constraint x B PBVap -y B P=0
C equilibrium constraint x C PCVap -y C P=0
R R

Put equations in matrix form


Matrix of coefficients A (6 ×6)

eqn xAR xBR xCR yA


R yB
R yCR

1 L 0 0 V 0 0
2 1 1 1 0 0 0
3 0 0 0 1 1 1
4 PAVap 0 0 P 0 0
5 0 PBVap 0 0 -P 0
6 0 0 PCVap 0 0 -P

Numerical Analysis / M. Sc. - 21 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
Vector of right hand side b(6×1)
Eqn b
1 Fz A R

2 1
3 1
4 0
5 0
6 0
A Matlab program for solving the above equations using Gaussian-Elimination
method is listed in Table 1.4

Table (1.4) Matlab code and results for solution example (1.12)
F = 100; V = 44.738; L = F - V; zA = 0.4; zB = 0.3; zC = 0.3; PvapA = 0.6;
PvapB = 1.0;PvapC = 2.0;P = 1.01325;
A = [L, 0,0,V,0,0;1,1,1,0,0,0;0,0,0,1,1,1;PvapA,0,0,-P,0,0;0,PvapB, 0,0,-P,0;
Matlab 0,0,PvapC,0,0,-P]
Code b =[F*zA; 1; 1; 0; 0; 0]
x = A\b;
xA = x(1),xB = x(2),xC = x(3),yA = x(4),yB = x(5),yC = x(6)
xA =
0.4893
xB =
0.3018
xC =
0.2090
Results yA =
0.2897
yB =
0.2978
yC =
0.4125

Example 1.13
Steady state mass balances on a single-stage liquid-liquid extractor.
Extract
E , (XEb, XEC, XEf) Solvent
S , (Xsb, XsC, Xsf)
Extractor

Feed Raffinate
F , (XFb, XFC, XFf) R , (XRb, XRC, XRf)

Numerical Analysis / M. Sc. - 22 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
This unit removes uses a recycled furfural stream as the solvent to extract benzene
from a cyclohexane product stream. The data you are given is:
F=100 mol/hr S=150 mol/hr R=95 mol/hr E=155 mol/hr
X Fb =0.1
R R X Sb =0.0010
R R X Rb =? R R X Eb =? R R

X Fc =0.9
R R X Sc =0.0001
R R X Rc =? R R X Ec =? R R

X Ff =0.0
R R X Sf =0.9989
R R X Rf =? R R X Ef =? R R

The equilibrium constants are:


X Eb X Ec
Kb = = 20.0 and KC = = 0.05
X Rb X Rc
Using MATLAB solve for steady state values of the unknowns.
Solution:
You have six unknowns, the compositions of the raffinate and the compositions of
the extract stream. In addition, you have seven independent equations, which are
Benzene mole balance RX Rb +EX EB =FX Fb +SX Sb R R R R R R R

Cyclohexane mole balance RX Rc +EX Ec =FX Fc +SX S c R R R R R R R R

furfural mole balance RX Rf +EX Ef =FX Ff +SX Sf (not used R R R R R R R R

dependent)
raffinate mole fraction constraint X Rb +X Rc +X Rf =1 R R R R R R

Extract mole fraction constraint X Eb +X Ec +X Ef =1 R R R R R R

Benzene equilibrium constraint X Eb -X Rb K b =0 R R R R R R

Cyclohexane equilibrium constraint X Ec -X Rc K c =0 R R R R R R

Note: you have six variables and seven equations therefore; you have to choice any
six equations from these equations. In this case, we neglect mole balance equation.
Put the equations in matrix form.

Matrix of coefficients A (6 ×6)


Eqn X Rb R X Rc R X Ef R X Eb R X EcR X Ef
R

1 R 0 0 E 0 0
2 0 R 0 0 E 0
3 1 1 1 0 0 0
4 0 0 0 1 1 1
5 -Kb 0 0 1 0 0
6 0 -Kc 0 0 1 0

Vector of right hand side b (6×1)

Numerical Analysis / M. Sc. - 23 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
Eqn. B
1 FX Fb +SX Sb
2 FX Fc +SX Sc
3 1
4 1
5 0
6 0
A Matlab program for solving the above equations using Gaussian-Elimination
method is listed in Table 1.5

Table (1.5) Matlab code and results for solution example (1.13)
F=100;S=150;R=95;E=155;
Kb=20;Kc=0.05;XFb=.1;XFc=.9;XFf=0;
XSb=.001;XSc=.0001;XSf=.9989;
Matlab A=[R,0,0,E,0,0;0,R,0,0,E,0;1,1,1,0,0,0;0,0,0,1,1,1;-Kb,0,0,1,0,0;0,-Kc,0,0,1,0];
Code B=[F*XFb+S*XSb;F*XFc+S*XSc;1;1;0;0];
X=A\B;
XRb=X(1),XRc=X(2),XRf=X(3),XEb=X(4),XEc=X(5),XEf=X(6)
XRb =
0.0032
XRc =
0.8761
XRf =
0.1208
Results XEb =
0.0635
XEc =
0.0438
XEf =
0.8927

Numerical Analysis / M. Sc. - 24 - Written by Assoc. Prof.


Chap. 1 Dr. Zaidoon M. Shakoor
Chapter 2: Solution of Non-Linear Equations

2.1 Solutions of Non-Linear Equations in One Variable (Root Finding)

2.1.1 Graphical Methods


A simple method for obtaining a root of the equation f(x) = 0 is to plot the function
and observe where it crosses the x axis. There is much available software that will
facilitate making a plot of a function. We will use Matlab exclusively for the course
notes; however you can use other software such as Excel or Matcad for your work.

Example 2.1
(1 − e−0.15x) =50 using the graphical method.
600
Solve
x
Solution
(1 − e−0.15x) − 50 can be plotted in Figure 2.1 using the
600
The function f(x) =
x
Matlab statements listed in table (2.1).

Table (2.1) Matlab code for solving example (2.1) using graphical method
x=4:0.1:20;
fx=600*(1-exp(-0.15*x))./x-50;
Matlab plot(x,fx,[0 20],[0 0])
Code xlabel('x');
ylabel('f(x)')
grid on; zoom on

20

15

10

0
f(x)

-5

-10

-15

-20

-25
0 2 4 6 8 10 12 14 16 18 20
x

Figure 2.1 The graphical method for roots finding.

Numerical Analysis / M. Sc. - 25 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
The Matlab Zoom on statement allows the function to be zoomed in at the cursor
with left mouse click (right mouse click will zoom out). Each time you click the axes
limits will be changed by a factor of 2 (in or out). You can zoom in as many times as
necessary for the desired accuracy. Figure 2.2 shows the approximate root x to be
8.79

0.06

0.04

0.02

0
f(x)

-0.02

-0.04

-0.06

-0.08

8.75 8.76 8.77 8.78 8.79 8.8 8.81 8.82


x

Figure 2.2 The graphical method for roots finding with Matlab Zoom on.

The plot of a function between x 1 and x 2 is important for understanding its behavior
within this interval. More than one root can occur within the interval when f(x 1 ) and
f(x 2 ) are on opposite sides of the x axis. The roots can also occur within the interval
when f(x 1 ) and f(x 2 ) are on the same sides of the x axis. Since the functions that are
tangent to the x axis satisfy the requirement f(x) = 0 at this point, the tangent point is
called a multiple root.

2.1.2 The Bisection Method


The bisection method or interval halving can be used to determine the solution to f(x)
= 0 on an interval [x 1 = a, x 2 = b] if f(x) is real and continuous on the interval and
f(x 1 ) and f(x 2 ) have opposite signs. We assume for simplicity that the root in this
interval is unique. The location of the root is then calculated as lying at the midpoint
of the subinterval within which the functions have opposite signs. The process is
repeated to any specified accuracy.
The procedure can be summarized in the following steps

Numerical Analysis / M. Sc. - 26 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
Let f(x 1 ) f(x 2 ) < 0 on an interval [x 1 = a, x 2 = b]
1
Step 1 Let x x = (x 1 + x 2 ); f 1 = f(x 1 ); f 2 = f(x 2 )
2
Step 2 Evaluate f x = f(x x )
If f x f 1 > 0 then
x1 = xx; f1 = fx
else
x2 = xx; f2 = fx
end
Step 3 If abs(x 2 − x 1 ) > an error tolerance, go back to Step 1

Figure 2.3 shows first three iterations x 3 , x 4 , and x 5 of the bisection method.

15

10

x5 x3 x2=b
f(x)

0
x1 = a x4

-5

-10

-15
4 6 8 10 12 14 16
x

Figure 2.3 The first three iterations x 3 , x 4 , and x 5 of the bisection method.

x 1 =6 ⇒ f(x 1 ) =9.3430 and x 2 =14 ⇒ f(x 2 ) = -12.3910


1 1
x3 = (x 1 + x 2 ) = (6 + 14) = 10 ⇒ f(x 3 ) = -3.3878
2 2
1 1
f(x 1 ) f(x 3 ) < 0 ⇒ x 4 = (x 1 + x 3 ) = (6 + 10) = 8 ⇒ f(x 4 ) =2.4104
2 2
1 1
f(x 3 ) f(x 4 ) < 0 ⇒ x 5 = (x 3 + x 4 ) = (10 + 8) = 9 ⇒ f(x 4 ) = -0.6160
2 2

Numerical Analysis / M. Sc. - 27 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
1 1
Since f(x 1 ) and f(x 2 ) bracket the root and x 3 = (x 1 + x 2 ) = (a + b), the error after
2 2
1
the first iteration is less than or equal to (b − a).
2
A Matlab program for solving example 2.1 using bisection method is listed in Table
2.2 where the function f(x) is an input to the program.

Table (2.2) Matlab code and results for solving example (2.1) using bisection method
fun=inline('600*(1-exp(-0.15*x)) /x-50')
x1=6;
f1= fun (x1);
x2=14;
f2= fun (x2);
tol=1e-5;
for i=1:100
x3=(x1+x2)/2;
f3= fun(x3);
Matlab
if f1*f3<0
Code
x2=x3;
f2=f3;
else
x1=x3;
f1=f3;
end
if abs(x2-x1)<tol; break;end
end
x3
x3 =
Results
8.7892
The statement Inline is used to define the function at a given value of x.

Example 2.2
Use the bisection method to find the root of the equation x-cos(x) = 0 with a percent
relative error |ε t |≤ 1%. (The exact value is 0.7391)
Solution
f(x)= x-cos(x)

Numerical Analysis / M. Sc. - 28 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
We have seen before that there is a single root lies in the interval [0,1]. Therefore, we
start with x l =0 and x 2 =1, then iterate using the same procedure followed in example
2.1 to get the following tabulated results:
Iter Xl X2 X3 f(X l ) f(X 2 ) f(X 3 ) |ε t |
1 0.0 1.0 0.5 -1.0000 0.4597 -0.3776 32.35
2 0.5 1.0 0.75 -0.3776 0.4597 0.0183 1.48
3 0.5 0.75 0.625 -0.3776 0.0183 -0.1860 15.44
4 0.625 0.75 0.6875 -0.1860 0.0183 -0.0853 6.98
5 0.6875 0.75 0.7188 -0.0853 0.0183 -0.0339 2.75
6 0.7188 0.75 0.7344 -0.0339 0.0183 -0.0079 0.64
Then x=0.7344

Example 2.3
The friction factor f depends on the Reynolds number Re for turbulent flow in smooth
pipe according to the following relationship.
1
= −0.40 + 3 ln(Re f )
f
Use the bisection method to compute f for Re = 25200 that lies between [0.001 ,0.1 ].

Solution
Re-write the above equation in the form
1
E ( f ) = −0.40 + 3 ln(25200 f ) −
f
Iter fl f2 f3 E(f l ) E(f 2 ) E(f 3 )
1 0.0010 0.1000 0.0505 -20.4514 11.9973 10.1179
2 0.0010 0.0505 0.0258 -20.4514 10.1179 7.7528
3 0.0010 0.0258 0.0134 -20.4514 7.7528 4.7705
4 0.0010 0.0134 0.0072 -20.4514 4.7705 1.0841
5 0.0010 0.0072 0.0041 -20.4514 1.0841 -3.2373
6 0.0041 0.0072 0.0056 -3.2373 1.0841 -0.6453
7 0.0056 0.0072 0.0064 -0.6453 1.0841 0.2946
8 0.0056 0.0064 0.0060 -0.6453 0.2946 -0.1536
Then according to above table f = 0.006

Numerical Analysis / M. Sc. - 29 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
2.1.3 Secant Method (Linear Interpolation Method)
The bisection method is generally inefficient, it requires more function evaluations in
comparison with the secant method which is linear interpolation using the latest two
points. Figure 2.4 shows graphically the root x 3 obtained from the intersection of the
line AB with the x-axis.
f(x)
f(x2) B

x1 x3 x4
x5 x2

A
f(x1)

Figure 2.4 Graphical depiction of the secant method.

The intersection of the straight line with the x-axis can be obtained by using similar
triangles x 3 x 1 A and x 3 x 2 A or by using linear interpolation with the following
points.
x x1 x3 x2
f(x) f(x 1 ) 0 f(x 2 )
x3 − x 2 0 − f ( x2 ) x2 − x1
= ⇒ x 3 = x 2 − f(x 2 )
x2 − x1 f ( x 2 ) − f ( x1 ) f ( x2 ) − f ( x1 )
The next guess is then obtained from the straight line through two points [x 2 , f(x 2 )]
and [x 3 , f(x 3 )]. In general, the guessed valued is calculated from the two previous
points [x n-1 , f(x n-1 )] and [x n , f(x n )] as

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

The secant method always uses the latest two points without the requirement that they
bracket the root as shown in Figure 2.4 for points [x 3 , f(x 3 )] and [x 4 , f(x 4 )]. As a
consequence, the secant method can sometime diverge. A Matlab program for

Numerical Analysis / M. Sc. - 30 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
solving example 2.1 using secant method is listed in Table 2.3
Table (2.3) Matlab code and results for solving example (2.1) using secant method
fun=inline('600*(1-exp(-0.15*x))/x-50')
tol=1e-5;
x(1)=1;
f(1)= fun(x(1));
x(2)=14;
f(2)= fun(x(2));
Matlab for i=2:20
Code x(i+1)=x(i)-f(i)*(x(i)-x(i-1))/(f(i)-f(i-1))
f(i+1)= fun(x(i+1));
if abs(x(i+1)-x(i))<tol;
break;
end
end
x
x=
1.0000
14.0000
10.4956
8.3724
Results
8.8209
8.7897
8.7892
8.7892
The last value of x vector is the solution of the equation

Example 2.4
Use secant method to estimate the root of f(x)=e-x-x. with the two initial guesses x 0 =
0. and x 1 = 1.
Solution:
Iter x f(x)
Starting 0 1.0000
Values 1.0 -0.6321
1 0.6127 -0.0708
2 0.563838 0.00518
3 0.56717 -0.0000418

Numerical Analysis / M. Sc. - 31 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
Example 2.5
Repeat Examples 2.4 using the secant method with the two initial guesses x 0 = 2. and
x 1 = 3.
Solution:
Iter x f(x)
Starting 2 -1.8647
Values 3 -2.9502
1 0.2823 0.4718
2 0.6570 -0.1385
3 0.5719 -0.0075
4 0.5671
This method converges with the required accuracy after 5 iterations.

Example 2.6
Use secant method with initial guesses T = 300 and T = 350 to calculate the bubble
point of binary system (VCM 18 mol%, Water 82 mol%). The vapor pressure for this
components is calculated by:
VCM Po vcm =exp(14.9601-1803.84/(T-43.15))
Water Po w =exp(18.3036-3816.44/(T-46.13))
Where: K i = P o i /P t
P t =760
y i =K i ×x i
At Bubble point ∑y i =∑K i ×x i =1
Solution
1803.84 3816.44
(14.9601- ) 0.18 (18.3036 - T - 46.13 ) 0.82
f(T)= K i ×x i -1 = e T - 43.15
× +e × -1
760 760
Iter T f(T)
Initial 300.0000 -0.3086
Values 350.0000 1.4192
1 308.9299 -0.1132
2 311.9636 -0.0378
3 313.4866 0.0018
4 313.4157 -0.000028
Then at bubble point T=313.457 K

Numerical Analysis / M. Sc. - 32 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
2.1.4 The Newton-Raphson Method
The Newton-Raphson method and its modification is probably the most widely used
of all root-finding methods. Starting with an initial guess x 1 at the root, the next guess
x 2 is the intersection of the tangent from the point [x 1 , f(x 1 )] to the x-axis. The next
guess x 3 is the intersection of the tangent from the point [x 2 , f(x 2 )] to the x-axis as
shown in Figure 2.5. The process can be repeated until the desired tolerance is
attained.

f(x)
f(x1) B

x3 x2 x1

Figure 2.5 Graphical depiction of the Newton-Raphson method.

The derivative or slope f(x n ) can be approximated numerically as


f ( xn + ∆x ) − f ( xn )
f’(x n ) =
∆x

The Newton-Raphson method can be derived from the definition of a slope


f ( x1 ) − 0 f ( x1 )
f’(x 1 ) = ⇒ x2 = x1 −
x1 − x2 f ' ( x1 )

In general, from the point [x n , f(x n )], the next guess is calculated as
f ( xn )
x n+1 = x n −
f ' ( xn )

A Matlab program for solving example 2.1 using Newton- Raphson method is listed
in Table 2.4 .

Numerical Analysis / M. Sc. - 33 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
Table (2.4) Matlab code and results for solving example (2.1) using Newton-
Raphson method
f=inline('600*(1-exp(-0.15*x))/x-50');
df=inline('(90.0*exp(-0.15*x))/x + (600*exp(-0.15*x) - 600)/x^2');
tol=1e-5;
x(1)=1;
for i=2:20
Matlab
x(i+1)=x(i)-f(x(i))/df(x(i));
Code
if abs(x(i+1)-x(i))<tol;
break;
end
end
x'
x=
1.0000
0.0031
5.9278
Results 8.4473
8.7840
8.7892
8.7892
The last value of x vector is the solution of the equation

Example 2.7
Use Newton-Raphson method to estimate the root of f(x)=e-x-x. Show all details of
the iterations. Hint: the root is located between 0 and 1.
Solution:
Iter Xi f(X i ) f’(X i ) X i+1 |ε a (%)|
1 0.0 1.0 -2.0 0.5 100.00
2 0.5 0.1065 -1.6065 0.5663 11.71
3 0.5663 0.0013 -1.5676 0.5671 0.15
4 0.5671 0.0000 -1.5676 0.5671 0.00

Example 2.8
Repeat Example 2.7 starting with x o = 5.
Solution:
Iter Xi f(X i ) f’(X i ) X i+1 |ε a (%)|
1 5.0 -4.9933 -1.0067 0.04016 12351
2 0.04016 0.92048 -1.9606 0.5096 92.12
3 0.5096 0.0911 -1.6007 0.5665 10.04
Numerical Analysis / M. Sc. - 34 - Written by Assoc. Prof.
Chap. 2 Dr. Zaidoon M. Shakoor
4 0.5665 0.0010 -1.5675 0.5671 0.000

Example 2.9
Apply Newton-Raphson method to solve Redlich-Kwong equation which used to
estimate the molar volume of saturated vapor of methyl chloride at 333.15 K and
13.76 bar
RT a
P= − 0.5
V − b T V (V + b)
If you know that:-
A=1.5651×108 cm6 bar mol-2 K1/2
b=44.891 cm3 mol-1
R=83.14 cm 3.bar.K-1.mol-1

Solution
RT a
f (V ) = − 0.5 −P
V − b T V (V + b)
83.14 × 333.15 1.5651 × 108
f (V ) = − − 13.76
V − 44.891 333.150.5V (V + 44.891)
27698 8574764.131
f (V ) = − − 13.76
V − 44.891 V (V + 44.891)
- 27698 8574764.131 8574764.131
f ′(V) = + + 2
(V − 44.891) 2
V(V + 44.891) 2
V (V + 44.891)
It’s better to start with ideal molar volume as initial value of V
RT 83.14 × 333.15
V= = = 2012.943
P 13.76
Iter Vi f(V i ) f’(V i ) V i+1
1 2012.943 -1.75623 -0.00512 1669.717
2 1669.717 0.291628 -0.00695 1711.673
3 1711.673 0.005731 -0.00668 1712.531
4 1712.531 0.00000231 -0.00667 1712.531
The molar volume equal to 1712.531 cm3 mol-1

Numerical Analysis / M. Sc. - 35 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
2.2 Solving System of Non-Linear Systems of Equations
A nonlinear system of equations has at least one equation which is not first degree.

Examples: x2 + y2 = 25 y = 3x2 – 4x + 2 xy = 9
2x + 3y = 7 x2 + y = 8 3x2 – y2 = 12
The solutions of a nonlinear system are the points of intersection of the graphs of the
equations. Some systems have one point of intersection; some have more than one
point of intersection; and some have no points of intersection.

2.2.1 Analytical Methods for Solving Systems of Equations


Solutions of nonlinear systems of equations can be found using the substitution or the
elimination method. The substitution method is preferable for a system with one
linear equation. The elimination method is preferable in most, but not all, cases when
both equations are nonlinear.

2.2.1.1. The Substitution Method


Solve one of the equations for a first degree variable. Substitute the resulting
expression in for that variable in the other equation. Solve for the remaining variable.
Back substitute to find the value(s) of the first variable. Write your solutions as
ordered pairs.

Example 2.10
Solve x2 + 2x = y + 6
x + y = –2
Solution
x + y = –2 → y = –2 – x → x2 + 2x = –2 – x + 6 →
x2 + 3x – 4 = 0 → (x + 4) (x – 1) = 0 → x = –4 & x = 1
y = –2 – (–4) = 2 & y = –2 – 1 = –3 →

Solution: (–4, 2) (1, –3)

Numerical Analysis / M. Sc. - 36 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
To check graphically, enter y 1 = x2 + 2x – 6 & y2 = –2 – x
Find the points of intersection.

2.2.1.2. The Elimination Method

Line the equations up vertically so like terms are underneath each other. If needed,
multiply each equation by a number so that when the equations are added together one
of the variables is eliminated. Solve for the remaining variable. Back substitute to find
the value(s) of the eliminated variable. Write your solutions as ordered pairs.

Example 2.11
Solve 3x2 + 5y2 = 17
2x2 – 3y2 = 5
Solution:
3x2 + 5y2 = 17 3R 1 9x2 + 15y2 = 51

2x2 – 3y2 = 5 5R 2 → 10x2 – 15y2 = 25


19x2 = 76
x2 = 4 → x = –2 & x = 2
2 2
3(–2) + 5y = 17 → 12 + 5y2 = 17 →

5y2 = 5 → y2 = 1 → y = –1 & y=1


3(2)2 + 5y2 = 17 → 12 + 5y2 = 17 →

5y2 = 5 → y2 = 1 → y = –1 & y=1


Solutions: (–2, –1) (–2, 1) (2, –1) (2, 1)

2.2.2 Numerical Methods for Solving Systems of Equations


2.2.2.1 Fixed point iteration for systems of non-linear equations
Using an initial guess, solve for each variable in the system and use fixed-point
iteration to estimate the solution.
One of the most important drawbacks of the fixed iteration method is that the
convergence of the method is dependent on how the equations are formulated. May
diverge quickly, in that case try solving for the variables in a different way.

It can be shown that sufficient convergence criteria for two equations are:
∂f1 ∂f
+ 1 <1
∂x1 ∂x2

Numerical Analysis / M. Sc. - 37 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
And
∂f 2 ∂f 2
+ <1
∂x1 ∂x2
This represents a very restrictive criteria and that’s why fixed point iteration method
is not used to solve systems of non-linear equations.

Example 2.12
Solve using 5 iteration of successive substitution where x = y = 1.5 initially:
 x 2 + y 2 = 5

 y − x 2 = −1

Solution

Using x = 5 − y 2 and y = x2 – 1 it is easy to show the following iterations:

Iteration xn yn
0 1.5 1.5
1 1.658 1.75
2 1.392 0.9375
3 2.030 3.121
4 Non-real Non-real
5
So it is apparent that successive substitution this will not work using these formulas.

Using y = 5 − x 2 and x = 1 + y we can just as easily show the following iterations:

Iteration xn yn
0 1.5 1.5
1 1.5811 1.5811
2 1.607 1.555
3 1.599 1.564
4 1.601 1.561
5 1.600 1.562
 1 + 17 − 1 + 17 
Which is rapidly converging on the true solution of  , 
 2 2 
 
As with the Jacobi iterative process, convergence is assured only for a system of
Numerical Analysis / M. Sc. - 38 - Written by Assoc. Prof.
Chap. 2 Dr. Zaidoon M. Shakoor
diagonally dominant linear equations. For systems that are neither linear nor
diagonally dominant, convergence is a function of the equations themselves as well
as the values of x’s chosen to start the iterations.

Example 2.13
As an example of applying the Jacobi method to a system of non-linear equations,
consider the following system:
4 x1 − x 2 + x 3 =7
4x1 −8( x 2 ) + x 3 =−21
2

−2x1 + x 2 +5( x 3 ) =15


2

Solving the equations for each of the unknowns (x’s), we have the following:
 7+ x 2 − x 3
2

x1 = 
 4 
−21−4x1 − x 3
x2 =
−8
15+2x1 − x 2
x3 =
5
Using these relationships in a Jacobi algorithm with starting values of x 1 = x 2 = x 3 =
0, we can show the convergence of the algorithm over ten iterations in the following
table:
Itr x1 x2 x3
0 0 0 0
1 3.0625 1.620185 1.732051
2 2.9654 2.091114 1.975086
3 3.164866 2.086764 1.941118
4 3.191267 2.109519 1.961783
5 3.193133 2.113257 1.963314
6 3.195105 2.113523 1.963314
7 3.195343 2.113757 1.963501
8 3.195385 2.113790 1.963514
9 3.195403 2.113796 1.963516
10 3.195406 2.113798 1.963518

Numerical Analysis / M. Sc. - 39 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
2.2.2.2 Newton-Raphson for solving systems of non-linear equations
The Newton-Raphson formula is the following:
f ( xi )
xi +1 = xi −
f ' ( xi )
This formula can be obtained using Taylor series expansion. We can do the same
approach for a system of equations, but considering a Taylor series that account for
the presence of both variables:
∂f1(i ) ∂f1(i )
f1(i +1) = f1(i ) + (x1(i +1) − x1(i ) ) + (x2(i +1) − x2(i ) ) + ...
∂x1 ∂x2
and
∂f 2(i ) ∂f 2(i )
f 2(i +1) = f 2(i ) + (x1(i +1) − x1(i ) ) + (x2(i +1) − x2(i ) ) + ...
∂x1 ∂x2

For the root estimate f1(i+1) and f 2(i+1) must be equal zero.

Therefore:
∂f1(i ) ∂f ∂f ∂f
x1(i+1) + 1(i ) x2 (i+1) = − f1(i ) + x1(i ) 1(i ) + x2(i ) 1(i )
∂x1 ∂x2 ∂x1 ∂x2
and
∂f 2(i ) ∂f ∂f ∂f
x1(i+1) + 2(i ) x2(i+1) = − f 2(i ) + x1(i ) 2(i ) + x2(i ) 2(i )
∂x1 ∂x2 ∂x1 ∂x2

Finally:
∂f 2 (i ) ∂f1(i )
f1(i ) − f 2 (i )
∂x 2 ∂x 2
x 1(i +1) = x 1(i ) −
∂f1(i ) ∂f 2 (i ) ∂f1(i ) ∂f 2 (i )

∂x 1 ∂x 2 ∂x 2 ∂x 1
∂f1(i ) ∂f 2 (i )
f 2 (i ) − f1(i )
∂x 1 ∂x 1
x 2 (i +1) = x 2 (i ) −
∂f1(i ) ∂f 2 (i ) ∂f1(i ) ∂f 2 (i )

∂x 1 ∂x 2 ∂x 2 ∂x 1

Which is an iterative method to solve the system of nonlinear equations.

Note also that the Newton-Raphson method can be generalized to solve N


simultaneous equations.

Numerical Analysis / M. Sc. - 40 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
Example 2.14
Solve the following system using Newton-Raphson method:
x 2 + y 2 − 8x − 4 y + 11 = 0
x 2 + y 2 − 20 x + 75 = 0

By tacking a starting point as (x=2; y=4) and ε = 10−5 .


Solution
∂f 2 (i ) ∂f1(i )
f 1(i ) − f 2 (i )
∂y ∂y
x ( i +1 ) = x ( i ) −
∂f1(i ) ∂f 2 (i ) ∂f1(i ) ∂f 2 (i )

∂x ∂y ∂y ∂x
∂f1(i ) ∂f 2 (i )
f 2 (i ) − f1(i )
y ( i +1 ) = y ( i ) − ∂x ∂x
∂f1(i ) ∂f 2 (i ) ∂f1(i ) ∂f 2 (i )

∂x ∂y ∂y ∂x

Let f1 = x 2 + y 2 − 8x − 4 y + 11 and f 2 = x 2 + y 2 − 20x + 75


∂f1,i ∂f1,i ∂f 2,i ∂f 2,i
Thus: = 2 x − 8, = 2 y − 4, = 2 x − 20, and = 2y
∂x ∂y ∂x ∂y
Hence when x =2 and y = 4 we find that:
∂f1, 0 ∂f1, 0 ∂f 2, 0
= 2(2) − 8 = −4, = 2(4) − 4 = 4, = 2(2) − 20 = −16, and
∂x ∂y ∂x
∂f 2, 0
= 2(4) = 8
∂y

Also f1, 0 = 22 + 42 − 8(2) − 4(4) + 11 = −1 and f 2, 0 = 22 + 42 − 20(2) + 75 = 55


∂f1, 0 ∂f 2, 0 ∂f1, 0 ∂f 2, 0
− = (−4)(8) − (4)(−16) = 32
∂x ∂y ∂y ∂x
So for the first iteration we see that:
(−1)(8) − (55)(4)
x1 = 2 − ≈ 9.1250
32
(55)(−4) − (−1)(−16)
y1 = 4 − ≈ 11.3750
32

x 0 − x1 2 − 9.125
ε= = = 3.5625
x0 2

Now we find that iteration 2 produces:

Numerical Analysis / M. Sc. - 41 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
∂f1,1 ∂f1,1
= 2(9.1250) − 8 = 10.25, = 2(11.375) − 4 = 18.75,
∂x ∂y
∂f 2,1 ∂f 2,1
= 2(9.1250) − 20 = -1.75, and = 2(11.375) = 22.75
∂x ∂y

Also f1,1 = 9.1252 + 11.375 2 − 8(9.125) − 4(11.375) + 11 = 105.1563 and

f 2,1 = 9.1252 + 11.375 2 − 20(9.125) + 75 = 105.1563

∂f1,1 ∂f 2,1 ∂f1,1 ∂f 2,1


− = (10.25)(22.75) − (18.75)(−1.75) = 266
∂x ∂y ∂y ∂x
So for the second iteration we see that:
(105.1563)(22.75) − (105.1563)(18.75)
x 2 = 9.125 − ≈ 7.543703
266
(105.1563)(10.25) − (105.1563)(-1.75)
y 2 = 11.375 − ≈ 6.631109
266

x 0 − x1 9.125 − 7.543703
ε = = = 0.173293
x0 9.125

∂f1 ∂f1 ∂f 2 ∂f 2
Itr f1 f2 x y εs
∂x ∂y ∂x ∂y
2 4
1 -1 55 -4 4 -16 8 9.125 11.375 3.5625
2 105.1563 105.1563 10.25 18.75 -1.75 22.75 7.543703 6.631109 0.173293
3 25.005 25.005 7.087406 9.262218 -4.91259 13.26222 6.826694 4.480083 0.095047
4 5.141014 5.141014 5.653389 4.960166 -6.34661 8.960166 6.576327 3.728981 0.036675
5 0.626838 0.626838 5.152654 3.457963 -6.84735 7.457963 6.535955 3.607865 0.006139
6 0.016299 0.016299 5.07191 3.215731 -6.92809 7.215731 6.534848 3.604543 0.000169
7 1.23E-05 1.23E-05 5.069696 3.209087 -6.9303 7.209087 6.534847 3.604541 1.28×10-7

Numerical Analysis / M. Sc. - 42 - Written by Assoc. Prof.


Chap. 2 Dr. Zaidoon M. Shakoor
Chapter 3: Solution of Ordinary Differential Equations

3.1 Solution of First-Order Ordinary Differential Equations


An equation that consists of derivatives is called a differential equation. Differential
equations have applications in all areas of science and engineering. Mathematical
formulation of most of the physical and engineering problems lead to differential
equations. So, it is important for engineers and scientists to know how to set up
differential equations and solve them. Differential equations are of two types
1) Ordinary differential equation (ODE).
2) Partial differential equations (PDE).
An ordinary differential equation is that in which all the derivatives are with respect
to a single independent variable. Examples of ordinary differential equation include:
dy
1) + y = sin(x) , y (0) = 1 ,
dx
d2y dy dy
2) 2
+2 + y=0 , (0) = 2 , y (0) = 4
dx dx dx
d3y d2y dy d2y dy
3) 3
+ 3 2
+ 5 + y = sin x , 2
(0) = 12 , (0) = 2 , y (0) = 4
dx dx dx dx dx
First order ordinary differential equations are of the form:
dx
= f=( x, t ) with x(0) x0
dt
On the left hand side is the derivative of the dependent variable x with respect to
the independent variable t. On the right hand side, there is a function that may depend
on both x and t.
Many differential equations cannot be solved exactly. Numerical methods have
been developed to approximate solutions. Numerical analysis is a field in mathematics
that is concerned with developing approximate numerical methods and assessing their
accuracy, for instance for solving differential equations. We will discuss the most basic
method such Taylor, Euler and Runge-Kutta methods.

3.1.1 Taylor Series Method


Function y (x) can be expanded over a small interval x using the Taylor series from a
start or reference point x
1 2 1 1
y( x + h ) = y( x ) + hy′( x ) + h y′′( x ) + h 3 y′′′( x ) + h 4 y ( 4 ) ( x ) +  (1)
2! 3! 4!
Numerical Analysis / M. Sc. - 43 - Written by Assoc. Prof.
Chap. 3 Dr. Zaidoon M. Shakoor
Where hi = xi +1 − xi = h, a constant.
Example 3.1
Solve the following ordinary differential equation (ODE) using Taylor’s method of
order 2 with h= 0.2
dy
= y − x2 + 1 , for 0 ≤ x ≤ 2 , with y(0) = 0.5
dx
Solution
d2y dy
2
= − 2x = y − x2 + 1 − 2x
dx dx
1
y n+1 = y n + (y n − x n 2 + 1)h + ( yn − x n 2 − 2x n + 1)h2
2
i x y
1 0 0.5000
2 0.2000 0.8300
3 0.4000 1.2158
4 0.6000 1.6521
5 0.8000 2.1323
6 1.0000 2.6486
7 1.2000 3.1913
8 1.4000 3.7486
9 1.6000 4.3061
10 1.8000 4.8463
11 2.0000 5.3477

3.1.2. Euler’s Method


Euler’s method is the simplest and least useful of these three methods. If we are
dy
solving a first-order differential equation of the form = f ( t , y ) with the initial
dt
condition y(0)=A, Euler’s method begins by approximating the first derivative as
dy y (t + ∆t ) − y (t )

dt ∆t

Setting this equal to f(t,y) and solving for y (t + ∆t ) yields the following algorithm for

advancing the numerical solution of an ordinary differential equation:

Numerical Analysis / M. Sc. - 44 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
dy
= f ( x, y ) y( 0 ) = y0
dx
yn +1 = yn + h × f ( x, y )

Using Euler’s method we have the following consideration:


x1 = x0 + h y1 = y 0 + h ⋅ f ( x0 , y 0 )
x 2 = x1 + h y 2 = y1 + h ⋅ f ( x1 , y1 )
x3 = x 2 + h y3 = y 2 + h ⋅ f ( x2 , y 2 )
 
 
 

Example 3.2:
Apply Euler’s method to approximate the solution of the initial value problem
dy
= 2 y with y (0) = 5 (2)
dx
Solution:
We know that the analytical solution of equation (2) is , y = 5 exp(2 x) . We numerically
solve equation (2) using Euler’s method with h=0.1 in the time interval [0, 0.5], and
then check how well this method performs. We have f ( y ) = 2 y . Then
x0 = 0
x1 = x0 + h = 0 + 0.1 = 0.1
x 2 = x1 + h = 0.1 + 0.1 = 0.2
x3 = x 2 + h = 0.2 + 0.1 = 0.3
x 4 = x3 + h = 0.3 + 0.1 = 0.4
x5 = x 4 + h = 0.4 + 0.1 = 0.5
And
y0 = 5
y1 = y 0 + hf ( y 0 ) = 5 + (
0.1)( 2)(5) = 6

h f ( y0 )

y 2 = y1 + hf ( y1 ) = 6 + (0.1)(2)(6) = 7.2
y 3 = y 2 + hf ( y 2 ) = 7.2 + (0.1)(2)(7.2) = 8.64
y 4 = y 3 + hf ( y 3 ) = 8.64 + (0.1)(2)(8.64) = 10.368
y 5 = y 4 + hf ( y 4 ) = 10.368 + (0.1)(2)(10.368) = 12.4416

We summarize this in the following table. If h=0.1, then

Numerical Analysis / M. Sc. - 45 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
x y Exact Difference
0 5 5 0
0.1 6 6.107014 0.107014
0.2 7.2 7.459123 0.259123
0.3 8.64 9.110594 0.470594
0.4 10.368 11.1277 0.759705
0.5 12.4416 13.59141 1.149809
The third column contains the exact values, y = 5 exp(2 x) . The last column contains
the absolute error after each step, computed as |y-yExact |. We see that when h=0.1, the
numerical approximation is not very good after five steps. If we repeat the same
approximation with a smaller value for h, say h=0.01, the following table results for
the first five steps:
x y Exact Difference
0 5 5 0
0.01 5.1 5.101007 0.001007
0.02 5.202 5.204054 0.002054
0.03 5.30604 5.309183 0.003143
0.04 5.412161 5.416435 0.004275
0.05 5.520404 5.525855 0.005451

Doing five steps only gets us to x=0.05. We can do more steps until we reach x=0.5.
We find that the final point will be:
x y Exact Difference
0.5 13.45794 13.59141 0.133469

Choosing a smaller value for h resulted in a better approximation at x=0.5 but also
required more steps. One source of error in the approximation comes from the
approximation itself.

Example 3.3
Consider the following initial value problem:
dx 2
=t +t with the initial condition: x (0) = 0.5 from t=0 to t=2
dt
Solution:

Numerical Analysis / M. Sc. - 46 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
dt=0.2; t(1)=0; x(1)= 0.5;
for n=1:10
t(n+1)=t(n)+dt
x(n+1)=x(n)+dt*(t(n)^2+t(n))
end
plot (t,x)
xlabel('Time (t)')
ylabel('x(t)')

The result is in Figure (3.1)


5

4.5

3.5

3
x(t)

2.5

1.5

0.5
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Time (t)

Figure 3.1: Euler method used to solve exercise1


This ODE can be analytically integrated to get the true solution:
t3 t2
x( t ) = + + 0.5
3 2
Applying the explicit Euler's scheme with a step size h = 0.2 we get:
x(0.2) = x(0) + 0.2f (0)
That is
x(0.2) = 0.5 + 0 = 0.5

The true solution from is x(0.2) = 0.52267

The relative error (e r ) expressed in percent is


True - approximation
er = × 100
True
0.5 - 0.52267
er = × 100 = 4.5%
0.5
The calculations results are plotted in Figure 3.2 showing the true and the approximate
value for h=0.2. Although the general trend of the true and the approximate values is
Numerical Analysis / M. Sc. - 47 - Written by Assoc. Prof.
Chap. 3 Dr. Zaidoon M. Shakoor
the same, the error is large. One way to reduce this error is by choosing a smaller step
size. Figure 3.2 shows the solution when the step size is halved i.e. h=0.1. Since the
Euler's method is first order, the global error is halved O (h /2) while the local error is
quartered O (h2/4). To get acceptable levels of errors the step size has to be further
reduced to very low values. This will however considerably increase the computational
time since it will take a larger number of iterations for each step. Nevertheless the
Euler's method because of its simplicity and easiness for implementation is still
attractive for many engineering problems.

5.5

4.5

3.5
x(t)

2.5

1.5 Euler (h = 0.2)


Euler (h = 0.1)
1 True

0.5
0 0.5 1 1.5 2 2.5
Time (t)

Figure 3.2: Euler method used to solve exercise 1

3.1.3 Fourth order Runge-Kutta Method


dy
To find numerical solution to the initial value problem = f ( x, y ), y (0) = y 0 using
dx
Runge-Kutta method we have the following consideration:

yi +1 = yi +
1
(k1 + 2k2 + 2k3 + k4 )h
6

k1 = f (xi , yi )

 1 1 
k2 = f  xi + h, yi + k1h 
 2 2 
 1 1 
k3 = f  xi + h, yi + k2 h 
 2 2 
Numerical Analysis / M. Sc. - 48 - Written by Assoc. Prof.
Chap. 3 Dr. Zaidoon M. Shakoor
k4 = f (xi + h, yi + k3h )

This method gives more accurate result compared to Euler’s method

Example 3.4:
Solve the following ordinary differential equation (ODE) using fourth order
Runge-Kutta method to calculate y(x=0.2)
dy
= x + y ; y(0) = 1 , h = 0.1
dx
Solution:
k 1 = 0 + 1= 1

k 2 = (0+0.05)+(1+1×0.05)=1.10

k 3 = (0+0.05) +(1+1.1×0.05)= 1.1050

k 4 = (0+0.1)+(1+1.1050×0.1) = 1.2105
h
y1 = y o + ( k1 + 2k 2 + 2k 3 + k 4 )
6
0.1
y(0.1) = 1 + × (1 + 2 × 1.1 + 2 × 1.105 + 1.2105) = 1.11034
6

k 1 = 0.1 + 1.11034 = 1.21034

k 2 = (0.1+0.05)+(1.11034+1.21034×0.05) = 1.3209

k 3 = (0.1+0.05) +(1.11034+1.3209×0.05)= 1.3264

k 4 = (0.1+0.1)+( 1.11034+1.3264×0.1) = 1.4430


0.1
y(0.2) = 1.11034 + × (1.21034 + 2 × 1.3209 + 2 × 1.3264 + 1.4430) = 1.2428
6
at x=0.2 y=1.2428

Example 3.5:
A ball at 1200 K is allowed to cool down in air at an ambient temperature of 300 K.
Assuming heat is lost only due to radiation, the differential equation for the
temperature of the ball is given by

Numerical Analysis / M. Sc. - 49 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
= −2.2067 × 10 −12 (T 4 − 81 × 108 ) , T(0 ) = 1200 K
dT
dt
where T is in K and t in seconds. Find the temperature at t = 480 seconds using
Runge-Kutta 4th order method. Assume a step size of h = 240 seconds.

Solution
= −2.2067 × 10 −12 (T 4 − 81 × 108 )
dT
dt

f (t , T ) = −2.2067 × 10 −12 (T 4 − 81 × 108 )

Ti+1 = Ti +
h
(k1 + 2k 2 + 2k 3 + k 4 )
6

For i = 0 , t 0 = 0 , T0 = 1200 K

k1 = f (t 0 , T0 ) = f (0,1200 ) = −2.2067 × 10 −12 (1200 4 − 81 × 108 ) = −4.5579

   
k 2 = f  t 0 + h , T0 + k1h  = f  0 + (240 ),1200 + (− 4.5579 ) × 240 
1 1 1 1
 2 2   2 2 
(
= f (120,653.05) = −2.2067 × 10 −12 653.054 − 81 × 108 )
= −0.38347
   
k 3 = f  t 0 + h , T0 + k 2 h  = f  0 + (240 ),1200 + (− 0.38347 ) × 240 
1 1 1 1
 2 2   2 2 
( )
= f (120,1154 .0 ) = −2.2067 × 10 −12 1154 .0 4 − 81 × 108 = −3.8954

k 4 = f (t 0 + h , T0 + k 3h ) = f (0 + 240,1200 + (− 3.894 ) × 240 ) = f (240,265.10 )

= −2.2067 × 10 −12 (265.10 4 − 81 × 108 ) = 0.069750

h
T1 = T0 + ( k1 + 2k 2 + 2k 3 + k 4 )
6
= 1200 +
240
(− 4.5579 + 2(− 0.38347 ) + 2(− 3.8954) + (0.069750))
6
= 675.65 K

T1 is the approximate temperature at t=t 1


t = t 0 + h = 0 + 240 = 240
For i = 1, t1 = 240, T1 = 675.65 K

k1 = f (t1 , T1 ) = f (240,675.65) = −2.2067 × 10 −12 (675.654 − 81 × 108 )

= −0.44199
Numerical Analysis / M. Sc. - 50 - Written by Assoc. Prof.
Chap. 3 Dr. Zaidoon M. Shakoor
   
k 2 = f  t1 + h , T1 + k1h  = f  240 + (240 ),675.65 + (− 0.44199 )240 
1 1 1 1
 2 2   2 2 
= f (360,622.61) = −2.2067 × 10 −12 (622.614 − 81 × 108 )

= −0.31372
   
k 3 = f  t1 + h , T1 + k 2 h  = f  240 + (240 ),675.65 + (− 0.31372 ) × 240 
1 1 1 1
 2 2   2 2 
= f (360,638.00 )
(
= −2.2067 × 10 −12 638.00 4 − 81 × 10 8 )
= −0.34775
k 4 = f (t1 + h , T1 + k 3h ) = f (240 + 240,675.65 + (− 0.34775) × 240 ) = f (480,592.19 )

= 2.2067 × 10 −12 (592.19 4 − 81 × 108 )

= −0.25351
h
T2 = T1 + (k1 + 2k 2 + 2k 3 + k 4 )
6
= 675.65 +
240
(− 0.44199 + 2(− 0.31372) + 2(− 0.34775) + (− 0.25351))
6
= 594.91 K

T2 is the approximate temperature at time t 2


t 2 = t1 + h = 240 + 240 = 480

Table 1 and Figure 2 show the effect of step size on the value of the calculated
temperature at t = 480 seconds.

Table: Value of temperature at time, t = 480 s for different step sizes


Step size, h T(480) E t | εt | %
480 -90.278 737.85 113.94
240 594.91 52.660 8.1319
120 646.16 1.4122 0.21807
60 647.54 0.033626 0.0051926
30 647.57 0.00086900 0.00013419

Example 3.6
Using Matlab Commands solve the following equation using both Eular and
Runge-Kutta method and to approximate the solution of the initial value problem
dy
= x + y, y (0) = 1 with step size h = 0.1.
dx
Numerical Analysis / M. Sc. - 51 - Written by Assoc. Prof.
Chap. 3 Dr. Zaidoon M. Shakoor
Solution:
Eular Runge-Kutta
clear all, clc,format short clear all, clc,format short
x(1)=0; x(1)=0;y(1)=1;h=0.1;
y(1)=1; f=inline('x+y');
h=0.5 % f(x,y) = x+y
for i=1:5 for i=1:5
x(i+1)=x(i)+h; x(i+1)=x(i)+h;
dy=x(i)+y(i); k1 = f(x(i),y(i));
y(i+1)=y(1)+h*dy; k2 = f(x(i)+h/2,y(i)+k1*h/2);
end k3 = f( x(i) + h/2,y(i)+k2*h/2);
y_exact= -1-x+2*exp(x); k4 = f( x(i) + h,y(i)+k3*h);
error=y_exact-y y(i+1)=y(i)+(1/6)*h*(k1 +2*k2 + 2*k3 +k4);
table=[x',y',y_exact',error'] end
y_exact= -1-x+2*exp(x); error=y_exact-y
table=[x',y',y_exact',error']
table = table =
0 1.0000 1.0000 0 0 1.0000 1.0000 0
0.1000 1.1000 1.1103 0.0103 0.1000 1.1103 1.1103 0.0000
0.2000 1.1200 1.2428 0.1228 0.2000 1.2428 1.2428 0.0000
0.3000 1.1320 1.3997 0.2677 0.3000 1.3997 1.3997 0.0000
0.4000 1.1432 1.5836 0.4404 0.4000 1.5836 1.5836 0.0000
0.5000 1.1543 1.7974 0.6431 0.5000 1.7974 1.7974 0.0000

3.3.1 MATLAB Built-In Routines for solving ODES


MATLAB have several sub programs (Routines) to solve ODES;
• ode113:Variable order solution to non-stiff system
• ode15s:Variable order, multistep method for solution of stiff system
• ode23: Lower order adaptive step size routine for non-stiff systems
• ode23s:Lower order adaptive step size routine for stiff systems
• ode45: Higher order adaptive step size routine for non-stiff system

To solve the example 3.3 using MATLAB Routine, We just have to write a
function which returns the rate of change of the vector x.

function dx = example(t,x)
dx(1,1) = t^2+t;

The above file named example.m must be saved in default MATLAB folder then
Numerical Analysis / M. Sc. - 52 - Written by Assoc. Prof.
Chap. 3 Dr. Zaidoon M. Shakoor
the following code must run.

Vspan = 0:0.2:2;
x0=0.5;
[t,x] = ode45('example',Vspan,x0);
plot (t,x,'k*:')
xlabel('Time (t)')
ylabel('x(t)')
legend('Runge Kutta integration')

The numerical solution, computed using ODE45 is given below in Figure (3.3)

5 Runge Kutta integration

4
x(t)

0 0.5 1 1.5 2
Time (t)
Figure (3.3): Solution produced by ODE45.

3.2 Solving Simultaneous First-Order Ordinary Differential Equations


3.2.1 Integration two simultaneous first-order ordinary differential equations
Consider the following system of first-order ODE’s describing the dependence of two
dependent variables y and z on one independent variable x:
dy
= f ( x, y , z )
dx
dz
= g ( x, y , z )
dx
These two differential equations are coupled and must be integrated simultaneously
because both equations involve both dependent variables.
Initial conditions are required giving the values of y and z at the initial value of x. The
algorithm for 4th-order Runge-Kutta integration of two coupled ODEs is:

Numerical Analysis / M. Sc. - 53 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
h
yi + 1 = yi + ( k11 + 2k 21 + 2k31 + k41 )
6
h
zi + 1 = zi + ( k12 + 2k 22 + 2k32 + k42 )
6

k11 = f ( xi , yi , zi )
k12 = g( xi , yi , zi )
k 21 = f ( xi + 0.5 h , yi + 0.5 hk11 , zi + 0.5 hk12 )
k 22 = g ( xi + 0.5 h , yi + 0.5 hk11 , zi + 0.5 hk12 )
k32 = f ( xi + 0.5 h , yi + 0.5 hk 21 , zi + 0.5 hk 22 )
k32 = g( xi + 0.5 h , yi + 0.5 hk 21 , zi + 0.5 hk 22 )
k41 = f ( xi + h , yi + hk31 , zi + hk32 )
k42 = g( xi + h , yi + hk31 , zi + hk32 )
As example an exothermic reaction in unsteady-state continuous stirred tank reactor
and exothermic reaction in a plug flow reactor with heat exchange through the reactor
wall.
From the one and two ODE examples, you can extend the method to integration of
three coupled ODE’s. Three coupled ODE’s would be encountered, for example, for
reaction of gases in a steady-state non-isothermal plug flow reactor with significant
pressure drop (dC/dx =, dT/dx =, and dP/dx=).

3.2.2 Integration of a system of first-order ordinary differential equations


dy1
= f1(x, y1 , y2 ,…, ym )
dx
dy2
= f 2 (x, y1 , y2 ,…, ym )
dx
.....................
......................
dym
= f m (x, y1 , y2 ,…, ym )
dx
The solution of the above equations is:

h
yin + 1 = yin + (k1,i + 2k 2,i + 2k3,i + k4,i ) Where i = 1, 2, ..., m and
6

k1,i = f i (x n , y1n , y2n …, ymn )

h n hk1,1 n hk1,2 hk
k 2,i = f i (x n + , y1 + , y2 + …, ymn + 1,m )
2 2 2 2

Numerical Analysis / M. Sc. - 54 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
h n hk 2 ,1 n hk 2 ,2 hk
k3,i = f i (x n + , y1 + , y2 + …, ymn + 2 ,m )
2 2 2 2

k4,i = f i (x n + h , y1n + hk3 ,1 , y2n + hk3 ,2 …, ymn + hk3 ,m )

The idea of the solution to a system of differential equations is similar to a solution of a


single differential equation.

Example 3.7:
Using fourth order Runge-Kutta method with step size h = 0.1 solve
dy1
= y1 y2 + x , y 1 (0) = 1
dx
dy 2
= xy 2 + y 1 , y 2 (0) = -1
dx

To calculate y 1 (0.1) and y 2 (0.1)

Solution
At x = 0, y 1 = 1, y2 = -1
k 1,1 = y1 y2 + x = (1)(-1) + 0 = -1
k 1,2 = xy 2 + y 1 = (0)(-1) + 1= 1
k 2,1 = (y 1 +0.5hk 1,1 )(y 2 +0.5hk 1,2 ) + (x+0.5h) =(0.95)(-0.95) + 0.05 = -0.8525
k 2,2 = (x+0.5h)(y 2 +0.5hk 1,2 ) +(y 1 +0.5hk 1,1 ) =(0.05)(-0.95) + 0.95 = 0.9025
k 3,1 = (y 1 +0.5hk 2,1 )(y 2 +0.5hk 2,2 ) + (x+0.5h) =(0.9574)(-0.9549) + 0.05= -0.8642
k 3,2 = (x+0.5h)(y 2 +0.5hk 2,2 ) +(y 1 +0.5hk 2,1 ) =(0.05)(-0.9549) + 0.9574 = 0.9096
k 4,1 = (y 1 +hk 3,1 )(y 2 +hk 3,2 ) + (x+h) =(0.9136)(-0.9091) + 0.1 = -0.7305
k 4,2 = (x+h)(y 2 +hk 3,2 ) +(y1 +hk 3,1 ) =(0.1)(-0.9091) + 0.9136= 0.8227
at x=0.1
y1 (0.1) = y 1 (0) + (h/6)(k 1,1 + 2k 2,1 + 2k 3,1 + k 4,1 )

y1 (0.1) = 1 +(0.1/6) [(-1) + 2(-0.8525) + 2(-0.864) + (-0.730)]= 0.9139

y2 (0.1) = y 2 (0) +(h/6) (k 1,2 + 2k 2,2 + 2k 3,2 + k 4,2 )

y2 (0.1) = -1 + [(1) + 2(0.9025) + 2(0.909) + (0.823)]= -0.9092

Example 3.8
Use ode45 Matlab Command to solve the following first order system for y1 and y2 at
0 ≤ x ≤ 1.

Numerical Analysis / M. Sc. - 55 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
dy1
= y1 y2 + x , y1 (0) = 1
dx
dy2
= xy 2 + y1 , y2 (0) = -1
dx
Using fourth order Runge-Kutta method with step size h = 0.1
Solution:
The Matlab routines ode45 can be used to solve the system. A Matlab function must
be created to evaluate the slopes as a column vector. The function name in this
example is exode(x, y) which must be saved first in the hard drive with the same
name exode.m.
function dydx = exode(x,y)
dydx (1,1)=y(1)*y(2)+x;
dydx (2,1)=x*y(2)+y(1);

The command ode45 is then evaluated from the command windows. Matlab will set
the step size to achieve a preset accuracy that can be changed by user.
The independent variable can also be specified at certain locations between the initial
and final values and Matlab will provide the dependent value at these locations.
xspan=0:0.1:1;
[x,y]=ode45('exode',xspan,[1 , -1])
x= y=
0 1.0000 -1.0000
0.1000 0.9139 -0.9092
0.2000 0.8522 -0.8341
0.3000 0.8106 -0.7711
0.4000 0.7863 -0.7174
0.5000 0.7772 -0.6705
0.6000 0.7817 -0.6283
0.7000 0.7987 -0.5889
0.8000 0.8274 -0.5504
0.9000 0.8675 -0.5108
1.0000 0.9188 -0.4681

Exercise 3.9:
Let’s consider a simple example of a model of a plug flow reactor that is described by a
system of ordinary differential equations. A plug flow reactor is operated as shown in
Figure (3.4) below.

Numerical Analysis / M. Sc. - 56 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
A →
k1
B →
k2
C

Figure (3.4) Isothermal plug flow reactor

The plug flow initially has only reactant A, the components A react to form component
B. The mole balance for each component is given by the following differential
equations
dC A
u = − k 1C A
dz
dC
u B = k 1C A − k 2 C B
dz
dC
u C = k 2CB
dz
With the following initial values
C A (z=0) =1 kmol/m3 C B (z=0) =0 C C (z=0) =0 and k 1 =2 k 2 =3
If u=0.5 m/s and reactor length z=3 m. Solve the differential equations and plot the
concentration of each species along the reactor length
Solution:
We’ll start by writing the function defining the right hand side (RHS) of the ODEs. The
following function file ‘Example’ is used to set up the ode solver.

function dC= Example( z, C)


u = 0.5;
k1=2; k2=3;
dC(1,1) = -k1 *C(1) / u;
dC(2,1) = (k1 *C(1)-k2 *C(2)) / u;
dC(3,1) = k2 *C(2)/ u;

Now we’ll write a main script file to call ode45. CA, CB and CC must be defined
within the same matrix, and so by calling CA as C(1), CB as C(2) and CC as c(3), they
are listed as common to matrix C.
The following run file is created to obtain the solution:

Numerical Analysis / M. Sc. - 57 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
clear all, clc
[z , C] = ode45(' Example', [0:0.1:3], [1 0 0])
plot (z,C(:,1),'k+-',z,C(:,2),'k*:',z,C(:,3),'kd-.')
xlabel ('Length (m)');
ylabel ('Concentrations (kmol/m^3) ');
legend ('A', 'B', 'C')

The produced plot is as in Figure (3.5)

1
A
Concentrations (kmol/m3)

B
0.8
C

0.6

0.4

0.2

0
0 1 2 3
Length (m)
Figure (3.5): A, B and C concentrations along plug flow reactor

3.2.3 Solving Higher Order Ordinary Differential Equations


We have learned Euler’s and Runge-Kutta methods to solve first order ordinary
differential equations of the form
= f ( x, y ), y (0) = y0
dy
dx
What do we do to solve differential equations that are higher than first order? For
example an n th order differential equation of the form
dny d n −1 y
+ ao y = f (x )
dy
an n
+ a n −1 n −1
+  + a1
dx dx dx
with n − 1 initial conditions can be solved by assuming
y = z1 (1)

dy dz1
= = z2 (2)
dx dx
d 2 y dz 2
= = z3 (3)
dx 2 dx

Numerical Analysis / M. Sc. - 58 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor

d n −1 y dz n −1
= = zn (n)
dx n −1 dx
d n y dzn
=
dx n dx
1  d n −1 y 
− a0 y + f ( x )
dy
=  − an −1 n −1  − a1
an  dx dx 

=
1
(− a n−1 z n  − a1 z 2 − a0 z1 + f (x )) (n+1)
an
The above Equations from (2) to (n+1) represent n first order differential equations as
follows
dz1
= z 2 = f 1 ( z1 , z 2 , , x )
dx
dz 2
= z 3 = f 2 ( z1 , z 2 , , x )
dx

dz n
=
1
(− a n−1 z n  − a1 z 2 − a0 z1 + f (x ))
dx an
Each of the n first order ordinary differential equations is accompanied by one initial
condition. These first order ordinary differential equations are simultaneous in nature
but can be solved by the methods used for solving first order ordinary differential
equations that we have already learned.

Higher Order Ordinary Differential System of first Order Ordinary


Equations Differential Equations
d2y dy dy
2
− 2 + 2 y = e 2 x sin x =z , y (0) = −0.4
dx dx dx
with dz
= e 2 x sin x − 2 y + 2 z , z (0) = −0.6
dy dx
y(0)= -0.4 , (0) = −0.6
dx
d3y d2y dy dy
3
+2 2 −3 = x 2 + y, = z, y (0) = 4
dx dx dx dx
with dz
2
=u, z (0) = 2
d y dy dx
2
(0) = 1, (0) = 2 , y (0) = 4
dx dx du
= x 2 + y − 2u + 3z , u (0) = 1
dx
d4y dy y dy
4
+ − + 3y = , = z, y ( 0) = 1
dx dx x dx

Numerical Analysis / M. Sc. - 59 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
with dz
=u, z (0) = 1
d3y d2y dy dx
3
( 0 ) = 0 . 5, 2
(0) = 0.25, (0) = 1 , y (0) = 1
dx dx dx du
=v, u (0) = 0.25
dx
dv y
= − 3y + z , v(0) = 0.5
dx x

Example 3.10
Re-write the following differential equation as a set of first order differential equations.
d2y
+ 2 + 5 y = e − x , y (0) = 5, y ′(0) = 7
dy
3 2
dx dx
Solution
The ordinary differential equation would be rewritten as follows. Assume
dy d 2 y dz
= z, Then =
dx dx 2 dx
Substituting this in the given second order ordinary differential equation gives
dz
3 + 2z + 5 y = e −x
dx
dz 1 − x
(
= e − 2z − 5 y
dx 3
)
The set of two simultaneous first order ordinary differential equations complete with
the initial conditions then is
= z, y (0) = 5
dy
dx

= (e − 2 z − 5 y ), z (0) = 7 .
dz 1 − x
dx 3
Now one can apply any of the numerical methods used for solving first order ordinary
differential equations.

Example 3.11
Given the third-order ordinary differential equation and associated initial conditions
d3y d2y dy y (0) = 4, dy d2y
3
+ 3 2 + 5 + y = x2 , = 0.6 , 2 = 0.22
dx dx dx dx x =0 dx x =0

a. Write this differential equation as a system of first-order ordinary differential


equations
b. Using fourth order Runge-Kutaa method to estimate y(0.1) and y(0.2) taking
Δx=0.1
Numerical Analysis / M. Sc. - 60 - Written by Assoc. Prof.
Chap. 3 Dr. Zaidoon M. Shakoor
a)
dy
= z , y(0) = 4
dx
dz
= u , z(0) = 0.6
dx
du
= x 2 − 3u − 5z − y , u (0) = 0.22
dx
b) Solution (1)
First step of integration x=0 , y=4 , z=0.6 , u=0.22 , Δx=0.1
k11= z =0.6000
k21= u= 0.2200
k31=x2-3×u-5×z-y= 02-3×0.22-5×0.6-4=-7.6600
k12=z+0.5×Δx×k21=0.6+0.5×0.1×0.22= 0.6110
k22=(u+0.5×Δx×k31) =(0.22+0.5×0.1×(-7.6600)) = -0.1630
k32=(x+0.5×Δx)2 -3×(u+0.5×Δx×k31)-5×(z+0.5×Δx×k21)-(y+0.5×Δx×k11)
=(0+0.5×0.1) 2-3×(0.22+0.5×0.1×(-7.66))-5×(0.6+0.5×0.1×0.22)-(4+0.5×0.1×0.6)
= -6.5935
k13=z+0.5×Δx×k22=0.6+0.5×0.1×(-0.1630)= 0.5918
k23=u+0.5×Δx×k32 =0.22+0.5×0.1×(-6.5935) = -0.1097
k33=(x+0.5×Δx) 2 -3×(u+0.5×Δx×k32)-5×(z+0.5×Δx×k22)-(y+0.5×Δx×k12)
=(0+0.5×0.1)2-3×(0.22+0.5×0.1×(-6.5935))-5×(0.6+0.5×0.1×(-0.1630))-(4+0.5×0.1×
0.6110) = -6.6583
k14=z+Δx×k23=0.6+0.1×(-0.1097)= 0.5890
k24=u+Δx×k33=0.22+0.1×(-6.6583) = -0.4458
k34=(x+Δx) 2 -3×(u+Δx×k33)-5×(z+Δx×k23)-(y+Δx×k13)
=(0+0.1)2-3×(0.22+0.1×(-6.6583))-5×(0.6+0.1×(-0.1097))-(4+0.1×0.5918) = -5.6569
x=x+Δx=0+0.1= 0.1000
y=y+Δx/6×(k11+2×k12+2×k13+k14)=4+0.1/6×(0.6+2×0.6110+2×0.5918+0.5890)
= 4.0599
z=z+Δx/6*(k21+2*k22+2*k23+k24)=0.6+0.1/6 ×(0.2200+2×(-0.1630)+2×(-0.1097)+
( -0.4458))= 0.5871
u=u+Δx/6*(k31+2*k32+2*k33+k34)=u+0.1/6 ×(-7.6600+2×(-6.5935)+2×(-6.6583)+
(-5.6569))=-0.4437
dy dz d 2 y
Then at x=0.1 , y=4.0599, z = = 0.5871 , u= = = -0.4437
dx dx dx 2
Numerical Analysis / M. Sc. - 61 - Written by Assoc. Prof.
Chap. 3 Dr. Zaidoon M. Shakoor
Second step of integration x=0.1 , z=0.5871 , u=-0.4437 , Δx=0.1
k11= z = 0.5871
k21= u= -0.4437
k31=x2-3×u-5×z-y= 0.12-3×(-0.4437)-5×(0.5871)- 4.0599=-5.6546
k12=z+0.5×Δx×k21=0.5871+0.5×0.1×(-0.4437)= 0.5650
k22=(u+0.5×Δx×k31) =((-0.4437)+0.5×0.1×(-5.6546)) = -0.7264
k32=(x+0.5×Δx)2-3×(u+0.5×Δx×k31)-5×(z+0.5×Δx×k21)-(y+0.5×Δx×k11)
=(0.1+0.5×0.1)2-3×(-0.4437+0.5×0.1×(-5.6546))-5×(0.5871+0.5×0.1×(-0.4437))-(
4.0599+0.5×0.1×0.5871) = -4.7124
k13=z+0.5×Δx×k22=0.5871+0.5×0.1×(-0.7264)= 0.5508
k23=u+0.5×Δx×k32 =-0.4437+0.5×0.1×(-4.7124) = -0.6793
k33=(x+0.5×Δx)2 -3×(u+0.5×Δx×k32)-5×(z+0.5×Δx×k22)-(y+0.5×Δx×k12)
=(0.1+0.5×0.1)2-3×(-0.4437+0.5×0.1×(-4.7124))-5×(0.5871+0.5×0.1×(-0.7264))-(
4.0599+0.5×0.1×0.5650) = -4.7819
k14=z+Δx×k23=0.5871+0.1×(-0.6793)= 0.5192
k24=u+Δx×k33=-0.4437+0.1×(-4.7819) = -0.9219
k34=(x+Δx)2-3×(u+Δx×k33)-5×(z+Δx×k23)-(y+Δx×k13)
=(0.1+0.1)2-3×(-0.4437+0.1×(-4.7819))-5×(0.5871+0.1×(-0.6793))-(4.0599+0.1×0.5
508) = -3.9055
x=x+Δx=0.1+0.1= 0.2
y=y+Δx/6×(k11+2×k12+2×k13+k14)=
4.0599+0.1/6×(0.5871+2×0.5650+2×0.5508+0.5192)= 4.1155
z=z+Δx/6*(k21+2*k22+2*k23+k24)=
0.5871+0.1/6×((-0.4437)+2×(-0.7264)+2×(-0.6793)+( -0.9219))= 0.5175
u=u+Δx/6*(k31+2*k32+2*k33+k34) =
-0.4437 +0.1/6×((-5.6546) +2×( -4.7124 )+2×(-4.7819)+ (-3.9055 ))= -0.9195
dy dz d 2 y
Then at x=0.2 , y=4.1155, z = = 0.5175 , u= = = -0.9195
dx dx dx 2

Solution 2 : Using Matlab commands:


We’ll start by writing the function defining the right hand side (RHS) of the ODEs. The
following function file ‘Ex’ is used to set up the ode solver.

Numerical Analysis / M. Sc. - 62 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
function dq = Ex(x,q)
y=q(1);z=q(2);u=q(3);
dq(1,1)=z;
dq(2,1)=u;
dq(3,1)=x^2-3*u-5*z-y;

The following run file is created to obtain the solution:


clear all,clc,format compact
[x,q]=ode45('Ex',[0:0.1:0.2],[4,0.6,0.22])
y=q(:,1)

The produced results will be


x=
0
0.1000
0.2000
q=
4.0000 0.6000 0.2200
4.0599 0.5871 -0.4437
4.1155 0.5175 -0.9195
y=
4.0000
4.0599
4.1155

Numerical Analysis / M. Sc. - 63 - Written by Assoc. Prof.


Chap. 3 Dr. Zaidoon M. Shakoor
Chapter 4: Solution of Partial Differential Equations

Classification of second-order linear equations

Examples of different PDEs are:

(a) Elliptic
∂ 2u ∂ 2u
+ 2 =0 (Laplace’s equation)
∂x 2 ∂y
∂ 2u ∂ 2u
+ 2 +e=0 (Poisson’s equation)
∂x 2 ∂y
(b) Parabolic
∂u ∂ 2u
=α 2 ; where α is a positive constant
∂y ∂x

∂u ∂ 2u ∂ 2u
= α ( 2 + 2 ) + e; (Parabolic in time t and elliptic in spatial dimensions x
∂t ∂x ∂y
and y)
(c) Hyperbolic
∂ 2u 2∂ u
2
− c =0 (wave’s equation)
∂t 2 ∂x 2

The unsteady state heat conduction equation is a parabolic differential equation


ρCp ∂T = ∇⋅(k∇T) + q′′′
∂t

For constant physical properties and no heat generation, the heat conduction equation
becomes
ρCp ∂T = k∇2T or ∂T = α∇2T
∂t ∂t
k
Where: α = is the thermal diffusivity of the materials.
ρc p
We must consider changes in time as well as in space for parabolic equations. The
domain of elliptic equations is a closed region whereas the domain of parabolic
equations is an open region in time.

Numerical Analysis / M. Sc. 64 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
4.1 Numerical Differentiation
Numerical Differentiation is a method used to approximate the value of a derivative
over a continuous region [a,b].
Let f(x) is a continuous function with step size h. There are forward, backward and
centered difference methods to approximate the derivatives of f(x) at a point x i .

4.1.1 Forward Difference Approximation of the First Derivative


We know
f ( x + ∆x ) − f ( x )
f ′( x ) = lim
∆x → 0 ∆x
f ( x + ∆x ) − f ( x )
For a finite ' ∆x' . f ′(x ) ≅
∆x
f (x)

x x + ∆x x

Figure 4.1: Graphical representation of forward


difference approximation of first derivative

So if you want to find the value of f ′(x ) at x = xi , we may choose another point ' Δx'
ahead as x = xi +1 . This gives
f ( xi +1 ) − f ( xi )
f ′( xi ) ≅
∆x
f ( xi +1 ) − f ( xi )
= Where Δx = xi +1 − xi
xi +1 − xi

4.1.2 Backward Difference Approximation of the First Derivative


We know
f ( x + ∆x ) − f ( x )
f ′( x ) = lim
∆x →0 ∆x
For a finite ' Δx' ,

Numerical Analysis / M. Sc. 65 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
f ( x + ∆x ) − f ( x )
f ′( x ) ≅
∆x
If ' Δx' is chosen as a negative number,
f (x + ∆x ) − f (x )
f ′(x ) ≅
∆x
f (x ) − f (x − ∆x )
f ′(x ) =
∆x
This is a backward difference approximation as you are taking a point backward
from x . To find the value of f ′(x ) at x = xi , we may choose another point ' Δx' behind
as x = xi −1 . This gives
f ( xi ) − f (xi −1 )
f ′( xi ) ≅
∆x
f ( xi ) − f ( xi −1 )
= where ∆x = xi − xi −1
xi − xi −1

f (x)

x
x − ∆x x
Figure 4.2 Graphical representation of backward
difference approximation of first derivative

4.1.3 Central Difference Approximation of the First Derivative


As shown above, both forward and backward divided difference approximation of the
first derivative are accurate on the order of 0(Δx ) . Can we get better approximations?
Yes, another method to approximate the first derivative is called the Central
difference approximation of the first derivative.
From Taylor series
f ′′( xi ) ′′′( )
f ( xi +1 ) = f ( xi ) + f ′( xi )Δx + (Δx )2 + f xi (Δx )3 +  (1)
2! 3!
f ′′( xi )
(∆x )2 − f (xi ) (∆x )3 + 
′′′
f ( xi −1 ) = f ( xi ) − f ′( xi )∆x + (2)
2! 3!
Subtracting equation (2) from equation (1)

Numerical Analysis / M. Sc. 66 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
2 f ′′′( xi )
f ( xi +1 ) − f ( xi −1 ) = f ′( xi )(2∆x ) + (∆x )3 + 
3!
f (xi +1 ) − f ( xi −1 ) f ′′′( xi )
f ′( xi ) = − (∆x )2 + 
2∆x 3!
f ( xi +1 ) − f (xi −1 )
f ′( xi ) = + 0(∆x )
2
2∆x
f (xi +1 ) − f (xi −1 )
f ′( xi ) =
2∆x
Hence showing that we have obtained a more accurate formula as the error is of the
order of 0(∆x )2 .
f (x)

x − ∆x x x + ∆x

Figure 4.3 Graphical Representation of central


difference approximation of first derivative.

4.1.4 Higher Order Derivatives

Example: Second order derivative:


Note that for the centered formulation, it is a derivation of a derivative:
f (xi +1 ) − f ( xi ) f (xi ) − f ( xi −1 )

∆x ∆x f ( xi +1 ) − 2 f ( xi ) + f ( xi −1 )
f ' ' (x ) ≅ =
∆x (∆x) 2
f (xi + 2 ) − 2 f ( xi +1 ) + f ( xi )
f ' ' (x ) ≅
Forward
(∆x) 2
f (xi ) − 2 f ( xi −1 ) + f ( xi − 2 )
f ' ' (x ) ≅
Backward
(∆x) 2
f ( xi +1 ) − 2 f ( xi ) + f ( xi −1 )
f ' ' (x ) ≅
Centered
(∆x) 2

I) Forward Difference Methods


First Derivative
Numerical Analysis / M. Sc. 67 Written by Assoc. Prof.
Chap. 4 Dr. Zaidoon M. Shakoor
f (xi +1 ) − f (xi )
f ' ( xi ) =
∆x
Second Derivative
f ( xi + 2 ) − 2 f ( xi +1 ) + f ( xi )
f ' ' ( xi ) =
(∆x) 2
Third Derivative
f ( xi + 3 ) − 3 f ( xi + 2 ) + 3 f ( xi +1 ) − f ( xi )
f (3) ( xi ) =
(∆x)3
Fourth Derivative
f (xi + 4 ) − 4 f ( xi + 3 ) + 6 f (xi + 2 ) − 4 f ( xi +1 ) + f (xi )
f (4 ) ( xi ) =
(∆x) 4
II) Backward Difference Methods
First Derivative
f ( xi ) − f ( xi −1 )
f ' ( xi ) =
∆x
Second Derivative
f ( xi ) − 2 f ( xi −1 ) + f ( xi − 2 )
f ' ' ( xi ) =
(∆x) 2
Third Derivative
f ( xi ) − 3 f ( xi −1 ) + 3 f ( xi − 2 ) − f ( xi −3 )
f (3) ( xi ) =
(∆x)3
Fourth Derivative
f ( xi ) − 4 f ( xi −1 ) + 6 f ( xi − 2 ) − 4 f ( xi − 3 ) + f (xi − 4 )
f (4 ) ( xi ) =
(∆x) 4
III) Central Difference Methods
First Derivative
f ( xi +1 ) − f ( xi −1 )
f ' ( xi ) =
2∆x
Second Derivative
f (xi +1 ) − 2 f (xi ) + f (xi −1 )
f ' ' ( xi ) =
(∆x) 2
Third Derivative
f ( xi + 2 ) − 2 f ( xi +1 ) + 2 f ( xi −1 ) − f ( xi − 2 )
f (3) ( xi ) =
2(∆x)3
Fourth Derivative
f (xi + 2 ) − 4 f ( xi +1 ) + 6 f ( xi ) − 4 f ( xi −1 ) + f ( xi − 2 )
f (4 ) ( xi ) =
(∆x) 4

Numerical Analysis / M. Sc. 68 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
4.2 Solution of Partial Differential Equations
4.2.1 Explicit Method
Let consider the one dimensional unsteady heat conduction equation
∂T = α ∂ T
2

∂t ∂x 2
We have an explicit method if the time derivative is approximated by a forward
difference and the second spatial derivative is approximated by a central difference
n +1
∂T = Ti − Ti
n

∂t ∆t
∂ 2T Ti +n1 − 2Ti n + Ti −n1
=
∂x 2 ∆x 2
In finite difference form, the heat conduction equation becomes
Ti n +1 − Ti n Ti +n1 − 2Ti n + Ti −n1

∆t ∆x 2
The explicit scheme is depicted in Figure 4.4 where three nodes in the previous time
tn are required to compute the node at the present time tn+1.
xi-1 xi xi+1

Grid point involved in space difference


n
t

Grid point involved in time difference

n+1
t

Figure (4.4): A computational diagram for the explicit method.

The temperature at node i can be obtained from the three previous time nodes as
α∆t  α∆t 
Ti n +1 = ( Ti +n1 + Ti −n1 ) + 1 − 2  Ti n
( ∆x ) 2
 ( ∆x ) 
2

A solution is stable when the errors at any stage of the computation are not amplified
but are attenuated as the computation progresses. For stable solution
 α∆t  α∆t 1
1 − 2  ≥0⇒
2 

 ( ∆x )  ( ∆x ) 2
2

Example 4.1:
∂ T 2

Solve the partial differential equation ∂T = 0.02 2 with the following initial and
∂t ∂x
boundary conditions Initial conditions: T(x, 0) = 100x for 0 ≤ x ≤ 0.5; T(x, 0) =
100(1− x) for 0.5 ≤ x ≤ 1 Boundary conditions: T(0, t) = 0; T(1, t) = 0

Numerical Analysis / M. Sc. 69 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
Solution:
α∆t  α∆t 
Ti n +1 = ( Ti +n1 + Ti −n1 ) + 1 − 2  Ti n
( ∆x ) 2
 ( ∆x ) 
2

Let ∆x = 0.2, ∆t = 0.5, we have


α∆t (0.02)(0.5) 1
= = 0.25 ≤
( ∆x ) 2
(0.2) 2
2
Ti n +1 = 0.25( Ti +n1 + Ti −n1 ) + 0.5 Ti n
The problem is symmetric about the value x = 1. Therefore at the node i
corresponding to x = 1, we have Ti +n1 = Ti −n1
Ti n +1 = 0.25( Ti −n1 + Ti −n1 ) + 0.5 Ti n = 0.5( Ti −n1 + Ti n )
Solution

x=0:0.1:1;
T=[0 20 40 60 80 100 80 60 40 20 0];
k=1;
plot(x,T,'k-*')
for t=0.5 :0.5:5
k=k+1;
T(k,1)=0;
T(k,11)=0;
for i=2 :10
T(k,i)=0.25*(T(k-1,i+1)+T(k-1,i-1))+0.5*T(k-1,i);
end
hold on
plot(x,T(k,:),'k-*')
hold off
end
xlabel('x'),ylabel ('Temperature')

The result of the MATLAB program is in Figure (4.5).

Numerical Analysis / M. Sc. 70 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
100

90

80

70

Temperature
60

50

40

30

20

10
Time
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
Figure (4.5): Temperature distribution using explicit method.

If you need to plot your results in three dimension plot, try the code after the above
program:
[X,Time]=meshgrid(x,[0:0.5:5])
surf(X,Time,T)
xlabel('x')
ylabel ('Time')
zlabel ('Temperature')

The result of the MATLAB program is in Figure (4.6).

100
Temperature

80

60

40

20

0
6
1
4
0.8
0.6
2 0.4
0.2
0 0
Time x
Figure (4.6): Three dimensional temperature distribution using explicit method.

Numerical Analysis / M. Sc. 71 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
Example 4.2:
Solve the heat diffusion problem of a steel bar of length 2 m with one end held at 100
o
C the other end at 0 oC and initially the bar is at 0 oC.
dT d 2T
=D 2 Where for steel D = 0.02 m2/hr. The boundary and initial conditions can
dt dx
be written T(0, t) = 100, T(2, t) = 0, T(x,0) = 0
Find the temperature distribution at times t = 4,8,12. . .20 hours, then plot your results
Solution:

L=2,dt=1,D=0.02;dx=sqrt(dt*D/.25);x=0:dx:L;
nx=(L+dx)/dx;T(1,1:nx)=0;T(1,1)=0;
time(1)=0;k=1;
for t=dt:dt:20
k=k+1
T(k,1)=100
for i=2:nx-1
T(k,i)=.25*(T(k-1,i+1)+T(k-1,i-1))+.5*T(k-1,i)
end
end
plot(x,T(4,:),'k-+'x,T(8,:),'k-^',x,T(12,:),'k-*',x,T(20,:),'k-o')
xlabel('length'),ylabel('temperature')
legend('T4','T8','T12','T20')

The result of the MATLAB program is in Figure (4.7)

100
T4
90 T8
T12
80
T20
70

60
temperature

50

40

30

20

10

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
length

Figure (4.7): Temperature distribution.

Numerical Analysis / M. Sc. 72 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
4.2.2 Gauss-Seidel Method
The Gauss-Seidel method may be used to solve a set of linear or nonlinear algebraic
equations. We will illustrate the method by solving a heat transfer problem. For
steady state, no heat generation, and constant k, the heat conduction equation is
simplified to Laplace equation
∂ 2T ∂ 2T
+ =0
∂x 2 ∂y 2
The above equation can be put in the finite difference form. We divide the medium of
interest into a number of small regions and apply the heat equation to these regions.
Each sub-region is assigned a reference point called a node or a nodal point. The
average temperature of a nodal point is then calculated by solving the resulting
equations from the energy balance. Accurate solutions can be obtained by choosing a
fine mesh with a large number of nodes.

Example 4.3
A long column with thermal conductivity k = 1 W/m⋅oK is maintained at 500oK on
three surfaces while the remaining surface is exposed to a convective environment
with h = 10 W/m2⋅oK and fluid temperature T ∞ . The cross sectional area of the
column is 1 m by 1 m. Using a grid spacing ∆x = ∆y = 0.25 m, determine the steady-
state temperature distribution in the column and the heat flow to the fluid per unit
length of the column.
Solution
The cross sectional area of the column is divided into many sub-areas called a grid or
nodal network with 25 nodes as shown in Figure 4.8. There are 12 nodal points with
unknown temperature, however only 8 unknowns need to be solved due to symmetry
so that the nodes to the left of the centerline are the same as those to the right.

∆x
500 K
m, n+1
∆y
1 2 1

500 K 3 4 3
m-1, n m, n m+1, n
5 6 5 500 K

7 8 7
h= 300 K 2
m, n-1
h = 10 W/m K
Figure (4.8): The grid for the column cross sectional area.

Numerical Analysis / M. Sc. 73 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
The energy balance is now applied to the control volume ∆x×∆y×1 belongs to node 1
which is an interior node. To make the derivation general, node 1 can be considered
as a node with index (m, n) in a two-dimensional grid as shown in Figure 1. The
directions of conduction heat flow are assumed to be the positive x and y directions.
For steady state with no heat generation
q (m-1, n)→(m, n) + q (m, n-1)→(m, n) = q (m, n)→(m+1, n) + q (m, n)→(m, n+1) (1)
Where: q (m-1, n)→(m, n) is the conduction heat flow between nodes (m-1, n) and (m, n).
Fourier’s law can be used to obtain
Tm−1,n − Tm ,n
q (m-1, n)→(m, n) = k(∆y×1)
∆x
Tm−1,n − Tm ,n
Where: (∆y×1) is the heat transfer area with a unit depth and is the finite-
∆x
difference approximation to the temperature gradient at the boundary between the
two nodes. Appling Fourier’s law to each term in Equation (1) yields
Tm−1,n − Tm ,n T −T T −T T −T
k(∆y×1) + k(∆x×1) m,n−1 m,n = k(∆y×1) m,n m+1,n + k(∆x×1) m,n m,n+1
∆x ∆y ∆x ∆y
The equation is divided by k(∆x×∆y×1) and simplified to
Tm+1,n − 2Tm ,n + Tm−1,n T − 2Tm ,n + Tm ,n −1
+ m,n+1 =0 (2)
∆x 2
∆y 2
For ∆x = ∆y, Eq. (2) becomes
Tm+1,n + Tm ,n +1 + Tm−1,n + Tm ,n −1
T m,n = (3)
4

The above result shows that the temperature of an interior node is just the average of
the temperatures of the four adjoining nodal points. Using this formula, the
temperatures for the first six nodes are
1
T1 = (T 2 + T 3 + 500 + 500)
4
1
T2 = (T 1 + T 4 + T 1 + 500)
4
1
T3 = (T 1 + T 4 + T 5 + 500)
4
1
T4 = (T 2 + T3 + T6 + T3 )
4
1
T5 = (T 3 + T 6 + T 7 + 500)
4
1
T6 = (T 4 + T5 + T8 + T5 )
4
Nodes 7 and 8 are not interior points; therefore Eq. (3) is not applicable.

Numerical Analysis / M. Sc. 74 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
∆x
500 K

∆y
1 2 1

500 K 3 4 3 5 6

5 6 5 500 K

7 8 7
500 K 7 8 7
h = 300 K
h = 10 W/m2K
Figure (4.9): Directions of heat flow for nodes 7 and 8.

Making energy balance for node 7 yields


∆y 500 − T7 T −T ∆y T −T
k( ×1) + k(∆x×1) 5 7 = k( ×1) 7 8 + h(∆x×1)(T 7 − 300)
2 ∆x ∆y 2 ∆x
2
Multiplying the above equation by we obtain
k
2h∆x
500 − T 7 + 2T 5 − 2T 7 = T 7 − T 8 + (T 7 − 300)
k
2h∆x 2 × 10 × 0.25
= = 5.0
k 1
1
T 7 = (2T 5 + T 8 + 2000)
9
Similarly an energy balance for node 8 yields
∆y T −T T −T ∆y T −T
k( ×1) 7 8 + k(∆x×1) 6 8 = k( ×1) 8 7 + h(∆x×1)(T 8 − 300)
2 ∆x ∆y 2 ∆x
2
Multiplying the above equation by we obtain
k
2h∆x
T 7 − T 8 + 2T 6 − 2T 8 = T 8 − T 7 + (T 8 − 300)
k
1
T 8 = (2T 6 + 2T 7 + 1500)
9

We have 8 linear equations with 8 unknowns that can be solved by matrix method or
iterations. As below the MATLAB program using Gauss-Seidel iteration to solve for
the temperatures.

Numerical Analysis / M. Sc. 75 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
T(1:8)=400;
for i=1:100
Tsave=T;
T(1)=0.25*(T(2)+T(3)+1000);
T(2)=0.25*(2*T(1)+T(4)+500);
T(3)=0.25*(T(1)+T(4)+T(5)+500);
T(4)=0.25*(T(2)+2*T(3)+T(6));
T(5)=0.25*(T(3)+T(6)+T(7)+500);
T(6)=0.25*(T(4)+2*T(5)+T(8));
T(7)=(2*T(5)+T(8)+2000)/9;
T(8)=(2*T(6)+2*T(7)+1500)/9;
eT=abs(T-Tsave);
if max(eT)<0.01; break; end
end
T

The program results will be


T=
489.2984
485.1473
472.0580
461.9985
436.9452
418.7346
356.9933
339.0507

4.2.3 Gaussian elimination Method


These 8 equations of example 4.3 can also be solve by matrix method.
1
T1 = (T 2 + T 3 + 500 + 500)
4
1
T2 = (T 1 + T 4 + T 1 + 500)
4
1
T3 = (T 1 + T 4 + T 5 + 500)
4
1
T4 = (T 2 + T 3 + T 6 + T 3 )
4
1
T5 = (T 3 + T 6 + T 7 + 500)
4
1
T6 = (T 4 + T 5 + T 8 + T 5 )
4
1
T7 = (2T 5 + T 8 + 2000)
9
1
T8 = (2T 6 + 2T 7 + 1500)
9

Numerical Analysis / M. Sc. 76 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
A=[1 , -1/4 , -1/4 , 0 , 0 , 0 , 0 , 0
-2/4 , 1 , 0 , -1/4 , 0 , 0 , 0 , 0
-1/4 , 0 , 1 , -1/4 , -1/4 , 0 , 0 , 0
0 , -1/4 , -2/4 , 1 , 0 , -1/4 , 0 , 0
0 , 0 , -1/4 , 0 , 1 , -1/4 , -1/4 , 0
0 , 0 , 0 , -1/4 , -2/4 , 1 , 0 , -1/4
0 , 0 , 0 , 0 , -2/9 , 0 , 1 , -1/9
0 , 0 , 0 , 0 , 0 , -2/9 , -2/9 , 1];
b=[1000/4;500/4;500/4;0;500/4;0;2000/9;1500/9]
T=A\b
The result will be:
T=
489.3047
485.1538
472.0651
462.0058
436.9498
418.7393
356.9946
339.052

Example 4.4:
Solve for the steady-state temperature in a rectangular slab that is 20 cm wide and 10
cm high. All edges are kept at 0oC except the right edge, which is at 100oC. No heat
gain or loss from the top and bottom surface of the slab as shown in Figure 4.10. Let
∆x = ∆y = 2.5 cm.

top surface

y
b

bottom surface

0 a x

Figure (4.10): A thin rectangular plate with insulated top and bottom surfaces

Solution
The two-dimensional heat conduction equation for steady state, no heat generation,
and k independent of T is given
∂ 2T ∂ 2T
+ =0
∂x 2 ∂y 2
Let i be the index in the x direction and j be the index in the y direction as shown in
Figure 3, the finite difference form of the two-dimensional heat conduction is
Numerical Analysis / M. Sc. 77 Written by Assoc. Prof.
Chap. 4 Dr. Zaidoon M. Shakoor
Ti +1,n − 2Ti , j + Ti −1, j Ti , j +1 − 2Ti , j + Ti , j −1
+ =0
∆x 2
∆y 2
For ∆x = ∆y, the temperature at the node (i, j) can be obtained as
Ti +1, j + Ti , j +1 + Ti −1, j + Ti , j −1
T i,j =
4
y
5

j3

1
1 2 3 4 5 6 7 8 9 x
i
Figure (4.11): The grid of the rectangular plate with insulated top and bottom
surfaces

From the boundary conditions


T(i, j = 1) = 0; T(i, j = 5) = 0
T(i = 1, j) = 0; T(i = 9, j) = 100

The temperature distribution can be solved by the successive-over-relaxation method


for which
T(i, j) = 0.25[T(i, j−1) + T(i, j+1) + T(i−1, j) + T(i+1, j)]
As below the MATLAB program to iterate for the temperature distribution using the
initial guesses of 5 for the interior nodes. The iteration is stopped when the maximum
difference between two successive iterations is less than 0.01.
T(1:9,1:5)=5;
T(:,1)=0;T(:,5)=0; T(1,:)=0;T(9,:)=100;
T_initial=T;
for n=1:100
Tsave=T;
for i=2:8
for j=2:4
T(i,j)=0.25*(T(i,j-1) + T(i,j+1) + T(i-1,j) + T(i+1,j));
end
end
eT=abs(T-Tsave);
if max(eT)<0.01,
break
end
end
T_final=T
Numerical Analysis / M. Sc. 78 Written by Assoc. Prof.
Chap. 4 Dr. Zaidoon M. Shakoor
The result will be as below
T_final =
0 0 0 0 0
0 0.3444 0.4888 0.3472 0
0 0.8999 1.2740 0.9043 0
0 1.9960 2.8158 2.0007 0
0 4.2829 6.0046 4.2872 0
0 9.1434 12.6425 9.1467 0
0 19.6570 26.2823 19.6591 0
0 43.2074 53.1743 43.2083 0
100.0000 100.0000 100.0000 100.0000 100.0000

Example 4.5:
The steady state concentration profile in 0.1 m radius spherical particle can be
described by the following equation:
∂ 2 C A 2 ∂C A
D Ae ( + ) − k1C A = 0
∂r 2 r ∂r
The boundary conditions: C A = C AS at r = R and dC A /dr = 0 at r = 0
With the aid of finite difference approximation and Gaussian elimination method,
solve system of linear equations and plot the concentration profile taken 20 points
radialy in the sphere if you know that:
Effective diffusivity; D Ae =2×10-7 m2/s
Reaction rate constant: K 1 =1×10-3
Concentration in sphere surface: C As =500 mol/m3

Figure (4.12) Spherical catalyst particle

Solution
Gaussian elimination Method

Numerical Analysis / M. Sc. 79 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
∂ 2 C A 2 ∂C A
D Ae ( + ) − k1C A = 0
∂r 2 r ∂r . . . . (1)
CA j +1 − 2CA j + CA j −1 2 CA j +1 − CA j −1 k
+ − 1 CA j = 0 . . . . (2)
(∆r ) 2
rj 2∆r D AE
1 1 2 k 1 1
( − )CA j +1 − ( + 1 )CA j + ( + )CA j −1 = 0 . . . . (3)
(∆r ) r j ∆r
2
(∆r ) 2
D AB (∆r ) r j ∆r
2

Solving eq. (3) using Gaussian elimination Method


DAE=2e-7;% m2/s
R=0.1;% m
K1=1e-3; CAs=500;% mol/m3
np=20; dr=R/(np-1);
r=0:dr:R;
A(1,1)=1;B(1,1)=0;
for j=2:np-1
A(j,j-1)=(1/dr^2)-(1/(r(j)*dr));
A(j,j)=-2*(1/dr^2)-K1/DAE;
A(j,j+1)=(1/dr^2)+(1/(r(j)*dr));
B(j,1)=0;
end
A(np,np)=1;B(np,1)=CAs;
CA=A\B
plot(r,CA)
xlabel('Radius (m)')
ylabel('Concentration (mol/m3)')
The results will be such in figure (4.13)

500

400
Concentration (mol/m3)

300

200

100

0
0 0.02 0.04 0.06 0.08 0.1
Radius (m)

Figure (4.13) Concentration distribution with respect to radius

Numerical Analysis / M. Sc. 80 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
Example 4.6: Un-Steady State concentration profile for a spherical catalyst
Re-solve example 4.5 assuming that the system is unsteady state
Solution:
dCA d 2 CA 2 dCA
= D AE ( + ) − k1CA . . . . (4)
dt dr r dr
CA mj+1 − CA mj CA mj+1 − 2CA mj + CA mj−1 2 CA mj+1 − CA mj−1
= D AE ( + ) − k1CA mj . . . . (5)
∆t (∆r ) 2
rj 2∆r
m +1
CA mj+1 − 2CA mj + CA mj−1 1 CA j +1 − CA j −1
m m

CA = ∆t D AE ( + ) − ∆t k1CA mj + CA mj . . . . (6)
(∆r ) 2 ∆r
j
rj

Solution 1: Using crack Nicolson method

DAE=2e-7;% m2/s
R=.1;% m
K1=20;
CAs=500;% mol/m3
np=20;
dr=R/(np-1);
r=0:dr:R;
CA(1:np,1)=0;CA(np,1)=CAs;
m=0;
for time=0:10:3600;
m=m+1;
for j=2:np-1
CA(j,m+1)=CA(j,m)+dt*DAE*((CA(j+1,m)-2*CA(j,m)+…
CA(j-1,m))/(dr^2)+(2/(r(j)))*(CA(j+1,m)-CA(j,m))/dr);
end
CA(np,m+1)=CAs;
CA(1,m+1)=CA(2,m+1);
end
plot(r,CA(:,1:100:end))
xlabel('Radius m')
zlabel('Time (sec)')
ylabel('Concentration (mol/m3)')

The results will be such in figure (4.14)

Numerical Analysis / M. Sc. 81 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
500

Concentration (mol/m3) 400

300 Time Increasing

200

100

Time =0
0
0 0.02 0.04 0.06 0.08 0.1
Radius m
Figure (4.14) Concentration distribution at different time periods

Solution 2
Using method of line integration using fourth order Runge-Kutta Method
Eq. (4) Re-written in the form
dCA j CA mj+1 − 2CA mj + CA mj−1 2 CA j +1 − CA j −1
m m

= D AE ( + ) − k1CA mj . . . . (7)
dt (∆r ) 2 rj 2∆r
Eq. (7) represents the concentration change of each point with respect to time and
other points. This equation can be written for each point except the terminal points (at
r=0 and at r=R). Therefore these equations can be solved using Fourth order Runge-
Kutta integration method
R=0.1;% m
CAs=500;% mol/m3
np=20;
r=0:R/(np-1):R;
CAo(1:np)=0;CAo(np)=CAs;
Time=0:100:3600;
[Ti,CA]=ode45('Code',Time,CAo);
[RX,TX]=meshgrid(r,Time);
surf(RX,TX,CA);
xlabel('Radius m')
zlabel('Time (sec)')
ylabel('Concentration (mol/m3)')

Numerical Analysis / M. Sc. 82 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor
function dCAdt=Code(t,CA)
DAE=2e-7;% m2/s
R=0.1;% m
K1=20;
CAs=500;% mol/m3
np=20;
r=0:R/(np-1):R;
dCAdt(np,1)=0;
for j=2:np-1
dCAdt(j,1)=DAE*((CA(j+1)-2*CA(j)+CA(j-1))/((r(j+1)-r(j))^2)...
+(1/(2*r(j)))*(CA(j+1)-CA(j))/(r(j+1)-r(j)));
end
dCAdt(1,1)=dCAdt(2,1);

The results will be such in figure (4.15)

500

400
Time (sec)

300

200

100

0
4000
0.1
2000
0.05

Concentr ation (mol/sec) 0 0 Radius m

Figure (4.15) Un-steady state concentration distribution

Numerical Analysis / M. Sc. 83 Written by Assoc. Prof.


Chap. 4 Dr. Zaidoon M. Shakoor

You might also like