# 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 =6;
end
% There are a total of 9 entries in each of the rate and state variable arrays.
% There are a total of 51 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 (day)');
LEGEND_ALGEBRAIC(:,1) = strpad('A_B in component A_B (mm2)');
LEGEND_STATES(:,1) = strpad('r_B in component r_B (mm)');
LEGEND_CONSTANTS(:,1) = strpad('X_c in component r_B (pM)');
LEGEND_CONSTANTS(:,2) = strpad('X_b in component r_B (pM)');
LEGEND_CONSTANTS(:,3) = strpad('X_y in component r_B (pM)');
LEGEND_CONSTANTS(:,4) = strpad('k_rB in component r_B (first_order_rate_constant)');
LEGEND_CONSTANTS(:,5) = strpad('k_for in component r_B (mm_per_day)');
LEGEND_CONSTANTS(:,6) = strpad('k_res in component r_B (mm_per_day)');
LEGEND_STATES(:,2) = strpad('xc in component xc (pM)');
LEGEND_STATES(:,3) = strpad('xb in component xb (pM)');
LEGEND_STATES(:,4) = strpad('xy in component xy (pM)');
LEGEND_CONSTANTS(:,7) = strpad('X_bss in component xy (pM)');
LEGEND_CONSTANTS(:,8) = strpad('X_yss in component xy (pM)');
LEGEND_CONSTANTS(:,9) = strpad('k_byp in component xy (first_order_rate_constant)');
LEGEND_CONSTANTS(:,10) = strpad('k_yd in component xy (first_order_rate_constant)');
LEGEND_ALGEBRAIC(:,6) = strpad('F_sti in component F_sti (pM_N_per_mm2)');
LEGEND_CONSTANTS(:,11) = strpad('k_Fs in component F_sti (mm2_per_N)');
LEGEND_CONSTANTS(:,12) = strpad('k_y in component F_sti (per_pM)');
LEGEND_ALGEBRAIC(:,5) = strpad('F_s in component F_s (N_per_mm2)');
LEGEND_ALGEBRAIC(:,3) = strpad('F_a in component F_s (newton)');
LEGEND_STATES(:,5) = strpad('x_no in component x_no (pM)');
LEGEND_CONSTANTS(:,13) = strpad('X_noe in component x_no (flux)');
LEGEND_CONSTANTS(:,14) = strpad('k_yno in component x_no (mm2_day_per_dyn)');
LEGEND_CONSTANTS(:,15) = strpad('k_nod in component x_no (first_order_rate_constant)');
LEGEND_STATES(:,6) = strpad('x_pge in component x_pge (pM)');
LEGEND_CONSTANTS(:,16) = strpad('X_pgex in component x_pge (flux)');
LEGEND_CONSTANTS(:,17) = strpad('k_pged in component x_pge (first_order_rate_constant)');
LEGEND_CONSTANTS(:,18) = strpad('k_nopge in component x_pge (first_order_rate_constant)');
LEGEND_CONSTANTS(:,19) = strpad('k_ypge in component x_pge (mm2_day_per_dyn)');
LEGEND_STATES(:,7) = strpad('x_opg in component x_opg (pM)');
LEGEND_CONSTANTS(:,20) = strpad('k_opgd in component x_opg (first_order_rate_constant)');
LEGEND_CONSTANTS(:,21) = strpad('k_nopg in component x_opg (first_order_rate_constant)');
LEGEND_CONSTANTS(:,22) = strpad('Io in component model_parameters (flux)');
LEGEND_CONSTANTS(:,23) = strpad('K_o_p in component model_parameters (picomole_per_day_picomole_cells)');
LEGEND_STATES(:,8) = strpad('xr in component xr (pM)');
LEGEND_ALGEBRAIC(:,4) = strpad('pi_c in component model_parameters (dimensionless)');
LEGEND_STATES(:,9) = strpad('x_kl in component x_kl (pM)');
LEGEND_ALGEBRAIC(:,2) = strpad('pi_L in component x_kl (dimensionless)');
LEGEND_CONSTANTS(:,24) = strpad('K_l_p in component x_kl (dimensionless)');
LEGEND_CONSTANTS(:,25) = strpad('K in component x_kl (pM)');
LEGEND_CONSTANTS(:,26) = strpad('k_nokl in component x_kl (first_order_rate_constant)');
LEGEND_CONSTANTS(:,27) = strpad('Il in component x_kl (flux)');
LEGEND_CONSTANTS(:,28) = strpad('rl in component x_kl (flux)');
LEGEND_CONSTANTS(:,29) = strpad('k1 in component x_kl (second_order_rate_constant)');
LEGEND_CONSTANTS(:,30) = strpad('k2 in component x_kl (first_order_rate_constant)');
LEGEND_CONSTANTS(:,31) = strpad('k3 in component x_kl (second_order_rate_constant)');
LEGEND_CONSTANTS(:,32) = strpad('k4 in component x_kl (first_order_rate_constant)');
LEGEND_CONSTANTS(:,33) = strpad('ko in component x_kl (first_order_rate_constant)');
LEGEND_CONSTANTS(:,51) = strpad('pi_p in component model_parameters (dimensionless)');
LEGEND_CONSTANTS(:,34) = strpad('D_R in component xr (flux)');
LEGEND_CONSTANTS(:,35) = strpad('k_pger in component xr (first_order_rate_constant)');
LEGEND_CONSTANTS(:,47) = strpad('D_B in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,36) = strpad('k_B in component xb (first_order_rate_constant)');
LEGEND_CONSTANTS(:,37) = strpad('D_C in component xc (flux)');
LEGEND_CONSTANTS(:,38) = strpad('D_A in component xc (first_order_rate_constant)');
LEGEND_CONSTANTS(:,39) = strpad('f0 in component model_parameters (dimensionless)');
LEGEND_CONSTANTS(:,40) = strpad('dB in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,41) = strpad('C_s in component model_parameters (pM)');
LEGEND_CONSTANTS(:,48) = strpad('P in component model_parameters (pM)');
LEGEND_CONSTANTS(:,49) = strpad('P_0 in component model_parameters (pM)');
LEGEND_CONSTANTS(:,50) = strpad('P_s in component model_parameters (pM)');
LEGEND_CONSTANTS(:,42) = strpad('SP in component model_parameters (flux)');
LEGEND_CONSTANTS(:,43) = strpad('k5 in component model_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,44) = strpad('k6 in component model_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,45) = strpad('IP in component model_parameters (flux)');
LEGEND_CONSTANTS(:,46) = strpad('kP in component model_parameters (first_order_rate_constant)');
LEGEND_RATES(:,1) = strpad('d/dt r_B in component r_B (mm)');
LEGEND_RATES(:,4) = strpad('d/dt xy in component xy (pM)');
LEGEND_RATES(:,5) = strpad('d/dt x_no in component x_no (pM)');
LEGEND_RATES(:,6) = strpad('d/dt x_pge in component x_pge (pM)');
LEGEND_RATES(:,7) = strpad('d/dt x_opg in component x_opg (pM)');
LEGEND_RATES(:,9) = strpad('d/dt x_kl in component x_kl (pM)');
LEGEND_RATES(:,8) = strpad('d/dt xr in component xr (pM)');
LEGEND_RATES(:,3) = strpad('d/dt xb in component xb (pM)');
LEGEND_RATES(:,2) = strpad('d/dt xc in component xc (pM)');
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.9912;
CONSTANTS(:,1) = 9.127e-4;
CONSTANTS(:,2) = 7.282e-4;
CONSTANTS(:,3) = 7.300e-3;
CONSTANTS(:,4) = 1.0e0;
CONSTANTS(:,5) = 1.0e-3;
CONSTANTS(:,6) = 10.0e-3;
STATES(:,2) = 9.127e-4;
STATES(:,3) = 7.282e-4;
STATES(:,4) = 7.300e-3;
CONSTANTS(:,7) = 7.282e-4;
CONSTANTS(:,8) = 7.300e-3;
CONSTANTS(:,9) = 1.00e-1;
CONSTANTS(:,10) = 1.00e0;
CONSTANTS(:,11) = 1.00e0;
CONSTANTS(:,12) = 1.00e0;
STATES(:,5) = 0.02;
CONSTANTS(:,13) = 0e0;
CONSTANTS(:,14) = 2e4;
CONSTANTS(:,15) = 1e3;
STATES(:,6) = 0.01;
CONSTANTS(:,16) = 0e0;
CONSTANTS(:,17) = 1e2;
CONSTANTS(:,18) = 1e0;
CONSTANTS(:,19) = 1e2;
STATES(:,7) = 0.01;
CONSTANTS(:,20) = 3.5e-1;
CONSTANTS(:,21) = 1e1;
CONSTANTS(:,22) = 0.0;
CONSTANTS(:,23) = 2e5;
STATES(:,8) = 7.734e-4;
STATES(:,9) = 0.01;
CONSTANTS(:,24) = 3e6;
CONSTANTS(:,25) = 10;
CONSTANTS(:,26) = 1e2;
CONSTANTS(:,27) = 0;
CONSTANTS(:,28) = 1e3;
CONSTANTS(:,29) = 1e-2;
CONSTANTS(:,30) = 10;
CONSTANTS(:,31) = 5.8e-4;
CONSTANTS(:,32) = 1.7e-2;
CONSTANTS(:,33) = 0.35;
CONSTANTS(:,34) = 7e-4;
CONSTANTS(:,35) = 1e-4;
CONSTANTS(:,36) = 0.189;
CONSTANTS(:,37) = 0.189;
CONSTANTS(:,38) = 0.7;
CONSTANTS(:,39) = 0.05;
CONSTANTS(:,40) = 0.7;
CONSTANTS(:,41) = 5e-3;
CONSTANTS(:,42) = 250;
CONSTANTS(:,43) = 0.02;
CONSTANTS(:,44) = 3;
CONSTANTS(:,45) = 0;
CONSTANTS(:,46) = 86;
CONSTANTS(:,47) =  CONSTANTS(:,39).*CONSTANTS(:,40);
CONSTANTS(:,48) = CONSTANTS(:,45)./CONSTANTS(:,46);
CONSTANTS(:,49) = CONSTANTS(:,42)./CONSTANTS(:,46);
CONSTANTS(:,50) = CONSTANTS(:,44)./CONSTANTS(:,43);
CONSTANTS(:,51) = (CONSTANTS(:,48)+CONSTANTS(:,49))./(CONSTANTS(:,48)+CONSTANTS(:,50));
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(:,5)./CONSTANTS(:,2)).*STATES(:,3)+ (1.00000./CONSTANTS(:,3)).*STATES(:,4)) - ( (CONSTANTS(:,6)./CONSTANTS(:,1)).*STATES(:,2)+ CONSTANTS(:,4).*STATES(:,1));
RATES(:,4) =  CONSTANTS(:,9).*(STATES(:,3) - CONSTANTS(:,7)) -  CONSTANTS(:,10).*(STATES(:,4) - CONSTANTS(:,8));
RATES(:,9) = (CONSTANTS(:,28)+CONSTANTS(:,27)) - ( CONSTANTS(:,26).*STATES(:,5)+ CONSTANTS(:,28).*((1.00000+ (CONSTANTS(:,29)./CONSTANTS(:,30)).*STATES(:,7)+ (CONSTANTS(:,31)./CONSTANTS(:,32)).*CONSTANTS(:,25))./( CONSTANTS(:,24).*CONSTANTS(:,51).*STATES(:,3))).*STATES(:,9));
ALGEBRAIC(:,4) = (STATES(:,2)+ CONSTANTS(:,39).*CONSTANTS(:,41))./(STATES(:,2)+CONSTANTS(:,41));
RATES(:,7) = ( (CONSTANTS(:,23)./1.00000).*ALGEBRAIC(:,4).*STATES(:,8)+CONSTANTS(:,22)+ CONSTANTS(:,21).*STATES(:,5)) -  CONSTANTS(:,20).*STATES(:,7);
RATES(:,8) = ( CONSTANTS(:,34).*ALGEBRAIC(:,4)+ CONSTANTS(:,35).*STATES(:,6)) -  (CONSTANTS(:,47)./ALGEBRAIC(:,4)).*STATES(:,8);
RATES(:,3) =  (CONSTANTS(:,47)./ALGEBRAIC(:,4)).*STATES(:,8) -  CONSTANTS(:,36).*STATES(:,3);
ALGEBRAIC(:,2) =  (( ( (CONSTANTS(:,31)./CONSTANTS(:,32)).*CONSTANTS(:,24)).*CONSTANTS(:,51).*STATES(:,3))./(1.00000+( CONSTANTS(:,31).*CONSTANTS(:,25))./CONSTANTS(:,32)+ (CONSTANTS(:,29)./( CONSTANTS(:,30).*CONSTANTS(:,33))).*( ((CONSTANTS(:,23)./1.00000)./CONSTANTS(:,51)).*STATES(:,8)+CONSTANTS(:,22)))).*(1.00000+CONSTANTS(:,27)./CONSTANTS(:,28));
RATES(:,2) =  CONSTANTS(:,37).*ALGEBRAIC(:,2) -  CONSTANTS(:,38).*ALGEBRAIC(:,4).*STATES(:,2);
ALGEBRAIC(:,1) =  power(STATES(:,1), 2.00000).* pi;
ALGEBRAIC(:,3) = piecewise({VOI>=100.000&VOI<130.000, 10000.0 }, 100.000);
ALGEBRAIC(:,5) = ALGEBRAIC(:,3)./ALGEBRAIC(:,1);
ALGEBRAIC(:,6) = ( ALGEBRAIC(:,5).*STATES(:,4))./(1.00000+exp( - ( CONSTANTS(:,11).*ALGEBRAIC(:,5)+ CONSTANTS(:,12).*STATES(:,4))));
RATES(:,5) = ( CONSTANTS(:,14).*ALGEBRAIC(:,6)+CONSTANTS(:,13)) -  CONSTANTS(:,15).*STATES(:,5);
RATES(:,6) = ( CONSTANTS(:,19).*ALGEBRAIC(:,6)+ CONSTANTS(:,18).*STATES(:,5)+CONSTANTS(:,16)) -  CONSTANTS(:,17).*STATES(:,6);
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(:,4) = (STATES(:,2)+ CONSTANTS(:,39).*CONSTANTS(:,41))./(STATES(:,2)+CONSTANTS(:,41));
ALGEBRAIC(:,2) =  (( ( (CONSTANTS(:,31)./CONSTANTS(:,32)).*CONSTANTS(:,24)).*CONSTANTS(:,51).*STATES(:,3))./(1.00000+( CONSTANTS(:,31).*CONSTANTS(:,25))./CONSTANTS(:,32)+ (CONSTANTS(:,29)./( CONSTANTS(:,30).*CONSTANTS(:,33))).*( ((CONSTANTS(:,23)./1.00000)./CONSTANTS(:,51)).*STATES(:,8)+CONSTANTS(:,22)))).*(1.00000+CONSTANTS(:,27)./CONSTANTS(:,28));
ALGEBRAIC(:,1) =  power(STATES(:,1), 2.00000).* pi;
ALGEBRAIC(:,3) = piecewise({VOI>=100.000&VOI<130.000, 10000.0 }, 100.000);
ALGEBRAIC(:,5) = ALGEBRAIC(:,3)./ALGEBRAIC(:,1);
ALGEBRAIC(:,6) = ( ALGEBRAIC(:,5).*STATES(:,4))./(1.00000+exp( - ( CONSTANTS(:,11).*ALGEBRAIC(:,5)+ CONSTANTS(:,12).*STATES(:,4))));
end

% Compute result of a piecewise function
function x = piecewise(cases, default)
set = [0];
for i = 1:2:length(cases)
if (length(cases{i+1}) == 1)
x(cases{i} & ~set,:) = cases{i+1};
else
x(cases{i} & ~set,:) = cases{i+1}(cases{i} & ~set);
end
set = set | cases{i};
if(set), break, end
end
if (length(default) == 1)
x(~set,:) = default;
else
x(~set,:) = default(~set);
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 Maldonado, Borchers, Findeisen and Allgower, 2006 at changeset d1c385b5e9d2.
Collaboration
To begin collaborating on this work, please use your git client and issue this command: