Sie sind auf Seite 1von 14

CHE654

Modeling Chemical Processes


Using MATLAB

Homework Set #1 Solutions


Class-16

Prepared by

Dr. Hong-ming Ku

King Mongkuts University of Technology Thonburi


Chemical Engineering Department
Chemical Engineering Practice School
July 2004 - 2012 Use with Permission of the Author Only

1. System of Linear Algebraic Equations, I


Use MATLAB to solve the following system of linear algebraic equations, correct to 4
decimal places:
x1 + 2x2 + x3 + x5 1
2x1 4x2 x3 + x4 + x5 + 2
2x2 + 4x3 + x4 6x5 + 6
3x1 + x4 2x5 4
x1 + x2 + x3 + 3x4 + 8x5

=
=
=
=
=

0
0
0
0
0

Solution:
The MATLAB M-file:
%
% CHE654 Problem #1: System of Linear Algebraic Equations
%
clc
clear
A = [1 2 1 0 1; 2 -4 -1 1 1; 0 -2 4 1 -6; -3 0 0 1 -2; 1 1 1 3 8];
b = [1 -2 -6 4 0]';
x = inv(A)*b
The MATLAB output:
x=
1.0000
2.5000
-3.5000
4.0000
-1.5000

3. System of Nonlinear Algebraic Equations


Use MATLAB to solve the following system of nonlinear algebraic equations:
exp(2x1) x2 4
x2 x32 1
x3 sin(x1)

= 0
= 0
= 0

Solution:
The MATLAB M-file:
%
% CHE654 Problem #3: System of Nonlinear Algebraic Equations
%
clc
clear
format short
x = solve('exp(2*x1)-x2-4=0','x2-x3^2-1=0','x3-sin(x1)=0')
double(x.x1)
double(x.x2)
double(x.x3)
The MATLAB output:
ans =
0.8590

 x1

ans =
1.5733

 x2

ans =
0.7572

 x3

4. Pressures in a Pipeline Network


Consider the following interconnected liquid pipeline network which contains 4 valves
with the following valve coefficients. Calculate the pressure at each location/node
indicated in the network.
Cv,1 = 0.2 ft3-(psia)1/2/hr
Cv,3 = 0.4 ft3-(psia)1/2/hr

Cv,2 = 0.1 ft3-(psia)1/2/hr


Cv,4 = 0.3 ft3-(psia)1/2/hr

1
P = 50 psia

Cv,1

Pump
Cv,2

Cv,3
2

Cv,4

P = 14.7 psia

Reservoir

Solution:
The MATLAB M-file:
%
% CHE654 Problem #4: Pressures in a Pipeline Network
%
clc
clear
format short
%
% These equations can be set up for the pressure at each node using mass balance.
%
% At node 1: Q1 = 0.2*sqrt(50-p1) = 0.1*sqrt(p1-14.7) + 0.4*sqrt(p1-p2)
% At node 2: Q2 = 0.4*sqrt(p1-p2) = 0.3*sqrt(p2-14.7)
%
p = solve('0.2*sqrt(50-p1)-0.1*sqrt(p1-14.7)-0.4*sqrt(p1-p2)=0', ...
'0.4*sqrt(p1-p2)-0.3*sqrt(p2-14.7)=0')
p.p1
p.p2
The MATLAB output:
ans =
23.774550128534704370179948586118

 P1

ans =
20.507712082262210796915167095116

 P2
4

5. Simple Calculations and Array Manipulations in MATLAB, I


Consider the following arrays:

A =

2
4
20 5
8
10
3

6
40
2
9

B = log10(A)

C =

2 4 1 8
9 11 23 7
0 -1 -5 6

Write MATLAB expressions in an M-file (script file) to do the following (use format
short in all calculations unless otherwise told):
(a) Determine the sum of the second column of B.
(b) Select the first 3 columns and the first 3 rows of A and B and add their determinants
.
(c) Determine the square root of the product (a scalar) between the last row of C and
the second column of A.
(d) Determine the sum of the second row of A divided element-by-element by the third
column of C.
(e) Evaluate the hyperbolic secant of (0.01 * A * C).
(f) Evaluate the following function: tan-1 (0.1 * B * C) in 6 decimal places.
Solution:
The MATLAB M-file:
%
% CHE654 Problem #5: Simple Calculations and Array Manipulations in MATLAB
%
clc
clear
format short
A = [2 4 6; 20 5 40; 8 10 2; 3 pi 9]
B = log10(A)
C = [2 4 1 8; 9 11 23 7; 0 -1 -5 6]
%
% Determine the sum of the second row of B
%
column2_B = B(:,2);
sum_column2_B = sum(column2_B)
%
% Select the first 3 columns and first 3 rows of A and B and add their determinants
%
A33 = A(1:3,1:3);
B33 = B(1:3,1:3);
sum_det = det(A33) + det(B33)
%
5

% Determine the square root of the product between the 2nd column of A and the
% last row of C
%
row3_C = C(3,:);
column2_A = A(:,2);
product = row3_C*column2_A;
square_root = sqrt(product)
%
% Determine the sum of the second row of A divided element-by-element by the
% the third column of C.
%
row2_A = A(2,:);
col3_C = C(:,3)';
row2A_col3C = row2_A./col3_C;
Sum = sum(row2A_col3C)
%
% Evaluate the hyperbolic secant of 0.01*A*C
%
A_C = 0.01*A*C;
hyperbolic_secant_AC = sech(A_C)
%
% Evaluate the inverse tangent of 0.1*B*C
%
B_C = 0.1*B*C;
inverse_tangent_BC = atan(B_C);
fprintf ('inverse_tangent_BC\n')
for i = 1:4
fprintf ('\n')
eachrow = inverse_tangent_BC(i,:);
fprintf ('%10.6f', eachrow)
end

The MATLAB output:


A=
2.0000
20.0000
8.0000
3.0000
B=
0.3010
1.3010
0.9031
0.4771
C=
2 4
9 11
0 -1

4.0000 6.0000
5.0000 40.0000
10.0000 2.0000
3.1416 9.0000
0.6021
0.6990
1.0000
0.4971

0.7782
1.6021
0.3010
0.9542

1 8
23 7
-5 6

sum_column2_B =
2.7982
sum_det =
1.3007e+003
square_root =
0 + 6.0125i
Sum =
12.2174
hyperbolic_secant_AC =
0.9250
0.7228
0.6187
0.9440

0.9028
0.6728
0.4649
0.9334

0.8252
0.8205
0.2025
0.9559

0.7477
0.0258
0.4407
0.6481

inverse_tangent_BC
0.541933
0.726860
0.824126
0.497344

0.613982
0.845948
0.960912
0.570935

0.798116
0.752727
1.150882
0.620084

0.845985
1.189092
1.013064
0.915935

7. Inverse and Determinant of 22 and 33 Matrices


Use MATLAB to write a program that can determine the inverse and determinants of
any 22 and 33 matrices, which can then be used to solve a system of linear algebraic
equations in 2 or 3 unknowns.
Recall that the formulae for calculating the determinant of 22 and 33 matrices are as
follows:

If

a
11
A=
a 21

If

a
11

A = a 21

a
31

a12

a 22

a12
a 22
a32

then

a13

a 23

a33

and

det( A) = a11 a 22 a12 a 21

a
a12
1 22

A =

det( A) a
21 a11

det( A) = a11 (a 33 a 22 a 32 a 23 ) a 21 (a33 a12 a 32 a13 )

then

+ a31 (a 23 a12 a 22 a13 )

a a a a
32 23
33 22
1
A 1 =
a31 a 23 a 33 a 21
det( A)
a a a a
31 22
32 21

a32 a13 a 33 a12


a 33 a11 a 31 a13
a31 a12 a 32 a11

a 23 a12 a 22 a13

a 21 a13 a 23 a11

a 22 a11 a 21 a12

Use the above formulae to solve the following two systems of linear algebraic
equations:
(a)

2 x1 3 x 2 = 6

x1 + 2 x 2 = 1

(b)

x1 + 2 x 2 6 x3 = 2

5 x1 + 3 x 2 + x3 = 0

Answers: (a) x1 = __9__, x2 = __4__

4 x1 x 2 2 x3 = 3

(b) x1 = __2.80__, x2 = __3.96__, x3 = __2.12__

Solution:
MATLAB Script File:
%
% CHE654: Inverse and Determinant of a 2x2 and 3x3 Matrices
%
clc
clear
% Solve 2 linear algebraic equations in 2 unknowns

a = [2 -3; -1 2];
detA = a(1,1)*a(2,2)-a(1,2)*a(2,1);
invA = [a(2,2) -a(1,2); -a(2,1) a(1,1)]./detA;
b = [6; -1];
x = invA*b
% Solve 3 linear algebraic equations in 3 unknowns
a = [1 2 -6; -5 3 1; 4 -1 -2];
b = [-2; 0; 3];
detA = a(1,1)*(a(3,3)*a(2,2)-a(3,2)*a(2,3)) - a(2,1)*(a(3,3)*a(1,2)- ...
a(3,2)*a(1,3)) + a(3,1)*(a(2,3)*a(1,2)-a(2,2)*a(1,3));
invA = [a(3,3)*a(2,2)-a(3,2)*a(2,3) a(3,2)*a(1,3)-a(3,3)*a(1,3) ...
a(2,3)*a(1,2)-a(2,2)*a(1,3); a(3,1)*a(2,3)-a(3,3)*a(2,1) ...
a(3,3)*a(1,1)-a(3,1)*a(1,3) a(2,1)*a(1,3)-a(2,3)*a(1,1); ...
a(3,2)*a(2,1)-a(3,1)*a(2,2) a(3,1)*a(1,2)-a(3,2)*a(1,1) ...
a(2,2)*a(1,1)-a(2,1)*a(1,2)]./detA;
x = invA*b

Output from MATLAB:


x=
9
4

x=
2.8000
3.9600
2.1200

12. Statistical Analyses Using Matrices in MATLAB


You are to use MATLAB to carry out some statistical analyses on a set of data given in
the table below.
Undergr. GPA

ChEPS GPA

3.58
3.33
3.28
3.27
3.15
3.33
2.97
3.09
3.22
2.89
3.05
2.90
3.10
3.01
3.31
3.45
3.22
3.19
3.14
3.16
3.45

3.97
3.71
3.57
3.54
3.61
3.75
3.72
3.42
4.00
3.66
3.89
3.28
3.71
3.64
3.64
3.64
3.64
3.61
3.78
3.64
3.85

The data consist of undergraduate GPAs of 21 students from


ChEPS Class-11 and their ChEPS GPAs upon graduation. We
like to find out if there is a strong correlation between these two
set of data. You are asked to carry out a number of statistical
analyses using MATLABs matrix functionality. You must first
create a 212 matrix that contain the data given in the table.
From that matrix, do the following:
1. Compute the maximum, minimum, mean, and median of
the students undergraduate GPAs and ChEPS GPAs.
2. Compute the standard deviations of undergraduate
GPAs and ChEPS GPAs using the following formula:

1 N
( xi ) 2
N 1 i =1
where N = total data size and = mean of the data. Do
not use the built-in function in MATLAB to compute
this number.
s=

3. Compute the Pearsons correlation coefficient r, which


indicates the correlation between undergraduate GPA
and ChEPS GPA, as given by the formula below:

r=

N ( xy ) ( x y )

[N ( x ) ( x) ] [N ( y ) ( y ) ]
2

where x = undergraduate GPA and y = ChEPS GPA.


Please note that you are not allowed to use for loop or any control statements in your
calculations. Use only vectors, matrices, and their manipulations. Finally, use fprintf to
output your results that look like the table below. The spacing is not important as long
as your table looks neat. Note that I want 2 decimal places for the maximum and the
minimum and 3 decimal places for the mean, the median, and the standard deviation.
For the correlation coefficient, I want an answer in 4 decimal places.

Maximum Minimum Mean Median STDEV


------------- ------------- --------- ----------- --------Underg GPA
x.xx
x.xx
x.xxx
x.xxx
x.xxx
ChEPS GPA
x.xx
x.xx
x.xxx
x.xxx
x.xxx
Pearson correlation coefficient r = 0.xxxx

10

Maximum

Minimum

Mean

Median

STDEV

Underg GPA

________

________

________

________

________

ChEPS GPA

________

________

________

________

________

Pearsons correlation coefficient r = ____________

