CHE656 Modeling Chemical Processes Using MATLAB Homework Set #1 Solutions Class-17 Prepared by Dr. Hong-ming Ku King Mongkut’s University of Technology Thonburi Chemical Engineering Department Chemical Engineering Practice School July 2004 - 2013 – Use with Permission of the Author Only 1 2. System of Linear Algebraic Equations, II Use MATLAB to solve the following system of linear algebraic equations, correct to 4 decimal places: x1 2x1 –3x1 –x2 –2x1 6x1 + – – + + – 3x2 2x2 2x2 2x4 3x4 2x2 + + + + + – 2x3 – x5 + 4x6 4x3 + x4 + x5 4x3 + 5x4 + 2x5 – x6 6x5 + 4x6 4x5 + 5x6 x3 – 3x4 + 4x5 + 2x6 = –2 = 1 = 0 = 3 = –6 =5 Solution: MATLAB Script File: % % Problem #2: System of Linear Algebraic Equations % clc clear A = [A = [1 3 2 0 -1 4; 2 -2 4 1 1 0; -3 -2 4 5 2 -1; 0 -1 0 2 6 4; -2 0 0 3 4 5; 6 -2 -1 -3 4 2]; b = [-2; 1; 0; 3; -6; 5]; x = inv(A)*b MATLAB Output (from x1 x6): x= -1.2770 3.4089 2.4459 -3.6267 4.2148 -2.9067 2 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.4 ft3-(psia)1/2/hr Cv,3 = 0.2 ft3-(psia)1/2/hr Cv,2 = 0.3 ft3-(psia)1/2/hr Cv,4 = 0.1 ft3-(psia)1/2/hr 1 P = 40 psia Cv,1 Pump Cv,2 Cv,3 2 Cv,4 P = 14.7 psia Reservoir Solution: MATLAB Script File: % % CHE656 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.4*sqrt(40-p1) = 0.3*sqrt(p1-14.7) + 0.2*sqrt(p1-p2) % At node 2: Q2 = 0.2*sqrt(p1-p2) = 0.1*sqrt(p2-14.7) % p = solve('0.1*sqrt(40-p1)-0.2*sqrt(p1-14.7)-0.3*sqrt(p1-p2)=0', ... '0.3*sqrt(p1-p2)-0.4*sqrt(p2-14.7)=0') double(p.p1) double(p.p2) The MATLAB output: ans = 27.6883 P1 ans = 25.0906 P2 3 6. Simple Calculations and Array Manipulations in MATLAB, II Consider the following arrays: A = 1 3 10 5 6 10 3 π 5 30 4 8 B = log10(A) C = 2 4 4 8 12 24 0 -1 5 8 7 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: MATLAB Script File: % % ChE 656 Problem #6: Simple Calculations and Array Manipulations in MATLAB % clc clear format short A = [1 3 5; 10 5 30; 6 10 4; 3 pi 8] B = log10(A) C = [2 4 4 8; 8 12 24 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) % % Determine the square root of the product between the 2nd column of A and the % last row of C 4 % 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) % % Evalute 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) ================= Solutions =================== A= 1.0000 3.0000 5.0000 10.0000 5.0000 30.0000 6.0000 10.0000 4.0000 3.0000 3.1416 8.0000 B= 0 0.4771 0.6990 1.0000 0.6990 1.4771 0.7782 1.0000 0.6021 0.4771 0.4971 0.9031 C= 2 4 4 8 5 8 0 12 -1 24 7 5 6 sum_column2_B = 2.6732 sum_det = 490.5800 square_root = 7.9906 Sum = 8.7083 hyperbolic_secant_AC = 0.9671 0.8436 0.6878 0.9534 0.9417 0.7967 0.4649 0.9189 0.6431 0.0899 0.1165 0.5188 0.8481 0.1044 0.4567 0.6779 inverse_tangent_BC 0.364629 0.649348 0.762714 0.458148 0.465764 0.828914 0.967387 0.608790 0.981121 1.229583 1.250270 1.071958 0.645652 1.139944 1.034867 0.904380 6 9. Using MATLAB to Solve the Peng-Robinson Equation of State The Peng-Robinson equation of state contains 2 empirical parameters a and b, and its conventional form is given by: P= where RT a − 2 Vm − b Vm + 2bVm − b 2 αR 2TC2 a = 0.45724 P C RT b = 0.07780 C PC α = [1 + (0.37464 + 1.54226ω − 0.26992ω 2 )(1 − Tr0.5 )]2 Tr = T TC P VAP (Tr = 0.7) PC ω = −1.0 − log10 The variables are defined by: P Vm T R TC PC PVAP = = = = = = = pressure in MPa molar volume in cm3/gmole temperature in K gas constant (8.314 cm3-MPa/gmole-K) the critical temperature (150.86 K for argon) the critical pressure (4.898 MPa for argon) vapor pressure (0.4946 MPa at Tr = 0.7 for argon) Alternatively, this equation of state can be expressed in polynomial forms as given below: A= aP R 2T 2 B= bP RT Z 3 + ( B − 1) Z 2 + ( A − 2 B − 3B 2 ) Z + (− AB + B 2 + B 3 ) = 0 where Z = PVm , the compressibility factor RT 7 Use MATLAB to compute the molar volume of argon using both the conventional form and the polynomial form at T = 105.6 K and P = 0.498 MPa. The two solutions should be the same. Because the two equation forms are both cubic in molar volume, you should obtain 3 different answers in volume. Briefly describe the physical meaning or significance of each answer. Answers: There are 3 roots or molar volumes for argon from the Peng-Robinson equation of state. The roots are 1580.9562, 134.3314, and 27.7585 cm3/gmol. The largest number represents the molar volume of argon in the gaseous state, while the smallest number represents the molar volume of argon in the liquid state at the given temperature and pressure. The middle number has no physical meaning or significance. Solution: MATLAB Script File: % % CHE656: Problem #9 % clc clear format short % Solve the traditional form of the equation R = 8.314; Tc = 150.86; Pc = 4.898; Pvap = 0.4946; w = -1.0 - log10(Pvap/Pc); T = 105.6; P = 0.498; Tr = T/Tc; alpha = (1+(0.37464+1.54226*w-0.26992*w^2)*(1-Tr^0.5))^2; a = 0.45724*(alpha*R^2*Tc^2/Pc); b = 0.07780*R*Tc/Pc; syms V F = P - R*T/(V-b) + a/(V^2+2*b*V-b^2); Vm = solve(F); Vm = double(Vm); fprintf ('The three molar volumes from tradtional form are:\n') fprintf ('%10.4f\n',Vm) % Solve the polynomial form of the equation A = a*P/(R^2*T^2); B = b*P/(R*T); syms Z F = Z^3+(B-1)*Z^2+(A-2*B-3*B^2)*Z+(-A*B+B^2+B^3); %a2 = B-1 %a1 = A-2*B-3*B^2 %a0 = -A*B+B^2+B^3 8 Z = solve(F); Z = double(Z); Vm = (R*T/P).*Z; fprintf ('\n') fprintf ('The three molar volumes from polynomial form are:\n') fprintf ('%10.4f\n',Vm) MATLAB Output : The three molar volumes from tradtional form are: 1580.9562 27.7585 134.3314 The three molar volumes from polynomial form are: 1580.9562 27.7585 134.3314 9 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 MATLAB’s matrix functionality. You must first create a 21×2 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 Pearson’s 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 2 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 2 Maximum Minimum Mean Median STDEV Underg GPA ________ ________ ________ ________ ________ ChEPS GPA ________ ________ ________ ________ ________ Pearson’s correlation coefficient r = ____________ Solution: MATLAB Script File: clc clear format short % CHE656: 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, ... 11 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) Output from MATLAB: 12