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 0 entries in each of the rate and state variable arrays.
% There are a total of 30 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_CONSTANTS(:,29) = strpad('SHbO2 in component SHbO2 (dimensionless)');
LEGEND_CONSTANTS(:,27) = strpad('KHbO2 in component KHbO2 (per_molar)');
LEGEND_CONSTANTS(:,21) = strpad('O2 in component O2 (molar)');
LEGEND_CONSTANTS(:,30) = strpad('SHbCO2 in component SHbCO2 (dimensionless)');
LEGEND_CONSTANTS(:,28) = strpad('KHbCO2 in component KHbCO2 (per_molar)');
LEGEND_CONSTANTS(:,23) = strpad('CO2 in component CO2 (molar)');
LEGEND_CONSTANTS(:,25) = strpad('Hrbc in component model_parameters (molar)');
LEGEND_CONSTANTS(:,1) = strpad('K2 in component model_parameters (per_molar)');
LEGEND_CONSTANTS(:,2) = strpad('K2_ in component model_parameters (molar)');
LEGEND_CONSTANTS(:,3) = strpad('K3 in component model_parameters (per_molar)');
LEGEND_CONSTANTS(:,4) = strpad('K3_ in component model_parameters (molar)');
LEGEND_CONSTANTS(:,26) = strpad('K4 in component K4 (per_molar)');
LEGEND_CONSTANTS(:,5) = strpad('K5_ in component model_parameters (molar)');
LEGEND_CONSTANTS(:,6) = strpad('K6_ in component model_parameters (molar)');
LEGEND_CONSTANTS(:,7) = strpad('O2_S in component model_parameters (micromolar)');
LEGEND_CONSTANTS(:,8) = strpad('H_S in component model_parameters (nanomolar)');
LEGEND_CONSTANTS(:,9) = strpad('n1 in component model_parameters (dimensionless)');
LEGEND_CONSTANTS(:,10) = strpad('n2 in component model_parameters (dimensionless)');
LEGEND_CONSTANTS(:,11) = strpad('CO2_S in component model_parameters (millimolar)');
LEGEND_CONSTANTS(:,12) = strpad('K4_ in component model_parameters (per_molar)');
LEGEND_CONSTANTS(:,13) = strpad('n0 in component model_parameters (dimensionless)');
LEGEND_CONSTANTS(:,20) = strpad('alpha_O2 in component alpha_O2 (M_mmHg)');
LEGEND_CONSTANTS(:,14) = strpad('PO2 in component model_parameters (mmHg)');
LEGEND_CONSTANTS(:,22) = strpad('alpha_CO2 in component alpha_CO2 (M_mmHg)');
LEGEND_CONSTANTS(:,15) = strpad('PCO2 in component model_parameters (mmHg)');
LEGEND_CONSTANTS(:,16) = strpad('Wpl in component model_parameters (ml_ml)');
LEGEND_CONSTANTS(:,17) = strpad('T in component model_parameters (celsius)');
LEGEND_CONSTANTS(:,18) = strpad('Rrbc in component model_parameters (dimensionless)');
LEGEND_CONSTANTS(:,24) = strpad('Hpl in component model_parameters (molar)');
LEGEND_CONSTANTS(:,19) = strpad('pHpl in component model_parameters (pH)');
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) = 29.5;
CONSTANTS(:,2) = 1E-6;
CONSTANTS(:,3) = 25.1;
CONSTANTS(:,4) = 1E-6;
CONSTANTS(:,5) = 2.63E-8;
CONSTANTS(:,6) = 1.91E-8;
CONSTANTS(:,7) = 146.0;
CONSTANTS(:,8) = 57.5;
CONSTANTS(:,9) = 1.06;
CONSTANTS(:,10) = 0.12;
CONSTANTS(:,11) = 1.31;
CONSTANTS(:,12) = 202123.0;
CONSTANTS(:,13) = 1.7;
CONSTANTS(:,14) = 100.0;
CONSTANTS(:,15) = 40.0;
CONSTANTS(:,16) = 0.94;
CONSTANTS(:,17) = 37.0;
CONSTANTS(:,18) = 0.69;
CONSTANTS(:,19) = 7.24;
CONSTANTS(:,20) = ((1.37000 - 0.0137000.*(CONSTANTS(:,17) - 37.0000))+ 0.000580000.*power(CONSTANTS(:,17) - 37.0000, 2.00000)).*(1.00000e-06./CONSTANTS(:,16));
CONSTANTS(:,21) = CONSTANTS(:,20).*CONSTANTS(:,14);
CONSTANTS(:,22) = ((3.07000 - 0.0570000.*(CONSTANTS(:,17) - 37.0000))+ 0.00200000.*power(CONSTANTS(:,17) - 37.0000, 2.00000)).*(1.00000e-05./CONSTANTS(:,16));
CONSTANTS(:,23) = CONSTANTS(:,22).*CONSTANTS(:,15);
CONSTANTS(:,24) = power(10.0000, - CONSTANTS(:,19));
CONSTANTS(:,25) = CONSTANTS(:,24)./CONSTANTS(:,18);
CONSTANTS(:,26) = CONSTANTS(:,12).*power(CONSTANTS(:,21)./CONSTANTS(:,7), CONSTANTS(:,13)).*power(CONSTANTS(:,25)./CONSTANTS(:,8), - CONSTANTS(:,9)).*power(CONSTANTS(:,23)./CONSTANTS(:,11), - CONSTANTS(:,10));
CONSTANTS(:,27) = ( CONSTANTS(:,26).*( CONSTANTS(:,3).*CONSTANTS(:,23).*(1.00000+CONSTANTS(:,4)./CONSTANTS(:,25))+(1.00000+CONSTANTS(:,25)./CONSTANTS(:,6))))./( CONSTANTS(:,1).*CONSTANTS(:,23).*(1.00000+CONSTANTS(:,2)./CONSTANTS(:,25))+(1.00000+CONSTANTS(:,25)./CONSTANTS(:,5)));
CONSTANTS(:,28) = ( CONSTANTS(:,1).*(1.00000+CONSTANTS(:,2)./CONSTANTS(:,25))+ CONSTANTS(:,3).*CONSTANTS(:,26).*(1.00000+CONSTANTS(:,4)./CONSTANTS(:,25)).*CONSTANTS(:,21))./(1.00000+CONSTANTS(:,25)./CONSTANTS(:,5)+ CONSTANTS(:,26).*(1.00000+CONSTANTS(:,25)./CONSTANTS(:,6)).*CONSTANTS(:,21));
CONSTANTS(:,29) = ( CONSTANTS(:,27).*CONSTANTS(:,21))./(1.00000+ CONSTANTS(:,27).*CONSTANTS(:,21));
CONSTANTS(:,30) = ( CONSTANTS(:,28).*CONSTANTS(:,23))./(1.00000+ CONSTANTS(:,28).*CONSTANTS(:,23));
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 = 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
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