Solution:
MATLAB Script File:
clc
clear
format short
% CHE654 Problem #12
GPA = [3.58 3.97; 3.33 3.71; 3.28 3.57; 3.27 3.54; 3.15 3.61; ...
3.33 3.75; 2.97 3.72; 3.09 3.42; 3.22 4.00; 2.89 3.66; ...
3.05 3.89; 2.90 3.28; 3.10 3.71; 3.01 3.64; 3.31 3.64; ...
3.45 3.64; 3.22 3.64; 3.19 3.61; 3.14 3.78; 3.16 3.64; ...
3.45 3.85];
U_GPA = GPA(:,1);
C_GPA = GPA(:,2);
n = length(U_GPA);
max_U_GPA = max(U_GPA);
min_U_GPA = min(U_GPA);
ave_U_GPA = mean(U_GPA);
med_U_GPA = median(U_GPA);
max_C_GPA = max(C_GPA);
min_C_GPA = min(C_GPA);
ave_C_GPA = mean(C_GPA);
med_C_GPA = median(C_GPA);
%
% mu_U_GPA = std(U_GPA)
% mu_C_GPA = std(C_GPA)
%
square = (U_GPA - ave_U_GPA).^2;
sum_square = sum(square);
std_U_GPA = sqrt(sum_square/(n-1));
square = (C_GPA - ave_C_GPA).^2;
sum_square = sum(square);
std_C_GPA = sqrt(sum_square/(n-1));
%
term1 = n*sum(U_GPA.*C_GPA);
term2 = sum(U_GPA)*sum(C_GPA);
term3 = n*sum(U_GPA.^2)-(sum(U_GPA)).^2;
term4 = n*sum(C_GPA.^2)-(sum(C_GPA)).^2;
r = (term1-term2)/sqrt((term3*term4));
fprintf ('
Maximum Minimum Mean Median STDEV\n')
fprintf ('
------- ------- ------- ------- -------\n')
fprintf ('Underg GPA %6.2f %9.2f %10.3f %9.3f %9.3f\n', max_U_GPA, ...
min_U_GPA, ave_U_GPA, med_U_GPA, std_U_GPA)
fprintf ('ChEPS GPA %7.2f %9.2f %10.3f %9.3f %9.3f\n', max_C_GPA, ...
min_C_GPA, ave_C_GPA, med_C_GPA, std_C_GPA)
fprintf ('\n')
fprintf ('Pearson correlation coefficient r = %6.4f\n', r)

11

Output from MATLAB:

12

13. Using MATLAB to Solve Simple ChE Problems


(a) Use MATLAB to determine the stoichiometric ratios of molecular species in the
following reaction:
C6H12O6 + NH3 + O2  C5H9NO2 + CO2 + H2O

= __________

= __________

= __________

= _________

(b) It is proposed that a steel tank be used to store carbon dioxide at 300 K. The tank is
2.5 m3 in volume, and the maximum pressure it can safely withstand is 100 atm.
Using the Beattie-Bridgeman equation of state given below, determine the
maximum number of moles of CO2 that can be stored in the tank

RT + + +
V
V2 V3
V4

R = gas constant = 0.08206 L-atm/gmole-K

where

P, V, T = pressure in atm, molar volume in L/gmole, and temperature in K


respectively
= R T B0 A0 R c
T2
= R T B0 b A0 a R c B0
T2
= R T B0 b c
T2
For CO2, A0 = 5.0065, a = 0.07132, B0 = 0.10476, b = 0.07235, and c = 66.0104
Note that 1 m3 = 1000 L and 1 kg-mole = 1000 gmoles

Solution:
MATLAB Script File:
%
% CHE654 Problem #13
%
clc
clear
%
% Part (a)
% a = c; 12 + 3a = 9c + 2d; 6 + 2b = 2c + 2 + d; 6 = 5c + 1
%
A = [1 0 -1 0; 3 0 -9 -2; 0 2 -2 -1; 0 0 -5 0];
b = [0 -12 -4 -5]';
x = inv(A)*b
%

13

% Part (b)
%
R = 0.08206;
A0 = 5.0065;
a = 0.07132;
B0 = 0.10476;
b = 0.07235;
c = 66.0e4;
T = 300;
P = 100;
beta = R*T*B0 - A0 - R*c/T^2;
gamma = R*T*B0*b - A0*a - R*c*B0/T^2;
delta = R*T*B0*b*c/T^2;
syms V
F = R*T/V + beta/V^2 + gamma/V^3 + delta/V^4 - P;
gV = solve(F);
CO2_V = double(gV)
nmoles = 2.5/CO2_V(3)

MATLAB Output :
x=
1.0000
0.5000
1.0000
3.0000
CO2_V =
0.0706 + 0.3464i
0.0706 - 0.3464i
0.3875
-0.2825
nmoles =
6.4508

14

Das könnte Ihnen auch gefallen