# 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 =0;
end
% There are a total of 5 entries in each of the rate and state variable arrays.
% There are a total of 36 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_STATES(:,1) = strpad('x1 in component x1 (microg_l)');
LEGEND_CONSTANTS(:,1) = strpad('lambda_1 in component model_parameters (first_order_rate_constant)');
LEGEND_STATES(:,2) = strpad('x3 in component x3 (microg_l)');
LEGEND_CONSTANTS(:,2) = strpad('a1 in component model_parameters (flux)');
LEGEND_CONSTANTS(:,3) = strpad('a2 in component model_parameters (flux)');
LEGEND_CONSTANTS(:,4) = strpad('a3 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,5) = strpad('a4 in component model_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,6) = strpad('a5 in component model_parameters (per_microg_l)');
LEGEND_CONSTANTS(:,7) = strpad('a6 in component model_parameters (per_microg_l2)');
LEGEND_CONSTANTS(:,8) = strpad('a7 in component model_parameters (per_microg_l)');
LEGEND_CONSTANTS(:,9) = strpad('a8 in component model_parameters (per_microg_l2)');
LEGEND_STATES(:,3) = strpad('x2 in component x2 (microg_l)');
LEGEND_CONSTANTS(:,10) = strpad('lambda_2 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,11) = strpad('a9 in component model_parameters (flux)');
LEGEND_CONSTANTS(:,12) = strpad('a10 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,13) = strpad('a11 in component model_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,14) = strpad('a12 in component model_parameters (per_microg_l)');
LEGEND_CONSTANTS(:,15) = strpad('a13 in component model_parameters (per_microg_l2)');
LEGEND_CONSTANTS(:,16) = strpad('a14 in component model_parameters (per_microg_l)');
LEGEND_CONSTANTS(:,17) = strpad('a15 in component model_parameters (per_microg_l2)');
LEGEND_CONSTANTS(:,34) = strpad('lambda_3_ in component model_parameters (first_order_rate_constant)');
LEGEND_STATES(:,4) = strpad('x4 in component x4 (microg_l)');
LEGEND_STATES(:,5) = strpad('x5 in component x5 (microg_l)');
LEGEND_CONSTANTS(:,18) = strpad('a16 in component model_parameters (flux)');
LEGEND_CONSTANTS(:,19) = strpad('a17 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,20) = strpad('a18 in component model_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,21) = strpad('a19 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,22) = strpad('a20 in component model_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,23) = strpad('a21 in component model_parameters (per_microg_l)');
LEGEND_CONSTANTS(:,24) = strpad('a22 in component model_parameters (per_microg_l2)');
LEGEND_CONSTANTS(:,25) = strpad('a23 in component model_parameters (per_microg_l)');
LEGEND_CONSTANTS(:,26) = strpad('a24 in component model_parameters (per_microg_l2)');
LEGEND_CONSTANTS(:,27) = strpad('a25 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,28) = strpad('a26 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,35) = strpad('lambda_4_ in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,29) = strpad('a27 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,36) = strpad('lambda_5_ in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,30) = strpad('a28 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,31) = strpad('lambda_3 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,32) = strpad('lambda_4 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,33) = strpad('lambda_5 in component model_parameters (first_order_rate_constant)');
LEGEND_RATES(:,1) = strpad('d/dt x1 in component x1 (microg_l)');
LEGEND_RATES(:,3) = strpad('d/dt x2 in component x2 (microg_l)');
LEGEND_RATES(:,2) = strpad('d/dt x3 in component x3 (microg_l)');
LEGEND_RATES(:,4) = strpad('d/dt x4 in component x4 (microg_l)');
LEGEND_RATES(:,5) = strpad('d/dt x5 in component x5 (microg_l)');
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.01067;
CONSTANTS(:,1) = 0.059;
STATES(:,2) = 6.51;
CONSTANTS(:,2) = 0.000017;
CONSTANTS(:,3) = 0.0023;
CONSTANTS(:,4) = 0.6;
CONSTANTS(:,5) = 45;
CONSTANTS(:,6) = 36;
CONSTANTS(:,7) = 216;
CONSTANTS(:,8) = 0.28;
CONSTANTS(:,9) = 0.36;
STATES(:,3) = 0.04665;
CONSTANTS(:,10) = 0.028;
CONSTANTS(:,11) = 0.0003;
CONSTANTS(:,12) = 0.18;
CONSTANTS(:,13) = 150;
CONSTANTS(:,14) = 18;
CONSTANTS(:,15) = 460;
CONSTANTS(:,16) = 0.46;
CONSTANTS(:,17) = 0.1;
STATES(:,4) = 60.61;
STATES(:,5) = 12.61;
CONSTANTS(:,18) = 0.04;
CONSTANTS(:,19) = 150;
CONSTANTS(:,20) = 3800;
CONSTANTS(:,21) = 57;
CONSTANTS(:,22) = 2600;
CONSTANTS(:,23) = 200;
CONSTANTS(:,24) = 9400;
CONSTANTS(:,25) = 10;
CONSTANTS(:,26) = 320;
CONSTANTS(:,27) = 0.04;
CONSTANTS(:,28) = 0.00097;
CONSTANTS(:,29) = 0.57;
CONSTANTS(:,30) = 0.0017;
CONSTANTS(:,31) = 0.0986;
CONSTANTS(:,32) = 0.024;
CONSTANTS(:,33) = 3e-5;
CONSTANTS(:,34) = CONSTANTS(:,31)+CONSTANTS(:,29)+CONSTANTS(:,30);
CONSTANTS(:,35) = CONSTANTS(:,32)+CONSTANTS(:,27);
CONSTANTS(:,36) = CONSTANTS(:,33)+CONSTANTS(:,28);
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
RATES(:,1) = (CONSTANTS(:,2)+(CONSTANTS(:,3)+ CONSTANTS(:,4).*STATES(:,1)+ CONSTANTS(:,5).*power(STATES(:,1), 2.00000))./(1.00000+ CONSTANTS(:,6).*STATES(:,1)+ CONSTANTS(:,7).*power(STATES(:,1), 2.00000)+ CONSTANTS(:,8).*STATES(:,2)+ CONSTANTS(:,9).*power(STATES(:,2), 2.00000))) -  CONSTANTS(:,1).*STATES(:,1);
RATES(:,3) = (CONSTANTS(:,11)+ CONSTANTS(:,12).*STATES(:,1)+ CONSTANTS(:,13).*power(STATES(:,1), 2.00000))./(1.00000+ CONSTANTS(:,14).*STATES(:,1)+ CONSTANTS(:,15).*power(STATES(:,1), 2.00000)+ CONSTANTS(:,16).*STATES(:,2)+ CONSTANTS(:,17).*power(STATES(:,2), 2.00000)) -  CONSTANTS(:,10).*STATES(:,3);
RATES(:,2) = (CONSTANTS(:,18)+( CONSTANTS(:,19).*STATES(:,1)+ CONSTANTS(:,20).*power(STATES(:,1), 2.00000)+ CONSTANTS(:,21).*STATES(:,3)+ CONSTANTS(:,22).*power(STATES(:,3), 2.00000))./(1.00000+ CONSTANTS(:,23).*STATES(:,1)+ CONSTANTS(:,24).*power(STATES(:,1), 2.00000)+ CONSTANTS(:,25).*STATES(:,3)+ CONSTANTS(:,26).*power(STATES(:,3), 2.00000))+ CONSTANTS(:,27).*STATES(:,4)+ CONSTANTS(:,28).*STATES(:,5)) -  CONSTANTS(:,34).*STATES(:,2);
RATES(:,4) =  CONSTANTS(:,29).*STATES(:,2) -  CONSTANTS(:,35).*STATES(:,4);
RATES(:,5) =  CONSTANTS(:,30).*STATES(:,2) -  CONSTANTS(:,36).*STATES(:,5);
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
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 Liu, Hu, Peng, Liu, 1999 at changeset 5229909e9ac5.
Collaboration
To begin collaborating on this work, please use your git client and issue this command: