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 =23;
end
% There are a total of 5 entries in each of the rate and state variable arrays.
% There are a total of 22 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_STATES(:,1) = strpad('V in component membrane (millivolt)');
    LEGEND_CONSTANTS(:,1) = strpad('RToF in component membrane (millivolt)');
    LEGEND_CONSTANTS(:,2) = strpad('K_c in component membrane (millimolar)');
    LEGEND_CONSTANTS(:,3) = strpad('K_i in component membrane (millimolar)');
    LEGEND_CONSTANTS(:,4) = strpad('Na_c in component membrane (millimolar)');
    LEGEND_CONSTANTS(:,5) = strpad('Na_i in component membrane (millimolar)');
    LEGEND_ALGEBRAIC(:,6) = strpad('i_ns in component non_specific_current (picoA_per_picoF)');
    LEGEND_ALGEBRAIC(:,11) = strpad('i_to in component transient_outward_K_current (picoA_per_picoF)');
    LEGEND_ALGEBRAIC(:,21) = strpad('i_K1 in component inward_rectifier (picoA_per_picoF)');
    LEGEND_ALGEBRAIC(:,16) = strpad('i_Kur in component ultra_rapid_K_current (picoA_per_picoF)');
    LEGEND_ALGEBRAIC(:,22) = strpad('i_b_Na in component background_currents (picoA_per_picoF)');
    LEGEND_ALGEBRAIC(:,23) = strpad('i_NaK in component sodium_potassium_pump (picoA_per_picoF)');
    LEGEND_ALGEBRAIC(:,1) = strpad('i_Stim in component membrane (picoA_per_picoF)');
    LEGEND_CONSTANTS(:,6) = strpad('stim_start in component membrane (millisecond)');
    LEGEND_CONSTANTS(:,7) = strpad('stim_end in component membrane (millisecond)');
    LEGEND_CONSTANTS(:,8) = strpad('stim_period in component membrane (millisecond)');
    LEGEND_CONSTANTS(:,9) = strpad('stim_duration in component membrane (millisecond)');
    LEGEND_CONSTANTS(:,10) = strpad('stim_amplitude in component membrane (picoA_per_picoF)');
    LEGEND_CONSTANTS(:,11) = strpad('g_ns in component non_specific_current (nanoS_per_picoF)');
    LEGEND_CONSTANTS(:,21) = strpad('E_K in component transient_outward_K_current (millivolt)');
    LEGEND_CONSTANTS(:,12) = strpad('g_to in component transient_outward_K_current (nanoS_per_picoF)');
    LEGEND_STATES(:,2) = strpad('oa in component transient_outward_K_current_oa_gate (dimensionless)');
    LEGEND_STATES(:,3) = strpad('oi in component transient_outward_K_current_oi_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('alpha_oa in component transient_outward_K_current_oa_gate (per_millisecond)');
    LEGEND_ALGEBRAIC(:,7) = strpad('beta_oa in component transient_outward_K_current_oa_gate (per_millisecond)');
    LEGEND_ALGEBRAIC(:,17) = strpad('tau_oa in component transient_outward_K_current_oa_gate (millisecond)');
    LEGEND_ALGEBRAIC(:,12) = strpad('oa_inf in component transient_outward_K_current_oa_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,3) = strpad('alpha_oi in component transient_outward_K_current_oi_gate (per_millisecond)');
    LEGEND_ALGEBRAIC(:,8) = strpad('beta_oi in component transient_outward_K_current_oi_gate (per_millisecond)');
    LEGEND_ALGEBRAIC(:,18) = strpad('tau_oi in component transient_outward_K_current_oi_gate (millisecond)');
    LEGEND_ALGEBRAIC(:,13) = strpad('oi_inf in component transient_outward_K_current_oi_gate (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('g_Kur in component ultra_rapid_K_current (nanoS_per_picoF)');
    LEGEND_STATES(:,4) = strpad('ua in component ultra_rapid_K_current_ua_gate (dimensionless)');
    LEGEND_STATES(:,5) = strpad('ui in component ultra_rapid_K_current_ui_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('alpha_ua in component ultra_rapid_K_current_ua_gate (per_millisecond)');
    LEGEND_ALGEBRAIC(:,9) = strpad('beta_ua in component ultra_rapid_K_current_ua_gate (per_millisecond)');
    LEGEND_ALGEBRAIC(:,19) = strpad('tau_ua in component ultra_rapid_K_current_ua_gate (millisecond)');
    LEGEND_ALGEBRAIC(:,14) = strpad('ua_inf in component ultra_rapid_K_current_ua_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('alpha_ui in component ultra_rapid_K_current_ui_gate (per_millisecond)');
    LEGEND_ALGEBRAIC(:,10) = strpad('beta_ui in component ultra_rapid_K_current_ui_gate (per_millisecond)');
    LEGEND_ALGEBRAIC(:,20) = strpad('tau_ui in component ultra_rapid_K_current_ui_gate (millisecond)');
    LEGEND_ALGEBRAIC(:,15) = strpad('ui_inf in component ultra_rapid_K_current_ui_gate (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('g_K1 in component inward_rectifier (nanoS_per_picoF)');
    LEGEND_CONSTANTS(:,15) = strpad('g_b_Na in component background_currents (nanoS_per_picoF)');
    LEGEND_CONSTANTS(:,22) = strpad('E_Na in component background_currents (millivolt)');
    LEGEND_CONSTANTS(:,16) = strpad('k_NaK_K in component sodium_potassium_pump (millimolar)');
    LEGEND_CONSTANTS(:,17) = strpad('k_NaK_Na in component sodium_potassium_pump (millimolar)');
    LEGEND_CONSTANTS(:,18) = strpad('i_NaK_max in component sodium_potassium_pump (picoA_per_picoF)');
    LEGEND_CONSTANTS(:,19) = strpad('V_rev in component sodium_potassium_pump (millivolt)');
    LEGEND_CONSTANTS(:,20) = strpad('B in component sodium_potassium_pump (millivolt)');
    LEGEND_RATES(:,1) = strpad('d/dt V in component membrane (millivolt)');
    LEGEND_RATES(:,2) = strpad('d/dt oa in component transient_outward_K_current_oa_gate (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt oi in component transient_outward_K_current_oi_gate (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt ua in component ultra_rapid_K_current_ua_gate (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt ui in component ultra_rapid_K_current_ui_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 = [];
    STATES(:,1) = -80;
    CONSTANTS(:,1) = 26.54;
    CONSTANTS(:,2) = 5.4;
    CONSTANTS(:,3) = 139;
    CONSTANTS(:,4) = 140;
    CONSTANTS(:,5) = 11.2;
    CONSTANTS(:,6) = 100;
    CONSTANTS(:,7) = 10000;
    CONSTANTS(:,8) = 5000;
    CONSTANTS(:,9) = 2;
    CONSTANTS(:,10) = -100;
    CONSTANTS(:,11) = 0.018;
    CONSTANTS(:,12) = 0.01652;
    STATES(:,2) = 0.2488;
    STATES(:,3) = 0.1866;
    CONSTANTS(:,13) = 0.6;
    STATES(:,4) = 0.9933;
    STATES(:,5) = 0.0999;
    CONSTANTS(:,14) = 0.03;
    CONSTANTS(:,15) = 0.00607;
    CONSTANTS(:,16) = 1;
    CONSTANTS(:,17) = 11;
    CONSTANTS(:,18) = 2.002;
    CONSTANTS(:,19) = -150;
    CONSTANTS(:,20) = -200;
    CONSTANTS(:,21) =  CONSTANTS(:,1).*log(CONSTANTS(:,2)./CONSTANTS(:,3));
    CONSTANTS(:,22) =  CONSTANTS(:,1).*log(CONSTANTS(:,4)./CONSTANTS(:,5));
    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(:,2) = 0.650000./(exp((STATES(:,1)+10.0000)./ - 8.50000)+exp((STATES(:,1) - 30.0000)./ - 59.0000));
    ALGEBRAIC(:,7) = 0.650000./(2.50000+exp((STATES(:,1)+82.0000)./17.0000));
    ALGEBRAIC(:,17) = 15.0000./(ALGEBRAIC(:,2)+ALGEBRAIC(:,7));
    ALGEBRAIC(:,12) = 1.00000./(1.00000+exp((STATES(:,1)+20.4700)./ - 17.5400));
    RATES(:,2) = (ALGEBRAIC(:,12) - STATES(:,2))./ALGEBRAIC(:,17);
    ALGEBRAIC(:,3) = 1.00000./(18.5300+exp((STATES(:,1)+113.700)./10.9500));
    ALGEBRAIC(:,8) = 1.00000./(35.5600+exp((STATES(:,1)+1.26000)./ - 7.44000));
    ALGEBRAIC(:,18) = 15.0000./(ALGEBRAIC(:,3)+ALGEBRAIC(:,8));
    ALGEBRAIC(:,13) = 1.00000./(1.00000+exp((STATES(:,1)+43.1000)./5.30000));
    RATES(:,3) = (ALGEBRAIC(:,13) - STATES(:,3))./ALGEBRAIC(:,18);
    ALGEBRAIC(:,4) = 0.650000./(exp((STATES(:,1)+10.0000)./ - 8.50000)+exp((STATES(:,1) - 30.0000)./ - 59.0000));
    ALGEBRAIC(:,9) = 0.650000./(2.50000+exp((STATES(:,1)+82.0000)./17.0000));
    ALGEBRAIC(:,19) = 1.00000./(ALGEBRAIC(:,4)+ALGEBRAIC(:,9));
    ALGEBRAIC(:,14) = 1.00000./(1.00000+exp((STATES(:,1)+33.3000)./ - 9.60000));
    RATES(:,4) = (ALGEBRAIC(:,14) - STATES(:,4))./ALGEBRAIC(:,19);
    ALGEBRAIC(:,5) = 1.00000./(21.0000+exp((STATES(:,1) - 185.000)./28.0000));
    ALGEBRAIC(:,10) = 1.00000./exp((STATES(:,1) - 158.000)./ - 16.0000);
    ALGEBRAIC(:,20) = 5.00000./(ALGEBRAIC(:,5)+ALGEBRAIC(:,10));
    ALGEBRAIC(:,15) = 1.00000./(1.00000+exp((STATES(:,1) - 99.4500)./27.4800));
    RATES(:,5) = (ALGEBRAIC(:,15) - STATES(:,5))./ALGEBRAIC(:,20);
    ALGEBRAIC(:,6) =  CONSTANTS(:,11).*STATES(:,1);
    ALGEBRAIC(:,11) =  CONSTANTS(:,12).*power(STATES(:,2), 3.00000).*STATES(:,3).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,21) = ( CONSTANTS(:,14).*(STATES(:,1)+86.7500))./(1.00000+exp((STATES(:,1)+20.0000)./20.0000));
    ALGEBRAIC(:,16) =  ( CONSTANTS(:,13).*0.00500000+0.0500000./(1.00000+exp( - (STATES(:,1) - 15.0000)./13.0000))).*power(STATES(:,4), 3.00000).*STATES(:,5).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,22) =  CONSTANTS(:,15).*(STATES(:,1) - CONSTANTS(:,22));
    ALGEBRAIC(:,23) = ( (( (( CONSTANTS(:,18).*CONSTANTS(:,2))./(CONSTANTS(:,2)+CONSTANTS(:,16))).*power(CONSTANTS(:,5), 1.50000))./(power(CONSTANTS(:,5), 1.50000)+power(CONSTANTS(:,17), 1.50000))).*(STATES(:,1) - CONSTANTS(:,19)))./(STATES(:,1) - CONSTANTS(:,20));
    ALGEBRAIC(:,1) = piecewise({VOI>=CONSTANTS(:,6)&VOI<=CONSTANTS(:,7)&(VOI - CONSTANTS(:,6)) -  floor((VOI - CONSTANTS(:,6))./CONSTANTS(:,8)).*CONSTANTS(:,8)<=CONSTANTS(:,9), CONSTANTS(:,10) }, 0.00000);
    RATES(:,1) =  - (ALGEBRAIC(:,6)+ALGEBRAIC(:,16)+ALGEBRAIC(:,11)+ALGEBRAIC(:,21)+ALGEBRAIC(:,22)+ALGEBRAIC(:,23)+ALGEBRAIC(:,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(:,2) = 0.650000./(exp((STATES(:,1)+10.0000)./ - 8.50000)+exp((STATES(:,1) - 30.0000)./ - 59.0000));
    ALGEBRAIC(:,7) = 0.650000./(2.50000+exp((STATES(:,1)+82.0000)./17.0000));
    ALGEBRAIC(:,17) = 15.0000./(ALGEBRAIC(:,2)+ALGEBRAIC(:,7));
    ALGEBRAIC(:,12) = 1.00000./(1.00000+exp((STATES(:,1)+20.4700)./ - 17.5400));
    ALGEBRAIC(:,3) = 1.00000./(18.5300+exp((STATES(:,1)+113.700)./10.9500));
    ALGEBRAIC(:,8) = 1.00000./(35.5600+exp((STATES(:,1)+1.26000)./ - 7.44000));
    ALGEBRAIC(:,18) = 15.0000./(ALGEBRAIC(:,3)+ALGEBRAIC(:,8));
    ALGEBRAIC(:,13) = 1.00000./(1.00000+exp((STATES(:,1)+43.1000)./5.30000));
    ALGEBRAIC(:,4) = 0.650000./(exp((STATES(:,1)+10.0000)./ - 8.50000)+exp((STATES(:,1) - 30.0000)./ - 59.0000));
    ALGEBRAIC(:,9) = 0.650000./(2.50000+exp((STATES(:,1)+82.0000)./17.0000));
    ALGEBRAIC(:,19) = 1.00000./(ALGEBRAIC(:,4)+ALGEBRAIC(:,9));
    ALGEBRAIC(:,14) = 1.00000./(1.00000+exp((STATES(:,1)+33.3000)./ - 9.60000));
    ALGEBRAIC(:,5) = 1.00000./(21.0000+exp((STATES(:,1) - 185.000)./28.0000));
    ALGEBRAIC(:,10) = 1.00000./exp((STATES(:,1) - 158.000)./ - 16.0000);
    ALGEBRAIC(:,20) = 5.00000./(ALGEBRAIC(:,5)+ALGEBRAIC(:,10));
    ALGEBRAIC(:,15) = 1.00000./(1.00000+exp((STATES(:,1) - 99.4500)./27.4800));
    ALGEBRAIC(:,6) =  CONSTANTS(:,11).*STATES(:,1);
    ALGEBRAIC(:,11) =  CONSTANTS(:,12).*power(STATES(:,2), 3.00000).*STATES(:,3).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,21) = ( CONSTANTS(:,14).*(STATES(:,1)+86.7500))./(1.00000+exp((STATES(:,1)+20.0000)./20.0000));
    ALGEBRAIC(:,16) =  ( CONSTANTS(:,13).*0.00500000+0.0500000./(1.00000+exp( - (STATES(:,1) - 15.0000)./13.0000))).*power(STATES(:,4), 3.00000).*STATES(:,5).*(STATES(:,1) - CONSTANTS(:,21));
    ALGEBRAIC(:,22) =  CONSTANTS(:,15).*(STATES(:,1) - CONSTANTS(:,22));
    ALGEBRAIC(:,23) = ( (( (( CONSTANTS(:,18).*CONSTANTS(:,2))./(CONSTANTS(:,2)+CONSTANTS(:,16))).*power(CONSTANTS(:,5), 1.50000))./(power(CONSTANTS(:,5), 1.50000)+power(CONSTANTS(:,17), 1.50000))).*(STATES(:,1) - CONSTANTS(:,19)))./(STATES(:,1) - CONSTANTS(:,20));
    ALGEBRAIC(:,1) = piecewise({VOI>=CONSTANTS(:,6)&VOI<=CONSTANTS(:,7)&(VOI - CONSTANTS(:,6)) -  floor((VOI - CONSTANTS(:,6))./CONSTANTS(:,8)).*CONSTANTS(:,8)<=CONSTANTS(:,9), CONSTANTS(:,10) }, 0.00000);
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