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 =15;
end
% There are a total of 6 entries in each of the rate and state variable arrays.
% There are a total of 33 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('V in component membrane (millivolt)');
    LEGEND_CONSTANTS(:,1) = strpad('R in component membrane (millijoule_per_mole_kelvin)');
    LEGEND_CONSTANTS(:,2) = strpad('T in component membrane (kelvin)');
    LEGEND_CONSTANTS(:,3) = strpad('F in component membrane (coulomb_per_mole)');
    LEGEND_CONSTANTS(:,4) = strpad('Cm in component membrane (microF_per_cm2)');
    LEGEND_ALGEBRAIC(:,12) = strpad('i_K_dr in component delayed_rectifier_K_channel_current (nanoA_per_cm2)');
    LEGEND_ALGEBRAIC(:,13) = strpad('i_K_Ca in component Ca_sensitive_K_current (nanoA_per_cm2)');
    LEGEND_ALGEBRAIC(:,14) = strpad('i_K_ATP in component ATP_sensitive_K_current (nanoA_per_cm2)');
    LEGEND_ALGEBRAIC(:,5) = strpad('i_fast in component fast_current (nanoA_per_cm2)');
    LEGEND_ALGEBRAIC(:,10) = strpad('i_Ca in component calcium_current (nanoA_per_cm2)');
    LEGEND_ALGEBRAIC(:,11) = strpad('i_NS in component cationic_nonselective_inward_current (nanoA_per_cm2)');
    LEGEND_ALGEBRAIC(:,15) = strpad('i_NaL in component Na_leak_current (nanoA_per_cm2)');
    LEGEND_CONSTANTS(:,5) = strpad('g_fast in component fast_current (microS_per_cm2)');
    LEGEND_CONSTANTS(:,6) = strpad('V_fast in component fast_current (millivolt)');
    LEGEND_ALGEBRAIC(:,1) = strpad('m_infinity in component fast_current_m_gate (dimensionless)');
    LEGEND_STATES(:,2) = strpad('h in component fast_current_h_gate (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('Vm in component fast_current_m_gate (millivolt)');
    LEGEND_CONSTANTS(:,8) = strpad('Sm in component fast_current_m_gate (millivolt)');
    LEGEND_CONSTANTS(:,9) = strpad('lamda_h in component fast_current_h_gate (per_second)');
    LEGEND_ALGEBRAIC(:,6) = strpad('tau_h in component fast_current_h_gate (second)');
    LEGEND_ALGEBRAIC(:,2) = strpad('h_infinity in component fast_current_h_gate (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('Vh in component fast_current_h_gate (millivolt)');
    LEGEND_CONSTANTS(:,11) = strpad('Sh in component fast_current_h_gate (millivolt)');
    LEGEND_CONSTANTS(:,12) = strpad('K_Ca in component calcium_current (micromolar)');
    LEGEND_CONSTANTS(:,13) = strpad('P_Ca in component calcium_current (nanoA_per_micromolar_per_cm2)');
    LEGEND_CONSTANTS(:,14) = strpad('Ca_o in component calcium_current (micromolar)');
    LEGEND_STATES(:,3) = strpad('Ca_i in component cytosolic_calcium (micromolar)');
    LEGEND_ALGEBRAIC(:,9) = strpad('f_infinity in component calcium_current_f_gate (dimensionless)');
    LEGEND_STATES(:,4) = strpad('d in component calcium_current_d_gate (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('lamda_d in component calcium_current_d_gate (per_second)');
    LEGEND_ALGEBRAIC(:,7) = strpad('tau_d in component calcium_current_d_gate (second)');
    LEGEND_ALGEBRAIC(:,3) = strpad('d_infinity in component calcium_current_d_gate (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('Vd in component calcium_current_d_gate (millivolt)');
    LEGEND_CONSTANTS(:,17) = strpad('Sd in component calcium_current_d_gate (millivolt)');
    LEGEND_CONSTANTS(:,18) = strpad('g_NS in component cationic_nonselective_inward_current (microS_per_cm2)');
    LEGEND_CONSTANTS(:,19) = strpad('K_NS in component cationic_nonselective_inward_current (micromolar)');
    LEGEND_CONSTANTS(:,20) = strpad('VNS in component cationic_nonselective_inward_current (millivolt)');
    LEGEND_STATES(:,5) = strpad('Ca_lum in component cytosolic_calcium (micromolar)');
    LEGEND_CONSTANTS(:,21) = strpad('V_K in component delayed_rectifier_K_channel_current (millivolt)');
    LEGEND_CONSTANTS(:,22) = strpad('g_K_dr in component delayed_rectifier_K_channel_current (microS_per_cm2)');
    LEGEND_STATES(:,6) = strpad('n in component delayed_rectifier_K_channel_current_n_gate (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('lamda_n in component delayed_rectifier_K_channel_current_n_gate (per_second)');
    LEGEND_CONSTANTS(:,24) = strpad('Vn in component delayed_rectifier_K_channel_current_n_gate (millivolt)');
    LEGEND_CONSTANTS(:,25) = strpad('Sn in component delayed_rectifier_K_channel_current_n_gate (millivolt)');
    LEGEND_ALGEBRAIC(:,4) = strpad('n_infinity in component delayed_rectifier_K_channel_current_n_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('tau_n in component delayed_rectifier_K_channel_current_n_gate (second)');
    LEGEND_CONSTANTS(:,26) = strpad('g_K_Ca in component Ca_sensitive_K_current (microS_per_cm2)');
    LEGEND_CONSTANTS(:,27) = strpad('g_K_ATP in component ATP_sensitive_K_current (microS_per_cm2)');
    LEGEND_CONSTANTS(:,28) = strpad('g_NaL in component Na_leak_current (microS_per_cm2)');
    LEGEND_CONSTANTS(:,29) = strpad('V_Na in component Na_leak_current (millivolt)');
    LEGEND_CONSTANTS(:,30) = strpad('k_rel in component cytosolic_calcium (per_second)');
    LEGEND_CONSTANTS(:,31) = strpad('k_Ca in component cytosolic_calcium (per_second)');
    LEGEND_CONSTANTS(:,32) = strpad('k_pump in component cytosolic_calcium (per_second)');
    LEGEND_CONSTANTS(:,33) = strpad('omega in component cytosolic_calcium (micromolar_cm2_per_nanoA_per_second)');
    LEGEND_RATES(:,1) = strpad('d/dt V in component membrane (millivolt)');
    LEGEND_RATES(:,2) = strpad('d/dt h in component fast_current_h_gate (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt d in component calcium_current_d_gate (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt n in component delayed_rectifier_K_channel_current_n_gate (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt Ca_i in component cytosolic_calcium (micromolar)');
    LEGEND_RATES(:,5) = strpad('d/dt Ca_lum in component cytosolic_calcium (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) = -38.34146;
    CONSTANTS(:,1) = 8314;
    CONSTANTS(:,2) = 310;
    CONSTANTS(:,3) = 96485;
    CONSTANTS(:,4) = 1;
    CONSTANTS(:,5) = 600;
    CONSTANTS(:,6) = 80;
    STATES(:,2) = 0.214723;
    CONSTANTS(:,7) = -25;
    CONSTANTS(:,8) = 9;
    CONSTANTS(:,9) = 12.5;
    CONSTANTS(:,10) = -48;
    CONSTANTS(:,11) = -7;
    CONSTANTS(:,12) = 1;
    CONSTANTS(:,13) = 2;
    CONSTANTS(:,14) = 2500;
    STATES(:,3) = 0.6959466;
    STATES(:,4) = 0.0031711238;
    CONSTANTS(:,15) = 2.5;
    CONSTANTS(:,16) = -10;
    CONSTANTS(:,17) = 5;
    CONSTANTS(:,18) = 5;
    CONSTANTS(:,19) = 50;
    CONSTANTS(:,20) = -20;
    STATES(:,5) = 102.686;
    CONSTANTS(:,21) = -75;
    CONSTANTS(:,22) = 600;
    STATES(:,6) = 0.1836403;
    CONSTANTS(:,23) = 12.5;
    CONSTANTS(:,24) = -18;
    CONSTANTS(:,25) = 14;
    CONSTANTS(:,26) = 5;
    CONSTANTS(:,27) = 2;
    CONSTANTS(:,28) = 0.3;
    CONSTANTS(:,29) = 80;
    CONSTANTS(:,30) = 0.2;
    CONSTANTS(:,31) = 7;
    CONSTANTS(:,32) = 30;
    CONSTANTS(:,33) = 0.2;
    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(:,5) =   - CONSTANTS(:,30).*(STATES(:,5) - STATES(:,3))+ CONSTANTS(:,32).*STATES(:,3);
    ALGEBRAIC(:,6) = 1.00000./( CONSTANTS(:,9).*(exp((CONSTANTS(:,10) - STATES(:,1))./( 2.00000.*CONSTANTS(:,11)))+exp((STATES(:,1) - CONSTANTS(:,10))./( 2.00000.*CONSTANTS(:,11)))));
    ALGEBRAIC(:,2) = 1.00000./(1.00000+exp((CONSTANTS(:,10) - STATES(:,1))./CONSTANTS(:,11)));
    RATES(:,2) = (ALGEBRAIC(:,2) - STATES(:,2))./ALGEBRAIC(:,6);
    ALGEBRAIC(:,7) = 1.00000./( CONSTANTS(:,15).*(exp((CONSTANTS(:,16) - STATES(:,1))./( 2.00000.*CONSTANTS(:,17)))+exp((STATES(:,1) - CONSTANTS(:,16))./( 2.00000.*CONSTANTS(:,17)))));
    ALGEBRAIC(:,3) = 1.00000./(1.00000+exp((CONSTANTS(:,16) - STATES(:,1))./CONSTANTS(:,17)));
    RATES(:,4) = (ALGEBRAIC(:,3) - STATES(:,4))./ALGEBRAIC(:,7);
    ALGEBRAIC(:,4) = 1.00000./(1.00000+exp((CONSTANTS(:,24) - STATES(:,1))./CONSTANTS(:,25)));
    ALGEBRAIC(:,8) = 1.00000./( CONSTANTS(:,23).*(exp((CONSTANTS(:,24) - STATES(:,1))./( 2.00000.*CONSTANTS(:,25)))+exp((STATES(:,1) - CONSTANTS(:,24))./( 2.00000.*CONSTANTS(:,25)))));
    RATES(:,6) = (ALGEBRAIC(:,4) - STATES(:,6))./ALGEBRAIC(:,8);
    ALGEBRAIC(:,9) = CONSTANTS(:,12)./(CONSTANTS(:,12)+STATES(:,3));
    ALGEBRAIC(:,10) = ( (( CONSTANTS(:,13).*STATES(:,4).*ALGEBRAIC(:,9).*2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2))).*(CONSTANTS(:,14) -  STATES(:,3).*exp(( 2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2)))))./(1.00000 - exp(( 2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    RATES(:,3) =  CONSTANTS(:,30).*(STATES(:,5) - STATES(:,3)) - ( CONSTANTS(:,33).*ALGEBRAIC(:,10)+ CONSTANTS(:,31).*STATES(:,3)+ CONSTANTS(:,32).*STATES(:,3));
    ALGEBRAIC(:,12) =  CONSTANTS(:,22).*power(STATES(:,6), 4.00000).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,13) =  (( CONSTANTS(:,26).*power(STATES(:,3), 3.00000))./(power(CONSTANTS(:,12), 3.00000)+power(STATES(:,3), 3.00000))).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,14) =  CONSTANTS(:,27).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,1) = 1.00000./(1.00000+exp((CONSTANTS(:,7) - STATES(:,1))./CONSTANTS(:,8)));
    ALGEBRAIC(:,5) =  CONSTANTS(:,5).*power(ALGEBRAIC(:,1), 3.00000).*STATES(:,2).*(STATES(:,1) - CONSTANTS(:,6));
    ALGEBRAIC(:,11) =  (( CONSTANTS(:,18).*power(CONSTANTS(:,19), 2.00000))./(power(CONSTANTS(:,19), 2.00000)+power(STATES(:,5), 2.00000))).*((STATES(:,1) - CONSTANTS(:,20))./(1.00000 - exp( 0.100000.*(CONSTANTS(:,20) - STATES(:,1)))) - 10.0000);
    ALGEBRAIC(:,15) =  CONSTANTS(:,28).*(STATES(:,1) - CONSTANTS(:,29));
    RATES(:,1) =  - (ALGEBRAIC(:,12)+ALGEBRAIC(:,13)+ALGEBRAIC(:,14)+ALGEBRAIC(:,5)+ALGEBRAIC(:,10)+ALGEBRAIC(:,11)+ALGEBRAIC(:,15))./CONSTANTS(:,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(:,6) = 1.00000./( CONSTANTS(:,9).*(exp((CONSTANTS(:,10) - STATES(:,1))./( 2.00000.*CONSTANTS(:,11)))+exp((STATES(:,1) - CONSTANTS(:,10))./( 2.00000.*CONSTANTS(:,11)))));
    ALGEBRAIC(:,2) = 1.00000./(1.00000+exp((CONSTANTS(:,10) - STATES(:,1))./CONSTANTS(:,11)));
    ALGEBRAIC(:,7) = 1.00000./( CONSTANTS(:,15).*(exp((CONSTANTS(:,16) - STATES(:,1))./( 2.00000.*CONSTANTS(:,17)))+exp((STATES(:,1) - CONSTANTS(:,16))./( 2.00000.*CONSTANTS(:,17)))));
    ALGEBRAIC(:,3) = 1.00000./(1.00000+exp((CONSTANTS(:,16) - STATES(:,1))./CONSTANTS(:,17)));
    ALGEBRAIC(:,4) = 1.00000./(1.00000+exp((CONSTANTS(:,24) - STATES(:,1))./CONSTANTS(:,25)));
    ALGEBRAIC(:,8) = 1.00000./( CONSTANTS(:,23).*(exp((CONSTANTS(:,24) - STATES(:,1))./( 2.00000.*CONSTANTS(:,25)))+exp((STATES(:,1) - CONSTANTS(:,24))./( 2.00000.*CONSTANTS(:,25)))));
    ALGEBRAIC(:,9) = CONSTANTS(:,12)./(CONSTANTS(:,12)+STATES(:,3));
    ALGEBRAIC(:,10) = ( (( CONSTANTS(:,13).*STATES(:,4).*ALGEBRAIC(:,9).*2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2))).*(CONSTANTS(:,14) -  STATES(:,3).*exp(( 2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2)))))./(1.00000 - exp(( 2.00000.*CONSTANTS(:,3).*STATES(:,1))./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,12) =  CONSTANTS(:,22).*power(STATES(:,6), 4.00000).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,13) =  (( CONSTANTS(:,26).*power(STATES(:,3), 3.00000))./(power(CONSTANTS(:,12), 3.00000)+power(STATES(:,3), 3.00000))).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,14) =  CONSTANTS(:,27).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,1) = 1.00000./(1.00000+exp((CONSTANTS(:,7) - STATES(:,1))./CONSTANTS(:,8)));
    ALGEBRAIC(:,5) =  CONSTANTS(:,5).*power(ALGEBRAIC(:,1), 3.00000).*STATES(:,2).*(STATES(:,1) - CONSTANTS(:,6));
    ALGEBRAIC(:,11) =  (( CONSTANTS(:,18).*power(CONSTANTS(:,19), 2.00000))./(power(CONSTANTS(:,19), 2.00000)+power(STATES(:,5), 2.00000))).*((STATES(:,1) - CONSTANTS(:,20))./(1.00000 - exp( 0.100000.*(CONSTANTS(:,20) - STATES(:,1)))) - 10.0000);
    ALGEBRAIC(:,15) =  CONSTANTS(:,28).*(STATES(:,1) - CONSTANTS(:,29));
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