% MATLAB EXPLANATION
%
% Example 4 - FOR LOOP
%
% There are two basic types of loops in programming, and MATLAB can execute
% both of them.
%
% One type of loop is the FOR loop.  The FOR loop is used when a set of
% calculations is to performed multiple times, and the number of times the
% iteration to be performed is KNOWN.  That is, the number of times a set
% of calculations is to be executed has been determined by the time the loop
% is entered.
%
% An example of the FOR loop is allowing a reaction to occur for a given
% amount of time. Fof example, the Michaelis-Menten Equation with product
% inhibition is given bY:
%
% dCsub/dt = Vmax*Csub/(Csub + Km*(1 + Cprod/Kinh))
%
% where,
%
% Csub = concentration of substrate (e.g., mmol/L)
% Cprod = concentration of production (e.g., mmol/L)
% Vmax = maximum reaction rate (mmol/Lmin)
% Km = Michaelis constant (mmol/L)
% Kinh = inhibition constant (mmol/L)
%
% We will solve this problem using "Euler's Method", which is an
% approximation of the differential equation.
% 
% Delta(Csub) ~ Csub(i+1) - Csub(i)
% 
% then
% 
% Csub(i+1) = Vmax*Csub(i)*Deltatime/(Csub(i) + Km*(1 + Cprod(i)/Kinh))+ Csub(i)
%

% Initial Conditions

Csubold = 10;                % mmol/L
Csubinit = Csubold;          % mmol/L
Cprod = 0;                   % mmol/L
Km = 3;                      % mmol/L
Vmax = 1.5;                  % mmol/Lmin 
Kinh = 4;                    % mmol/L
deltatime = 0.000001;        % min

% The FOR loop ends with an 'end' statement. 
% We will solve the differential equation for 30 minutes.

for realtime = 0.0:deltatime:30
    Csubnew = -Vmax*Csubold*deltatime/(Csubold + Km*(1 + Cprod/Kinh)) + Csubold;
    Cprod = Csubinit - Csubnew;
    Csubold = Csubnew;   % Update the value of Csubold for the next iteration
end

%
% In the FOR loop expression, three numbers are separated by two colons.
% That is, the expression is:
%
% for variable = A:B:C
%
% where,
%
% A = the value that the variable will take on the first time in the loop
% B = the increment.  The value of the variable will change by this
% quantity each iteration of the loop
% C = the final value that the variable will attain.  When the variable
% exceeds this value then the loop will end.    
%
%

format short
Cprod
