# 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 =4;
end
% There are a total of 2 entries in each of the rate and state variable arrays.
% There are a total of 13 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 (minute)');
LEGEND_ALGEBRAIC(:,1) = strpad('B in component B (micromolar)');
LEGEND_CONSTANTS(:,1) = strpad('F in component B (micromolar)');
LEGEND_CONSTANTS(:,2) = strpad('n2 in component B (dimensionless)');
LEGEND_CONSTANTS(:,3) = strpad('K2 in component B (per_micromolar)');
LEGEND_STATES(:,1) = strpad('Ca in component Ca (micromolar)');
LEGEND_ALGEBRAIC(:,4) = strpad('R in component R (flux)');
LEGEND_CONSTANTS(:,4) = strpad('Vmax in component R (flux)');
LEGEND_CONSTANTS(:,5) = strpad('Km in component R (micromolar)');
LEGEND_ALGEBRAIC(:,3) = strpad('fu in component R (dimensionless)');
LEGEND_CONSTANTS(:,13) = strpad('ISF in component R (dimensionless)');
LEGEND_CONSTANTS(:,6) = strpad('age in component R (dimensionless)');
LEGEND_STATES(:,2) = strpad('C in component C (micromolar)');
LEGEND_CONSTANTS(:,7) = strpad('Va in component Ca (ml)');
LEGEND_CONSTANTS(:,8) = strpad('Vv in component Ca (ml)');
LEGEND_CONSTANTS(:,9) = strpad('Qc in component model_constants (flow)');
LEGEND_ALGEBRAIC(:,2) = strpad('Cv in component Cv (micromolar)');
LEGEND_CONSTANTS(:,10) = strpad('Q in component model_constants (flow)');
LEGEND_CONSTANTS(:,11) = strpad('P in component model_constants (dimensionless)');
LEGEND_CONSTANTS(:,12) = strpad('V in component C (ml)');
LEGEND_RATES(:,1) = strpad('d/dt Ca in component Ca (micromolar)');
LEGEND_RATES(:,2) = strpad('d/dt C in component C (micromolar)');
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 = [];
CONSTANTS(:,1) = 0.48;
CONSTANTS(:,2) = 1;
CONSTANTS(:,3) = 0.8532;
STATES(:,1) = 6.6685;
CONSTANTS(:,4) = 9.433e-3;
CONSTANTS(:,5) = 198;
CONSTANTS(:,6) = 5;
STATES(:,2) = 0;
CONSTANTS(:,7) = 2148;
CONSTANTS(:,8) = 3431;
CONSTANTS(:,9) = 6445.65;
CONSTANTS(:,10) = 1221.34;
CONSTANTS(:,11) = 15.61;
CONSTANTS(:,12) = 1454;
CONSTANTS(:,13) =  - 8.32120+ 2.04010.*CONSTANTS(:,6)+ 4.19620.*arbitrary_log( CONSTANTS(:,6).*365.000, 10);
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(:,2) = (( CONSTANTS(:,10).*STATES(:,2))./CONSTANTS(:,11))./CONSTANTS(:,9);
RATES(:,1) = ( CONSTANTS(:,9).*(ALGEBRAIC(:,2) - STATES(:,1)))./(CONSTANTS(:,7)+CONSTANTS(:,8));
ALGEBRAIC(:,1) = ( CONSTANTS(:,1).*CONSTANTS(:,2).*CONSTANTS(:,3).*STATES(:,1))./(1.00000+ CONSTANTS(:,3).*CONSTANTS(:,1));
ALGEBRAIC(:,3) = CONSTANTS(:,1)./(CONSTANTS(:,1)+ALGEBRAIC(:,1));
ALGEBRAIC(:,4) = ( CONSTANTS(:,13).*CONSTANTS(:,4).*ALGEBRAIC(:,3).*STATES(:,2))./(CONSTANTS(:,5)+ ALGEBRAIC(:,3).*STATES(:,2));
RATES(:,2) = ( CONSTANTS(:,10).*(STATES(:,1) - STATES(:,2)./CONSTANTS(:,11)) -  ALGEBRAIC(:,4).*1.00000)./CONSTANTS(:,12);
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(:,2) = (( CONSTANTS(:,10).*STATES(:,2))./CONSTANTS(:,11))./CONSTANTS(:,9);
ALGEBRAIC(:,1) = ( CONSTANTS(:,1).*CONSTANTS(:,2).*CONSTANTS(:,3).*STATES(:,1))./(1.00000+ CONSTANTS(:,3).*CONSTANTS(:,1));
ALGEBRAIC(:,3) = CONSTANTS(:,1)./(CONSTANTS(:,1)+ALGEBRAIC(:,1));
ALGEBRAIC(:,4) = ( CONSTANTS(:,13).*CONSTANTS(:,4).*ALGEBRAIC(:,3).*STATES(:,2))./(CONSTANTS(:,5)+ ALGEBRAIC(:,3).*STATES(:,2));
end

% Compute a logarithm to any base" +
function x = arbitrary_log(a, base)
x = log(a) ./ log(base);
end

% Pad out or shorten strings to a set length
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 Yang, Tong, Mccarver, Hines, Beard, 2006 at changeset 4c73f9690f45.
Collaboration
To begin collaborating on this work, please use your git client and issue this command: