% MATLAB file used in the solution of Example 15.4 of 
% "A First Course on Kinetics and Reaction Engineering."

% Data provided in the problem statement, in consistent units
Diam=0.25; % cm
L=10.; % cm
T=1500; % K
P=1.0; % atm
Rgas=82.06; % (atm-cm^3)/(mol-K)
Vfr0=[10 20 30 40 60 75 100]'; % cm^3/min
fCOS=[0.99 0.93 0.86 0.78 0.66 0.60 0.51]';

% Calculate the inlet and outlet molar COS flow rates and k for each data
% point
n0COS = (P/Rgas/T)*Vfr0;
nCOS = n0COS.*(1-fCOS);
k = (-4/(pi()*Diam^2*L*P))*(n0COS - nCOS + 2*n0COS.*log(nCOS./n0COS));

% Calculate the average value of k and the standard deviation
k_avg = mean(k)
k_dev = std(k)

% Make a plot of (k - k_avg) vs. fCOS to see if there are systematic trends
plot(fCOS,(k - k_avg),'o',fCOS,zeros(length(fCOS),1),'-');
ylabel('k - k_avg');
xlabel('f_COS');
