Numerical Analysis M.SC PDF
Numerical Analysis M.SC PDF
Numerical analysis is similar in that problems solved, but the only procedures that
are used are arithmetic: add, subtract, multiply, divide and compare.
• 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.
1) Model Error: due to the mismatch between the physical situation and the
mathematical model.
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.
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.
Example:
• Absolute error = | true value – approximate value |, which is usually used when the
magnitude of the true value is small.
While
true value − approximate value
Percent relative error, ε t = × 100%
true value
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
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:
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
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
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:-
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
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
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
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
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
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
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
O balance: x 3 =4x 5 R R R R
Re-write these as homogeneous equations, each having zero on its right hand
side:
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
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
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
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)
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
dependent)
raffinate mole fraction constraint X Rb +X Rc +X Rf =1 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.
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
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
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
0.06
0.04
0.02
0
f(x)
-0.02
-0.04
-0.06
-0.08
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.
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.
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)
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
x1 x3 x4
x5 x2
A
f(x1)
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
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
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
f(x)
f(x1) B
x3 x2 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 .
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
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.
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 →
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
→
It can be shown that sufficient convergence criteria for two equations are:
∂f1 ∂f
+ 1 <1
∂x1 ∂x2
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
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.
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
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
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
x 0 − x1 2 − 9.125
ε= = = 3.5625
x0 2
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
Setting this equal to f(t,y) and solving for y (t + ∆t ) yields the following algorithm for
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
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:
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)
5.5
4.5
3.5
x(t)
2.5
0.5
0 0.5 1 1.5 2 2.5
Time (t)
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 )
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 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 2 = (0.1+0.05)+(1.11034+1.21034×0.05) = 1.3209
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
Solution
= −2.2067 × 10 −12 (T 4 − 81 × 108 )
dT
dt
Ti+1 = Ti +
h
(k1 + 2k 2 + 2k 3 + k 4 )
6
For i = 0 , t 0 = 0 , T0 = 1200 K
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
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
= −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 )
= −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
Table 1 and Figure 2 show the effect of step size on the value of the calculated
temperature at t = 480 seconds.
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
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)
4
x(t)
0 0.5 1 1.5 2
Time (t)
Figure (3.3): Solution produced by ODE45.
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=).
h
yin + 1 = yin + (k1,i + 2k 2,i + 2k3,i + k4,i ) Where i = 1, 2, ..., m and
6
h n hk1,1 n hk1,2 hk
k 2,i = f i (x n + , y1 + , y2 + …, ymn + 1,m )
2 2 2 2
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
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 )
Example 3.8
Use ode45 Matlab Command to solve the following first order system for y1 and y2 at
0 ≤ x ≤ 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.
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.
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:
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
dy dz1
= = z2 (2)
dx dx
d 2 y dz 2
= = z3 (3)
dx 2 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.
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) 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
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.
x x + ∆x x
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
f (x)
x
x − ∆x x
Figure 4.2 Graphical representation of backward
difference approximation of first derivative
x − ∆x x x + ∆x
∂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
n+1
t
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
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')
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')
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.
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')
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
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.
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.
∆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.
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.
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
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
Solution
Gaussian elimination Method
500
400
Concentration (mol/m3)
300
200
100
0
0 0.02 0.04 0.06 0.08 0.1
Radius (m)
CA = ∆t D AE ( + ) − ∆t k1CA mj + CA mj . . . . (6)
(∆r ) 2 ∆r
j
rj
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)')
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)')
500
400
Time (sec)
300
200
100
0
4000
0.1
2000
0.05