clc; clear
global k C0
k = zeros(3,1);
k(1) = 0.25; k(2) = 0.1; k(3) = 5;
C0 = zeros(6,1);
C0(1) = 1.5; C0(2) = 2; C0(3:end) = 0;
Vmax = 50; v0 = 10;
[tau,C] = ode45(@firstorder,[0 5],[C0(1) C0(2) C0(3) C0(4) C0(5) C0(6)]);
X = (C0(1)-C(:,1))/C0(1);
[Ca_max,iMaxA] = max(C(:,1));
[Cb_max,iMaxB] = max(C(:,2));
[Cc_max,iMaxC] = max(C(:,3));
[Cd_max,iMaxD] = max(C(:,4));
[Ce_max,iMaxE] = max(C(:,5));
[Cf_max,iMaxF] = max(C(:,6));
V = tau*v0;
plotyy(V,C(:,1),V,X); hold on; plot(V,C(:,2)); plot(V,C(:,3));
plot(V,C(:,4)); plot(V,C(:,5)); plot(V,C(:,6));
yyaxis right
ylim([0 1]);
xlabel('Volume of Reactor (L)');
ylabel('Concentration (M)');
legend('C_A','C_B','C_C','C_D','C_E','C_F','X_A');
plot(V(iMaxA),C(iMaxA,1),'o','HandleVisibility','off'); plot(V(iMaxB),C(iMaxB,2),'o','HandleVisibility','off'); plot(V(iMaxC),C(iMaxC,3),'o','HandleVisibility','off');
plot(V(iMaxD),C(iMaxD,4),'o','HandleVisibility','off'); plot(V(iMaxE),C(iMaxE,5),'o','HandleVisibility','off'); plot(V(iMaxF),C(iMaxF,6),'o','HandleVisibility','off');
function dC = firstorder(tau,C)
global k
dC = zeros(6,1);
dC(1) = -k(1)*C(1)*C(2)^2 - 3*k(2)*C(1)*C(4);
dC(2) = -2*k(1)*C(1)*C(2)^2 - k(3)*C(2)*C(3)^2;
dC(3) = k(1)*C(1)*C(2)^2 + k(2)*C(1)*C(4) - 2*k(3)*C(2)*C(3)^2;
dC(4) = k(1)*C(1)*C(2)^2 - 2*k(2)*C(1)*C(4) + k(3)*C(2)*C(3)^2;
dC(5) = k(2)*C(1)*C(4);
dC(6) = k(3)*C(2)*C(3)^2;
end