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 =14;
end
% There are a total of 7 entries in each of the rate and state variable arrays.
% There are a total of 24 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_ALGEBRAIC(:,1) = strpad('Vm in component membrane (millivolt)');
    LEGEND_CONSTANTS(:,1) = strpad('Cm in component membrane (microfarad)');
    LEGEND_ALGEBRAIC(:,12) = strpad('I_b in component I_b (nanoampere)');
    LEGEND_ALGEBRAIC(:,3) = strpad('I_Kir in component I_Kir (nanoampere)');
    LEGEND_ALGEBRAIC(:,10) = strpad('I_Shkr in component I_Shkr (nanoampere)');
    LEGEND_ALGEBRAIC(:,14) = strpad('I_stim in component I_stim (nanoampere)');
    LEGEND_STATES(:,1) = strpad('VmReal in component membrane (millivolt)');
    LEGEND_CONSTANTS(:,2) = strpad('GKir in component I_Kir (microsiemens)');
    LEGEND_CONSTANTS(:,3) = strpad('aKir in component I_Kir (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('bKir in component I_Kir (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('EK in component I_Kir (millivolt)');
    LEGEND_ALGEBRAIC(:,2) = strpad('OKir in component I_Kir (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('R in component model_parameters (millijoule_per_kelvin_mole)');
    LEGEND_CONSTANTS(:,6) = strpad('T in component model_parameters (kelvin)');
    LEGEND_CONSTANTS(:,7) = strpad('F in component model_parameters (coulomb_per_mole)');
    LEGEND_CONSTANTS(:,8) = strpad('Ko in component model_parameters (millimolar)');
    LEGEND_CONSTANTS(:,9) = strpad('Ki in component model_parameters (millimolar)');
    LEGEND_CONSTANTS(:,10) = strpad('PShkr in component I_Shkr (microlitre_per_second)');
    LEGEND_STATES(:,2) = strpad('C0ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_STATES(:,3) = strpad('C1ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_STATES(:,4) = strpad('C2ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_STATES(:,5) = strpad('C3ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_STATES(:,6) = strpad('C4ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_STATES(:,7) = strpad('OShkrReal in component I_Shkr (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('C0Shkr in component I_Shkr (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('C1Shkr in component I_Shkr (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('C2Shkr in component I_Shkr (dimensionless)');
    LEGEND_ALGEBRAIC(:,7) = strpad('C3Shkr in component I_Shkr (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('C4Shkr in component I_Shkr (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('OShkr in component I_Shkr (dimensionless)');
    LEGEND_ALGEBRAIC(:,11) = strpad('kv in component I_Shkr (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,13) = strpad('k_v in component I_Shkr (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('kv0 in component I_Shkr (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('zv in component I_Shkr (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('k_v0 in component I_Shkr (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('z_v in component I_Shkr (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('ko in component I_Shkr (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('k_o in component I_Shkr (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('Gb in component I_b (microsiemens)');
    LEGEND_CONSTANTS(:,18) = strpad('Eb in component I_b (millivolt)');
    LEGEND_CONSTANTS(:,19) = strpad('stim_start in component I_stim (second)');
    LEGEND_CONSTANTS(:,20) = strpad('stim_end in component I_stim (second)');
    LEGEND_CONSTANTS(:,21) = strpad('stim_period in component I_stim (second)');
    LEGEND_CONSTANTS(:,22) = strpad('stim_duration in component I_stim (second)');
    LEGEND_CONSTANTS(:,23) = strpad('stim_amplitude in component I_stim (nanoampere)');
    LEGEND_RATES(:,1) = strpad('d/dt VmReal in component membrane (millivolt)');
    LEGEND_RATES(:,2) = strpad('d/dt C0ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt C1ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt C2ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt C3ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt C4ShkrReal in component I_Shkr (dimensionless)');
    LEGEND_RATES(:,7) = strpad('d/dt OShkrReal in component I_Shkr (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) = 4.5e-6;
    STATES(:,1) = -58.00;
    CONSTANTS(:,2) = 1e-3;
    CONSTANTS(:,3) = 0.94;
    CONSTANTS(:,4) = 1.26;
    CONSTANTS(:,5) = 8310;
    CONSTANTS(:,6) = 295;
    CONSTANTS(:,7) = 9.65e4;
    CONSTANTS(:,8) = 5;
    CONSTANTS(:,9) = 140;
    CONSTANTS(:,10) = 5.4e-9;
    STATES(:,2) = 9.11e-1;
    STATES(:,3) = 8.57e-2;
    STATES(:,4) = 3.02e-3;
    STATES(:,5) = 4.74e-5;
    STATES(:,6) = 2.79e-7;
    STATES(:,7) = 0;
    CONSTANTS(:,11) = 30;
    CONSTANTS(:,12) = 1.28;
    CONSTANTS(:,13) = 2;
    CONSTANTS(:,14) = -1.53;
    CONSTANTS(:,15) = 77;
    CONSTANTS(:,16) = 18;
    CONSTANTS(:,17) = 6.9e-6;
    CONSTANTS(:,18) = 0;
    CONSTANTS(:,19) = 0.1;
    CONSTANTS(:,20) = 10;
    CONSTANTS(:,21) = 1;
    CONSTANTS(:,22) = 0.001;
    CONSTANTS(:,23) = 0.1;
    CONSTANTS(:,24) =  (( CONSTANTS(:,5).*CONSTANTS(:,6))./CONSTANTS(:,7)).*log(CONSTANTS(:,8)./CONSTANTS(:,9));
    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(:,8) = piecewise({STATES(:,6)<0.00000, 0.00000 , STATES(:,6)>1.00000, 1.00000 }, STATES(:,6));
    ALGEBRAIC(:,9) = piecewise({STATES(:,7)<0.00000, 0.00000 , STATES(:,7)>1.00000, 1.00000 }, STATES(:,7));
    RATES(:,7) =  CONSTANTS(:,15).*ALGEBRAIC(:,8) -  CONSTANTS(:,16).*ALGEBRAIC(:,9);
    ALGEBRAIC(:,4) = piecewise({STATES(:,2)<0.00000, 0.00000 , STATES(:,2)>1.00000, 1.00000 }, STATES(:,2));
    ALGEBRAIC(:,5) = piecewise({STATES(:,3)<0.00000, 0.00000 , STATES(:,3)>1.00000, 1.00000 }, STATES(:,3));
    ALGEBRAIC(:,1) = piecewise({abs(STATES(:,1))<1.00000e-07, 1.00000e-07 }, STATES(:,1));
    ALGEBRAIC(:,11) =  CONSTANTS(:,11).*exp(( ALGEBRAIC(:,1).*CONSTANTS(:,12).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6)));
    ALGEBRAIC(:,13) =  CONSTANTS(:,13).*exp(( ALGEBRAIC(:,1).*CONSTANTS(:,14).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6)));
    RATES(:,2) =  ALGEBRAIC(:,13).*ALGEBRAIC(:,5) -  4.00000.*ALGEBRAIC(:,11).*ALGEBRAIC(:,4);
    ALGEBRAIC(:,6) = piecewise({STATES(:,4)<0.00000, 0.00000 , STATES(:,4)>1.00000, 1.00000 }, STATES(:,4));
    RATES(:,3) = ( 2.00000.*ALGEBRAIC(:,13).*ALGEBRAIC(:,6)+ 4.00000.*ALGEBRAIC(:,11).*ALGEBRAIC(:,4)) -  ( 3.00000.*ALGEBRAIC(:,11)+ALGEBRAIC(:,13)).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,7) = piecewise({STATES(:,5)<0.00000, 0.00000 , STATES(:,5)>1.00000, 1.00000 }, STATES(:,5));
    RATES(:,4) = ( 3.00000.*ALGEBRAIC(:,13).*ALGEBRAIC(:,7)+ 3.00000.*ALGEBRAIC(:,11).*ALGEBRAIC(:,5)) -  ( 2.00000.*ALGEBRAIC(:,11)+ 2.00000.*ALGEBRAIC(:,13)).*ALGEBRAIC(:,6);
    RATES(:,5) = ( 4.00000.*ALGEBRAIC(:,13).*ALGEBRAIC(:,8)+ 2.00000.*ALGEBRAIC(:,11).*ALGEBRAIC(:,6)) -  (ALGEBRAIC(:,11)+ 3.00000.*ALGEBRAIC(:,13)).*ALGEBRAIC(:,7);
    RATES(:,6) = ( CONSTANTS(:,16).*ALGEBRAIC(:,9)+ ALGEBRAIC(:,11).*ALGEBRAIC(:,7)) -  (CONSTANTS(:,15)+ 4.00000.*ALGEBRAIC(:,13)).*ALGEBRAIC(:,8);
    ALGEBRAIC(:,12) =  CONSTANTS(:,17).*(ALGEBRAIC(:,1) - CONSTANTS(:,18));
    ALGEBRAIC(:,2) = 1.00000./(CONSTANTS(:,3)+exp(( CONSTANTS(:,4).*(ALGEBRAIC(:,1) - CONSTANTS(:,24)).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6))));
    ALGEBRAIC(:,3) =  CONSTANTS(:,2).*ALGEBRAIC(:,2).*power(( CONSTANTS(:,8).*0.00100000), 1.0 ./ 2).*(ALGEBRAIC(:,1) - CONSTANTS(:,24));
    ALGEBRAIC(:,10) = ( (( CONSTANTS(:,10).*ALGEBRAIC(:,9).*ALGEBRAIC(:,1).*power(CONSTANTS(:,7), 2.00000))./( CONSTANTS(:,5).*CONSTANTS(:,6))).*(CONSTANTS(:,9) -  CONSTANTS(:,8).*exp((  - ALGEBRAIC(:,1).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6)))))./(1.00000 - exp((  - ALGEBRAIC(:,1).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6))));
    ALGEBRAIC(:,14) = piecewise({VOI>=CONSTANTS(:,19)&(VOI<=CONSTANTS(:,20)&(VOI - CONSTANTS(:,19)) -  floor((VOI - CONSTANTS(:,19))./CONSTANTS(:,21)).*CONSTANTS(:,21)<=CONSTANTS(:,22)), CONSTANTS(:,23) }, 0.00000);
    RATES(:,1) =  - ((ALGEBRAIC(:,3)+ALGEBRAIC(:,10)+ALGEBRAIC(:,12)) - ALGEBRAIC(:,14))./CONSTANTS(:,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(:,8) = piecewise({STATES(:,6)<0.00000, 0.00000 , STATES(:,6)>1.00000, 1.00000 }, STATES(:,6));
    ALGEBRAIC(:,9) = piecewise({STATES(:,7)<0.00000, 0.00000 , STATES(:,7)>1.00000, 1.00000 }, STATES(:,7));
    ALGEBRAIC(:,4) = piecewise({STATES(:,2)<0.00000, 0.00000 , STATES(:,2)>1.00000, 1.00000 }, STATES(:,2));
    ALGEBRAIC(:,5) = piecewise({STATES(:,3)<0.00000, 0.00000 , STATES(:,3)>1.00000, 1.00000 }, STATES(:,3));
    ALGEBRAIC(:,1) = piecewise({abs(STATES(:,1))<1.00000e-07, 1.00000e-07 }, STATES(:,1));
    ALGEBRAIC(:,11) =  CONSTANTS(:,11).*exp(( ALGEBRAIC(:,1).*CONSTANTS(:,12).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6)));
    ALGEBRAIC(:,13) =  CONSTANTS(:,13).*exp(( ALGEBRAIC(:,1).*CONSTANTS(:,14).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6)));
    ALGEBRAIC(:,6) = piecewise({STATES(:,4)<0.00000, 0.00000 , STATES(:,4)>1.00000, 1.00000 }, STATES(:,4));
    ALGEBRAIC(:,7) = piecewise({STATES(:,5)<0.00000, 0.00000 , STATES(:,5)>1.00000, 1.00000 }, STATES(:,5));
    ALGEBRAIC(:,12) =  CONSTANTS(:,17).*(ALGEBRAIC(:,1) - CONSTANTS(:,18));
    ALGEBRAIC(:,2) = 1.00000./(CONSTANTS(:,3)+exp(( CONSTANTS(:,4).*(ALGEBRAIC(:,1) - CONSTANTS(:,24)).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6))));
    ALGEBRAIC(:,3) =  CONSTANTS(:,2).*ALGEBRAIC(:,2).*power(( CONSTANTS(:,8).*0.00100000), 1.0 ./ 2).*(ALGEBRAIC(:,1) - CONSTANTS(:,24));
    ALGEBRAIC(:,10) = ( (( CONSTANTS(:,10).*ALGEBRAIC(:,9).*ALGEBRAIC(:,1).*power(CONSTANTS(:,7), 2.00000))./( CONSTANTS(:,5).*CONSTANTS(:,6))).*(CONSTANTS(:,9) -  CONSTANTS(:,8).*exp((  - ALGEBRAIC(:,1).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6)))))./(1.00000 - exp((  - ALGEBRAIC(:,1).*CONSTANTS(:,7))./( CONSTANTS(:,5).*CONSTANTS(:,6))));
    ALGEBRAIC(:,14) = piecewise({VOI>=CONSTANTS(:,19)&(VOI<=CONSTANTS(:,20)&(VOI - CONSTANTS(:,19)) -  floor((VOI - CONSTANTS(:,19))./CONSTANTS(:,21)).*CONSTANTS(:,21)<=CONSTANTS(:,22)), CONSTANTS(:,23) }, 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