# Generated Code

The following is matlab code generated by the CellML API from this CellML file. (Back to language selection)

The raw code is available.

```function [VOI, STATES, ALGEBRAIC, CONSTANTS] = mainFunction()
% This is the "main function".  In Matlab, things work best if you rename this function to match the filename.
[VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel();
end

function [algebraicVariableCount] = getAlgebraicVariableCount()
% Used later when setting a global variable with the number of algebraic variables.
% Note: This is not the "main method".
algebraicVariableCount =16;
end
% There are a total of 9 entries in each of the rate and state variable arrays.
% There are a total of 16 entries in the constant variable array.
%

function [VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel()
% Create ALGEBRAIC of correct size
global algebraicVariableCount;  algebraicVariableCount = getAlgebraicVariableCount();
% Initialise constants and state variables
[INIT_STATES, CONSTANTS] = initConsts;

% Set timespan to solve over
tspan = [0, 10];

% Set numerical accuracy options for ODE solver
options = odeset('RelTol', 1e-06, 'AbsTol', 1e-06, 'MaxStep', 1);

% Solve model with ODE solver
[VOI, STATES] = ode15s(@(VOI, STATES)computeRates(VOI, STATES, CONSTANTS), tspan, INIT_STATES, options);

% Compute algebraic variables
[RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS);
ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI);

% Plot state variables against variable of integration
[LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends();
figure();
plot(VOI, STATES);
xlabel(LEGEND_VOI);
l = legend(LEGEND_STATES);
set(l,'Interpreter','none');
end

function [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends()
LEGEND_STATES = ''; LEGEND_ALGEBRAIC = ''; LEGEND_VOI = ''; LEGEND_CONSTANTS = '';
LEGEND_VOI = strpad('time in component environment (hour)');
LEGEND_STATES(:,1) = strpad('A in component A (molecules)');
LEGEND_ALGEBRAIC(:,1) = strpad('RXN1 in component RXN1 (flux)');
LEGEND_ALGEBRAIC(:,2) = strpad('RXN2 in component RXN2 (flux)');
LEGEND_ALGEBRAIC(:,5) = strpad('RXN5 in component RXN5 (flux)');
LEGEND_ALGEBRAIC(:,11) = strpad('RXN11 in component RXN11 (flux)');
LEGEND_ALGEBRAIC(:,6) = strpad('RXN6 in component RXN6 (flux)');
LEGEND_ALGEBRAIC(:,10) = strpad('RXN10 in component RXN10 (flux)');
LEGEND_ALGEBRAIC(:,12) = strpad('RXN12 in component RXN12 (flux)');
LEGEND_STATES(:,2) = strpad('C in component C (molecules)');
LEGEND_ALGEBRAIC(:,3) = strpad('RXN3 in component RXN3 (flux)');
LEGEND_STATES(:,3) = strpad('DA in component DA (molecules)');
LEGEND_ALGEBRAIC(:,7) = strpad('RXN7 in component RXN7 (flux)');
LEGEND_STATES(:,4) = strpad('DAp in component DAp (molecules)');
LEGEND_ALGEBRAIC(:,8) = strpad('RXN8 in component RXN8 (flux)');
LEGEND_STATES(:,5) = strpad('DR in component DR (molecules)');
LEGEND_ALGEBRAIC(:,13) = strpad('RXN13 in component RXN13 (flux)');
LEGEND_STATES(:,6) = strpad('DRP in component DRP (molecules)');
LEGEND_ALGEBRAIC(:,14) = strpad('RXN14 in component RXN14 (flux)');
LEGEND_STATES(:,7) = strpad('MA in component MA (molecules)');
LEGEND_ALGEBRAIC(:,9) = strpad('RXN9 in component RXN9 (flux)');
LEGEND_STATES(:,8) = strpad('MR in component MR (molecules)');
LEGEND_ALGEBRAIC(:,15) = strpad('RXN15 in component RXN15 (flux)');
LEGEND_ALGEBRAIC(:,16) = strpad('RXN16 in component RXN16 (flux)');
LEGEND_STATES(:,9) = strpad('R in component R (molecules)');
LEGEND_ALGEBRAIC(:,4) = strpad('RXN4 in component RXN4 (flux)');
LEGEND_CONSTANTS(:,1) = strpad('Gamma_1 in component RXN1 (second_order_rate)');
LEGEND_CONSTANTS(:,2) = strpad('Delta_1 in component RXN2 (first_order_rate)');
LEGEND_CONSTANTS(:,3) = strpad('Delta_2 in component RXN3 (first_order_rate)');
LEGEND_CONSTANTS(:,4) = strpad('Delta_3 in component RXN4 (first_order_rate)');
LEGEND_CONSTANTS(:,5) = strpad('Gamma_2 in component RXN5 (second_order_rate)');
LEGEND_CONSTANTS(:,6) = strpad('Thetha_1 in component RXN6 (first_order_rate)');
LEGEND_CONSTANTS(:,7) = strpad('Alpha_1 in component RXN7 (first_order_rate)');
LEGEND_CONSTANTS(:,8) = strpad('Alpha_2 in component RXN8 (first_order_rate)');
LEGEND_CONSTANTS(:,9) = strpad('Delta_4 in component RXN9 (first_order_rate)');
LEGEND_CONSTANTS(:,10) = strpad('BetaA_1 in component RXN10 (first_order_rate)');
LEGEND_CONSTANTS(:,11) = strpad('Gamma_3 in component RXN11 (second_order_rate)');
LEGEND_CONSTANTS(:,12) = strpad('Theta_2 in component RXN12 (first_order_rate)');
LEGEND_CONSTANTS(:,13) = strpad('Alpha_3 in component RXN13 (first_order_rate)');
LEGEND_CONSTANTS(:,14) = strpad('Alpha_4 in component RXN14 (first_order_rate)');
LEGEND_CONSTANTS(:,15) = strpad('Delta_5 in component RXN15 (first_order_rate)');
LEGEND_CONSTANTS(:,16) = strpad('BetaR_1 in component RXN16 (first_order_rate)');
LEGEND_RATES(:,1) = strpad('d/dt A in component A (molecules)');
LEGEND_RATES(:,2) = strpad('d/dt C in component C (molecules)');
LEGEND_RATES(:,3) = strpad('d/dt DA in component DA (molecules)');
LEGEND_RATES(:,4) = strpad('d/dt DAp in component DAp (molecules)');
LEGEND_RATES(:,5) = strpad('d/dt DR in component DR (molecules)');
LEGEND_RATES(:,6) = strpad('d/dt DRP in component DRP (molecules)');
LEGEND_RATES(:,7) = strpad('d/dt MA in component MA (molecules)');
LEGEND_RATES(:,8) = strpad('d/dt MR in component MR (molecules)');
LEGEND_RATES(:,9) = strpad('d/dt R in component R (molecules)');
LEGEND_STATES  = LEGEND_STATES';
LEGEND_ALGEBRAIC = LEGEND_ALGEBRAIC';
LEGEND_RATES = LEGEND_RATES';
LEGEND_CONSTANTS = LEGEND_CONSTANTS';
end

function [STATES, CONSTANTS] = initConsts()
VOI = 0; CONSTANTS = []; STATES = []; ALGEBRAIC = [];
STATES(:,1) = 0.0;
STATES(:,2) = 0.0;
STATES(:,3) = 1;
STATES(:,4) = 0.0;
STATES(:,5) = 1;
STATES(:,6) = 1;
STATES(:,7) = 0.0;
STATES(:,8) = 0.0;
STATES(:,9) = 0.0;
CONSTANTS(:,1) = 2;
CONSTANTS(:,2) = 1;
CONSTANTS(:,3) = 1;
CONSTANTS(:,4) = 0.2;
CONSTANTS(:,5) = 1;
CONSTANTS(:,6) = 50;
CONSTANTS(:,7) = 50;
CONSTANTS(:,8) = 500;
CONSTANTS(:,9) = 10;
CONSTANTS(:,10) = 50;
CONSTANTS(:,11) = 1;
CONSTANTS(:,12) = 100;
CONSTANTS(:,13) = 0.01;
CONSTANTS(:,14) = 50;
CONSTANTS(:,15) = 0.5;
CONSTANTS(:,16) = 5;
if (isempty(STATES)), warning('Initial values for states not set');, end
end

function [RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS)
global algebraicVariableCount;
statesSize = size(STATES);
statesColumnCount = statesSize(2);
if ( statesColumnCount == 1)
STATES = STATES';
ALGEBRAIC = zeros(1, algebraicVariableCount);
utilOnes = 1;
else
statesRowCount = statesSize(1);
ALGEBRAIC = zeros(statesRowCount, algebraicVariableCount);
RATES = zeros(statesRowCount, statesColumnCount);
utilOnes = ones(statesRowCount, 1);
end
ALGEBRAIC(:,1) =  STATES(:,1).*STATES(:,9).*CONSTANTS(:,1);
ALGEBRAIC(:,3) =  STATES(:,2).*CONSTANTS(:,3);
RATES(:,2) =  (1.00000./1.00000).*(  - 1.00000.*ALGEBRAIC(:,3)+ALGEBRAIC(:,1));
ALGEBRAIC(:,5) =  STATES(:,1).*STATES(:,3).*CONSTANTS(:,5);
ALGEBRAIC(:,6) =  STATES(:,4).*CONSTANTS(:,6);
ALGEBRAIC(:,7) =  STATES(:,3).*CONSTANTS(:,7);
RATES(:,3) =  (1.00000./1.00000).*( -1.00000.*ALGEBRAIC(:,5)+ -1.00000.*ALGEBRAIC(:,7)+ALGEBRAIC(:,6)+ALGEBRAIC(:,7));
ALGEBRAIC(:,8) =  STATES(:,4).*CONSTANTS(:,8);
RATES(:,4) =  (1.00000./1.00000).*( -1.00000.*ALGEBRAIC(:,6)+ -1.00000.*ALGEBRAIC(:,8)+ALGEBRAIC(:,5)+ALGEBRAIC(:,8));
ALGEBRAIC(:,10) =  STATES(:,7).*CONSTANTS(:,10);
ALGEBRAIC(:,9) =  STATES(:,7).*CONSTANTS(:,9);
RATES(:,7) =  (1.00000./1.00000).*( -1.00000.*ALGEBRAIC(:,9)+ -1.00000.*ALGEBRAIC(:,10)+ALGEBRAIC(:,7)+ALGEBRAIC(:,8)+ALGEBRAIC(:,10));
ALGEBRAIC(:,2) =  STATES(:,1).*CONSTANTS(:,2);
ALGEBRAIC(:,11) =  STATES(:,1).*STATES(:,5).*CONSTANTS(:,11);
ALGEBRAIC(:,12) =  STATES(:,6).*CONSTANTS(:,12);
RATES(:,1) =  (1.00000./1.00000).*(  - 1.00000.*ALGEBRAIC(:,1)+  - 1.00000.*ALGEBRAIC(:,2)+  - 1.00000.*ALGEBRAIC(:,5)+  - 1.00000.*ALGEBRAIC(:,11)+ALGEBRAIC(:,6)+ALGEBRAIC(:,10)+ALGEBRAIC(:,12));
ALGEBRAIC(:,13) =  STATES(:,5).*CONSTANTS(:,13);
RATES(:,5) =  (1.00000./1.00000).*( -1.00000.*ALGEBRAIC(:,11)+ -1.00000.*ALGEBRAIC(:,13)+ALGEBRAIC(:,12)+ALGEBRAIC(:,13));
ALGEBRAIC(:,14) =  STATES(:,6).*CONSTANTS(:,14);
RATES(:,6) =  (1.00000./1.00000).*( -1.00000.*ALGEBRAIC(:,12)+ -1.00000.*ALGEBRAIC(:,14)+ALGEBRAIC(:,11)+ALGEBRAIC(:,14));
ALGEBRAIC(:,15) =  STATES(:,8).*CONSTANTS(:,15);
ALGEBRAIC(:,16) =  STATES(:,8).*CONSTANTS(:,16);
RATES(:,8) =  (1.00000./1.00000).*( -1.00000.*ALGEBRAIC(:,15)+ -1.00000.*ALGEBRAIC(:,16)+ALGEBRAIC(:,13)+ALGEBRAIC(:,14)+ALGEBRAIC(:,16));
ALGEBRAIC(:,4) =  STATES(:,9).*CONSTANTS(:,4);
RATES(:,9) =  (1.00000./1.00000).*( -1.00000.*ALGEBRAIC(:,1)+ -1.00000.*ALGEBRAIC(:,4)+ALGEBRAIC(:,3)+ALGEBRAIC(:,16));
RATES = RATES';
end

% Calculate algebraic variables
function ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI)
statesSize = size(STATES);
statesColumnCount = statesSize(2);
if ( statesColumnCount == 1)
STATES = STATES';
utilOnes = 1;
else
statesRowCount = statesSize(1);
utilOnes = ones(statesRowCount, 1);
end
ALGEBRAIC(:,1) =  STATES(:,1).*STATES(:,9).*CONSTANTS(:,1);
ALGEBRAIC(:,3) =  STATES(:,2).*CONSTANTS(:,3);
ALGEBRAIC(:,5) =  STATES(:,1).*STATES(:,3).*CONSTANTS(:,5);
ALGEBRAIC(:,6) =  STATES(:,4).*CONSTANTS(:,6);
ALGEBRAIC(:,7) =  STATES(:,3).*CONSTANTS(:,7);
ALGEBRAIC(:,8) =  STATES(:,4).*CONSTANTS(:,8);
ALGEBRAIC(:,10) =  STATES(:,7).*CONSTANTS(:,10);
ALGEBRAIC(:,9) =  STATES(:,7).*CONSTANTS(:,9);
ALGEBRAIC(:,2) =  STATES(:,1).*CONSTANTS(:,2);
ALGEBRAIC(:,11) =  STATES(:,1).*STATES(:,5).*CONSTANTS(:,11);
ALGEBRAIC(:,12) =  STATES(:,6).*CONSTANTS(:,12);
ALGEBRAIC(:,13) =  STATES(:,5).*CONSTANTS(:,13);
ALGEBRAIC(:,14) =  STATES(:,6).*CONSTANTS(:,14);
ALGEBRAIC(:,15) =  STATES(:,8).*CONSTANTS(:,15);
ALGEBRAIC(:,16) =  STATES(:,8).*CONSTANTS(:,16);
ALGEBRAIC(:,4) =  STATES(:,9).*CONSTANTS(:,4);
end

% Pad out or shorten strings to a set length
function strout = strpad(strin)
req_length = 160;
insize = size(strin,2);
if insize > req_length
strout = strin(1:req_length);
else
strout = [strin, blanks(req_length - insize)];
end
end

```
Source
Derived from workspace Vilar, Kueh, Barkai, Leibler, 2002 at changeset bfddd87b5e19.
Collaboration
To begin collaborating on this work, please use your git client and issue this command: