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 =5;
end
% There are a total of 4 entries in each of the rate and state variable arrays.
% There are a total of 26 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 (second)');
    LEGEND_STATES(:,1) = strpad('R_des in component R_des (micromolar)');
    LEGEND_CONSTANTS(:,25) = strpad('K_plus in component model_parameters (per_micromolar_per_second)');
    LEGEND_STATES(:,2) = strpad('C_cyto in component C_cyto (micromolar)');
    LEGEND_CONSTANTS(:,1) = strpad('n_i in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('K_act in component model_parameters (micromolar)');
    LEGEND_CONSTANTS(:,3) = strpad('n_a in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('K_minus in component model_parameters (per_second)');
    LEGEND_CONSTANTS(:,5) = strpad('K_1 in component model_parameters (per_second)');
    LEGEND_CONSTANTS(:,6) = strpad('b in component model_parameters (per_second)');
    LEGEND_ALGEBRAIC(:,3) = strpad('I_ra in component I_ra (micromolar)');
    LEGEND_CONSTANTS(:,7) = strpad('Ca_tot in component model_parameters (micromolar)');
    LEGEND_CONSTANTS(:,8) = strpad('alpha in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,9) = strpad('V_MP in component model_parameters (micromolar_per_second)');
    LEGEND_CONSTANTS(:,10) = strpad('n_p in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('K_p in component model_parameters (micromolar)');
    LEGEND_ALGEBRAIC(:,1) = strpad('I_rable in component I_rable (micromolar)');
    LEGEND_STATES(:,3) = strpad('IP3 in component IP3 (micromolar)');
    LEGEND_CONSTANTS(:,12) = strpad('K_IP in component model_parameters (micromolar)');
    LEGEND_CONSTANTS(:,26) = strpad('V_PLC in component V_PLC (micromolar)');
    LEGEND_ALGEBRAIC(:,2) = strpad('V_3K in component V_3K (micromolar)');
    LEGEND_ALGEBRAIC(:,4) = strpad('V_5P in component V_5P (micromolar)');
    LEGEND_CONSTANTS(:,13) = strpad('V_plc in component model_parameters (micromolar_per_second)');
    LEGEND_CONSTANTS(:,14) = strpad('gamma in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('V_k in component model_parameters (micromolar_per_second)');
    LEGEND_CONSTANTS(:,16) = strpad('K_k in component model_parameters (micromolar)');
    LEGEND_CONSTANTS(:,17) = strpad('n_d in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('K_d in component model_parameters (micromolar)');
    LEGEND_STATES(:,4) = strpad('IP4 in component IP4 (micromolar)');
    LEGEND_CONSTANTS(:,19) = strpad('V_p1 in component model_parameters (micromolar_per_second)');
    LEGEND_CONSTANTS(:,20) = strpad('K_p1 in component model_parameters (micromolar)');
    LEGEND_CONSTANTS(:,21) = strpad('K_p2 in component model_parameters (micromolar)');
    LEGEND_ALGEBRAIC(:,5) = strpad('V_15P in component V_15P (micromolar)');
    LEGEND_CONSTANTS(:,22) = strpad('k in component model_parameters (per_second)');
    LEGEND_CONSTANTS(:,23) = strpad('V_p2 in component model_parameters (micromolar_per_second)');
    LEGEND_CONSTANTS(:,24) = strpad('K_inh in component model_parameters (micromolar)');
    LEGEND_RATES(:,1) = strpad('d/dt R_des in component R_des (micromolar)');
    LEGEND_RATES(:,2) = strpad('d/dt C_cyto in component C_cyto (micromolar)');
    LEGEND_RATES(:,3) = strpad('d/dt IP3 in component IP3 (micromolar)');
    LEGEND_RATES(:,4) = strpad('d/dt IP4 in component IP4 (micromolar)');
    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.1;
    STATES(:,2) = 0.1;
    CONSTANTS(:,1) = 4;
    CONSTANTS(:,2) = 0.56;
    CONSTANTS(:,3) = 3;
    CONSTANTS(:,4) = 0.5;
    CONSTANTS(:,5) = 2.57;
    CONSTANTS(:,6) = 7e-4;
    CONSTANTS(:,7) = 80;
    CONSTANTS(:,8) = 0.1;
    CONSTANTS(:,9) = 4;
    CONSTANTS(:,10) = 2;
    CONSTANTS(:,11) = 0.35;
    STATES(:,3) = 0.1;
    CONSTANTS(:,12) = 1;
    CONSTANTS(:,13) = 1.3;
    CONSTANTS(:,14) = 0.2;
    CONSTANTS(:,15) = 0.5;
    CONSTANTS(:,16) = 1;
    CONSTANTS(:,17) = 2;
    CONSTANTS(:,18) = 0.3;
    STATES(:,4) = 0.1;
    CONSTANTS(:,19) = 5;
    CONSTANTS(:,20) = 10;
    CONSTANTS(:,21) = 2;
    CONSTANTS(:,22) = 0.01;
    CONSTANTS(:,23) = 0.2;
    CONSTANTS(:,24) = 0.15;
    CONSTANTS(:,25) = CONSTANTS(:,4)./power(CONSTANTS(:,24), CONSTANTS(:,1));
    CONSTANTS(:,26) =  CONSTANTS(:,14).*CONSTANTS(:,13).*1.00000;
    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(:,25).*power(STATES(:,2), CONSTANTS(:,1)).*1.00000.*( ((1.00000 - STATES(:,1))./(1.00000+power(STATES(:,2)./CONSTANTS(:,2), CONSTANTS(:,3)))).*1.00000) -  CONSTANTS(:,4).*STATES(:,1);
    ALGEBRAIC(:,1) = ( (1.00000 - STATES(:,1)).*STATES(:,3))./(CONSTANTS(:,12)+STATES(:,3));
    ALGEBRAIC(:,3) = ( ALGEBRAIC(:,1).*1.00000)./(1.00000+power(CONSTANTS(:,2)./STATES(:,2), CONSTANTS(:,3)));
    RATES(:,2) =  CONSTANTS(:,5).*1.00000.*( CONSTANTS(:,6).*1.00000+ ALGEBRAIC(:,3).*1.00000).*( (CONSTANTS(:,7) -  STATES(:,2).*(CONSTANTS(:,8)+1.00000)).*1.00000) -  CONSTANTS(:,9).*( (power(STATES(:,2), CONSTANTS(:,10))./(power(CONSTANTS(:,11), CONSTANTS(:,10))+power(STATES(:,2), CONSTANTS(:,10)))).*1.00000);
    ALGEBRAIC(:,2) =  CONSTANTS(:,15).*(STATES(:,3)./(CONSTANTS(:,16)+STATES(:,3))).*(power(STATES(:,2), CONSTANTS(:,17))./(power(CONSTANTS(:,18), CONSTANTS(:,17))+power(STATES(:,2), CONSTANTS(:,17)))).*1.00000;
    ALGEBRAIC(:,4) =  CONSTANTS(:,19).*1.00000.*(STATES(:,3)./( CONSTANTS(:,20).*(1.00000+STATES(:,4)./CONSTANTS(:,21))+STATES(:,3)));
    RATES(:,3) = ((CONSTANTS(:,26) - ALGEBRAIC(:,2)) - ALGEBRAIC(:,4))./1.00000;
    ALGEBRAIC(:,5) = ( CONSTANTS(:,23).*1.00000.*STATES(:,4))./( CONSTANTS(:,21).*(1.00000+STATES(:,3)./CONSTANTS(:,20))+STATES(:,4));
    RATES(:,4) =  (ALGEBRAIC(:,2) - ALGEBRAIC(:,5)).*1.00000 -  CONSTANTS(:,22).*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) = ( (1.00000 - STATES(:,1)).*STATES(:,3))./(CONSTANTS(:,12)+STATES(:,3));
    ALGEBRAIC(:,3) = ( ALGEBRAIC(:,1).*1.00000)./(1.00000+power(CONSTANTS(:,2)./STATES(:,2), CONSTANTS(:,3)));
    ALGEBRAIC(:,2) =  CONSTANTS(:,15).*(STATES(:,3)./(CONSTANTS(:,16)+STATES(:,3))).*(power(STATES(:,2), CONSTANTS(:,17))./(power(CONSTANTS(:,18), CONSTANTS(:,17))+power(STATES(:,2), CONSTANTS(:,17)))).*1.00000;
    ALGEBRAIC(:,4) =  CONSTANTS(:,19).*1.00000.*(STATES(:,3)./( CONSTANTS(:,20).*(1.00000+STATES(:,4)./CONSTANTS(:,21))+STATES(:,3)));
    ALGEBRAIC(:,5) = ( CONSTANTS(:,23).*1.00000.*STATES(:,4))./( CONSTANTS(:,21).*(1.00000+STATES(:,3)./CONSTANTS(:,20))+STATES(:,4));
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