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 =13;
end
% There are a total of 2 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 (millisecond)');
    LEGEND_ALGEBRAIC(:,11) = strpad('P_CE in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('P_PE in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,7) = strpad('P_SE in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('Ca in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,13) = strpad('C_2 in component equations (per_millisec)');
    LEGEND_ALGEBRAIC(:,2) = strpad('phi_A_1 in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,12) = strpad('pi_n in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('A in component equations (dimensionless)');
    LEGEND_STATES(:,1) = strpad('A_1 in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('S in component equations (um)');
    LEGEND_ALGEBRAIC(:,10) = strpad('n in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('n_1 in component equations (dimensionless)');
    LEGEND_STATES(:,2) = strpad('n_2 in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,28) = strpad('G_V in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,27) = strpad('q_V in component equations (per_millisec)');
    LEGEND_CONSTANTS(:,26) = strpad('V in component equations (um_per_millisec)');
    LEGEND_CONSTANTS(:,29) = strpad('F_V in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,30) = strpad('p_V in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,1) = strpad('alpha_1 in component equations (per_um)');
    LEGEND_CONSTANTS(:,2) = strpad('alpha_2 in component equations (per_um)');
    LEGEND_CONSTANTS(:,3) = strpad('beta_1 in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('beta_2 in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('lambda in component equations (per_um)');
    LEGEND_CONSTANTS(:,6) = strpad('V_max in component equations (um_per_millisec)');
    LEGEND_CONSTANTS(:,7) = strpad('Ca_m in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('t_d in component equations (millisecond)');
    LEGEND_CONSTANTS(:,9) = strpad('a_c in component equations (per_millisec2)');
    LEGEND_CONSTANTS(:,10) = strpad('b_c in component equations (per_millisec2)');
    LEGEND_CONSTANTS(:,11) = strpad('C_1 in component equations (per_millisec)');
    LEGEND_CONSTANTS(:,12) = strpad('C_20 in component equations (per_millisec)');
    LEGEND_CONSTANTS(:,13) = strpad('q_k in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,25) = strpad('V_1 in component equations (um_per_millisec)');
    LEGEND_CONSTANTS(:,14) = strpad('a in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('m_0 in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('g_1 in component equations (per_um)');
    LEGEND_CONSTANTS(:,17) = strpad('g_2 in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('pi_min in component equations (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('S_0 in component equations (um)');
    LEGEND_CONSTANTS(:,20) = strpad('q_1 in component equations (per_millisec)');
    LEGEND_CONSTANTS(:,21) = strpad('q_2 in component equations (per_millisec)');
    LEGEND_CONSTANTS(:,22) = strpad('q_3 in component equations (per_millisec)');
    LEGEND_CONSTANTS(:,23) = strpad('TnC in component equations (dimensionless)');
    LEGEND_ALGEBRAIC(:,3) = strpad('l_1 in component user_defined (um)');
    LEGEND_ALGEBRAIC(:,5) = strpad('l_2 in component user_defined (um)');
    LEGEND_CONSTANTS(:,24) = strpad('dl_1_dt in component user_defined (um_per_millisec)');
    LEGEND_RATES(:,1) = strpad('d/dt A_1 in component equations (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt n_2 in component equations (dimensionless)');
    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;
    STATES(:,2) = 0;
    CONSTANTS(:,1) = 14.6;
    CONSTANTS(:,2) = 14.6;
    CONSTANTS(:,3) = 1;
    CONSTANTS(:,4) = 0.0012;
    CONSTANTS(:,5) = 30;
    CONSTANTS(:,6) = 0.0043;
    CONSTANTS(:,7) = 45e-3;
    CONSTANTS(:,8) = 170;
    CONSTANTS(:,9) = 2.4e-4;
    CONSTANTS(:,10) = 5e-4;
    CONSTANTS(:,11) = 2.9e-2;
    CONSTANTS(:,12) = 0.2;
    CONSTANTS(:,13) = 4;
    CONSTANTS(:,14) = 0.25;
    CONSTANTS(:,15) = 0.87;
    CONSTANTS(:,16) = 0.4;
    CONSTANTS(:,17) = 0.6;
    CONSTANTS(:,18) = 5e-2;
    CONSTANTS(:,19) = 0.77;
    CONSTANTS(:,20) = 0.017;
    CONSTANTS(:,21) = 0.26;
    CONSTANTS(:,22) = 0.03;
    CONSTANTS(:,23) = 1;
    CONSTANTS(:,24) = 0.00000;
    CONSTANTS(:,25) =  0.100000.*CONSTANTS(:,6);
    CONSTANTS(:,26) =  - CONSTANTS(:,24);
    CONSTANTS(:,27) = piecewise({CONSTANTS(:,26)<=0.00000, CONSTANTS(:,20) - ( CONSTANTS(:,21).*CONSTANTS(:,26))./CONSTANTS(:,6) }, CONSTANTS(:,22));
    CONSTANTS(:,28) = 1.00000+( 0.600000.*CONSTANTS(:,26))./CONSTANTS(:,6);
    CONSTANTS(:,29) = ( CONSTANTS(:,14).*(1.00000+CONSTANTS(:,26)./CONSTANTS(:,6)))./(CONSTANTS(:,14) - CONSTANTS(:,26)./CONSTANTS(:,6));
    CONSTANTS(:,30) = CONSTANTS(:,29)./CONSTANTS(:,28);
    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(:,2) =  CONSTANTS(:,27).*( CONSTANTS(:,15).*CONSTANTS(:,28) - STATES(:,2));
    ALGEBRAIC(:,1) = piecewise({VOI<=CONSTANTS(:,8),  CONSTANTS(:,7).*power(1.00000 - exp(  - CONSTANTS(:,9).*power(VOI, 2.00000)), 2.00000) },  CONSTANTS(:,7).*power( (1.00000 - exp(  - CONSTANTS(:,9).*power(VOI, 2.00000))).*exp(  - CONSTANTS(:,10).*power(VOI - CONSTANTS(:,8), 2.00000)), 2.00000));
    ALGEBRAIC(:,2) = exp(  - CONSTANTS(:,13).*STATES(:,1));
    ALGEBRAIC(:,3) = piecewise({200.000<=VOI&VOI<=201.000, 0.00000 }, 0.00000);
    ALGEBRAIC(:,8) = piecewise({ CONSTANTS(:,16).*ALGEBRAIC(:,3)+CONSTANTS(:,17)<0.00000, 0.00000 , 0.00000<= CONSTANTS(:,16).*ALGEBRAIC(:,3)+CONSTANTS(:,17)& CONSTANTS(:,16).*ALGEBRAIC(:,3)+CONSTANTS(:,17)<=1.00000,  CONSTANTS(:,16).*ALGEBRAIC(:,3)+CONSTANTS(:,17) }, 1.00000);
    ALGEBRAIC(:,10) =  ALGEBRAIC(:,8).*STATES(:,2);
    ALGEBRAIC(:,12) = piecewise({0.750000<=ALGEBRAIC(:,10)&ALGEBRAIC(:,10)<=1.00000, CONSTANTS(:,18) , 0.250000<=ALGEBRAIC(:,10)&ALGEBRAIC(:,10)<0.750000, power(CONSTANTS(:,18),  2.00000.*ALGEBRAIC(:,10) - 0.500000) }, 1.00000);
    RATES(:,1) =  CONSTANTS(:,11).*ALGEBRAIC(:,1).*(CONSTANTS(:,23) - STATES(:,1)) -  CONSTANTS(:,12).*ALGEBRAIC(:,12).*ALGEBRAIC(:,2).*STATES(:,1);
   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(:,8),  CONSTANTS(:,7).*power(1.00000 - exp(  - CONSTANTS(:,9).*power(VOI, 2.00000)), 2.00000) },  CONSTANTS(:,7).*power( (1.00000 - exp(  - CONSTANTS(:,9).*power(VOI, 2.00000))).*exp(  - CONSTANTS(:,10).*power(VOI - CONSTANTS(:,8), 2.00000)), 2.00000));
    ALGEBRAIC(:,2) = exp(  - CONSTANTS(:,13).*STATES(:,1));
    ALGEBRAIC(:,3) = piecewise({200.000<=VOI&VOI<=201.000, 0.00000 }, 0.00000);
    ALGEBRAIC(:,8) = piecewise({ CONSTANTS(:,16).*ALGEBRAIC(:,3)+CONSTANTS(:,17)<0.00000, 0.00000 , 0.00000<= CONSTANTS(:,16).*ALGEBRAIC(:,3)+CONSTANTS(:,17)& CONSTANTS(:,16).*ALGEBRAIC(:,3)+CONSTANTS(:,17)<=1.00000,  CONSTANTS(:,16).*ALGEBRAIC(:,3)+CONSTANTS(:,17) }, 1.00000);
    ALGEBRAIC(:,10) =  ALGEBRAIC(:,8).*STATES(:,2);
    ALGEBRAIC(:,12) = piecewise({0.750000<=ALGEBRAIC(:,10)&ALGEBRAIC(:,10)<=1.00000, CONSTANTS(:,18) , 0.250000<=ALGEBRAIC(:,10)&ALGEBRAIC(:,10)<0.750000, power(CONSTANTS(:,18),  2.00000.*ALGEBRAIC(:,10) - 0.500000) }, 1.00000);
    ALGEBRAIC(:,4) =  0.500000.*ALGEBRAIC(:,3)+CONSTANTS(:,19);
    ALGEBRAIC(:,5) = ALGEBRAIC(:,3)+1.87000;
    ALGEBRAIC(:,6) =  CONSTANTS(:,4).*(exp( CONSTANTS(:,2).*ALGEBRAIC(:,5)) - 1.00000);
    ALGEBRAIC(:,7) =  CONSTANTS(:,3).*(exp( CONSTANTS(:,1).*(ALGEBRAIC(:,5) - ALGEBRAIC(:,3))) - 1.00000);
    ALGEBRAIC(:,9) = ( STATES(:,1).*ALGEBRAIC(:,4))./1.00000;
    ALGEBRAIC(:,11) =  CONSTANTS(:,5).*ALGEBRAIC(:,4).*STATES(:,1).*ALGEBRAIC(:,10).*CONSTANTS(:,30);
    ALGEBRAIC(:,13) =  CONSTANTS(:,12).*ALGEBRAIC(:,12).*ALGEBRAIC(:,2);
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