The slides of Chapter 1 of the book entitled "MATALB Applications in Chemical Engineering": Solution of a System of Linear Equations. Author: Prof. Chyi-Tsong Chen (陳奇中 教授); Center for General Education, National Quemoy University; Kinmen, Taiwan; E-mail: chyitsongchen@gmail.com.
Ebook purchase: https://play.google.com/store/books/details/MATLAB_Applications_in_Chemical_Engineering?id=kpxwEAAAQBAJ&hl=en_US&gl=US
2. 01Solution of a System of Linear Equations
solving a system of linear equations with
MATLAB.
the basic concepts and properties of the linear
equation systems are first reviewed,
introduction of the relevant MATLAB commands.
typical examples are provided to demonstrate how
to use MATLAB in the solution of chemical
engineering problems that are formulated with a
system of linear equations.
2
3. 01Solution of a System of Linear Equations
1.1 Properties of linear equation systems and the
relevant MATLAB commands
• 1.1.1 A simple example: the liquid blending problem
3
Tank
number
Volume fraction of each component (%)
A B C D E
1 55.8 7.8 16.4 11.7 8.3
2 20.4 52.1 11.5 9.2 6.8
3 17.1 12.3 46.1 14.1 10.4
4 18.5 13.9 11.5 47.0 9.1
5 19.2 18.5 21.3 10.4 30.6
Table 1.1 Volume fraction of components in each tank.
4. 01Solution of a System of Linear Equations
• Suppose a customer requests a blended product of 40 liters, in
which the volume fraction (%) of each component (A, B, C, D,
and E) should be 25, 18, 23, 18, and 16, respectively.
Assuming that the density is unchanged, determine the volume
usage of each tank to meet the specification.
4
5. 01Solution of a System of Linear Equations
5
Mass balance of component A:
55.8V1 + 20.4V2 + 17.1V3 + 18.5V4 + 19.2V5 = 25×40
(1.1-1a)
Mass balance of component B:
7.8V1 + 52.1 V2 + 12.3 V3 + 13.9 V4 + 18.5 V5 = 18×40
(1.1-1b)
Mass balance of component C:
16.4V1 + 11.5 V2 + 46.1 V3 + 11.5 V4 + 21.3 V5 = 23×40
(1.1-1c)
Mass balance of component D:
11.7V1 + 9.2 V2 + 14.1 V3 + 47.0 V4 + 10.4 V5 = 18×40
(1.1-1d)
Mass balance of component E:
6. 01Solution of a System of Linear Equations
6
(1.1-2a)
(1.1-2b)
7. 01Solution of a System of Linear Equations
7
>> A= [ 55.8 20.4 17.1 18.5 19.2
7.8 52.1 12.3 13.9 18.5
16.4 11.5 46.1 11.5 21.3
11.7 9.2 14.1 47.0 10.4
8.3 6.8 10.4 9.1 30.6]; % matrix A
>> b=[1000 720 920 720 640]'; % vector b (column vector)
>> x =Ab % find the solution
x=
6.8376
4.1795
8.6398
7.3268
13.0163
8. 01Solution of a System of Linear Equations
1.1.2 Relevant properties of linear equation
systems
8
Ax = b
10. 01Solution of a System of Linear Equations
• Existence and uniqueness of solutions:
The solution of Ax = b exists if and only if the following
condition holds (Wylie and Barrett, 1995)
rank(A) = rank([A b])
10
Types
of solution
Conditions
Non-homogeneous
equations
Ax = b
Homogeneous equations
Ax = 0
Case 1
rank(A) = rank([A b])
rank(A) = n
Unique solution
Unique solution
x = 0 (trivial solution)
Case 2
rank(A) = rank([A b])
rank(A) < n
Infinite number of
solutions
Infinite number of solutions,
including nonzero solutions
Case 3 rank(A) < rank([A b]) No solution
Table 1.2 Solution types of a system of linear equations.
11. 01Solution of a System of Linear Equations
1.1.3 Relevant MATLAB commands
11
>> A=[1 2; 3 4] % input the matrix A to be evaluated
A=
1 2
3 4
>> rank(A) % calculating the rank
ans =
2
>> A = [1 2; 3 4] % input the matrix A to be transformed
A =
1 2
3 4
>> rref(A)
ans =
1 0
0 1
12. 01Solution of a System of Linear Equations
• Method 1: finding the solution by an inverse matrix, x = A1b
12
>> b = [1 2]'
b =
1
2
>> rank([A b])
ans =
2
>> x = inv (A)*b % as A is a full rank matrix, A1 exists
x=
0
0.5000
13. 01Solution of a System of Linear Equations
• Method 2: finding the solution with the MATLAB command
x = Ab
13
>> x =Ab
x=
0
0.5000
• Method 2: finding the solution with the MATLAB command
x = Ab
>> rref([A b])
ans=
1.0000 0 0
0 1.0000 0.5000
14. 01Solution of a System of Linear Equations
Example 1-1-1
14
(1.1-6)
Ans:
>> A=[1 2 3; 1 1 1]; % coefficient matrix A
>> b=[2 4]'; % vector b
>> r1=rank(A)
r1=
2
>> r2=rank([A b]) % r1= r2 and r1<3, therefore an infinite number of
solutions exist.
r2=
2
15. 01Solution of a System of Linear Equations
Example 1-1-1
15
Ans:
>> x=Ab % NOTE: when y is set to 0, one of the solutions is found.
x=
5.0000
0
-1.0000
>> x=pinv(A)*b % the solution that is nearest to the origin is found.
x=
4.3333
1.3333
-1.6667
16. 01Solution of a System of Linear Equations
Example 1-1-2
16
(1.1-7)
when the parameter a is equal to 9 and 10.
17. 01Solution of a System of Linear Equations
Example 1-1-2
17
Ans:
>> A=[1 1; 1 2; 1 5];
>> b= [1 3 9]';
>> r1=rank(A)
r1=
2
>> r2=rank([ A b]) % r1=r2 and r1=n=2, the solution is unique
r2=
2
>> X=Ab
X=
-1.0000
2.0000
(1) Case 1: a = 9
18. 01Solution of a System of Linear Equations
Example 1-1-2
18
Ans:
>> A=[1 1; 1 2; 1 5];
>> b= [1 3 9]';
>> r1=rank(A)
r1=
2
>> r2=rank([ A b]) % r1<r2; no solution theoretically exists
r2=
3
>> X=Ab
X=
-1.3846
2.2692
(2) Case 2: a = 10
19. 01Solution of a System of Linear Equations
19
Command Description
inv(A) Calculate the inverse matrix of A when det(A) ≠ 0
pinv(A) Calculate the pseudo inverse matrix of A when det(A)=0
rank(A) Determine the rank of the matrix A
rref(A) Transform matrix A into its reduced row Echelon form
x = inv(A)*b Find the unique solution x
x = pinv(A)*b Find the solution of x that is closest to the origin
x = Ab Find the solution x, and the meaning of this solution depends on
the characteristics of the system of linear equations
NOTE: In addition, MATLAB has a right divisor by which the simultaneous
linear equations xA = b can be solved, where 𝐱 ∈ 𝑅1×𝑛, 𝐀 ∈ 𝑅 𝑛×𝑚, 𝑎𝑛𝑑 𝐛 ∈ 𝑅1×𝑚.
Its usage is x = b/A.
20. 01Solution of a System of Linear Equations
1.2 Chemical engineering examples
Example 1-2-1
Composition analysis of a distillation column system
Figure 1.1 schematically illustrates a distillation column system
that is used to separate the following four components: para-
xylene, styrene, toluene, and benzene (Cutlip and Shacham, 1999).
Based on the required composition of each exit stream shown in
this figure, calculate (a) the molar flow rates of D2, D3, B2, and B3,
and (b) the molar flow rates and compositions of streams of D1
and B1.
20
21. 01Solution of a System of Linear Equations
Problem formulation and analysis:
(a) overall mass balance
Para-xylene: 0.07D2 + 0.18B2 + 0.15D3 + 0.24B3 = 0.15 80
Styrene: 0.03D2 + 0.25B2 + 0.10D3 + 0.65B3 = 0.25 80
Toluene: 0.55D2 + 0.41B2 + 0.55D3 + 0.09B3 = 0.40 80
Benzene: 0.35D2 + 0.16B2 + 0.20D3 + 0.02B3 = 0.20 80
Ax = b (1.2-1)
21
22. 01Solution of a System of Linear Equations
Problem formulation and analysis:
(a) overall mass balance
22
and
x= [D2 B2 D3 B3]T
24. 01Solution of a System of Linear Equations
Problem formulation and analysis:
(b)
Para-xylene : D1 x1=0.07D2+0.18B2
Styrene : D1 x2=0.03D2+0.25B2
Toluene : D1 x3=0.55D2+0.41B2
Benzene : D1 x4=0.35D2+0.16B2
24
D1 = D2 + B2
25. 01Solution of a System of Linear Equations
Problem formulation and analysis:
(b)
25
B1 = D3 + B3
26. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_1.m ───────────────
%
% Example 1-2-1 Composition analysis of a distillation column system
%
clear
clc
%
% given data
%
A=[0.07 0.18 0.15 0.24
0.03 0.25 0.10 0.65
0.55 0.41 0.55 0.09
0.35 0.16 0.20 0.02]; % coefficient matrix
b=80*[0.15 0.25 0.40 0.20]'; % coefficient vector
%
r1=rank(A); % the rank of matrix A
r2=rank([A b]); % the rank of the augmented matrix
26
27. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_1.m ───────────────
[m,n]=size(A); % determine the size of the matrix A
if (r1==r2)&(r1==n) % check the uniqueness of the solution
%
% solving for D2,B2,D3,B3
%
x=Ab;
D2=x(1); B2=x(2); D3=x(3); B3=x(4);
disp('flow rate at each outlet (kg-mol/min)')
disp(' D2 B2 D3 B3')
disp([D2 B2 D3 B3])
%
% calculating the composition of the stream D1
%
D1=D2+B2;
Dx=A(:,[1 2])*[D2 B2]'/D1;
disp('The composition of the stream D1:')
disp(Dx‘)
27
28. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_1.m ───────────────
%
% calculating the composition of the stream B1
%B1=D3+B3;
Bx=A(:,[3 4])*[D3 B3]'/B1;
disp('The composition of the stream B1:')
disp(Bx')
else
error('The solution is not unique.')
fprintf('n The rank of matrix A is %i.',rank(A))
end
─────────────────────────────────────────────────
28
29. 01Solution of a System of Linear Equations
Execution results:
>> ex1_2_1
flow rate at each outlet (kg-mol/min)
D2 B2 D3 B3
29.8343 14.9171 13.7017 21.5470
The composition of the stream D1:
0.1067 0.1033 0.5033 0.2867
The composition of the stream B1:
0.2050 0.4362 0.2688 0.0900
29
30. 01Solution of a System of Linear Equations
Example 1-2-2
Temperature analysis of an insulated stainless steel pipeline
Figure 1.2 depicts a stainless steel pipe whose inner and outer diameters are 20
mm and 25 mm, respectively; i.e., D1 = 20 mm and D2 = 25 mm. This pipe is
covered with an insulation material whose thickness is 35 mm. The
temperature of the saturated vapor (steam) flowing through the pipeline is Ts =
150C, and the external and internal convective heat transfer coefficients are,
respectively, measured to be hi = 1,500 W/m2K and h0 = 5 W/m2K. Besides,
the thermal conductivity of the stainless steel and the insulation material are ks
= 45 W/mK and ki = 0.065, respectively. If the ambient temperature is Ta =
25C, determine the internal and external wall temperatures of the stainless
steel pipe, T1 and T2 , and the exterior wall temperature of the insulation
material, T3.
30
31. 01Solution of a System of Linear Equations
Problem formulation and analysis:
31
Figure 1.2 A stainless steel pipe covered with an external insulation material.
32. 01Solution of a System of Linear Equations
Problem formulation and analysis:
Heat transfer from steam to pipe
32
Heat transfer from pipe to the insulation material
Heat transfer from insulation material to the surroundings
33. 01Solution of a System of Linear Equations
Problem formulation and analysis:
33
34. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_2.m ───────────────
%
% Example 1-2-2 Temperature analysis of an insulated pipeline
%
clear
clc
%
% given data
%
D1=20/1000; % inner diameter (converting mm to m)
D2=25/1000; % outer diameter (converting mm to m)
DI=35/1000; % thickness of insulating materials (m)
34
35. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_2.m ───────────────
Ts=150+273; % vapor temperature (K)
Ta=25+273; % ambient air temperature (K)
hi=1500; interior convective heat transfer coefficient (W/m^2.K)
ho=5; % exterior convective heat transfer coefficient (W/m^2.K)
ks=45; % heat conductivity coefficient of the steel pipe (W/m.K)
ki=0.065; % heat conductivity coefficient of the insulation material (W/m.K)
%
D3=D2+2*DI; % the total diameter including the insulation material
%
% coefficient matrix A
%
35
36. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_2.m ───────────────
A=[2*ks/log(D2/D1)+hi*D1 -2*ks/log(D2/D1) 0
ks/log(D2/D1) -ks/log(D2/D1)-ki/log(D3/D2) ki/log(D3/D2)
0 2*ki/log(D3/D2) -2*ki/log(D3/D2)-ho*D3];
%
% coefficient vector b
%
b=[hi*D1*Ts 0 -ho*D3*Ta]';
%
% check the singularity
%
if det(A) == 0
36
37. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_2.m ───────────────
fprintf('n rank of matrix A=%i', rank(A))
error('Matrix A is a singular matrix.') % exit in case of error
end
%
% det(A) ~= 0, a unique solution exists
%
T=Ab; % find the solution
%
% results printing
%
fprintf('n T1=%.3f °C n T2=%.3f °C n T3=%.3f °C n',T-273)
─────────────────────────────────────────────────
37
38. 01Solution of a System of Linear Equations
Execution results:
>> ex1_2_2
T1=149.664 °C
T2=149.639 °C
T3=46.205 °C
38
39. 01Solution of a System of Linear Equations
Example 1-2-3
Composition analysis of a set of CSTRs
Figure 1.3 schematically illustrates a set of continuous stirred-tank reactors
(CSTRs) in which the reaction 𝐴
𝑘 𝑗
𝐵 is occurring in the tank i
(Constantinides and Mostoufi, 1999).
39
40. 01Solution of a System of Linear Equations
Example 1-2-3
Composition analysis of a set of CSTRs
40
Figure 1.3 A set of continuous stirred-tank reactors.
41. 01Solution of a System of Linear Equations
41
Tank number Volume Vi (L) Reaction constant ki (h1)
1 1,000 0.2
2 1,200 0.1
3 300 0.3
4 600 0.5
42. 01Solution of a System of Linear Equations
• Furthermore, this reaction system is assumed to possess the
following properties:
1) The reaction occurs in the liquid phase, and its steady state
has been reached.
2) The changes of liquid volumes and density variations in
the reaction tanks are negligible.
3) The reaction in the tank i obeys the first-order reaction kinetics and
the reaction rate equation is expressed as follows:
ri =VikiCAi (1.2-8)
• Determine the exit concentration of each reaction tank, i.e.,
CAi = ? for i = 1, 2, 3, and 4.
42
43. 01Solution of a System of Linear Equations
Problem formulation and analysis:
input=output + quantity lost due to reaction
Tank 1: 1,000CA0 = 1,000CA1+V1k1CA1 (1.2-9a)
Tank 2: 1,000CA1 + 100CA3 =1,100CA2+V2k2CA2 (1.2-9b)
Tank 3 1,100CA2 + 100CA4 =1,200CA3+V3k3CA3 (1.2-9c)
Tank 4: 1,100CA3 = 1,100CA4 +V4k4CA4 (1.2-9d)
43
44. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_3.m ───────────────
%
% Example 1-2-3 Composition analysis of a set of CSTRs
%
clear
clc
%
% given data
%
V=[1000 1200 300 600]; % volume of each tank (L)
k=[0.2 0.1 0.3 0.5]; % reaction constant in each tank (1/h)
%
V1=V(1); V2=V(2); V3=V(3); V4=V(4);
k1=k(1); k2=k(2); k3=k(3); k4=k(4);
%
% the coefficient matrix
%
A=[1000+V1*k1 0 0 0
44
45. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_3.m ───────────────
1000 -(1100+V2*k2) 100 0
0 1100 -(1200+V3*k3) 100
0 0 1100 -(1100+V4*k4)];
%
% the coefficient vector b
%
b=[1000 0 0 0]';
%
% check whether A is a singular matrix
%
if det(A) == 0
fprintf('n rank=%i n', rank(A))
error('Matrix A is a singular matrix.')
end
%
% finding a solution%
45
46. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_3.m ───────────────
Ab=rref([A b]);
CA=Ab(:, length(b)+1);
%
% results printing
%
disp('The exit concentration of each reactor is (mol/L):')
for i=1:length(b)
fprintf('CA(%d)=%5.3f n',i,CA(i))
end
─────────────────────────────────────────────────
46
47. 01Solution of a System of Linear Equations
Execution results:
>> ex1_2_3
The exit concentration of each reactor is (mol/L):
CA(1)=0.833
CA(2)=0.738
CA(3)=0.670
CA(4)=0.527
47
48. 01Solution of a System of Linear Equations
Example 1-2-4
Independent reactions in a reaction system
48
Let A1, A2, A3, and A4 denote C2H4, H2, CH4, and C2H6
respectively.
49. 01Solution of a System of Linear Equations
49
>> A=[1 1 0 -1; 1 0 2 -2];
>> rank(A)
ans=
2
51. 01Solution of a System of Linear Equations
Problem formulation and the MATLAB solution:
Let NH3, O2, N2, H2O, NO2, and NO be respectively denoted as
A1 – A6
51
52. 01Solution of a System of Linear Equations
Problem formulation and the MATLAB solution:
>> A=[-4 -5 0 6 0 4
-4 -3 2 6 0 0
-4 0 5 6 0 -6
0 -1 0 0 2 -2
0 1 1 0 0 -2
0 -2 -1 0 2 0 ];
>> r=rank(A)
r=
3
52
53. 01Solution of a System of Linear Equations
Problem formulation and the MATLAB solution:
>> [a, jb]=rref(A’) % determine the independent reactions based
on AT
a=
1.0000 0 -1.5000 0 -0.5000 0.5000
0 1.0000 2.5000 0 0.5000 -0.5000
0 0 0 1.0000 0 1.0000
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
jb=
1 2 4
53
54. 01Solution of a System of Linear Equations
Example 1-2-4
Composition distributions in a distillation column
Figure 1.4 schematically depicts a distillation column which has a
total condenser at the top, a reboiler at the bottom, and the side
streams for liquid removal. In a two-component system, the
equilibrium relationship can be expressed by the following
equation (Wang and Henke, 1966):
54
(1.213)
55. 01Solution of a System of Linear Equations
where x1 and x2 represent, respectively, the concentration of
component 1 (the one with a lower boiling point) and that of
component 2 (the one having a higher boiling point) in the liquid
phase, and y 1
M
denotes the concentration of component 1 in the
gas phase. Suppose the parameters in (1.2-13) are estimated to be
α1 = 2.0 and α2 = 1.0 and the operating conditions are as follows:
55
56. 01Solution of a System of Linear Equations
1) The distillation column contains 10 plates, and the raw
material, which is fed into the fifth plate, is with a flow rate of
F = 1.0 and has the q value of 0.5. Besides, the feed
composition is z1 = 0.5 and z2 = 0.5.
2) The reflux ratio is R = 3.
3) The distillate is D = 0.5 and the bottom product is B =0.5.
Based on the above-mentioned operating conditions, determine
the composition distribution on each plate of the distillation
column.
56
57. 01Solution of a System of Linear Equations
Problem formulation and analysis:
(1) Mass balance for component i on plate j
57
Variable Description
Vj Amount of vapor moving upward from plate j (excluding Wj)
Wj Amount of vapor removed from plate j
Lj Amount of liquid moving downward from plate j (excluding Uj)
Uj Amount of liquid removed from plate j
xij Concentration of component i in the liquid phase on plate j
yij Concentration of component i in the vapor phase on plate j
Fj Amount of materials fed into plate j
zij Concentration of component i in the raw materials fed into plate j
58. 01Solution of a System of Linear Equations
Problem formulation and analysis:
(2) The overall mass balance around the total condenser and plate j
58
where
D = V 1 + U1
59. 01Solution of a System of Linear Equations
59
Figure 1.4 A distillation column
with side streams.
60. 01Solution of a System of Linear Equations
Problem formulation and analysis:
(3) The gas and liquid flow rates affected by the q-value
60
Vj + 1 (1 + q)Fj = Vj + Wj
Lj−1 + qFi = Lj + Uj
61. 01Solution of a System of Linear Equations
Problem formulation and analysis:
(3) The gas and liquid flow rates affected by the q-value
61
for j = 2, 3, …, N − 1 and those for j = N are
62. 01Solution of a System of Linear Equations
MATLAB program design:
62
Stop
Giving operational conditions
initializing the concentration
on each plate (Note 1)
M
ij ijy and K are respectively computed
using (1.2-13) and (1.2-19).
The new value of xij is
obtained using Equation (1.2-20).
Check if convergence
occurs using (1.2-21)
xij = xij xij
(Note 2)
Yes
No
print results
63. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
%
% Example 1-2-5: composition distributions in a distillation column
%
clear; close all;
clc
%
% given data
%
N=10; % total number of plates (including the total condenser and reboiler)
m=2; % no. of components
q=0.5; % the q value
F=1; % flow rate of the feed
z=[0.5 0.5]; % composition of the feed
R=3; % reflux ratio
D=0.5; % molar flow rate of the distillate
B=0.5; % molar flow rate of the bottom product
JF=5; % position of the feed plate
alpha1=2; % equilibrium constant in equation (1.2-13)
63
64. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
alpha2=1; % equilibrium constant in equation (1.2-13)
TOL=1.e-5; % tolerance value
%
% operating data of the distillation column
%
% side streams
%
FI=zeros(1,N);
FI(JF)=F; % the feed flow rate at the feed plate
%
% composition of the side stream inlets
%
zc=zeros(m,N);
zc(:,JF)=z'; % composition of the feed at the feed plate
%
% flow rate at the side stream outlets, W and U
%
64
65. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
W=zeros(1, N);
U=zeros(1, N);
U(1)=0.5; % output at the top
%
% initial concentration change of each plate
%
x=zeros(m, N);
y=x;
%
for i=1:m
x(i,:)=z(m)*ones(1,N); % initial guess values
end
%
% vapor and liquid flow rates at each plate
%
Q=zeros(1,N);
Q(JF)=q; % status of the feed
65
66. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
L(1)=R*D;
V(1)=D-U(1);
V(2)=L(1)+V(1)+U(1);
FL=FI.*Q;
FV=FI.*(1-Q);
for j=2:N-1
L(j)=L(j-1)+FL(j)-U(j);
end
L(N)=B;
for j=3:N
V(j)=V(j-1)-FV(j-1)+W(j-1);
end
%
%
ICHECK=1; % stopping flag: 1= not convergent yet
it_no=1;
%
% iteration begins
66
67. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
%
while ICHECK == 1
%
% normalization of concentration
%
for i=1:m
x(i,:)=x(i,:)./sum(x);
end
%
% calculating the vapor phase equilibrium composition using (1.2-13)
%
y(1,:)=alpha1*x(1,:)./(alpha1*x(1,:)+alpha2*x(2,:));
y(2,:)=1-y(1,:);
%
% calculating the equilibrium constant
%
y(1,:)=alpha1*x(1,:)./(alpha1*x(1,:)+alpha2*x(2,:));
y(2,:)=1-y(1,:);
67
68. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
%
% calculating the equilibrium constant
%
K=y./x;
%
% finding the liquid phase concentration
%
for i=1:m
for j=2:N-1
a(j)=L(j-1);
b(j)=-(V(j)+W(j))*K(i,j)-L(j)-U(j);
c(j)=V(j+1)*K(i,j+1);
d(j)=-FI(j)*zc(i,j);
end
b(1)=-L(1)-V(1)*K(i,1)-U(1);
c(1)=V(2)*K(i,2);
d(1)=0;
68
69. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
a(N)=L(N-1);
b(N)=-V(N)*K(i,N)-B;
d(N)=-FI(N)*zc(i,N);
%
% forming the coefficient matrix
%
A=zeros(N,N);
A(1,:)=[b(1) c(1) zeros(1,N-2)];
A(2,:)=[a(2) b(2) c(2) zeros(1,N-3)];
for j=3:N-2
A(j,:)=[zeros(1,j-2) a(j) b(j) c(j) zeros(1,N-j-1)];
end
A(N-1,:)=[zeros(1,N-3) a(N-1) b(N-1) c(N-1)];
A(N,:)=[zeros(1,N-2) a(N) b(N)];
if det(A) == 0 % check the singularity of matrix A
error('det(A) = 0, singular matrix')
end
69
70. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
%
% finding a solution
%
x(i,:)=(Ad')';
end
%
% checking convergence
%
y_eq=K.*x;
%
y(1,:)=alpha1*x(1,:)./(alpha1*x(1,:)+alpha2*x(2,:));
y(2,:)=1-y(1,:);
%
difference=abs(y-y_eq);
err=sum(sum(difference)); % error
%
if err<= TOL
ICHECK=0; % convergence has been reached
70
71. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
else
it_no=it_no+1; % not yet convergent and do next iteration
end
end
%
% results printing
%
fprintf('n iteration no.=%in',it_no)
disp('composition at each plate:')
disp('plate no. x1 x2 y1 y2')
for j=1:N
fprintf('%3i %15.5f %10.5f %10.5f %10.5fn',j,x(1,j),x(2,j),y(1,j),y(2,j))
end
%
% results plotting
%
figure(1)
71
72. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_5.m ───────────────
plot(1:N,x(1,:),'r',1:N,x(2,:),'-.b')
xlabel('plate number')
ylabel('x1 and x2')
legend('x1','x2')
title('liquid phase composition at each plate')
%
figure(2)
plot(1:N,y(1,:),'r',1:N,y(2,:),'-.b')
xlabel('plate number')
ylabel('y1 and y2')
legend('y1','y2')
title('vapor phase composition at each plate')
─────────────────────────────────────────────────
72
76. 01Solution of a System of Linear Equations
Example 1-2-6
Steady-state analysis of a batch reaction system
Consider a batch reactor in which the following reactions are occurring
(Constantinides and Mostoufi, 1999):
76
77. 01Solution of a System of Linear Equations
All the reactions obey the first-order kinetic mechanism, and,
under the operating condition of constant pressure and
temperature, the reaction rates are listed in the following table:
77
k21 k31 k32 k34 k54 k64 k65
0.1 0.1 0.1 0.1 0.05 0.2 0.15
k12 k13 k23 k43 k45 k46 k56
0.2 0.05 0.05 0.2 0.1 0.2 0.2
Besides, the initial concentration (mol/L) of each component is
as follows:
A0 B0 C0 D0 E0 F0
1.5 0 0 0 1.0 0
Calculate the concentration of each component as a steady state
is reached.
78. 01Solution of a System of Linear Equations
Problem formulation and analysis:
78
79. 01Solution of a System of Linear Equations
Problem formulation and analysis:
79
80. 01Solution of a System of Linear Equations
Problem formulation and analysis:
80
81. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_6.m ───────────────
%
% Example 1-2-6 Steady-state analysis of a batch reaction system
%
clear
clc
close all
%
% given data
%
k21=0.1; k12=0.2; k31=0.1; k13=0.05; k32=0.1; k23=0.05; k34=0.1;
k43=0.2; k54=0.05; k45=0.1; k64=0.2; k46=0.2; k65=0.15; k56=0.2;
%
% initial concentration
%
x0=[1.5 0 0 0 1 0]';
%
% Coefficient matrix
%
81
82. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_6.m ───────────────
A=[-(k21+k31) k12 k13 0 0 0
k21 -(k12+k32) k23 0 0 0
k31 k32 -(k13+k23+k43) k34 0 0
0 0 k43 -(k34+k54+k64) k45 k46
0 0 0 k54 -(k45+k65) k56
0 0 0 k64 k65 -(k46+k56)];
%
[m,n]=size(A);
[v,d]=eig(A); % the eigenvalue and eigenvector of A
%
lambda=diag(d); % eigenvalue
%
c=vx0; % finding the coefficient vector C
%
check=abs(lambda)<=eps; % check if the eigenvalue is close to 0
%
% steady state value computation
82
83. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_6.m ───────────────
%
C=c.*check;
x_final=v*C;
%
disp(' ')
disp('steady-state concentration of each component (mol/L)')
disp(' A B C D E F')
disp(x_final')
─────────────────────────────────────────────────
83
84. 01Solution of a System of Linear Equations
Execution results:
>> ex1_2_6
steady-state concentration of each component (mol/L)
84
A B C D E F
0.2124 0.1274 0.3398 0.6796 0.5825 0.5583
NOTE:
expm(A*t)
85. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_6b.m ───────────────
%
% Solve Example 1-2-6 with the matrix function method
%
clear; close all;
clc
%
% given data
%
k21=0.1; k12=0.2; k31=0.1; k13=0.05; k32=0.1; k23=0.05; k34=0.1;
k43=0.2; k54=0.05; k45=0.1; k64=0.2; k46=0.2; k65=0.15; k56=0.2;
%
% initial concentration
%
x0=[1.5 0 0 0 1 0]';
%
% forming the coefficient matrix
%
A=[-(k21+k31) k12 k13 0 0 0
85
86. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_6b.m ───────────────
k21 -(k12+k32) k23 0 0 0
k31 k32 -(k13+k23+k43) k34 0 0
0 0 k43 -(k34+k54+k64) k45 k46
0 0 0 k54 -(k45+k65) k56
0 0 0 k64 k65 -(k46+k56)];
%
t=0; %
h=1; % time increment
x_old=x0;
i_check=1;
tout=0;
xout=x0';
while i_check == 1 % continues if not yet converged
t=t+h;
x_new=expm(A*t)*x0; % calculating the state value
if max(x_new-x_old) <= 1.e-6 % check for convergence
i_check=0;
end
86
87. 01Solution of a System of Linear Equations
MATLAB program design:
─────────────── ex1_2_6b.m ───────────────
tout=[tout;t];
xout=[xout; x_new'];
x_old=x_new;
end
disp(' ')
disp('steady-state concentration of each component (mol/L)')
disp(' A B C D E F')
disp(x_new')
%
% plot the results
%
plot(tout,xout')
xlabel('time')
ylabel('states')
legend('A','B','C','D','E','F')
─────────────────────────────────────────────────
87
88. 01Solution of a System of Linear Equations
Execution results:
>> ex1_2_6b
steady-state concentration of each component (mol/L)
88
A B C D E F
0.2124 0.1274 0.3398 0.6796 0.5825 0.5582
90. 01Solution of a System of Linear Equations
1.4 Summary of the MATLAB commands related
to this chapter
90
Command Function
det Matrix determinant
rank Rank of a matrix
rref Reduced row Echelon form of a matrix
Solution of the linear equation system Ax = b, x = ?
/ Solution of the linear equation system xA = b, x = ?
inv Matrix inverse
pinv The Moore-Penrose pseudo inverse of a matrix