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 =12;
end
% There are a total of 4 entries in each of the rate and state variable arrays.
% There are a total of 20 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(:,7) = strpad('rel_LH_E2_P4_RP_LH in component RP_LH (microg_day)');
    LEGEND_STATES(:,1) = strpad('RP_LH in component RP_LH (microg)');
    LEGEND_ALGEBRAIC(:,11) = strpad('syn_LH_E2_P4 in component RP_LH (microg_day)');
    LEGEND_CONSTANTS(:,1) = strpad('V0_LH in component RP_LH (microg_day)');
    LEGEND_CONSTANTS(:,2) = strpad('V1_LH in component RP_LH (microg_day)');
    LEGEND_CONSTANTS(:,3) = strpad('h in component RP_LH (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('Km_LH in component RP_LH (ng_L)');
    LEGEND_CONSTANTS(:,5) = strpad('Ki_LHP in component RP_LH (nmol_L)');
    LEGEND_CONSTANTS(:,6) = strpad('kLH_rel in component RP_LH (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('CLH_P in component RP_LH (L_nmol)');
    LEGEND_CONSTANTS(:,8) = strpad('CLH_E in component RP_LH (L_ng)');
    LEGEND_ALGEBRAIC(:,4) = strpad('E2 in component E2 (ng_L)');
    LEGEND_ALGEBRAIC(:,5) = strpad('E2_dE in component E2_dE (ng_L)');
    LEGEND_ALGEBRAIC(:,6) = strpad('P4 in component P4 (nmol_L)');
    LEGEND_ALGEBRAIC(:,9) = strpad('P4_dP in component P4_dP (nmol_L)');
    LEGEND_STATES(:,2) = strpad('LH in component LH (microg_l)');
    LEGEND_CONSTANTS(:,9) = strpad('v_dit in component LH (litre)');
    LEGEND_ALGEBRAIC(:,1) = strpad('clear_LH in component LH (microg_l_day)');
    LEGEND_CONSTANTS(:,10) = strpad('kLH_cl in component LH (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,8) = strpad('rel_FSH_E2_P4_RP_FSH in component RP_FSH (microg_day)');
    LEGEND_STATES(:,3) = strpad('RP_FSH in component RP_FSH (microg)');
    LEGEND_ALGEBRAIC(:,12) = strpad('syn_FSH_Ih in component RP_FSH (microg_day)');
    LEGEND_CONSTANTS(:,11) = strpad('V_FSH in component RP_FSH (microg_day)');
    LEGEND_CONSTANTS(:,12) = strpad('Ki_FSH_Ih in component RP_FSH (U_L)');
    LEGEND_CONSTANTS(:,13) = strpad('kFSH_rel in component RP_FSH (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('CFSH_P in component RP_FSH (L_nmol)');
    LEGEND_CONSTANTS(:,15) = strpad('CFSH_E in component RP_FSH (L_ng2)');
    LEGEND_ALGEBRAIC(:,10) = strpad('Ih_dIh in component Ih_dIh (U_L)');
    LEGEND_STATES(:,4) = strpad('FSH in component FSH (microg_l)');
    LEGEND_CONSTANTS(:,16) = strpad('v_dis in component FSH (litre)');
    LEGEND_ALGEBRAIC(:,3) = strpad('clear_FSH in component FSH (microg_l_day)');
    LEGEND_CONSTANTS(:,17) = strpad('kFSH_cl in component FSH (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,18) = strpad('dE in component E2_dE (day)');
    LEGEND_CONSTANTS(:,19) = strpad('dP in component P4_dP (day)');
    LEGEND_ALGEBRAIC(:,2) = strpad('Ih in component Ih (U_L)');
    LEGEND_CONSTANTS(:,20) = strpad('dIh in component Ih_dIh (day)');
    LEGEND_RATES(:,1) = strpad('d/dt RP_LH in component RP_LH (microg)');
    LEGEND_RATES(:,2) = strpad('d/dt LH in component LH (microg_l)');
    LEGEND_RATES(:,3) = strpad('d/dt RP_FSH in component RP_FSH (microg)');
    LEGEND_RATES(:,4) = strpad('d/dt FSH in component FSH (microg_l)');
    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) = 467.0;
    CONSTANTS(:,1) = 1400.0;
    CONSTANTS(:,2) = 95900.0;
    CONSTANTS(:,3) = 8.0;
    CONSTANTS(:,4) = 360.0;
    CONSTANTS(:,5) = 26.0;
    CONSTANTS(:,6) = 3.0;
    CONSTANTS(:,7) = 0.024;
    CONSTANTS(:,8) = 0.008;
    STATES(:,2) = 40.0;
    CONSTANTS(:,9) = 2.5;
    CONSTANTS(:,10) = 14.0;
    STATES(:,3) = 0.0;
    CONSTANTS(:,11) = 4400.0;
    CONSTANTS(:,12) = 1176.5;
    CONSTANTS(:,13) = 45.0;
    CONSTANTS(:,14) = 3.0;
    CONSTANTS(:,15) = 0.005;
    STATES(:,4) = 150.0;
    CONSTANTS(:,16) = 2.5;
    CONSTANTS(:,17) = 8.21;
    CONSTANTS(:,18) = 0.42;
    CONSTANTS(:,19) = 2.9;
    CONSTANTS(:,20) = 2.0;
    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(:,4) = (300.000 - ( 240.000.*power(VOI+1.00000, 2.00000))./(3.00000+power(VOI+1.00000, 2.00000)))+ 90.0000.*exp( - (power(VOI - 8.00000, 2.00000)./10.0000));
    ALGEBRAIC(:,6) =  52.0000.*exp( - (power(VOI - 7.00000, 2.00000)./18.0000));
    ALGEBRAIC(:,7) = ( CONSTANTS(:,6).*(1.00000+ CONSTANTS(:,7).*ALGEBRAIC(:,6)).*STATES(:,1))./(1.00000+ CONSTANTS(:,8).*ALGEBRAIC(:,4));
    ALGEBRAIC(:,1) =  CONSTANTS(:,10).*STATES(:,2);
    RATES(:,2) = ALGEBRAIC(:,7)./CONSTANTS(:,9) - ALGEBRAIC(:,1);
    ALGEBRAIC(:,8) = ( CONSTANTS(:,13).*(1.00000+ CONSTANTS(:,14).*ALGEBRAIC(:,6)).*STATES(:,3))./(1.00000+ CONSTANTS(:,15).*power(ALGEBRAIC(:,4), 2.00000));
    ALGEBRAIC(:,3) =  CONSTANTS(:,17).*STATES(:,4);
    RATES(:,4) = ALGEBRAIC(:,8)./CONSTANTS(:,16) - ALGEBRAIC(:,3);
    ALGEBRAIC(:,5) = (300.000 - ( 240.000.*power((VOI+1.00000) - CONSTANTS(:,18), 2.00000))./(3.00000+power((VOI+1.00000) - CONSTANTS(:,18), 2.00000)))+ 90.0000.*exp( - (power(VOI - (CONSTANTS(:,18)+8.00000), 2.00000)./10.0000));
    ALGEBRAIC(:,9) =  52.0000.*exp( - (power(VOI - (CONSTANTS(:,19)+7.00000), 2.00000)./18.0000));
    ALGEBRAIC(:,11) = (CONSTANTS(:,1)+( CONSTANTS(:,2).*power(ALGEBRAIC(:,5), CONSTANTS(:,3)))./(power(CONSTANTS(:,4), CONSTANTS(:,3))+power(ALGEBRAIC(:,5), CONSTANTS(:,3))))./(1.00000+ALGEBRAIC(:,9)./CONSTANTS(:,5));
    RATES(:,1) = ALGEBRAIC(:,11) - ALGEBRAIC(:,7);
    ALGEBRAIC(:,10) = 300.000+ 1330.00.*exp( - (power(VOI - (7.00000+CONSTANTS(:,20)), 2.00000)./19.0000));
    ALGEBRAIC(:,12) = CONSTANTS(:,11)./(1.00000+ALGEBRAIC(:,10)./CONSTANTS(:,12));
    RATES(:,3) = ALGEBRAIC(:,12) - ALGEBRAIC(:,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(:,4) = (300.000 - ( 240.000.*power(VOI+1.00000, 2.00000))./(3.00000+power(VOI+1.00000, 2.00000)))+ 90.0000.*exp( - (power(VOI - 8.00000, 2.00000)./10.0000));
    ALGEBRAIC(:,6) =  52.0000.*exp( - (power(VOI - 7.00000, 2.00000)./18.0000));
    ALGEBRAIC(:,7) = ( CONSTANTS(:,6).*(1.00000+ CONSTANTS(:,7).*ALGEBRAIC(:,6)).*STATES(:,1))./(1.00000+ CONSTANTS(:,8).*ALGEBRAIC(:,4));
    ALGEBRAIC(:,1) =  CONSTANTS(:,10).*STATES(:,2);
    ALGEBRAIC(:,8) = ( CONSTANTS(:,13).*(1.00000+ CONSTANTS(:,14).*ALGEBRAIC(:,6)).*STATES(:,3))./(1.00000+ CONSTANTS(:,15).*power(ALGEBRAIC(:,4), 2.00000));
    ALGEBRAIC(:,3) =  CONSTANTS(:,17).*STATES(:,4);
    ALGEBRAIC(:,5) = (300.000 - ( 240.000.*power((VOI+1.00000) - CONSTANTS(:,18), 2.00000))./(3.00000+power((VOI+1.00000) - CONSTANTS(:,18), 2.00000)))+ 90.0000.*exp( - (power(VOI - (CONSTANTS(:,18)+8.00000), 2.00000)./10.0000));
    ALGEBRAIC(:,9) =  52.0000.*exp( - (power(VOI - (CONSTANTS(:,19)+7.00000), 2.00000)./18.0000));
    ALGEBRAIC(:,11) = (CONSTANTS(:,1)+( CONSTANTS(:,2).*power(ALGEBRAIC(:,5), CONSTANTS(:,3)))./(power(CONSTANTS(:,4), CONSTANTS(:,3))+power(ALGEBRAIC(:,5), CONSTANTS(:,3))))./(1.00000+ALGEBRAIC(:,9)./CONSTANTS(:,5));
    ALGEBRAIC(:,10) = 300.000+ 1330.00.*exp( - (power(VOI - (7.00000+CONSTANTS(:,20)), 2.00000)./19.0000));
    ALGEBRAIC(:,12) = CONSTANTS(:,11)./(1.00000+ALGEBRAIC(:,10)./CONSTANTS(:,12));
    ALGEBRAIC(:,2) = 300.000+ 1330.00.*exp( - (power(VOI - 7.00000, 2.00000)./19.0000));
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