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 5 entries in each of the rate and state variable arrays.
% There are a total of 16 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_CONSTANTS(:,1) = strpad('T in component parameters (kelvin)');
    LEGEND_CONSTANTS(:,2) = strpad('K in component parameters_propagation (per_ms)');
    LEGEND_CONSTANTS(:,3) = strpad('V_initial in component initial_conditions (mV)');
    LEGEND_VOI = strpad('time in component time (ms)');
    LEGEND_ALGEBRAIC(:,7) = strpad('INa in component INa (uA_per_cmsq)');
    LEGEND_ALGEBRAIC(:,12) = strpad('IK in component IK (uA_per_cmsq)');
    LEGEND_ALGEBRAIC(:,14) = strpad('Ileak in component Ileak (uA_per_cmsq)');
    LEGEND_ALGEBRAIC(:,15) = strpad('Ii in component propagated_AP (uA_per_cmsq)');
    LEGEND_ALGEBRAIC(:,1) = strpad('Im in component propagated_AP (uA_per_cmsq)');
    LEGEND_ALGEBRAIC(:,16) = strpad('I in component propagated_AP (uA_per_cmsq)');
    LEGEND_ALGEBRAIC(:,13) = strpad('g in component propagated_AP (mS_per_cmsq)');
    LEGEND_ALGEBRAIC(:,11) = strpad('gK in component IK (mS_per_cmsq)');
    LEGEND_ALGEBRAIC(:,2) = strpad('gNa in component INa (mS_per_cmsq)');
    LEGEND_STATES(:,1) = strpad('V in component propagated_AP (mV)');
    LEGEND_ALGEBRAIC(:,3) = strpad('minus_V in component propagated_AP (mV)');
    LEGEND_STATES(:,3) = strpad('X in component hh_gating_variable (dimensionless)');
    LEGEND_STATES(:,4) = strpad('X in component hh_gating_variable (dimensionless)');
    LEGEND_STATES(:,5) = strpad('X in component hh_gating_variable (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('n_initial in component initial_conditions (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('m_initial in component initial_conditions (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('h_initial in component initial_conditions (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('VNa in component parameters (mV)');
    LEGEND_CONSTANTS(:,8) = strpad('VK in component parameters (mV)');
    LEGEND_CONSTANTS(:,9) = strpad('Vleak in component parameters (mV)');
    LEGEND_CONSTANTS(:,10) = strpad('gNa_max in component parameters (mS_per_cmsq)');
    LEGEND_CONSTANTS(:,11) = strpad('gK_max in component parameters (mS_per_cmsq)');
    LEGEND_CONSTANTS(:,12) = strpad('gleak_max in component parameters (mS_per_cmsq)');
    LEGEND_CONSTANTS(:,13) = strpad('Cm in component parameters (uF_per_cmsq)');
    LEGEND_CONSTANTS(:,14) = strpad('V_initial in component initial_conditions (mV)');
    LEGEND_ALGEBRAIC(:,4) = strpad('alpha in component alpha_m (per_ms)');
    LEGEND_CONSTANTS(:,15) = strpad('phi in component temperature_factor (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('beta in component beta_m (per_ms)');
    LEGEND_ALGEBRAIC(:,5) = strpad('alpha in component alpha_h (per_ms)');
    LEGEND_ALGEBRAIC(:,9) = strpad('beta in component beta_h (per_ms)');
    LEGEND_ALGEBRAIC(:,6) = strpad('alpha in component alpha_n (per_ms)');
    LEGEND_CONSTANTS(:,16) = strpad('phi in component temperature_factor (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('beta in component beta_n (per_ms)');
    LEGEND_STATES(:,2) = strpad('d/dt V in component propagated_AP (mV)');
    LEGEND_RATES(:,2) = strpad('d^2/dtV in component propagated_AP (mV)');
    LEGEND_RATES(:,4) = strpad('d/dt X in component hh_gating_variable (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt X in component hh_gating_variable (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt X in component hh_gating_variable (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) = 18.5;
    CONSTANTS(:,2) = 10.47;
    CONSTANTS(:,3) = 0;
    STATES(:,2) = 0.0;
    CONSTANTS(:,4) = 0.315;
    CONSTANTS(:,5) = 0.042;
    CONSTANTS(:,6) = 0.608;
    CONSTANTS(:,7) = -115.0;
    CONSTANTS(:,8) = 12.0;
    CONSTANTS(:,9) = -10.613;
    CONSTANTS(:,10) = 120.0;
    CONSTANTS(:,11) = 36.0;
    CONSTANTS(:,12) = 0.3;
    CONSTANTS(:,13) = 1.0;
    CONSTANTS(:,14) = 0.0;
    CONSTANTS(:,15) = power(3.00000, (CONSTANTS(:,1) - 6.30000)./10.0000);
    CONSTANTS(:,16) = power(3.00000, (CONSTANTS(:,1) - 6.30000)./10.0000);
    STATES(:,1) = CONSTANTS(:,3);
    STATES(:,3) = CONSTANTS(:,6);
    STATES(:,4) = CONSTANTS(:,5);
    STATES(:,5) = CONSTANTS(:,4);
    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(:,4) =  (( 0.100000.*(STATES(:,1)+25.0000))./(exp((STATES(:,1)+25.0000)./10.0000) - 1.00000)).*CONSTANTS(:,15);
    ALGEBRAIC(:,8) =  4.00000.*exp(STATES(:,1)./18.0000).*CONSTANTS(:,15);
    RATES(:,4) =  ALGEBRAIC(:,4).*(1.00000 - STATES(:,4)) -  ALGEBRAIC(:,8).*STATES(:,4);
    ALGEBRAIC(:,5) =  0.0700000.*exp(STATES(:,1)./20.0000).*CONSTANTS(:,15);
    ALGEBRAIC(:,9) =  (1.00000./(exp((STATES(:,1)+30.0000)./10.0000)+1.00000)).*CONSTANTS(:,15);
    RATES(:,3) =  ALGEBRAIC(:,5).*(1.00000 - STATES(:,3)) -  ALGEBRAIC(:,9).*STATES(:,3);
    ALGEBRAIC(:,6) =  (( 0.0100000.*(STATES(:,1)+10.0000))./(exp((STATES(:,1)+10.0000)./10.0000) - 1.00000)).*CONSTANTS(:,16);
    ALGEBRAIC(:,10) =  0.125000.*exp(STATES(:,1)./80.0000).*CONSTANTS(:,16);
    RATES(:,5) =  ALGEBRAIC(:,6).*(1.00000 - STATES(:,5)) -  ALGEBRAIC(:,10).*STATES(:,5);
    ALGEBRAIC(:,2) =  CONSTANTS(:,10).*power(STATES(:,4), 3.00000).*STATES(:,3);
    ALGEBRAIC(:,7) =  ALGEBRAIC(:,2).*(STATES(:,1) - CONSTANTS(:,7));
    ALGEBRAIC(:,11) =  CONSTANTS(:,11).*power(STATES(:,5), 4.00000);
    ALGEBRAIC(:,12) =  ALGEBRAIC(:,11).*(STATES(:,1) - CONSTANTS(:,8));
    ALGEBRAIC(:,14) =  CONSTANTS(:,12).*(STATES(:,1) - CONSTANTS(:,9));
    ALGEBRAIC(:,15) = ALGEBRAIC(:,7)+ALGEBRAIC(:,12)+ALGEBRAIC(:,14);
    RATES(:,2) =  CONSTANTS(:,2).*(STATES(:,2)+ (1.00000./CONSTANTS(:,13)).*ALGEBRAIC(:,15));
    RATES(:,1) = STATES(:,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(:,4) =  (( 0.100000.*(STATES(:,1)+25.0000))./(exp((STATES(:,1)+25.0000)./10.0000) - 1.00000)).*CONSTANTS(:,15);
    ALGEBRAIC(:,8) =  4.00000.*exp(STATES(:,1)./18.0000).*CONSTANTS(:,15);
    ALGEBRAIC(:,5) =  0.0700000.*exp(STATES(:,1)./20.0000).*CONSTANTS(:,15);
    ALGEBRAIC(:,9) =  (1.00000./(exp((STATES(:,1)+30.0000)./10.0000)+1.00000)).*CONSTANTS(:,15);
    ALGEBRAIC(:,6) =  (( 0.0100000.*(STATES(:,1)+10.0000))./(exp((STATES(:,1)+10.0000)./10.0000) - 1.00000)).*CONSTANTS(:,16);
    ALGEBRAIC(:,10) =  0.125000.*exp(STATES(:,1)./80.0000).*CONSTANTS(:,16);
    ALGEBRAIC(:,2) =  CONSTANTS(:,10).*power(STATES(:,4), 3.00000).*STATES(:,3);
    ALGEBRAIC(:,7) =  ALGEBRAIC(:,2).*(STATES(:,1) - CONSTANTS(:,7));
    ALGEBRAIC(:,11) =  CONSTANTS(:,11).*power(STATES(:,5), 4.00000);
    ALGEBRAIC(:,12) =  ALGEBRAIC(:,11).*(STATES(:,1) - CONSTANTS(:,8));
    ALGEBRAIC(:,14) =  CONSTANTS(:,12).*(STATES(:,1) - CONSTANTS(:,9));
    ALGEBRAIC(:,15) = ALGEBRAIC(:,7)+ALGEBRAIC(:,12)+ALGEBRAIC(:,14);
    ALGEBRAIC(:,1) =   - CONSTANTS(:,13).*STATES(:,2);
    ALGEBRAIC(:,3) =  - STATES(:,1);
    ALGEBRAIC(:,13) = ALGEBRAIC(:,11)+ALGEBRAIC(:,2)+CONSTANTS(:,12);
    ALGEBRAIC(:,16) =  (CONSTANTS(:,13)./CONSTANTS(:,2)).*RATES(:,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