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

% Data provided in the problem statement, in consistent units

V = 1; % L (basis)
CA0=[1.00 1.00 1.00 1.00 0.75 0.75 0.75 0.75 0.50 0.50 0.50 0.50 0.25 0.25 0.25 0.25]'; % mol per L
CB0=[1.00 0.75 0.50 0.25 1.00 0.75 0.50 0.25 1.00 0.75 0.50 0.25 1.00 0.75 0.50 0.25]'; % mol per L
tau=[94. 20.1 2.9 1.3 0.9 1.1 3.8 1.1 2.4 1.0 2.3 2.4 21.1 1.2 4.4 20.8]'; % s
CA=[0.10 0.29 0.58 0.83 0.50 0.52 0.40 0.63 0.25 0.37 0.29 0.36 0.05 0.18 0.13 0.09]'; % mol per L

% Calculate corresponding values of x1, x2 and y_hat
VFR = V./tau;
nA = VFR.*CA;
nA0 = VFR.*CA0;
nB0 = VFR.*CB0;
nB = nB0 - nA0 + nA;
CB = nB./VFR;
x1 = log(CA);
x2 = log(CB);
y_hat = log(nA0-nA);

% Create the matrix x
x = [x1 x2 ones(length(x1),1)];

% Use the MATLAB script file "FitLinSR.m" from "A First Course on
% Kinetics and Reaction Engineering" to fit a straight line to the data.
FitLinSR

% Calculate k, alpha and beta and their uncertainties
alpha = m(1)
alpha_u = m_u(1)
beta = m(2)
beta_u = m_u(2)
k = exp(b)/V
k_u = k*b_u


