% Step 1: Introduction of Problem Parameters and Initial Conditions

k_01 = 0.15;
k_02 = 0.10;
k_03 = 0.05;

s_0  = 0.5;
e_0  = 0.2;
c_0  = 0.0;
p_0  = 0.2;

% Step 2: Initialization of Time Grid and Solution Components

T    = 1000;
h    = 25;
t    = (0:h:T)';

s    = zeros(length(t),1);
e    = zeros(length(t),1);
c    = zeros(length(t),1);
p    = zeros(length(t),1);

s(1) = s_0;
e(1) = e_0;
c(1) = c_0;
p(1) = p_0;

% Step 3: Explicit Reformulation of Implicit Eulerian Time-Stepping Method

for j = 1:1:(length(t)-1)
  h      = t(j+1) - t(j);
  A      = (h*k_01+h^2*k_01*k_03)/(1+h*k_02+h*k_03);
  B      = (e(j)+(h*(k_02+k_03)*c(j))/(1+h*k_02+h*k_03));
  C      = (h*k_01/(1+h*k_02+h*k_03));
  D      = (s(j)+(h*k_02*c(j))/(1+h*k_02+h*k_03));

  s(j+1) = (C*D-A*B-1)/(2*C) + sqrt(((C*D-A*B-1)/(2*C))^2+D/C);
  e(j+1) = (e(j)+(h*(k_02+k_03)*c(j))/(1+h*k_02+h*k_03))/(1+(h*k_01*s(j+1))/(1+h*k_02+h*k_03));
  c(j+1) = (c(j)+h*k_01*e(j+1)*s(j+1))/(1+h*k_02+h*k_03);
  p(j+1) = p(j)+h*k_03*c(j+1);
endfor

% Step 4: Plotting of Solution Components and Conservation Laws

figure(1)
plot(t,s,'linewidth',0.8,'marker','*','linestyle','-')
hold on
plot(t,e,'linewidth',0.8,'marker','+','linestyle','-')
hold on
plot(t,c,'linewidth',0.8,'marker','square','linestyle','-')
hold on
plot(t,p,'linewidth',0.8,'marker','diamond','linestyle','-')
title('Concentrations of enzymatic reaction','fontsize',20)
legend({'s(t)','e(t)','c(t)','p(t)'},'location','eastoutside','fontsize',14)
xlabel('Time','fontsize',14)
ylabel('Concentrations','fontsize',14)
xlim([0 1000])
ylim([0 0.8])
hold off

figure(2)
plot(t,e+c,'linewidth',0.8,'marker','*')
hold on
plot(t,s+c+p,'linewidth',0.8,'marker','+')
title('Conservation laws of enzymatic reaction','fontsize',20)
legend({'e(t)+c(t)','s(t)+c(t)+p(t)'},'location','eastoutside','fontsize',14)
xlabel('Time','fontsize',14)
ylabel('Conservation laws','fontsize',14)
xlim([0 1000])
ylim([0 0.8])
hold off
