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 =3;
end
% There are a total of 13 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_VOI = strpad('time in component environment (minute)');
    LEGEND_STATES(:,1) = strpad('cAMP in component cAMP (nmol_g)');
    LEGEND_CONSTANTS(:,1) = strpad('Ko in component cAMP (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('Ka in component cAMP (per_nanomolar)');
    LEGEND_CONSTANTS(:,3) = strpad('Kb in component cAMP (per_nanomolar)');
    LEGEND_CONSTANTS(:,4) = strpad('Kdsm in component cAMP (nmol_g)');
    LEGEND_CONSTANTS(:,5) = strpad('Vmsm in component cAMP (flux)');
    LEGEND_CONSTANTS(:,6) = strpad('Vdsm in component cAMP (flux)');
    LEGEND_ALGEBRAIC(:,1) = strpad('ACTH in component cAMP (picomolar)');
    LEGEND_ALGEBRAIC(:,2) = strpad('IS in component IS (nmol_g)');
    LEGEND_CONSTANTS(:,7) = strpad('Crpt in component IS (nmol_g)');
    LEGEND_CONSTANTS(:,8) = strpad('K in component IS (dimensionless)');
    LEGEND_CONSTANTS(:,9) = strpad('Kd in component IS (nmol_g)');
    LEGEND_CONSTANTS(:,10) = strpad('n in component IS (dimensionless)');
    LEGEND_STATES(:,2) = strpad('V in component V (flux)');
    LEGEND_CONSTANTS(:,11) = strpad('P in component V (minute_2)');
    LEGEND_CONSTANTS(:,12) = strpad('Q in component V (first_order_rate_constant)');
    LEGEND_STATES(:,3) = strpad('W in component W (flux)');
    LEGEND_CONSTANTS(:,13) = strpad('T in component W (nmol2_min2_g2)');
    LEGEND_CONSTANTS(:,14) = strpad('U in component W (first_order_rate_constant)');
    LEGEND_STATES(:,4) = strpad('CHOC in component CHOC (nmol_g)');
    LEGEND_CONSTANTS(:,15) = strpad('Lmtr in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,5) = strpad('Kmtr in component Kmtr (first_order_rate_constant)');
    LEGEND_STATES(:,6) = strpad('CHOM in component CHOM (nmol_g)');
    LEGEND_CONSTANTS(:,16) = strpad('Kbac in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,7) = strpad('Kfor in component Kfor (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('Kcb in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,18) = strpad('Kcf in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,8) = strpad('CHON in component CHON (nmol_g)');
    LEGEND_STATES(:,9) = strpad('CHOL in component CHOL (nmol_g)');
    LEGEND_CONSTANTS(:,19) = strpad('C in component Kmtr (g_nmol_min2)');
    LEGEND_CONSTANTS(:,20) = strpad('D in component Kmtr (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,21) = strpad('R in component Kfor (g_nmol_min2)');
    LEGEND_CONSTANTS(:,22) = strpad('S in component Kfor (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,23) = strpad('Vm in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,24) = strpad('Km in component model_parameters (nmol_g)');
    LEGEND_STATES(:,10) = strpad('PREG in component PREG (nmol_g)');
    LEGEND_CONSTANTS(:,25) = strpad('Vmptr in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,26) = strpad('Kmptr in component model_parameters (nmol_g)');
    LEGEND_STATES(:,11) = strpad('PRO in component PRO (nmol_g)');
    LEGEND_CONSTANTS(:,27) = strpad('HA in component PRO (dimensionless)');
    LEGEND_CONSTANTS(:,28) = strpad('AH in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,12) = strpad('HYPR in component HYPR (nmol_g)');
    LEGEND_CONSTANTS(:,29) = strpad('HY in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,13) = strpad('CORT in component CORT (nmol_g)');
    LEGEND_CONSTANTS(:,30) = strpad('LH in component CORT (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,3) = strpad('LH_CORT in component CORT (flux)');
    LEGEND_RATES(:,1) = strpad('d/dt cAMP in component cAMP (nmol_g)');
    LEGEND_RATES(:,2) = strpad('d/dt V in component V (flux)');
    LEGEND_RATES(:,3) = strpad('d/dt W in component W (flux)');
    LEGEND_RATES(:,4) = strpad('d/dt CHOC in component CHOC (nmol_g)');
    LEGEND_RATES(:,6) = strpad('d/dt CHOM in component CHOM (nmol_g)');
    LEGEND_RATES(:,9) = strpad('d/dt CHOL in component CHOL (nmol_g)');
    LEGEND_RATES(:,5) = strpad('d/dt Kmtr in component Kmtr (first_order_rate_constant)');
    LEGEND_RATES(:,7) = strpad('d/dt Kfor in component Kfor (first_order_rate_constant)');
    LEGEND_RATES(:,8) = strpad('d/dt CHON in component CHON (nmol_g)');
    LEGEND_RATES(:,10) = strpad('d/dt PREG in component PREG (nmol_g)');
    LEGEND_RATES(:,11) = strpad('d/dt PRO in component PRO (nmol_g)');
    LEGEND_RATES(:,12) = strpad('d/dt HYPR in component HYPR (nmol_g)');
    LEGEND_RATES(:,13) = strpad('d/dt CORT in component CORT (nmol_g)');
    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.95;
    CONSTANTS(:,1) = 0.013;
    CONSTANTS(:,2) = 0.01;
    CONSTANTS(:,3) = 1000.0;
    CONSTANTS(:,4) = 10.0;
    CONSTANTS(:,5) = 6.0;
    CONSTANTS(:,6) = 10.0;
    CONSTANTS(:,7) = 3.0;
    CONSTANTS(:,8) = 80.0;
    CONSTANTS(:,9) = 2.11;
    CONSTANTS(:,10) = 4.0;
    STATES(:,2) = 11.3;
    CONSTANTS(:,11) = 0.052;
    CONSTANTS(:,12) = 0.042;
    STATES(:,3) = 10.0;
    CONSTANTS(:,13) = 8.0;
    CONSTANTS(:,14) = 0.0015;
    STATES(:,4) = 532.0;
    CONSTANTS(:,15) = 1.65;
    STATES(:,5) = 0.446;
    STATES(:,6) = 11.3;
    CONSTANTS(:,16) = 10.0;
    STATES(:,7) = 0.370;
    CONSTANTS(:,17) = 0.01;
    CONSTANTS(:,18) = 0.00033;
    STATES(:,8) = 3.03;
    STATES(:,9) = 3000.0;
    CONSTANTS(:,19) = 6.25;
    CONSTANTS(:,20) = 125.0;
    CONSTANTS(:,21) = 3.0;
    CONSTANTS(:,22) = 76.0;
    CONSTANTS(:,23) = 1890.0;
    CONSTANTS(:,24) = 270.0;
    STATES(:,10) = 6.56;
    CONSTANTS(:,25) = 500.0;
    CONSTANTS(:,26) = 150.0;
    STATES(:,11) = 0.64;
    CONSTANTS(:,27) = 0.5;
    CONSTANTS(:,28) = 16.4;
    STATES(:,12) = 0.64;
    CONSTANTS(:,29) = 16.4;
    STATES(:,13) = 5.2;
    CONSTANTS(:,30) = 0.724;
    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(:,3) =  CONSTANTS(:,13).*power(STATES(:,4), -1.00000) -  CONSTANTS(:,14).*STATES(:,3);
    RATES(:,4) = (STATES(:,2)+STATES(:,3)+ CONSTANTS(:,15).*STATES(:,6)) -  STATES(:,5).*STATES(:,4);
    RATES(:,6) = ( STATES(:,5).*STATES(:,4)+ CONSTANTS(:,16).*STATES(:,8)+ CONSTANTS(:,18).*STATES(:,9)) - ( CONSTANTS(:,15).*STATES(:,6)+ CONSTANTS(:,17).*STATES(:,6)+ STATES(:,7).*STATES(:,6));
    RATES(:,9) =  CONSTANTS(:,17).*STATES(:,6) -  CONSTANTS(:,18).*STATES(:,9);
    RATES(:,8) =  STATES(:,7).*STATES(:,6) - ( CONSTANTS(:,16).*STATES(:,8)+( CONSTANTS(:,23).*STATES(:,8))./(CONSTANTS(:,24)+STATES(:,8)));
    RATES(:,10) = ( CONSTANTS(:,23).*STATES(:,8))./(CONSTANTS(:,24)+STATES(:,8)) - ( CONSTANTS(:,25).*STATES(:,10))./(CONSTANTS(:,26)+STATES(:,10));
    RATES(:,11) =  CONSTANTS(:,27).*(( CONSTANTS(:,25).*STATES(:,10))./(CONSTANTS(:,26)+STATES(:,10))) -  CONSTANTS(:,28).*STATES(:,11);
    RATES(:,12) =  CONSTANTS(:,28).*STATES(:,11) -  CONSTANTS(:,29).*STATES(:,12);
    RATES(:,13) =  CONSTANTS(:,29).*STATES(:,12) -  CONSTANTS(:,30).*STATES(:,13);
    ALGEBRAIC(:,1) = piecewise({VOI>=0.00000&VOI<10.0000, 12.0000 , VOI>=10.0000&VOI<20.0000, 16.0000 , VOI>=20.0000&VOI<35.0000, 12.0000 , VOI>=35.0000&VOI<45.0000, 16.0000 }, 12.0000);
    RATES(:,1) = ( CONSTANTS(:,5).*CONSTANTS(:,1).*(1.00000+ CONSTANTS(:,3).*ALGEBRAIC(:,1)))./((1.00000+ CONSTANTS(:,2).*ALGEBRAIC(:,1))+ CONSTANTS(:,1).*(1.00000+ CONSTANTS(:,3).*ALGEBRAIC(:,1))) - ( CONSTANTS(:,6).*STATES(:,1))./(CONSTANTS(:,4)+STATES(:,1));
    ALGEBRAIC(:,2) = ( CONSTANTS(:,8).*CONSTANTS(:,7).*power(STATES(:,1), CONSTANTS(:,10)))./(power(CONSTANTS(:,9), CONSTANTS(:,10))+power(STATES(:,1), CONSTANTS(:,10)));
    RATES(:,2) =  CONSTANTS(:,11).*ALGEBRAIC(:,2) -  CONSTANTS(:,12).*STATES(:,2);
    RATES(:,5) =  CONSTANTS(:,19).*ALGEBRAIC(:,2) -  CONSTANTS(:,20).*STATES(:,5);
    RATES(:,7) =  CONSTANTS(:,21).*ALGEBRAIC(:,2) -  CONSTANTS(:,22).*STATES(:,7);
   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(:,1) = piecewise({VOI>=0.00000&VOI<10.0000, 12.0000 , VOI>=10.0000&VOI<20.0000, 16.0000 , VOI>=20.0000&VOI<35.0000, 12.0000 , VOI>=35.0000&VOI<45.0000, 16.0000 }, 12.0000);
    ALGEBRAIC(:,2) = ( CONSTANTS(:,8).*CONSTANTS(:,7).*power(STATES(:,1), CONSTANTS(:,10)))./(power(CONSTANTS(:,9), CONSTANTS(:,10))+power(STATES(:,1), CONSTANTS(:,10)));
    ALGEBRAIC(:,3) =  CONSTANTS(:,30).*STATES(:,13);
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
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