Beruflich Dokumente
Kultur Dokumente
Prepared by
Dr. Hong-ming Ku
=
=
=
=
=
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
= 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
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
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
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
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
a
a12
1 22
A =
det( A) a
21 a11
then
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
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
4 x1 x 2 2 x3 = 3
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
x=
2.8000
3.9600
2.1200
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
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=
r=
N ( xy ) ( x y )
[N ( x ) ( x) ] [N ( y ) ( y ) ]
2
10
Maximum
Minimum
Mean
Median
STDEV
Underg GPA
________
________
________
________
________
ChEPS GPA
________
________
________
________
________
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
12
= __________
= __________
= __________
= _________
(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
where
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