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 =2;
end
% There are a total of 5 entries in each of the rate and state variable arrays.
% There are a total of 35 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 (hour)');
    LEGEND_STATES(:,1) = strpad('GH in component GH (ng_ml)');
    LEGEND_CONSTANTS(:,1) = strpad('GHS in component GH (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('k1 in component GH (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,3) = strpad('kr1 in component GH (ng_ml_hr)');
    LEGEND_CONSTANTS(:,4) = strpad('t1 in component GH (pg_ml)');
    LEGEND_CONSTANTS(:,5) = strpad('n1 in component GH (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('n2 in component GH (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('g0 in component GH (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('ng0 in component GH (dimensionless)');
    LEGEND_CONSTANTS(:,9) = strpad('tg0 in component GH (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('t2 in component GH (pg_ml)');
    LEGEND_STATES(:,2) = strpad('SRIF_PeV in component SRIF_PeV (pg_ml)');
    LEGEND_CONSTANTS(:,34) = strpad('F1_GHS in component F (dimensionless)');
    LEGEND_STATES(:,3) = strpad('GHRH in component GHRH (pg_ml)');
    LEGEND_STATES(:,4) = strpad('ghr_GHRH in component ghr_GHRH (pg_ml)');
    LEGEND_CONSTANTS(:,11) = strpad('k4 in component SRIF_PeV (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('kr4 in component SRIF_PeV (pg_ml_hr)');
    LEGEND_CONSTANTS(:,13) = strpad('t5 in component SRIF_PeV (ng_ml)');
    LEGEND_CONSTANTS(:,14) = strpad('n5 in component SRIF_PeV (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('S_basal in component SRIF_PeV (pg_ml_hr)');
    LEGEND_STATES(:,5) = strpad('SRIF_ArC in component SRIF_ArC (pg_ml)');
    LEGEND_CONSTANTS(:,16) = strpad('k2 in component SRIF_ArC (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('kr2 in component SRIF_ArC (pg_ml_hr)');
    LEGEND_CONSTANTS(:,18) = strpad('t3 in component SRIF_ArC (pg_ml)');
    LEGEND_CONSTANTS(:,19) = strpad('n3 in component SRIF_ArC (dimensionless)');
    LEGEND_CONSTANTS(:,20) = strpad('k3 in component GHRH (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,21) = strpad('kr3 in component GHRH (pg_ml_hr)');
    LEGEND_CONSTANTS(:,22) = strpad('t4 in component GHRH (pg_ml)');
    LEGEND_CONSTANTS(:,23) = strpad('n4 in component GHRH (dimensionless)');
    LEGEND_CONSTANTS(:,35) = strpad('F2_GHS in component F (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('g1 in component F (dimensionless)');
    LEGEND_CONSTANTS(:,25) = strpad('g2 in component F (dimensionless)');
    LEGEND_CONSTANTS(:,26) = strpad('tg1 in component F (dimensionless)');
    LEGEND_CONSTANTS(:,27) = strpad('tg2 in component F (dimensionless)');
    LEGEND_CONSTANTS(:,28) = strpad('ng1 in component F (dimensionless)');
    LEGEND_CONSTANTS(:,29) = strpad('ng2 in component F (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('dghr_GHRH_dt in component ghr_GHRH (pg_ml_hr)');
    LEGEND_ALGEBRAIC(:,1) = strpad('inject in component ghr_GHRH (pg_ml_hr)');
    LEGEND_CONSTANTS(:,30) = strpad('kghr in component ghr_GHRH (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,31) = strpad('C in component ghr_GHRH (pg_ml_hr)');
    LEGEND_CONSTANTS(:,32) = strpad('onset in component ghr_GHRH (hour)');
    LEGEND_CONSTANTS(:,33) = strpad('duration in component ghr_GHRH (hour)');
    LEGEND_RATES(:,1) = strpad('d/dt GH in component GH (ng_ml)');
    LEGEND_RATES(:,2) = strpad('d/dt SRIF_PeV in component SRIF_PeV (pg_ml)');
    LEGEND_RATES(:,5) = strpad('d/dt SRIF_ArC in component SRIF_ArC (pg_ml)');
    LEGEND_RATES(:,3) = strpad('d/dt GHRH in component GHRH (pg_ml)');
    LEGEND_RATES(:,4) = strpad('d/dt ghr_GHRH in component ghr_GHRH (pg_ml)');
    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.0;
    CONSTANTS(:,1) = 20.0;
    CONSTANTS(:,2) = 3.0;
    CONSTANTS(:,3) = 600.0;
    CONSTANTS(:,4) = 357.0;
    CONSTANTS(:,5) = 5.0;
    CONSTANTS(:,6) = 2.0;
    CONSTANTS(:,7) = 2.0;
    CONSTANTS(:,8) = 2.9;
    CONSTANTS(:,9) = 200.0;
    CONSTANTS(:,10) = 10.0;
    STATES(:,2) = 0.0;
    STATES(:,3) = 0.0;
    STATES(:,4) = 0.0;
    CONSTANTS(:,11) = 25.0;
    CONSTANTS(:,12) = 20400.0;
    CONSTANTS(:,13) = 10.0;
    CONSTANTS(:,14) = 2.0;
    CONSTANTS(:,15) = 900.0;
    STATES(:,5) = 0.0;
    CONSTANTS(:,16) = 25.0;
    CONSTANTS(:,17) = 2200.0;
    CONSTANTS(:,18) = 400.0;
    CONSTANTS(:,19) = 2.0;
    CONSTANTS(:,20) = 40.0;
    CONSTANTS(:,21) = 63000.0;
    CONSTANTS(:,22) = 28.0;
    CONSTANTS(:,23) = 5.0;
    CONSTANTS(:,24) = 90000.0;
    CONSTANTS(:,25) = 100.0;
    CONSTANTS(:,26) = 390.0;
    CONSTANTS(:,27) = 10000.0;
    CONSTANTS(:,28) = 3.0;
    CONSTANTS(:,29) = 2.0;
    CONSTANTS(:,30) = 15.0;
    CONSTANTS(:,31) = 10000.0;
    CONSTANTS(:,32) = 2.0;
    CONSTANTS(:,33) = 0.2;
    CONSTANTS(:,34) =  CONSTANTS(:,24).*(power(CONSTANTS(:,1)./CONSTANTS(:,26), CONSTANTS(:,28))./(1.00000+power(CONSTANTS(:,1)./CONSTANTS(:,26), CONSTANTS(:,28))));
    CONSTANTS(:,35) =  CONSTANTS(:,25).*(power(CONSTANTS(:,1)./CONSTANTS(:,27), CONSTANTS(:,29))./(1.00000+power(CONSTANTS(:,1)./CONSTANTS(:,27), CONSTANTS(:,29))));
    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(:,3).*(power((STATES(:,3)+STATES(:,4))./CONSTANTS(:,4), CONSTANTS(:,5))./(power((STATES(:,3)+STATES(:,4))./CONSTANTS(:,4), CONSTANTS(:,5))+1.00000)+ CONSTANTS(:,7).*(power(CONSTANTS(:,1)./CONSTANTS(:,9), CONSTANTS(:,8))./(1.00000+power(CONSTANTS(:,1)./CONSTANTS(:,9), CONSTANTS(:,8))))+(1.00000+CONSTANTS(:,34))./(1.00000+power(STATES(:,2)./CONSTANTS(:,10), CONSTANTS(:,6))+CONSTANTS(:,34))) -  CONSTANTS(:,2).*STATES(:,1);
    RATES(:,2) =  - ( CONSTANTS(:,11).*STATES(:,2))+ CONSTANTS(:,12).*(power(STATES(:,1)./CONSTANTS(:,13), CONSTANTS(:,14))./(power(STATES(:,1)./CONSTANTS(:,13), CONSTANTS(:,14))+1.00000))+CONSTANTS(:,15);
    RATES(:,5) =  CONSTANTS(:,17).*(power((STATES(:,3)+STATES(:,4))./CONSTANTS(:,18), CONSTANTS(:,19))./(1.00000+power((STATES(:,3)+STATES(:,4))./CONSTANTS(:,18), CONSTANTS(:,19)))) -  CONSTANTS(:,16).*STATES(:,5);
    RATES(:,3) = ( CONSTANTS(:,21).*((1.00000+CONSTANTS(:,35))./(1.00000+power((STATES(:,2)+STATES(:,5))./CONSTANTS(:,22), CONSTANTS(:,23))+CONSTANTS(:,35)))+ STATES(:,4).*1.00000) -  CONSTANTS(:,20).*STATES(:,3);
    ALGEBRAIC(:,1) = piecewise({VOI<CONSTANTS(:,32), 0.00000 , VOI>=CONSTANTS(:,32)&VOI<=CONSTANTS(:,32)+CONSTANTS(:,33), CONSTANTS(:,31) , VOI>CONSTANTS(:,32)+CONSTANTS(:,33), 0.00000 }, NaN);
    RATES(:,4) = ALGEBRAIC(:,1) -  CONSTANTS(:,30).*STATES(:,4);
   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<CONSTANTS(:,32), 0.00000 , VOI>=CONSTANTS(:,32)&VOI<=CONSTANTS(:,32)+CONSTANTS(:,33), CONSTANTS(:,31) , VOI>CONSTANTS(:,32)+CONSTANTS(:,33), 0.00000 }, NaN);
    ALGEBRAIC(:,2) = ALGEBRAIC(:,1) -  CONSTANTS(:,30).*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
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