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 =16;
end
% There are a total of 4 entries in each of the rate and state variable arrays.
% There are a total of 32 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 (ms)');
    LEGEND_CONSTANTS(:,1) = strpad('epi in component environment (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('endo in component environment (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('mcell in component environment (dimensionless)');
    LEGEND_STATES(:,1) = strpad('u in component membrane (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('Vm in component membrane (mV)');
    LEGEND_CONSTANTS(:,4) = strpad('V_0 in component membrane (mV)');
    LEGEND_CONSTANTS(:,5) = strpad('V_fi in component membrane (mV)');
    LEGEND_ALGEBRAIC(:,9) = strpad('J_fi in component fast_inward_current (per_ms)');
    LEGEND_ALGEBRAIC(:,15) = strpad('J_so in component slow_outward_current (per_ms)');
    LEGEND_ALGEBRAIC(:,16) = strpad('J_si in component slow_inward_current (per_ms)');
    LEGEND_ALGEBRAIC(:,2) = strpad('J_stim in component membrane (per_ms)');
    LEGEND_ALGEBRAIC(:,3) = strpad('m in component m (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('u_m in component m (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('p in component p (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('u_p in component p (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('q in component q (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('u_q in component q (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('r in component r (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('u_r in component r (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('tau_fi in component fast_inward_current (ms)');
    LEGEND_CONSTANTS(:,18) = strpad('u_u in component fast_inward_current (dimensionless)');
    LEGEND_STATES(:,2) = strpad('v in component fast_inward_current_v_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,7) = strpad('v_inf in component fast_inward_current_v_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('tau_v_minus in component fast_inward_current_v_gate (ms)');
    LEGEND_CONSTANTS(:,31) = strpad('tau_v1_minus in component fast_inward_current_v_gate (ms)');
    LEGEND_CONSTANTS(:,13) = strpad('tau_v2_minus in component fast_inward_current_v_gate (ms)');
    LEGEND_CONSTANTS(:,8) = strpad('tau_v_plus in component fast_inward_current_v_gate (ms)');
    LEGEND_ALGEBRAIC(:,12) = strpad('tau_o in component slow_outward_current (ms)');
    LEGEND_CONSTANTS(:,15) = strpad('tau_o1 in component slow_outward_current (ms)');
    LEGEND_CONSTANTS(:,32) = strpad('tau_o2 in component slow_outward_current (ms)');
    LEGEND_ALGEBRAIC(:,14) = strpad('tau_so in component slow_outward_current (ms)');
    LEGEND_CONSTANTS(:,19) = strpad('tau_so1 in component slow_outward_current (ms)');
    LEGEND_CONSTANTS(:,20) = strpad('tau_so2 in component slow_outward_current (ms)');
    LEGEND_CONSTANTS(:,21) = strpad('k_so in component slow_outward_current (dimensionless)');
    LEGEND_CONSTANTS(:,22) = strpad('u_so in component slow_outward_current (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('tau_si in component slow_inward_current (ms)');
    LEGEND_STATES(:,3) = strpad('w in component slow_inward_current_w_gate (dimensionless)');
    LEGEND_STATES(:,4) = strpad('s in component slow_inward_current_s_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,11) = strpad('w_inf in component slow_inward_current_w_gate (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('tau_winf in component slow_inward_current_w_gate (ms)');
    LEGEND_CONSTANTS(:,25) = strpad('wstar_inf in component slow_inward_current_w_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,13) = strpad('tau_w_minus in component slow_inward_current_w_gate (ms)');
    LEGEND_CONSTANTS(:,26) = strpad('tau_w1_minus in component slow_inward_current_w_gate (ms)');
    LEGEND_CONSTANTS(:,27) = strpad('tau_w2_minus in component slow_inward_current_w_gate (ms)');
    LEGEND_CONSTANTS(:,28) = strpad('k_w_minus in component slow_inward_current_w_gate (dimensionless)');
    LEGEND_CONSTANTS(:,29) = strpad('u_w_minus in component slow_inward_current_w_gate (dimensionless)');
    LEGEND_CONSTANTS(:,30) = strpad('tau_w_plus in component slow_inward_current_w_gate (ms)');
    LEGEND_ALGEBRAIC(:,8) = strpad('tau_s in component slow_inward_current_s_gate (ms)');
    LEGEND_CONSTANTS(:,9) = strpad('tau_s1 in component slow_inward_current_s_gate (ms)');
    LEGEND_CONSTANTS(:,12) = strpad('tau_s2 in component slow_inward_current_s_gate (ms)');
    LEGEND_CONSTANTS(:,10) = strpad('k_s in component slow_inward_current_s_gate (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('u_s in component slow_inward_current_s_gate (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt u in component membrane (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt v in component fast_inward_current_v_gate (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt w in component slow_inward_current_w_gate (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt s in component slow_inward_current_s_gate (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 = [];
    CONSTANTS(:,1) = 1;
    CONSTANTS(:,2) = 0;
    CONSTANTS(:,3) = 0;
    STATES(:,1) = 0;
    CONSTANTS(:,4) = -83;
    CONSTANTS(:,5) = 2.7;
    CONSTANTS(:,6) = 0.3;
    CONSTANTS(:,7) = 0.13;
    STATES(:,2) = 1;
    CONSTANTS(:,8) = 1.45;
    STATES(:,3) = 1;
    STATES(:,4) = 0;
    CONSTANTS(:,9) = 2.7342;
    CONSTANTS(:,10) = 2.0994;
    CONSTANTS(:,11) = 0.9087;
    CONSTANTS(:,12) = piecewise({CONSTANTS(:,1)==1.00000, 16.0000 , CONSTANTS(:,2)==1.00000, 2.00000 }, 4.00000);
    CONSTANTS(:,13) = piecewise({CONSTANTS(:,1)==1.00000, 1150.00 , CONSTANTS(:,2)==1.00000, 10.0000 }, 1.45000);
    CONSTANTS(:,14) = piecewise({CONSTANTS(:,1)==1.00000, 0.00600000 , CONSTANTS(:,2)==1.00000, 0.0240000 }, 0.100000);
    CONSTANTS(:,15) = piecewise({CONSTANTS(:,1)==1.00000, 400.000 , CONSTANTS(:,2)==1.00000, 470.000 }, 410.000);
    CONSTANTS(:,16) = piecewise({CONSTANTS(:,1)==1.00000, 0.00600000 , CONSTANTS(:,2)==1.00000, 0.00600000 }, 0.00500000);
    CONSTANTS(:,17) = piecewise({CONSTANTS(:,1)==1.00000, 0.110000 , CONSTANTS(:,2)==1.00000, 0.104000 }, 0.0780000);
    CONSTANTS(:,18) = piecewise({CONSTANTS(:,1)==1.00000, 1.55000 , CONSTANTS(:,2)==1.00000, 1.56000 }, 1.61000);
    CONSTANTS(:,19) = piecewise({CONSTANTS(:,1)==1.00000, 30.0200 , CONSTANTS(:,2)==1.00000, 40.0000 }, 91.0000);
    CONSTANTS(:,20) = piecewise({CONSTANTS(:,1)==1.00000, 0.996000 , CONSTANTS(:,2)==1.00000, 1.20000 }, 0.800000);
    CONSTANTS(:,21) = piecewise({CONSTANTS(:,1)==1.00000, 2.04600 , CONSTANTS(:,2)==1.00000, 2.00000 }, 2.10000);
    CONSTANTS(:,22) = piecewise({CONSTANTS(:,1)==1.00000, 0.650000 , CONSTANTS(:,2)==1.00000, 0.650000 }, 0.600000);
    CONSTANTS(:,23) = piecewise({CONSTANTS(:,1)==1.00000, 1.88750 , CONSTANTS(:,2)==1.00000, 2.90130 }, 3.38490);
    CONSTANTS(:,24) = piecewise({CONSTANTS(:,1)==1.00000, 0.0700000 , CONSTANTS(:,2)==1.00000, 0.0273000 }, 0.0100000);
    CONSTANTS(:,25) = piecewise({CONSTANTS(:,1)==1.00000, 0.940000 , CONSTANTS(:,2)==1.00000, 0.780000 }, 0.500000);
    CONSTANTS(:,26) = piecewise({CONSTANTS(:,1)==1.00000, 60.0000 , CONSTANTS(:,2)==1.00000, 6.00000 }, 70.0000);
    CONSTANTS(:,27) = piecewise({CONSTANTS(:,1)==1.00000, 15.0000 , CONSTANTS(:,2)==1.00000, 140.000 }, 8.00000);
    CONSTANTS(:,28) = piecewise({CONSTANTS(:,1)==1.00000, 65.0000 , CONSTANTS(:,2)==1.00000, 200.000 }, 200.000);
    CONSTANTS(:,29) = piecewise({CONSTANTS(:,1)==1.00000, 0.0300000 , CONSTANTS(:,2)==1.00000, 0.0160000 }, 0.0160000);
    CONSTANTS(:,30) = piecewise({CONSTANTS(:,1)==1.00000, 200.000 , CONSTANTS(:,2)==1.00000, 280.000 }, 280.000);
    CONSTANTS(:,31) = piecewise({CONSTANTS(:,1)==1.00000, 60.0000 , CONSTANTS(:,2)==1.00000, 75.0000 }, 80.0000);
    CONSTANTS(:,32) = piecewise({CONSTANTS(:,1)==1.00000, 6.00000 , CONSTANTS(:,2)==1.00000, 6.00000 }, 7.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
    ALGEBRAIC(:,5) = piecewise({STATES(:,1)<CONSTANTS(:,7), 0.00000 }, 1.00000);
    ALGEBRAIC(:,8) =  (1.00000 - ALGEBRAIC(:,5)).*CONSTANTS(:,9)+ ALGEBRAIC(:,5).*CONSTANTS(:,12);
    RATES(:,4) = ((1.00000+ tanh( CONSTANTS(:,10).*(STATES(:,1) - CONSTANTS(:,11))))./2.00000 - STATES(:,4))./ALGEBRAIC(:,8);
    ALGEBRAIC(:,3) = piecewise({STATES(:,1)<CONSTANTS(:,6), 0.00000 }, 1.00000);
    ALGEBRAIC(:,7) = piecewise({STATES(:,1)<CONSTANTS(:,14), 1.00000 }, 0.00000);
    ALGEBRAIC(:,4) = piecewise({STATES(:,1)<CONSTANTS(:,14), 0.00000 }, 1.00000);
    ALGEBRAIC(:,10) =  ALGEBRAIC(:,4).*CONSTANTS(:,13)+ (1.00000 - ALGEBRAIC(:,4)).*CONSTANTS(:,31);
    RATES(:,2) = ( (1.00000 - ALGEBRAIC(:,3)).*(ALGEBRAIC(:,7) - STATES(:,2)))./ALGEBRAIC(:,10) - ( ALGEBRAIC(:,3).*STATES(:,2))./CONSTANTS(:,8);
    ALGEBRAIC(:,6) = piecewise({STATES(:,1)<CONSTANTS(:,16), 0.00000 }, 1.00000);
    ALGEBRAIC(:,11) =  (1.00000 - ALGEBRAIC(:,6)).*(1.00000 - ( STATES(:,1).*1.00000)./CONSTANTS(:,24))+ ALGEBRAIC(:,6).*CONSTANTS(:,25);
    ALGEBRAIC(:,13) = CONSTANTS(:,26)+( (CONSTANTS(:,27) - CONSTANTS(:,26)).*(1.00000+ tanh( CONSTANTS(:,28).*(STATES(:,1) - CONSTANTS(:,29)))))./2.00000;
    RATES(:,3) = ( (1.00000 - ALGEBRAIC(:,6)).*(ALGEBRAIC(:,11) - STATES(:,3)))./ALGEBRAIC(:,13) - ( ALGEBRAIC(:,6).*STATES(:,3))./CONSTANTS(:,30);
    ALGEBRAIC(:,9) = (  - ALGEBRAIC(:,3).*STATES(:,2).*(STATES(:,1) - CONSTANTS(:,6)).*(CONSTANTS(:,18) - STATES(:,1)))./CONSTANTS(:,17);
    ALGEBRAIC(:,12) =  (1.00000 - ALGEBRAIC(:,6)).*CONSTANTS(:,15)+ ALGEBRAIC(:,6).*CONSTANTS(:,32);
    ALGEBRAIC(:,14) = CONSTANTS(:,19)+( (CONSTANTS(:,20) - CONSTANTS(:,19)).*(1.00000+ tanh( CONSTANTS(:,21).*(STATES(:,1) - CONSTANTS(:,22)))))./2.00000;
    ALGEBRAIC(:,15) = ( STATES(:,1).*(1.00000 - ALGEBRAIC(:,5)))./ALGEBRAIC(:,12)+ALGEBRAIC(:,5)./ALGEBRAIC(:,14);
    ALGEBRAIC(:,16) = (  - ALGEBRAIC(:,5).*STATES(:,3).*STATES(:,4))./CONSTANTS(:,23);
    ALGEBRAIC(:,2) = piecewise({VOI>=100.000&VOI<=101.000,  - 1.00000 }, 0.00000);
    RATES(:,1) =  - (ALGEBRAIC(:,9)+ALGEBRAIC(:,15)+ALGEBRAIC(:,16)+ALGEBRAIC(:,2));
   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(:,5) = piecewise({STATES(:,1)<CONSTANTS(:,7), 0.00000 }, 1.00000);
    ALGEBRAIC(:,8) =  (1.00000 - ALGEBRAIC(:,5)).*CONSTANTS(:,9)+ ALGEBRAIC(:,5).*CONSTANTS(:,12);
    ALGEBRAIC(:,3) = piecewise({STATES(:,1)<CONSTANTS(:,6), 0.00000 }, 1.00000);
    ALGEBRAIC(:,7) = piecewise({STATES(:,1)<CONSTANTS(:,14), 1.00000 }, 0.00000);
    ALGEBRAIC(:,4) = piecewise({STATES(:,1)<CONSTANTS(:,14), 0.00000 }, 1.00000);
    ALGEBRAIC(:,10) =  ALGEBRAIC(:,4).*CONSTANTS(:,13)+ (1.00000 - ALGEBRAIC(:,4)).*CONSTANTS(:,31);
    ALGEBRAIC(:,6) = piecewise({STATES(:,1)<CONSTANTS(:,16), 0.00000 }, 1.00000);
    ALGEBRAIC(:,11) =  (1.00000 - ALGEBRAIC(:,6)).*(1.00000 - ( STATES(:,1).*1.00000)./CONSTANTS(:,24))+ ALGEBRAIC(:,6).*CONSTANTS(:,25);
    ALGEBRAIC(:,13) = CONSTANTS(:,26)+( (CONSTANTS(:,27) - CONSTANTS(:,26)).*(1.00000+ tanh( CONSTANTS(:,28).*(STATES(:,1) - CONSTANTS(:,29)))))./2.00000;
    ALGEBRAIC(:,9) = (  - ALGEBRAIC(:,3).*STATES(:,2).*(STATES(:,1) - CONSTANTS(:,6)).*(CONSTANTS(:,18) - STATES(:,1)))./CONSTANTS(:,17);
    ALGEBRAIC(:,12) =  (1.00000 - ALGEBRAIC(:,6)).*CONSTANTS(:,15)+ ALGEBRAIC(:,6).*CONSTANTS(:,32);
    ALGEBRAIC(:,14) = CONSTANTS(:,19)+( (CONSTANTS(:,20) - CONSTANTS(:,19)).*(1.00000+ tanh( CONSTANTS(:,21).*(STATES(:,1) - CONSTANTS(:,22)))))./2.00000;
    ALGEBRAIC(:,15) = ( STATES(:,1).*(1.00000 - ALGEBRAIC(:,5)))./ALGEBRAIC(:,12)+ALGEBRAIC(:,5)./ALGEBRAIC(:,14);
    ALGEBRAIC(:,16) = (  - ALGEBRAIC(:,5).*STATES(:,3).*STATES(:,4))./CONSTANTS(:,23);
    ALGEBRAIC(:,2) = piecewise({VOI>=100.000&VOI<=101.000,  - 1.00000 }, 0.00000);
    ALGEBRAIC(:,1) = CONSTANTS(:,4)+ STATES(:,1).*(CONSTANTS(:,5) - CONSTANTS(:,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