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 10 entries in each of the rate and state variable arrays.
% There are a total of 40 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_CONSTANTS(:,1) = strpad('v_sP in component nucleus (nanomolar_hour)');
    LEGEND_CONSTANTS(:,2) = strpad('v_mP in component nucleus (nanomolar_hour)');
    LEGEND_CONSTANTS(:,3) = strpad('K_IP in component nucleus (nanomolar)');
    LEGEND_CONSTANTS(:,4) = strpad('K_mP in component nucleus (nanomolar)');
    LEGEND_CONSTANTS(:,5) = strpad('v_sT in component nucleus (nanomolar_hour)');
    LEGEND_CONSTANTS(:,6) = strpad('v_mT in component nucleus (nanomolar_hour)');
    LEGEND_CONSTANTS(:,7) = strpad('K_IT in component nucleus (nanomolar)');
    LEGEND_CONSTANTS(:,8) = strpad('K_mT in component nucleus (nanomolar)');
    LEGEND_CONSTANTS(:,9) = strpad('k_d in component cytosol (per_hour)');
    LEGEND_CONSTANTS(:,10) = strpad('n in component nucleus (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('k_1 in component cytosol (per_hour)');
    LEGEND_CONSTANTS(:,12) = strpad('k_2 in component cytosol (per_hour)');
    LEGEND_CONSTANTS(:,13) = strpad('k_dN in component nucleus (per_hour)');
    LEGEND_STATES(:,1) = strpad('C in component cytosol (nanomolar)');
    LEGEND_STATES(:,2) = strpad('M_P in component nucleus (nanomolar)');
    LEGEND_STATES(:,3) = strpad('M_T in component nucleus (nanomolar)');
    LEGEND_STATES(:,4) = strpad('C_N in component nucleus (nanomolar)');
    LEGEND_CONSTANTS(:,14) = strpad('k_3 in component cytosol (per_nanomolar_hour)');
    LEGEND_CONSTANTS(:,15) = strpad('k_4 in component cytosol (per_hour)');
    LEGEND_CONSTANTS(:,16) = strpad('k_dC in component cytosol (per_hour)');
    LEGEND_STATES(:,5) = strpad('P_0 in component PER (nanomolar)');
    LEGEND_STATES(:,6) = strpad('P_1 in component PER (nanomolar)');
    LEGEND_STATES(:,7) = strpad('P_2 in component PER (nanomolar)');
    LEGEND_STATES(:,8) = strpad('T_0 in component TIM (nanomolar)');
    LEGEND_STATES(:,9) = strpad('T_1 in component TIM (nanomolar)');
    LEGEND_STATES(:,10) = strpad('T_2 in component TIM (nanomolar)');
    LEGEND_CONSTANTS(:,17) = strpad('V_1P in component PER (nanomolar_hour)');
    LEGEND_CONSTANTS(:,18) = strpad('V_2P in component PER (nanomolar_hour)');
    LEGEND_CONSTANTS(:,19) = strpad('V_3P in component PER (nanomolar_hour)');
    LEGEND_CONSTANTS(:,20) = strpad('V_4P in component PER (nanomolar_hour)');
    LEGEND_CONSTANTS(:,21) = strpad('K_1P in component PER (nanomolar)');
    LEGEND_CONSTANTS(:,22) = strpad('K_2P in component PER (nanomolar)');
    LEGEND_CONSTANTS(:,23) = strpad('K_3P in component PER (nanomolar)');
    LEGEND_CONSTANTS(:,24) = strpad('K_4P in component PER (nanomolar)');
    LEGEND_CONSTANTS(:,25) = strpad('K_dP in component PER (nanomolar)');
    LEGEND_CONSTANTS(:,26) = strpad('v_dP in component PER (nanomolar_hour)');
    LEGEND_CONSTANTS(:,27) = strpad('k_sP in component PER (per_hour)');
    LEGEND_CONSTANTS(:,28) = strpad('V_1T in component TIM (nanomolar_hour)');
    LEGEND_CONSTANTS(:,29) = strpad('V_2T in component TIM (nanomolar_hour)');
    LEGEND_CONSTANTS(:,30) = strpad('V_3T in component TIM (nanomolar_hour)');
    LEGEND_CONSTANTS(:,31) = strpad('V_4T in component TIM (nanomolar_hour)');
    LEGEND_CONSTANTS(:,32) = strpad('K_1T in component TIM (nanomolar)');
    LEGEND_CONSTANTS(:,33) = strpad('K_2T in component TIM (nanomolar)');
    LEGEND_CONSTANTS(:,34) = strpad('K_3T in component TIM (nanomolar)');
    LEGEND_CONSTANTS(:,35) = strpad('K_4T in component TIM (nanomolar)');
    LEGEND_CONSTANTS(:,36) = strpad('K_dT in component TIM (nanomolar)');
    LEGEND_ALGEBRAIC(:,1) = strpad('v_dT in component LD_cycle (nanomolar_hour)');
    LEGEND_CONSTANTS(:,37) = strpad('k_sT in component TIM (per_hour)');
    LEGEND_CONSTANTS(:,38) = strpad('PI in component LD_cycle (dimensionless)');
    LEGEND_CONSTANTS(:,39) = strpad('v_dT_dark in component LD_cycle (nanomolar_hour)');
    LEGEND_CONSTANTS(:,40) = strpad('v_dT_light in component LD_cycle (nanomolar_hour)');
    LEGEND_ALGEBRAIC(:,2) = strpad('P_t in component PER_total (nanomolar)');
    LEGEND_ALGEBRAIC(:,3) = strpad('T_t in component TIM_total (nanomolar)');
    LEGEND_RATES(:,2) = strpad('d/dt M_P in component nucleus (nanomolar)');
    LEGEND_RATES(:,3) = strpad('d/dt M_T in component nucleus (nanomolar)');
    LEGEND_RATES(:,4) = strpad('d/dt C_N in component nucleus (nanomolar)');
    LEGEND_RATES(:,1) = strpad('d/dt C in component cytosol (nanomolar)');
    LEGEND_RATES(:,5) = strpad('d/dt P_0 in component PER (nanomolar)');
    LEGEND_RATES(:,6) = strpad('d/dt P_1 in component PER (nanomolar)');
    LEGEND_RATES(:,7) = strpad('d/dt P_2 in component PER (nanomolar)');
    LEGEND_RATES(:,8) = strpad('d/dt T_0 in component TIM (nanomolar)');
    LEGEND_RATES(:,9) = strpad('d/dt T_1 in component TIM (nanomolar)');
    LEGEND_RATES(:,10) = strpad('d/dt T_2 in component TIM (nanomolar)');
    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) = 0.8;
    CONSTANTS(:,2) = 0.8;
    CONSTANTS(:,3) = 1;
    CONSTANTS(:,4) = 0.2;
    CONSTANTS(:,5) = 1;
    CONSTANTS(:,6) = 0.7;
    CONSTANTS(:,7) = 1;
    CONSTANTS(:,8) = 0.2;
    CONSTANTS(:,9) = 0.01;
    CONSTANTS(:,10) = 4;
    CONSTANTS(:,11) = 1.2;
    CONSTANTS(:,12) = 0.2;
    CONSTANTS(:,13) = 0.01;
    STATES(:,1) = 0.1790;
    STATES(:,2) = 0.09107;
    STATES(:,3) = 1.427;
    STATES(:,4) = 1.203;
    CONSTANTS(:,14) = 1.2;
    CONSTANTS(:,15) = 0.6;
    CONSTANTS(:,16) = 0.01;
    STATES(:,5) = 0.02324;
    STATES(:,6) = 0.02210;
    STATES(:,7) = 0.01251;
    STATES(:,8) = 0.5420;
    STATES(:,9) = 0.8000;
    STATES(:,10) = 4.733;
    CONSTANTS(:,17) = 8;
    CONSTANTS(:,18) = 1;
    CONSTANTS(:,19) = 8;
    CONSTANTS(:,20) = 1;
    CONSTANTS(:,21) = 2;
    CONSTANTS(:,22) = 2;
    CONSTANTS(:,23) = 2;
    CONSTANTS(:,24) = 2;
    CONSTANTS(:,25) = 0.2;
    CONSTANTS(:,26) = 2;
    CONSTANTS(:,27) = 0.9;
    CONSTANTS(:,28) = 8;
    CONSTANTS(:,29) = 1;
    CONSTANTS(:,30) = 8;
    CONSTANTS(:,31) = 1;
    CONSTANTS(:,32) = 2;
    CONSTANTS(:,33) = 2;
    CONSTANTS(:,34) = 2;
    CONSTANTS(:,35) = 2;
    CONSTANTS(:,36) = 0.2;
    CONSTANTS(:,37) = 0.9;
    CONSTANTS(:,38) = 3.141592653589793;
    CONSTANTS(:,39) = 2;
    CONSTANTS(:,40) = 4;
    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);
    else
        statesRowCount = statesSize(1);
        ALGEBRAIC = zeros(statesRowCount, algebraicVariableCount);
        RATES = zeros(statesRowCount, statesColumnCount);
    end
    RATES(:,2) =  CONSTANTS(:,1).*CONSTANTS(:,3) .^ CONSTANTS(:,10)./(CONSTANTS(:,3) .^ CONSTANTS(:,10)+STATES(:,4) .^ CONSTANTS(:,10)) -  CONSTANTS(:,2).*STATES(:,2)./(CONSTANTS(:,4)+STATES(:,2)) -  CONSTANTS(:,9).*STATES(:,2);
    RATES(:,3) =  CONSTANTS(:,5).*CONSTANTS(:,7) .^ CONSTANTS(:,10)./(CONSTANTS(:,7) .^ CONSTANTS(:,10)+STATES(:,4) .^ CONSTANTS(:,10)) -  CONSTANTS(:,6).*STATES(:,3)./(CONSTANTS(:,8)+STATES(:,3)) -  CONSTANTS(:,9).*STATES(:,3);
    RATES(:,4) =  CONSTANTS(:,11).*STATES(:,1) -  CONSTANTS(:,12).*STATES(:,4) -  CONSTANTS(:,13).*STATES(:,4);
    RATES(:,1) =  CONSTANTS(:,14).*STATES(:,7).*STATES(:,10) -  CONSTANTS(:,15).*STATES(:,1) -  CONSTANTS(:,11).*STATES(:,1)+ CONSTANTS(:,12).*STATES(:,4) -  CONSTANTS(:,16).*STATES(:,1);
    RATES(:,5) =  CONSTANTS(:,27).*STATES(:,2) -  CONSTANTS(:,17).*STATES(:,5)./(CONSTANTS(:,21)+STATES(:,5))+ CONSTANTS(:,18).*STATES(:,6)./(CONSTANTS(:,22)+STATES(:,6)) -  CONSTANTS(:,9).*STATES(:,5);
    RATES(:,6) =  CONSTANTS(:,17).*STATES(:,5)./(CONSTANTS(:,21)+STATES(:,5)) -  CONSTANTS(:,18).*STATES(:,6)./(CONSTANTS(:,22)+STATES(:,6)) -  CONSTANTS(:,19).*STATES(:,6)./(CONSTANTS(:,23)+STATES(:,6))+ CONSTANTS(:,20).*STATES(:,7)./(CONSTANTS(:,24)+STATES(:,7)) -  CONSTANTS(:,9).*STATES(:,6);
    RATES(:,7) =  CONSTANTS(:,19).*STATES(:,6)./(CONSTANTS(:,23)+STATES(:,6)) -  CONSTANTS(:,20).*STATES(:,7)./(CONSTANTS(:,24)+STATES(:,7)) -  CONSTANTS(:,14).*STATES(:,7).*STATES(:,10)+ CONSTANTS(:,15).*STATES(:,1) -  CONSTANTS(:,26).*STATES(:,7)./(CONSTANTS(:,25)+STATES(:,7)) -  CONSTANTS(:,9).*STATES(:,7);
    RATES(:,8) =  CONSTANTS(:,37).*STATES(:,3) -  CONSTANTS(:,28).*STATES(:,8)./(CONSTANTS(:,32)+STATES(:,8))+ CONSTANTS(:,29).*STATES(:,9)./(CONSTANTS(:,33)+STATES(:,9)) -  CONSTANTS(:,9).*STATES(:,8);
    RATES(:,9) =  CONSTANTS(:,28).*STATES(:,8)./(CONSTANTS(:,32)+STATES(:,8)) -  CONSTANTS(:,29).*STATES(:,9)./(CONSTANTS(:,33)+STATES(:,9)) -  CONSTANTS(:,30).*STATES(:,9)./(CONSTANTS(:,34)+STATES(:,9))+ CONSTANTS(:,31).*STATES(:,10)./(CONSTANTS(:,35)+STATES(:,10)) -  CONSTANTS(:,9).*STATES(:,9);
    ALGEBRAIC(:,1) = piecewise({ sin( CONSTANTS(:,38).*VOI./12.0000)<=0.00000, CONSTANTS(:,39) }, CONSTANTS(:,40));
    RATES(:,10) =  CONSTANTS(:,30).*STATES(:,9)./(CONSTANTS(:,34)+STATES(:,9)) -  CONSTANTS(:,31).*STATES(:,10)./(CONSTANTS(:,35)+STATES(:,10)) -  CONSTANTS(:,14).*STATES(:,7).*STATES(:,10)+ CONSTANTS(:,15).*STATES(:,1) -  ALGEBRAIC(:,1).*STATES(:,10)./(CONSTANTS(:,36)+STATES(:,10)) -  CONSTANTS(:,9).*STATES(:,10);
   RATES = RATES';
end

% Calculate algebraic variables
function ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI)
    ALGEBRAIC(:,1) = piecewise({ sin( CONSTANTS(:,38).*VOI./12.0000)<=0.00000, CONSTANTS(:,39) }, CONSTANTS(:,40));
    ALGEBRAIC(:,2) = STATES(:,5)+STATES(:,6)+STATES(:,7)+STATES(:,1)+STATES(:,4);
    ALGEBRAIC(:,3) = STATES(:,8)+STATES(:,9)+STATES(:,10)+STATES(:,1)+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