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 =9;
end
% There are a total of 5 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 (millisecond)');
    LEGEND_CONSTANTS(:,1) = strpad('tau_c in component nucleotides (second)');
    LEGEND_CONSTANTS(:,2) = strpad('eta in component nucleotides (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('v in component nucleotides (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('k in component nucleotides (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('phi in component nucleotides (dimensionless)');
    LEGEND_STATES(:,1) = strpad('ADP in component nucleotides (dimensionless)');
    LEGEND_STATES(:,2) = strpad('ATP in component nucleotides (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('C_m in component membrane (femtofarad)');
    LEGEND_ALGEBRAIC(:,4) = strpad('I_Ca in component Ca_current (femtoampere)');
    LEGEND_ALGEBRAIC(:,5) = strpad('I_K in component K_current (femtoampere)');
    LEGEND_ALGEBRAIC(:,8) = strpad('I_KCa in component Ca_activated_K_current (femtoampere)');
    LEGEND_ALGEBRAIC(:,9) = strpad('I_KATP in component ATP_sensitive_K_current (femtoampere)');
    LEGEND_STATES(:,3) = strpad('V in component membrane (millivolt)');
    LEGEND_CONSTANTS(:,6) = strpad('g_Ca_ in component Ca_current (picosiemens)');
    LEGEND_CONSTANTS(:,7) = strpad('V_Ca in component Ca_current (millivolt)');
    LEGEND_CONSTANTS(:,8) = strpad('v_m in component Ca_current (millivolt)');
    LEGEND_CONSTANTS(:,9) = strpad('s_m in component Ca_current (millivolt)');
    LEGEND_ALGEBRAIC(:,2) = strpad('m_infinity in component Ca_current (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('g_K_ in component K_current (picosiemens)');
    LEGEND_CONSTANTS(:,11) = strpad('V_K in component K_current (millivolt)');
    LEGEND_STATES(:,4) = strpad('n in component K_channel_activation (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('g_KCa_ in component Ca_activated_K_current (picosiemens)');
    LEGEND_CONSTANTS(:,13) = strpad('k_D in component Ca_activated_K_current (micromolar)');
    LEGEND_STATES(:,5) = strpad('c in component cytosolic_Ca (micromolar)');
    LEGEND_ALGEBRAIC(:,7) = strpad('omega in component Ca_activated_K_current (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('g_KATP_ in component ATP_sensitive_K_current (picosiemens)');
    LEGEND_CONSTANTS(:,15) = strpad('tau_n in component K_channel_activation (millisecond)');
    LEGEND_CONSTANTS(:,16) = strpad('v_n in component K_channel_activation (millivolt)');
    LEGEND_CONSTANTS(:,17) = strpad('s_n in component K_channel_activation (millivolt)');
    LEGEND_ALGEBRAIC(:,3) = strpad('n_infinity in component K_channel_activation (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('J_mem in component Ca_influx (micromolar_per_ms)');
    LEGEND_CONSTANTS(:,18) = strpad('f in component Ca_influx (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('alpha in component Ca_influx (micromolar_per_fA_ms)');
    LEGEND_CONSTANTS(:,20) = strpad('k_c in component Ca_influx (per_millisecond)');
    LEGEND_RATES(:,2) = strpad('d/dt ATP in component nucleotides (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt ADP in component nucleotides (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt V in component membrane (millivolt)');
    LEGEND_RATES(:,4) = strpad('d/dt n in component K_channel_activation (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt c in component cytosolic_Ca (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 = [];
    CONSTANTS(:,1) = 1200;
    CONSTANTS(:,2) = 185;
    CONSTANTS(:,3) = 10;
    CONSTANTS(:,4) = 20;
    STATES(:,1) = 0.085817;
    STATES(:,2) = 2.1047;
    CONSTANTS(:,5) = 5300;
    STATES(:,3) = -67.018;
    CONSTANTS(:,6) = 1200;
    CONSTANTS(:,7) = 25;
    CONSTANTS(:,8) = -20;
    CONSTANTS(:,9) = 12;
    CONSTANTS(:,10) = 3000;
    CONSTANTS(:,11) = -75;
    STATES(:,4) = 0.00011;
    CONSTANTS(:,12) = 300;
    CONSTANTS(:,13) = 0.3;
    STATES(:,5) = 0.15666;
    CONSTANTS(:,14) = 350;
    CONSTANTS(:,15) = 16;
    CONSTANTS(:,16) = -16;
    CONSTANTS(:,17) = 5.6;
    CONSTANTS(:,18) = 0.001;
    CONSTANTS(:,19) = 0.00000225;
    CONSTANTS(:,20) = 0.1;
    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
    ALGEBRAIC(:,1) =  STATES(:,2).*power(1.00000+ CONSTANTS(:,4).*STATES(:,1), 2.00000);
    RATES(:,2) = (CONSTANTS(:,3) - ALGEBRAIC(:,1))./( 1000.00.*CONSTANTS(:,1));
    RATES(:,1) = (ALGEBRAIC(:,1) -  CONSTANTS(:,2).*STATES(:,1))./( 1000.00.*CONSTANTS(:,1));
    ALGEBRAIC(:,3) = 1.00000./(1.00000+exp((CONSTANTS(:,16) - STATES(:,3))./CONSTANTS(:,17)));
    RATES(:,4) = (ALGEBRAIC(:,3) - STATES(:,4))./CONSTANTS(:,15);
    ALGEBRAIC(:,2) = 1.00000./(1.00000+exp((CONSTANTS(:,8) - STATES(:,3))./CONSTANTS(:,9)));
    ALGEBRAIC(:,4) =  CONSTANTS(:,6).*ALGEBRAIC(:,2).*(STATES(:,3) - CONSTANTS(:,7));
    ALGEBRAIC(:,6) =   - CONSTANTS(:,18).*( CONSTANTS(:,19).*ALGEBRAIC(:,4)+ CONSTANTS(:,20).*STATES(:,5));
    RATES(:,5) = ALGEBRAIC(:,6);
    ALGEBRAIC(:,5) =  CONSTANTS(:,10).*STATES(:,4).*(STATES(:,3) - CONSTANTS(:,11));
    ALGEBRAIC(:,7) = 1.00000./(1.00000+CONSTANTS(:,13)./STATES(:,5));
    ALGEBRAIC(:,8) =  CONSTANTS(:,12).*ALGEBRAIC(:,7).*(STATES(:,3) - CONSTANTS(:,11));
    ALGEBRAIC(:,9) = ( (STATES(:,3) - CONSTANTS(:,11)).*CONSTANTS(:,14))./STATES(:,2);
    RATES(:,3) =  - (ALGEBRAIC(:,4)+ALGEBRAIC(:,5)+ALGEBRAIC(:,8)+ALGEBRAIC(:,9))./CONSTANTS(:,5);
   RATES = RATES';
end

% Calculate algebraic variables
function ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI)
    ALGEBRAIC(:,1) =  STATES(:,2).*power(1.00000+ CONSTANTS(:,4).*STATES(:,1), 2.00000);
    ALGEBRAIC(:,3) = 1.00000./(1.00000+exp((CONSTANTS(:,16) - STATES(:,3))./CONSTANTS(:,17)));
    ALGEBRAIC(:,2) = 1.00000./(1.00000+exp((CONSTANTS(:,8) - STATES(:,3))./CONSTANTS(:,9)));
    ALGEBRAIC(:,4) =  CONSTANTS(:,6).*ALGEBRAIC(:,2).*(STATES(:,3) - CONSTANTS(:,7));
    ALGEBRAIC(:,6) =   - CONSTANTS(:,18).*( CONSTANTS(:,19).*ALGEBRAIC(:,4)+ CONSTANTS(:,20).*STATES(:,5));
    ALGEBRAIC(:,5) =  CONSTANTS(:,10).*STATES(:,4).*(STATES(:,3) - CONSTANTS(:,11));
    ALGEBRAIC(:,7) = 1.00000./(1.00000+CONSTANTS(:,13)./STATES(:,5));
    ALGEBRAIC(:,8) =  CONSTANTS(:,12).*ALGEBRAIC(:,7).*(STATES(:,3) - CONSTANTS(:,11));
    ALGEBRAIC(:,9) = ( (STATES(:,3) - CONSTANTS(:,11)).*CONSTANTS(:,14))./STATES(:,2);
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