function Example_11_4
% MATLAB solution to Example 11.4 of "A First Course on Kinetics and
% Reaction Engineering."
%
% Constants
V = 12; % L
VFR = 6; % L/min
m_tracer = 24; % g
rho = 1000; % g/L
MFR = VFR*rho; % equation (2)

% Experimental data from the table, adding in values for t = 0
t = [0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
2.2
2.4
2.6
2.8
3
5
7.5
10];

C = [0
0.04
1.97
1.76
1.47
1.36
1.26
1.08
0.91
0.88
0.70
0.56
0.64
0.50
0.48
0.45
0.14
0.03
0.05];

% Calculate F using equation (4)
F = zeros(length(t),1);
F(1) = 0.;
for i = 2:length(t)
    F(i) = MFR/rho/m_tracer*trapz(t(1:i),C(1:i));
end

% Calculate F_CSTR using equation (5), but with more data points so that a
% smooth curve results
t_CSTR = linspace(t(1),t(length(t)));
F_CSTR = zeros(length(t),1);
t_bar = V/VFR;
for i = 1:length(t_CSTR)
    F_CSTR(i) =1-exp(-t_CSTR(i)/t_bar);
end


% Plot the results
figure
plot(t,F,'or',t_CSTR,F_CSTR,'k')
xlabel('Lambda')
ylabel('F(lambda)')
legend('Lab','CSTR')

end

