% Example LEP-WEB17-1b: Reactions in a Series in a CSTR
%

Vspan = [0 12.6]; % Range for the independent variable
y0 = [1 0 0]; % Initial values for the dependent variables
k1=1;
k2=1;
Cao=1;
Cbo=0;
Cco=0.1;

tiledlayout(2,2)
set (gcf,'Position',[0,0.1,700,500])
tau=linspace(0,1.26,100);
for i=1:100
Ca(i) = Cao/(1+k1*tau(i)); 
Cb(i)=(Cbo+k1*Ca(i)*tau(i))/(1+k2*tau(i));
Cc(i)=Cco+k2*Cb(i)*tau(i);
ra(i) = -k1*Ca(i);  
rb(i) = k1*Ca(i)-k2*Cb(i); 
rc(i) = k2*Cb(i);  
X(i)=(Cao-Ca(i))/Cao;
end
nexttile
plot(tau,X(1:100),'LineWidth',2)
grid on
xlabel('tau, \tau')
ylabel('Conversion')
leg=legend('X');
leg.ItemTokenSize=[10,10];
title('Conversion profile')
xlim([0 1.26])
ylim([0 1])
nexttile
plot(tau,Ca(1:100),tau,Cb(1:100),tau,Cc(1:100),'LineWidth',1.5)
grid on
xlabel('tau, \tau')
ylabel('Concentration')
leg=legend('C_A','C_B','C_C');
leg.ItemTokenSize=[10,10];
title('Conversion profile')
xlim([0 1.26])
ylim([0 2])
nexttile
plot(tau,-ra(1:100),tau,-rb(1:100),tau,-rc(1:100),'LineWidth',1.5)
grid on
xlabel('tau, \tau')
ylabel('Rate')
leg=legend('-r_A','-r_B','-r_C');
leg.ItemTokenSize=[10,10];
title('Reaction rate profile')
xlim([0 1.26])
ylim([-2 2])