% MATLAB EXPLANATION
%
% Example 4b - FOR LOOP, output to an excel file
%
% This program is identical to the program shown in Example 4a except
% the output will go to an excel file in the E: directory.  On my computer,
% the E: directory is a card reader, and so the excel file is just placed
% on the top folder on the card.  You can place a file to whatever
% directory and folder you want.
%
% The program of Example 4a used a time step of one millionth of a minute.
% The total time that data were collected was 30 minutes.  Thus, the FOR
% loop was executed 30 million times.  It is unlikely that those many data
% points are going to be necessary to plot.  Instead, let's collect 300
% data points.  This means that we will want to store data every 0.1 minutes
% during the reaction, or after every 100,000 iterations.
%
% To store data to excel after 100,000 iterations, we will have an internal
% counter that keeps track of the number of iterations.  When the number of
% iterations reaches 100,000, we will store the time, the substrate
% concentration, and the production concentration in excel, and start
% the internal counter over again.
%
% Remember, this is the same reaction used in Example 4a, the
% Michaelis-Menten Equation with product inhibition 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)
%
% I 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

count = 0;
row = 3;

for realtime = 0.0:deltatime:30
    Csubnew = -Vmax*Csubold*deltatime/(Csubold + Km*(1 + Cprod/Kinh)) + Csubold;
    Cprod = Csubinit - Csubnew;
    Csubold = Csubnew; % This updates the value of Csubold for the next iteration through the loop
    count = count + 1;
    if(count>=100000)
        m = [realtime,Csubnew,Cprod];
        cellname = ['A' num2str(row) ':C' num2str(row)];
        xlswrite('E:examp4b.xls',m,'Worksheet A', cellname);
        count = 0;
        row = row + 1;
    end
end

% We have learned a bunch of new statements.   
%
% First, notice the IF statement.  The IF statement contains a
% conditional expression appearing in parentheses.  The code within the IF
% statement (between 'if' and 'end') will only execute if the conditional
% expression is true.  If the conditional expression is not true, then the
% program will jump to the first statement after the end of the if
% statement.
%
% The next set of statements to explain is the xlswrite statement and
% associated statements.  The first line is
% 
% m = [realtime,Csubnew,Cprd];
% 
% This statement defines a matrix having one row.  The matrix contains the
% elements to be stored in excel.  In this case, three values
% will be stored in excel in this order...realtime, Csubnew, Cprod.
%
% The second line is
% 
% cellname = ['A' num2str(row) ':C' num2str(row)];
% 
% This statement creates a string variable having the cells in excel where
% I want to send the data.  The first time into this if statement, the
% value of the variable row will be 3.  This must be converted from numbers
% to string variable types, with the result that cellname will be "A3:C3"
% the first time through the if statement.  Note that the variable row is
% incremented, so that the second time through the if statement, cellname
% will be 'A4:C4'.
%
% The third line is
%
% xlswrite('E:examp4b.xls',m,'Worksheet A', cellname);
%
% This statement contains 
%
% The name of the file 'E:example4b.xls'
% The matrix of data to be stored in the file, m, defined earlier
% The name of the Worksheet in the excel file 'Worksheet A'
% The cells where the data will be placed, cellname, defined earlier
%
% You have to be careful to increment the row, otherwise, data will be
% overwritten.  Also, note that one does not have to start storing the data
% onto row 1.  I typically start with row 2 or row 3 so that I have some rows
% which can be used as headers.