# 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 =10;
end
% There are a total of 8 entries in each of the rate and state variable arrays.
% There are a total of 29 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 (second)');
LEGEND_STATES(:,1) = strpad('c in component c (micromolar)');
LEGEND_CONSTANTS(:,1) = strpad('delta in component c (dimensionless)');
LEGEND_ALGEBRAIC(:,3) = strpad('J_serca in component J_serca (flux)');
LEGEND_CONSTANTS(:,29) = strpad('J_in in component J_in (flux)');
LEGEND_ALGEBRAIC(:,5) = strpad('J_pm in component J_pm (flux)');
LEGEND_ALGEBRAIC(:,1) = strpad('J_IPR in component J_IPR (flux)');
LEGEND_STATES(:,2) = strpad('ce in component ce (micromolar)');
LEGEND_CONSTANTS(:,2) = strpad('gamma in component ce (dimensionless)');
LEGEND_CONSTANTS(:,3) = strpad('p in component p (micromolar)');
LEGEND_CONSTANTS(:,4) = strpad('kf in component J_IPR (first_order_rate_constant)');
LEGEND_CONSTANTS(:,5) = strpad('g1 in component J_IPR (first_order_rate_constant)');
LEGEND_STATES(:,3) = strpad('A in component A (dimensionless)');
LEGEND_STATES(:,4) = strpad('O in component O (dimensionless)');
LEGEND_STATES(:,5) = strpad('R in component R (dimensionless)');
LEGEND_ALGEBRAIC(:,2) = strpad('phi_1 in component IPR_parameters (first_order_rate_constant)');
LEGEND_ALGEBRAIC(:,4) = strpad('phi_2 in component IPR_parameters (second_order_rate_constant)');
LEGEND_ALGEBRAIC(:,6) = strpad('phi_2_ in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,6) = strpad('k_1_ in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,7) = strpad('l_2_ in component IPR_parameters (first_order_rate_constant)');
LEGEND_STATES(:,6) = strpad('I_1 in component I_1 (dimensionless)');
LEGEND_STATES(:,7) = strpad('S in component S (dimensionless)');
LEGEND_ALGEBRAIC(:,7) = strpad('phi_3 in component IPR_parameters (second_order_rate_constant)');
LEGEND_ALGEBRAIC(:,8) = strpad('phi_4 in component IPR_parameters (first_order_rate_constant)');
LEGEND_ALGEBRAIC(:,9) = strpad('phi_4_ in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,8) = strpad('k_3_ in component IPR_parameters (first_order_rate_constant)');
LEGEND_STATES(:,8) = strpad('I_2 in component I_2 (dimensionless)');
LEGEND_ALGEBRAIC(:,10) = strpad('phi_5 in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,9) = strpad('k_1 in component IPR_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,10) = strpad('k_2 in component IPR_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,11) = strpad('k_2_ in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,12) = strpad('k_3 in component IPR_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,13) = strpad('k_4 in component IPR_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,14) = strpad('k_4_ in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,15) = strpad('l_2 in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,16) = strpad('l_4 in component IPR_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,17) = strpad('l_4_ in component IPR_parameters (second_order_rate_constant)');
LEGEND_CONSTANTS(:,18) = strpad('l_6 in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,19) = strpad('l_6_ in component IPR_parameters (first_order_rate_constant)');
LEGEND_CONSTANTS(:,20) = strpad('L_1 in component IPR_parameters (micromolar)');
LEGEND_CONSTANTS(:,21) = strpad('L_3 in component IPR_parameters (micromolar)');
LEGEND_CONSTANTS(:,22) = strpad('L_5 in component IPR_parameters (micromolar)');
LEGEND_CONSTANTS(:,23) = strpad('Vs in component J_serca (micromolar2_per_second)');
LEGEND_CONSTANTS(:,24) = strpad('Ks in component J_serca (micromolar)');
LEGEND_CONSTANTS(:,25) = strpad('Vp in component J_pm (flux)');
LEGEND_CONSTANTS(:,26) = strpad('Kp in component J_pm (micromolar)');
LEGEND_CONSTANTS(:,27) = strpad('alpha1 in component J_in (flux)');
LEGEND_CONSTANTS(:,28) = strpad('alpha2 in component J_in (first_order_rate_constant)');
LEGEND_RATES(:,1) = strpad('d/dt c in component c (micromolar)');
LEGEND_RATES(:,2) = strpad('d/dt ce in component ce (micromolar)');
LEGEND_RATES(:,5) = strpad('d/dt R in component R (dimensionless)');
LEGEND_RATES(:,4) = strpad('d/dt O in component O (dimensionless)');
LEGEND_RATES(:,6) = strpad('d/dt I_1 in component I_1 (dimensionless)');
LEGEND_RATES(:,8) = strpad('d/dt I_2 in component I_2 (dimensionless)');
LEGEND_RATES(:,7) = strpad('d/dt S in component S (dimensionless)');
LEGEND_RATES(:,3) = strpad('d/dt A in component A (dimensionless)');
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.01;
CONSTANTS(:,1) = 0.1;
STATES(:,2) = 0.01;
CONSTANTS(:,2) = 5.4;
CONSTANTS(:,3) = 10.0;
CONSTANTS(:,4) = 0.96;
CONSTANTS(:,5) = 0.002;
STATES(:,3) = 0.16;
STATES(:,4) = 0.01;
STATES(:,5) = 0.16;
CONSTANTS(:,6) = 0.04;
CONSTANTS(:,7) = 0.8;
STATES(:,6) = 0.16;
STATES(:,7) = 0.16;
CONSTANTS(:,8) = 29.8;
STATES(:,8) = 0.16;
CONSTANTS(:,9) = 0.64;
CONSTANTS(:,10) = 37.4;
CONSTANTS(:,11) = 1.4;
CONSTANTS(:,12) = 0.11;
CONSTANTS(:,13) = 4.0;
CONSTANTS(:,14) = 0.54;
CONSTANTS(:,15) = 1.7;
CONSTANTS(:,16) = 1.7;
CONSTANTS(:,17) = 2.5;
CONSTANTS(:,18) = 4707.0;
CONSTANTS(:,19) = 11.4;
CONSTANTS(:,20) = 0.12;
CONSTANTS(:,21) = 0.025;
CONSTANTS(:,22) = 54.7;
CONSTANTS(:,23) = 120.0;
CONSTANTS(:,24) = 0.18;
CONSTANTS(:,25) = 28.0;
CONSTANTS(:,26) = 0.42;
CONSTANTS(:,27) = 0.03;
CONSTANTS(:,28) = 0.2;
CONSTANTS(:,29) = CONSTANTS(:,27)+ CONSTANTS(:,28).*CONSTANTS(:,3);
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(:,9).*CONSTANTS(:,20)+CONSTANTS(:,15)).*STATES(:,1))./(CONSTANTS(:,20)+ STATES(:,1).*(1.00000+CONSTANTS(:,20)./CONSTANTS(:,21)));
RATES(:,6) =  ALGEBRAIC(:,2).*STATES(:,5) -  (CONSTANTS(:,6)+CONSTANTS(:,7)).*STATES(:,6);
ALGEBRAIC(:,3) =  (( CONSTANTS(:,23).*STATES(:,1))./(CONSTANTS(:,24)+STATES(:,1))).*(1.00000./STATES(:,2));
ALGEBRAIC(:,1) =  ( CONSTANTS(:,4).*power( 0.100000.*STATES(:,4)+ 0.900000.*STATES(:,3), 4.00000)+CONSTANTS(:,5)).*(STATES(:,2) - STATES(:,1));
RATES(:,2) =  CONSTANTS(:,2).*(ALGEBRAIC(:,3) - ALGEBRAIC(:,1));
ALGEBRAIC(:,5) = ( CONSTANTS(:,25).*power(STATES(:,1), 2.00000))./(power(CONSTANTS(:,26), 2.00000)+power(STATES(:,1), 2.00000));
RATES(:,1) = (ALGEBRAIC(:,1) - ALGEBRAIC(:,3))+ CONSTANTS(:,1).*(CONSTANTS(:,29) - ALGEBRAIC(:,5));
ALGEBRAIC(:,4) = ( CONSTANTS(:,10).*CONSTANTS(:,21)+ CONSTANTS(:,16).*STATES(:,1))./(CONSTANTS(:,21)+ STATES(:,1).*(1.00000+CONSTANTS(:,21)./CONSTANTS(:,20)));
ALGEBRAIC(:,6) = (CONSTANTS(:,11)+ CONSTANTS(:,17).*STATES(:,1))./(1.00000+STATES(:,1)./CONSTANTS(:,22));
RATES(:,5) = ( ALGEBRAIC(:,6).*STATES(:,4) - ( ALGEBRAIC(:,4).*CONSTANTS(:,3).*STATES(:,5)+ ALGEBRAIC(:,2).*STATES(:,5)))+ (CONSTANTS(:,7)+CONSTANTS(:,6)).*STATES(:,6);
ALGEBRAIC(:,7) = ( CONSTANTS(:,12).*CONSTANTS(:,22))./(STATES(:,1)+CONSTANTS(:,22));
RATES(:,7) =  1.00000.*ALGEBRAIC(:,7).*STATES(:,4) -  CONSTANTS(:,8).*STATES(:,7);
ALGEBRAIC(:,8) = ( ( CONSTANTS(:,13).*CONSTANTS(:,22)+CONSTANTS(:,18)).*STATES(:,1))./(STATES(:,1)+CONSTANTS(:,22));
ALGEBRAIC(:,9) = ( CONSTANTS(:,20).*(CONSTANTS(:,14)+CONSTANTS(:,19)))./(STATES(:,1)+CONSTANTS(:,20));
RATES(:,4) = ( ALGEBRAIC(:,4).*CONSTANTS(:,3).*STATES(:,5) -  (ALGEBRAIC(:,6)+ALGEBRAIC(:,8)+ 1.00000.*ALGEBRAIC(:,7)).*STATES(:,4))+ ALGEBRAIC(:,9).*STATES(:,3)+ CONSTANTS(:,8).*STATES(:,7);
ALGEBRAIC(:,10) = ( ( CONSTANTS(:,9).*CONSTANTS(:,20)+CONSTANTS(:,15)).*STATES(:,1))./(STATES(:,1)+CONSTANTS(:,20));
RATES(:,8) =  ALGEBRAIC(:,10).*STATES(:,3) -  (CONSTANTS(:,6)+CONSTANTS(:,7)).*STATES(:,8);
RATES(:,3) = ( ALGEBRAIC(:,8).*STATES(:,4) - ( ALGEBRAIC(:,9).*STATES(:,3)+ ALGEBRAIC(:,10).*STATES(:,3)))+ (CONSTANTS(:,6)+CONSTANTS(:,7)).*STATES(:,8);
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(:,9).*CONSTANTS(:,20)+CONSTANTS(:,15)).*STATES(:,1))./(CONSTANTS(:,20)+ STATES(:,1).*(1.00000+CONSTANTS(:,20)./CONSTANTS(:,21)));
ALGEBRAIC(:,3) =  (( CONSTANTS(:,23).*STATES(:,1))./(CONSTANTS(:,24)+STATES(:,1))).*(1.00000./STATES(:,2));
ALGEBRAIC(:,1) =  ( CONSTANTS(:,4).*power( 0.100000.*STATES(:,4)+ 0.900000.*STATES(:,3), 4.00000)+CONSTANTS(:,5)).*(STATES(:,2) - STATES(:,1));
ALGEBRAIC(:,5) = ( CONSTANTS(:,25).*power(STATES(:,1), 2.00000))./(power(CONSTANTS(:,26), 2.00000)+power(STATES(:,1), 2.00000));
ALGEBRAIC(:,4) = ( CONSTANTS(:,10).*CONSTANTS(:,21)+ CONSTANTS(:,16).*STATES(:,1))./(CONSTANTS(:,21)+ STATES(:,1).*(1.00000+CONSTANTS(:,21)./CONSTANTS(:,20)));
ALGEBRAIC(:,6) = (CONSTANTS(:,11)+ CONSTANTS(:,17).*STATES(:,1))./(1.00000+STATES(:,1)./CONSTANTS(:,22));
ALGEBRAIC(:,7) = ( CONSTANTS(:,12).*CONSTANTS(:,22))./(STATES(:,1)+CONSTANTS(:,22));
ALGEBRAIC(:,8) = ( ( CONSTANTS(:,13).*CONSTANTS(:,22)+CONSTANTS(:,18)).*STATES(:,1))./(STATES(:,1)+CONSTANTS(:,22));
ALGEBRAIC(:,9) = ( CONSTANTS(:,20).*(CONSTANTS(:,14)+CONSTANTS(:,19)))./(STATES(:,1)+CONSTANTS(:,20));
ALGEBRAIC(:,10) = ( ( CONSTANTS(:,9).*CONSTANTS(:,20)+CONSTANTS(:,15)).*STATES(:,1))./(STATES(:,1)+CONSTANTS(:,20));
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

```
Collaboration
To begin collaborating on this work, please use your git client and issue this command: