Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 LAB ASSESSMENT RUBRICS Student Name: AmmarUllah Khan Program (20 pts) (Excellent) (20~18) (Good) (17-14) (Average) (13-10) Presence in Lab 20% Programme r was present and actively participated during Lab Programmer was few minutes late but actively participated during Lab Programmer was late by half but actively participated during Lab Program is stylistically well designed Few inappropriat e design choices (i.e. poor variable names, improper indentation) Several inappropriat e design choices (i.e. poor variable names, improper indentation) Standards 20% Program execution 30% Design and correct output 20% Documentatio n 10% Program executes correctly with no error’s messages Program displays correct output with no errors Program is well documente d Program Program executes executes with a minor with a minor (easily fixed (not easily errors) fixed error) Output has minor errors Output has multiple errors Missing one required comment Missing two or more required comments (Fair) (09-05) (Poor) (04-00) Programmer Programmer failed to show was late by up at all or half and did was in lab but not actively failed participated participate in during Lab activities Program is poorly written Failed to write Program Program does Program does not execute at not execute all Output is incorrect Output is completely incorrect Most All documentatio documentatio n missing n missing Total Score = Remarks if any: Signature with date: __________________________ 1 /20 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 TITLE: Lab 7: Numerical differentiation and integration DESCRIPTION: Calculate the derivatives analytically and numerically for y= 4x^4 –3x^3 +5 at 0≤x≤3, interval = 0.1, and show them. CODES: 1. m. file: lab71 close all; clc; x=0:0.1:3; m=length(x); m1=m-1;% m-1. p=[1 -3 0 0 5]; y= polyval(p,x); dp=[1*4 -3*3 0 0 ]; dydx_a = polyval(dp,x); dydx_n=diff(y)./diff(x); fprintf('sol)\n x y dydx_anal dydx_nume \n'); for i=1:m1 fprintf(' %5.2f %12.3e %12.3e %12.3e \n', x(i),y(i),dydx_a(i),dydx_n(i)); end plot(x,y,'o-g', x,dydx_a,'or-', x(1:m1),dydx_n,'^b-'); legend('given data','analytical differentiation', 'numerical differentiation.'); 2 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 2. command window: sol) x 0.00 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00 1.10 1.20 1.30 1.40 1.50 1.60 1.70 1.80 1.90 2.00 2.10 2.20 2.30 2.40 2.50 2.60 2.70 2.80 2.90 y 5.000e+00 4.997e+00 4.978e+00 4.927e+00 4.834e+00 4.688e+00 4.482e+00 4.211e+00 3.874e+00 3.469e+00 3.000e+00 2.471e+00 1.890e+00 1.265e+00 6.096e-01 -6.250e-02 -7.344e-01 -1.387e+00 -1.998e+00 -2.545e+00 -3.000e+00 -3.335e+00 -3.518e+00 -3.517e+00 -3.294e+00 -2.813e+00 -2.030e+00 -9.049e-01 6.096e-01 2.561e+00 dydx_anal 0.000e+00 -8.600e-02 -3.280e-01 -7.020e-01 -1.184e+00 -1.750e+00 -2.376e+00 -3.038e+00 -3.712e+00 -4.374e+00 -5.000e+00 -5.566e+00 -6.048e+00 -6.422e+00 -6.664e+00 -6.750e+00 -6.656e+00 -6.358e+00 -5.832e+00 -5.054e+00 -4.000e+00 -2.646e+00 -9.680e-01 1.058e+00 3.456e+00 6.250e+00 9.464e+00 1.312e+01 1.725e+01 2.187e+01 dydx_nume -2.900e-02 -1.950e-01 -5.050e-01 -9.350e-01 -1.461e+00 -2.059e+00 -2.705e+00 -3.375e+00 -4.045e+00 -4.691e+00 -5.289e+00 -5.815e+00 -6.245e+00 -6.555e+00 -6.721e+00 -6.719e+00 -6.525e+00 -6.115e+00 -5.465e+00 -4.551e+00 -3.349e+00 -1.835e+00 1.500e-02 2.225e+00 4.819e+00 7.821e+00 1.126e+01 1.514e+01 1.951e+01 2.439e+01 3 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 RESULT & DISCUSSION: We’ve calculated the value of derivative for every 0.1 step values of x. MATLAB allows us to obtain a range of values for the given equation. 4 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 CASE STUDY: CODES: 1. m. file: lab72 close all; clc; t=[ 0 2.25 4.5 6.33 8.00 10.25 12.00 13.50 ... 15.60 17.85 19.60 27.00 30.00 38.00 41.00 45.00 47.00 57.00 63.00]; Cbr=[ 0.3335 0.2965 0.2660 0.2450 0.2255 0.2050 0.1910 0.1794 0.1632 0.1500 0.1429 0.1160 0.1053 0.0830 0.0767 0.0705 0.0678 0.0553 0.0482]; m=length(t); m1=m-1; t1=t(1:m1); 5 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 dCbrdt = diff(Cbr)./diff(t); y = log(-dCbrdt); x = log(Cbr(1:m1) ); p=polyfit(x,y,1); n=p(1); lnk=p(2); k=exp(lnk); y_hat = polyval(p,x); y_bar = mean(y); r2= sum((y_hat - y_bar).^2)/sum((y - y_bar).^2); subplot(2,1,1); plot(x,y,'o'); x_calc=-3:0.1:-0.7 y_calc= polyval(p,x_calc); plot(x_calc,y_calc,'b-'); title('linear plot for ln(-dC_{Br2}/dt) vs. ln(C_{Br2})'); xlabel('ln(C_{Br2})'); ylabel('ln(-dC_{Br2}/dt)'); s1=num2str(lnk,4); s2=num2str(n,4); s3=num2str(r2,4); eq=['ln(-dCBr_2/dt)= ',s1,' + ',s2,' * ln(CBr_2)',' r^2 = ',s3]; text(-2.5,-7.1,eq); fprintf('The calculated equation is %s.\n',eq); dCbrdt_cal = -k*Cbr.^n; subplot(2,1,2); plot(t1,dCbrdt,'bo',t,dCbrdt_cal,'g-'); legend('dC_{Br2}/dt (exp)','dC_{Br2}/dt (calc)') xlabel('t (min)'); ylabel('dC_{Br2}/dt (g-mol/dm^3 .min)'); fprintf(' t(min) Cbr dCbr/dt ln(Cbr) ln(-dVbr/dt) \n'); for i=1:1:m1 fprintf(' %6.2f %12.3e %12.3e %12.3e %12.3e \n',t(i),Cbr(i),dCbrdt(i),x(i),y(i) ); end 2. Command window: The calculated equation is ln(-dCBr_2/dt)= -2.342 + 1.575 * ln(CBr_2) r^2 = 0.9841. t(min) Cbr dCbr/dt ln(Cbr) ln(-dVbr/dt) 0.00 3.335e-01 -1.644e-02 -1.098e+00 -4.108e+00 2.25 2.965e-01 -1.356e-02 -1.216e+00 -4.301e+00 4.50 2.660e-01 -1.148e-02 -1.324e+00 -4.468e+00 6.33 2.450e-01 -1.168e-02 -1.406e+00 -4.450e+00 8.00 2.255e-01 -9.111e-03 -1.489e+00 -4.698e+00 6 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 10.25 2.050e-01 -8.000e-03 -1.585e+00 -4.828e+00 12.00 1.910e-01 -7.733e-03 -1.655e+00 -4.862e+00 13.50 1.794e-01 -7.714e-03 -1.718e+00 -4.865e+00 15.60 1.632e-01 -5.867e-03 -1.813e+00 -5.138e+00 17.85 1.500e-01 -4.057e-03 -1.897e+00 -5.507e+00 19.60 1.429e-01 -3.635e-03 -1.946e+00 -5.617e+00 27.00 1.160e-01 -3.567e-03 -2.154e+00 -5.636e+00 30.00 1.053e-01 -2.788e-03 -2.251e+00 -5.883e+00 38.00 8.300e-02 -2.100e-03 -2.489e+00 -6.166e+00 41.00 7.670e-02 -1.550e-03 -2.568e+00 -6.470e+00 45.00 7.050e-02 -1.350e-03 -2.652e+00 -6.608e+00 47.00 6.780e-02 -1.250e-03 -2.691e+00 -6.685e+00 57.00 5.530e-02 -1.183e-03 -2.895e+00 -6.739e+00 RESULTS AND DISCUSSION: The resulting graphs show the original non-linear graph and then the linear regression version with natural logarithm applied. 7 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 CASE STUDY 1. m.file: lab73 2. close all; clc; 3. t=[ 0 2.25 4.5 6.33 8.00 10.25 12.00 13.50 ... 4. 15.60 17.85 19.60 27.00 30.00 38.00 41.00 45.00 47.00 57.00 63.00]'; 5. CBr=[ 0.3335 0.2965 0.2660 0.2450 0.2255 0.2050 0.1910 0.1794 ... 6. 0.1632 0.1500 0.1429 0.1160 0.1053 0.0830 0.0767 0.0705 0.0678 0.0553 0.0482]'; % a) 7. x= t; y= CBr; y_bar = mean(y); % take x, y and ybar 8. p=polyfit(x,y,3); % make coefficients of the polynomial with polyfit() 9. y_hat=polyval(p,x); % take calculate values with polyval() 10. r2=sum((y_hat-y_bar).^2)/sum((y-y_bar).^2); 11. fprintf('Sol)\nC_Br2 = %10.3e*t^3 +%10.3e*t^2 +%10.3e*t +%10.3e, r^2=%6.4f\n\n',p,r2); 12. figure(1); set(1,'position',[10 100 500 600],'color','w'); 13. subplot(311); plot(t,CBr,'r.',t,y_hat,'b-'); 14. xlabel('t [min]'); ylabel('C_{Br2}'); 15. legend('given-data','calculated-value') 16. %b) 17. m=length(t); m1=m-1; t1=t(1:m1); CBr1=CBr(1:m1); % to fit the length, for later use. 18. dp=[3*p(1) 2*p(2) p(3)]; % coefficients of derivate of 3rd order polynomial. 19. dCBrdt_a= polyval(dp, t1); % with polyval() % analytical derivatives. 20. dCBrdt_n=diff(CBr)./diff(t); % with diff(). array operation. 21. subplot(312); plot(t1,dCBrdt_a,'r.', t1,dCBrdt_n,'bo'); 22. legend('analytical-diff', 'numerical-diff'); 23. xlabel('t [min]'); ylabel('dC_{Br2}/dt'); 24. % c) Applying equation (B) for analytical derivatives 25. x_a = log(CBr1); % take x use for eq(B). 26. y_a = log(-dCBrdt_a); % calculate y with analytical equation for eq(B).Attention - sign. 8 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 27. p_a=polyfit(x_a, y_a, 1); % calculate parameters with polyfit(). 28. y_hat_a= polyval(p_a, x_a ); % take the calculated values with polyval(). 29. y_bar_a = mean(y_a); % average with mean(); 30. r2_a= sum((y_hat_a - y_bar_a).^2)/sum((y_a - y_bar_a).^2); 31. n_a=p_a(1); k_a=exp(p_a(2)); % calculate k and n using coefficient of eq(B) 32. % Applying equation (B) for numerical derivatives. 33. % copy above(from x_a= ; to k_a= ;), and replace _n to _a. 34. x_n = log(CBr1); 35. y_n = log(-dCBrdt_n); 36. p_n=polyfit(x_n, y_n, 1); 37. y_hat_n= polyval(p_n, x_n ); 38. y_bar_n = mean(y_n); 39. r2_n= sum((y_hat_n - y_bar_n).^2)/sum((y_n - y_bar_n).^2); 40. n_n=p_n(1); k_n=exp(p_n(2)); 41. subplot(313); % lower figure is plotted with loglog(), 42. % the values which is not taken by log are entered. 43. % and take exp() to hat values for same x axis 44. loglog(CBr1,-dCBrdt_a,'r.', CBr1,exp(y_hat_a),'r-', CBr1,dCBrdt_n,'bo',CBr1,exp(y_hat_n),'b-'); 45. 46. legend('analytical-diff-data','analytical-diffcalc','numerical-diff-data','numerical-diff-calc'); 47. 48. 49. 50. 51. xlabel('C_{Br2}'); ylabel('-dC_{Br2}/dt '); fprintf('eq(B) : ln(-dCBr2/dt) = ln(k) + n ln(CBr2)\n') fprintf(' calculated values k n r^2\n'); fprintf(' from analytical eq. %12.3e %12.3e %6.4f\n',k_a, n_a, r2_a); 52. fprintf(' from numerical eq. %12.3e %12.3e %6.4f\n',k_n, n_n, r2_n); 53.Command window: Sol) C_Br2 = -2.043e-06*t^3 + 2.842e-04*t^2 +-1.428e-02*t + 3.266e-01, r^2=0.9982 eq(B) : ln(-dCBr2/dt) = ln(k) + n ln(CBr2) calculated values k n r^2 from analytical eq. 1.048e-01 1.603e+00 0.9404 9 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 from numerical eq. 9.615e-02 1.575e+00 0.9841 RESULTS & DISCUSSION: After running the program we obtain the graphs with comparison between analytical and numerical differentiation. The command window showed the required values for k, n and r2 for both analytical and numerical differentiation methods. 10 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 Task 2: Numerical differentiation Programs: Q4: Integrate from -1 to 1 with equal space of . Function File: lab7ex4fun.m function f=lab7ex4fun(x) f= x.^3 +1 ; %integrand 1.mfile: lab7ex4 close all; clc; h=0.1; % equal space a=x(1), ... ,x(n1)=b x= (-1 : h : 1); % interval divide by n, the number of calculation point is n+1. n1=length(x); % find number of calculate point = 21 % given function f= x.^3 +1 ; % analytical solution J_anal=x.^4/4-x(1).^4/4 + x - x(1); % F(x(i)) - F(a) % for rectangular rule n=n1-1; for i=1:n % there are 20 central points. sum_f = 0; x_center(i)=(x(i+1)+x(i))/2; %% make central points. for j=1 : i sum_f = sum_f + x_center(j)^3 +1; % calculate function value of central points and summing end J_rect(i) = h*sum_f; end % For trapezoidal rule for i=1:n1 % loop for every calculation points. if (i==1); sum_f=0; % no interval. elseif (i==2); sum_f=( f(1)+f(i) )/2; % only two ends. else % general cases i>=3. sum_f=( f(1)+f(i) )/2; % calculate for two ends. for k=2 : i-1; sum_f=sum_f+x(k)^3 +1; end % except of two ends. end % end of if J_trap(i) = h*sum_f; end % end of for i =1:n1 %Simpson rule & Simpson 3/8 rule warning off for i=1:n1; J_simpson(i) =quad ('lab7ex4fun',x(1),x(i)); end for i=1:n1; J_simpson8(i)=quad ('lab7ex4fun',x(1),x(i)); end plot(x,f,'ro-',x,J_anal,'r*',x_center,J_rect,'bv-', ... x,J_trap,'g^-',x,J_simpson,'mo-',x,J_simpson8,'k.-'); xlabel('x');ylabel('x^3'); legend('given data','analytical Integral','rectangular rule', ... 'trapezoidal rule','Simpson (quad)','Simpson (cubic)'); 11 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 2. Command window: Function File: lab7ex5fun.m function J=lab7ex5fun(x,p) %integrand, receive p from main J=polyval(p,x); 1.mfile: lab7ex5 close all; clc; X =[0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.85]; % make data 12 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 mrA =[0.0053 0.0052 0.0050 0.0045 0.0040 0.0033 0.0025 0.0018 0.00125 0.00100]; FA0=0.867; x=X; y=1./mrA; %% make variables for polyfit(). plot(x,y,'ro'); hold on; grid on; p=polyfit(x,y,4); % calculate coef of degree 4 polynomial, % calculated p is used in quad(). xn = 0: 0.01: 0.85; % make x-axis variable to draw in figure(interval 0.01) yn = polyval(p,xn); % make y-axis variables; plot(xn,yn,'b-'); Xp=X(1:2:9); mrAp=mrA(1:2:9); imrAp=y(1:2:9); % select variables for printout. fprintf('Plug Flow Reactor(PFR) Volume(V) for conversion(X)\n') fprintf(' X -rA 1/(-rA) V \n'); for i=1: length(Xp) J(i)=quad('lab7ex5fun',0,Xp(i),[],[],p); V(i)=FA0*J(i); fprintf('%7.2f %8.5f %8.2f %8.2f\n',Xp(i),mrAp(i),imrAp(i),V(i)); end plot(Xp,V,'mo-'); xlabel('Conversion [-]'); ylabel('1/-r_A or V[dm^3]'); legend('1/(-r_A)_{exp}','1/(-r_A)_{cal}','V(dm^3)'); 2.Command window: Plug Flow Reactor(PFR) Volume(V) for conversion(X) X -rA 1/(-rA) V 0.00 0.00530 188.68 0.00 0.20 0.00500 200.00 33.13 0.40 0.00400 250.00 71.93 0.60 0.00250 400.00 125.81 0.80 0.00125 800.00 224.52 13 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 Function File: lab7ex6fun.m function J=lab7ex6fun(x,p) %integrand, receive p from main J=polyval(p,x); 1.mfile: lab7ex6 close all; clc; T =[ 50 100 150 200 273.15 300 400 500 ... 600 700 800 900 1000 1100 1200 1300 1400 1500]; Cp=[ 34.06 41.30 48.79 56.07 68.74 73.93 94.01 112.59 ... 128.70 142.67 154.77 163.35 174.60 182.67 189.74 195.85 201.21 205.89]; % make variable for polyfit(). In this, no transformation is required. 14 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 plot(T,Cp,'o'); hold on; grid on; p=polyfit(T,Cp,5); % p is used in quad. T_new = 40: 1: 1500; % for line drowing, make x-variable for plot(interval 1); Cp_calc = polyval(p,T_new); % make y-variable with polyval(); plot(T_new,Cp_calc,'b-'); warning off Tref=25+273.15; % make ref temp fprintf('Temp[K] Cp mean_Cp \n'); for i=1: length(T) J_Cp(i)=quad('lab7ex6fun',Tref,T(i),[],[],p); Cp_mean(i)=J_Cp(i)/(T(i)-Tref); fprintf('%7.2f %8.2f %8.2f \n',T(i),Cp(i),Cp_mean(i)); end plot(T,Cp_mean,'ro-'); xlabel('Temp (K)'); ylabel('Cp (kJ/kg-mol K)'); legend('Cp','Cp-calc','Cp-mean'); new_x_tick=(0:200:1600); % declare variable for rearrange of tick mark as like figure. set(gca,'xtick',new_x_tick); % set tick mark rearrange (for x-axis values change) 2.Command Window: Temp[K] Cp mean_Cp 50.00 34.06 52.70 100.00 41.30 56.53 150.00 48.79 60.72 200.00 56.07 65.17 273.15 68.74 71.99 300.00 73.93 74.54 400.00 94.01 84.09 500.00 112.59 93.44 600.00 128.70 102.33 700.00 142.67 110.61 800.00 154.77 118.22 900.00 163.35 125.17 1000.00 174.60 131.51 1100.00 182.67 137.32 15 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 1200.00 189.74 142.69 1300.00 195.85 147.69 1400.00 201.21 152.34 1500.00 205.89 156.63 16 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 Function File: lab7ex7fun.m function g=lab7ex7fun(V) global P R T a b g=P-( R*T/(V-b) -a/(V*(V+b)*sqrt(T) ) ); lab7ex7fun2.m function J=lab7ex7fun2(x,p) %integrand, receive p from main J=polyval(p,x); 17 Date: 11-1-2022 LAB MANUAL CHEMICAL PROCESS OPTIMIZATION (CH-404) Lab No: _7___ Roll No: CH-18062 1.mfile: lab7ex7 close all; clc; global P R T a b %given data R=0.08206 ; % atm.L/mol.K Tc=647.4 ; Pc=218.3 ; % critical values a=0.42747*R^2*Tc^(5/2)/Pc; b=0.08664*R*Tc/Pc; % make T, P for enthalpy calculate TC=100 ; % given temperature in Celsius T0= 273.15; % reference temperature = 0 C T=TC+T0; % convert to Kelvin P=1; % given Pressure (atm) % initial guess, calculate by ideal gas low V0=R*T/P ; % the unit of molar volume is [L/mol] V=fzero('lab7ex7fun',V0); Z=P*V/R/T ; % compressibility factor fprintf('\n Pressure Temp. Molar Vol Zfactor \n') fprintf('% 7.2f %7.3f %7.3f %7.4f \n', P,T,V,Z); % given data for enthalpy calculation H_l_o=0; % kJ/kg DHvap_o=2501.3; %kJ/kg coef=[7.700, 0.04594e-2, 0.2521e-5, -0.8587e-9]; % given data p=coef(length(coef):-1:1); % coeff of highest order for polyval is 1. change coeff order J_Cp = quad('lab7ex7fun2',T0,T,[],[],p); % integration [cal/gmol] J_Cp = J_Cp*4.1868/18; % [kJ/kg] convert units. H_v_o = H_l_o + DHvap_o + J_Cp; % enthalpy at ref temp [kJ/kg] H_calib = 101.4/18*R*T*(Z-1- 1.5*a/(b*R*T^1.5)*log(1+b/V)); % kJ/kg H_v = H_v_o + H_calib; fprintf('\n DHvap_o J_Cp H_v^o H_calib H_v (kJ/kg_steam)\n') fprintf('%8.2f %8.2f %8.2f %8.2f %8.2f \n', DHvap_o, J_Cp, H_v_o, H_calib, H_v); fprintf('The steam Table value is 2676.1 at same conditions.\n') 2.Command Window: Pressure Temp. Molar Vol Zfactor 1.00 373.150 30.403 0.9929 DHvap_o J_Cp H_v^o H_calib H_v (kJ/kg_steam) 2501.30 188.04 2689.34 -3.25 2686.09 The steam Table value is 2676.1 at same conditions 18