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 =19;
end
% There are a total of 7 entries in each of the rate and state variable arrays.
% There are a total of 17 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_CONSTANTS(:,1) = strpad('C_m in component environment (fF)');
    LEGEND_STATES(:,1) = strpad('q_K_o in component environment (fmol)');
    LEGEND_STATES(:,2) = strpad('q_K_i in component environment (fmol)');
    LEGEND_STATES(:,3) = strpad('q_Na_o in component environment (fmol)');
    LEGEND_STATES(:,4) = strpad('q_Na_i in component environment (fmol)');
    LEGEND_STATES(:,5) = strpad('q_x0_funny in component environment (fmol)');
    LEGEND_STATES(:,6) = strpad('q_x1_funny in component environment (fmol)');
    LEGEND_STATES(:,7) = strpad('q_mem in component environment (fC)');
    LEGEND_CONSTANTS(:,2) = strpad('R in component environment (J_per_K_per_mol)');
    LEGEND_CONSTANTS(:,3) = strpad('T in component environment (kelvin)');
    LEGEND_CONSTANTS(:,4) = strpad('F in component environment (C_per_mol)');
    LEGEND_ALGEBRAIC(:,14) = strpad('v_funny_K in component funny (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,15) = strpad('v_funny_Na in component funny (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,19) = strpad('I_mem_funny in component funny (fA)');
    LEGEND_CONSTANTS(:,5) = strpad('kappa_funny_Na in component funny_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,6) = strpad('kappa_funny_K in component funny_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,7) = strpad('kappa_R_xf in component funny_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,8) = strpad('K_Na_i in component funny_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,9) = strpad('K_Na_o in component funny_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,10) = strpad('K_K_i in component funny_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,11) = strpad('K_K_o in component funny_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,12) = strpad('K_x0_funny in component funny_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,13) = strpad('K_x1_funny in component funny_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,14) = strpad('zNa in component funny_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('zK in component funny_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('z_xff in component funny_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('z_xfr in component funny_parameters (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('mu_K_i in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,2) = strpad('mu_K_o in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,3) = strpad('mu_Na_i in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,4) = strpad('mu_Na_o in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,5) = strpad('V_mem in component funny (J_per_C)');
    LEGEND_ALGEBRAIC(:,7) = strpad('Am_Na in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,6) = strpad('Am_K in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,10) = strpad('Af_K in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,11) = strpad('Ar_K in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,12) = strpad('Af_Na in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,13) = strpad('Ar_Na in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,8) = strpad('mu_x0_funny in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,9) = strpad('mu_x1_funny in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,16) = strpad('Af_R_xf in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,17) = strpad('Ar_R_xf in component funny (J_per_mol)');
    LEGEND_ALGEBRAIC(:,18) = strpad('v_xf in component funny (fmol_per_sec)');
    LEGEND_RATES(:,2) = strpad('d/dt q_K_i in component environment (fmol)');
    LEGEND_RATES(:,1) = strpad('d/dt q_K_o in component environment (fmol)');
    LEGEND_RATES(:,4) = strpad('d/dt q_Na_i in component environment (fmol)');
    LEGEND_RATES(:,3) = strpad('d/dt q_Na_o in component environment (fmol)');
    LEGEND_RATES(:,7) = strpad('d/dt q_mem in component environment (fC)');
    LEGEND_RATES(:,5) = strpad('d/dt q_x0_funny in component environment (fmol)');
    LEGEND_RATES(:,6) = strpad('d/dt q_x1_funny in component environment (fmol)');
    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) = 153400;
    STATES(:,1) = 27.9828;
    STATES(:,2) = 5510;
    STATES(:,3) = 9.3276;
    STATES(:,4) = 0.00456;
    STATES(:,5) = 5.4799070E-06;
    STATES(:,6) = 5.4799070E-06;
    STATES(:,7) = -8.5e4;
    CONSTANTS(:,2) = 8.31;
    CONSTANTS(:,3) = 310;
    CONSTANTS(:,4) = 96500;
    CONSTANTS(:,5) = 45.0247;
    CONSTANTS(:,6) = 91.4974;
    CONSTANTS(:,7) = 0.188833;
    CONSTANTS(:,8) = 1.80864;
    CONSTANTS(:,9) = 13.2633;
    CONSTANTS(:,10) = 2.57828;
    CONSTANTS(:,11) = 18.9074;
    CONSTANTS(:,12) = 0.00306604;
    CONSTANTS(:,13) = 61.5887;
    CONSTANTS(:,14) = 1;
    CONSTANTS(:,15) = 1;
    CONSTANTS(:,16) = -1.8309039;
    CONSTANTS(:,17) = 0.787348905;
    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) = STATES(:,7)./CONSTANTS(:,1);
    ALGEBRAIC(:,6) =  CONSTANTS(:,15).*CONSTANTS(:,4).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,1) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,10).*STATES(:,2));
    ALGEBRAIC(:,9) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,13).*STATES(:,6));
    ALGEBRAIC(:,10) = ALGEBRAIC(:,9)+ALGEBRAIC(:,1)+ALGEBRAIC(:,6);
    ALGEBRAIC(:,2) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,11).*STATES(:,1));
    ALGEBRAIC(:,11) = ALGEBRAIC(:,9)+ALGEBRAIC(:,2);
    ALGEBRAIC(:,14) = piecewise({ALGEBRAIC(:,6)==0.00000,  1.00000.*CONSTANTS(:,6).*(exp(ALGEBRAIC(:,10)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,11)./( CONSTANTS(:,2).*CONSTANTS(:,3)))) },  ((( 1.00000.*CONSTANTS(:,6).*ALGEBRAIC(:,6))./( CONSTANTS(:,2).*CONSTANTS(:,3)))./(exp(ALGEBRAIC(:,6)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - 1.00000)).*(exp(ALGEBRAIC(:,10)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,11)./( CONSTANTS(:,2).*CONSTANTS(:,3)))));
    RATES(:,2) =  - ALGEBRAIC(:,14);
    RATES(:,1) = ALGEBRAIC(:,14);
    ALGEBRAIC(:,7) =  CONSTANTS(:,14).*CONSTANTS(:,4).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,3) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,8).*STATES(:,4));
    ALGEBRAIC(:,12) = ALGEBRAIC(:,9)+ALGEBRAIC(:,3)+ALGEBRAIC(:,7);
    ALGEBRAIC(:,4) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,9).*STATES(:,3));
    ALGEBRAIC(:,13) = ALGEBRAIC(:,9)+ALGEBRAIC(:,4);
    ALGEBRAIC(:,15) = piecewise({ALGEBRAIC(:,7)==0.00000,  1.00000.*CONSTANTS(:,5).*(exp(ALGEBRAIC(:,12)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,13)./( CONSTANTS(:,2).*CONSTANTS(:,3)))) },  ((( 1.00000.*CONSTANTS(:,5).*ALGEBRAIC(:,7))./( CONSTANTS(:,2).*CONSTANTS(:,3)))./(exp(ALGEBRAIC(:,7)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - 1.00000)).*(exp(ALGEBRAIC(:,12)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,13)./( CONSTANTS(:,2).*CONSTANTS(:,3)))));
    RATES(:,4) =  - ALGEBRAIC(:,15);
    RATES(:,3) = ALGEBRAIC(:,15);
    ALGEBRAIC(:,8) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,12).*STATES(:,5));
    ALGEBRAIC(:,16) = ALGEBRAIC(:,8)+ CONSTANTS(:,16).*CONSTANTS(:,4).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,17) = ALGEBRAIC(:,9)+ CONSTANTS(:,17).*CONSTANTS(:,4).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,18) =  CONSTANTS(:,7).*(exp(ALGEBRAIC(:,16)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,17)./( CONSTANTS(:,2).*CONSTANTS(:,3))));
    RATES(:,5) =  - ALGEBRAIC(:,18);
    RATES(:,6) = ALGEBRAIC(:,18);
    ALGEBRAIC(:,19) =  CONSTANTS(:,4).*((  - CONSTANTS(:,15).*ALGEBRAIC(:,14)+ CONSTANTS(:,14).*ALGEBRAIC(:,15)+ CONSTANTS(:,17).*ALGEBRAIC(:,18)) -  CONSTANTS(:,16).*ALGEBRAIC(:,18));
    RATES(:,7) = ALGEBRAIC(:,19);
   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) = STATES(:,7)./CONSTANTS(:,1);
    ALGEBRAIC(:,6) =  CONSTANTS(:,15).*CONSTANTS(:,4).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,1) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,10).*STATES(:,2));
    ALGEBRAIC(:,9) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,13).*STATES(:,6));
    ALGEBRAIC(:,10) = ALGEBRAIC(:,9)+ALGEBRAIC(:,1)+ALGEBRAIC(:,6);
    ALGEBRAIC(:,2) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,11).*STATES(:,1));
    ALGEBRAIC(:,11) = ALGEBRAIC(:,9)+ALGEBRAIC(:,2);
    ALGEBRAIC(:,14) = piecewise({ALGEBRAIC(:,6)==0.00000,  1.00000.*CONSTANTS(:,6).*(exp(ALGEBRAIC(:,10)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,11)./( CONSTANTS(:,2).*CONSTANTS(:,3)))) },  ((( 1.00000.*CONSTANTS(:,6).*ALGEBRAIC(:,6))./( CONSTANTS(:,2).*CONSTANTS(:,3)))./(exp(ALGEBRAIC(:,6)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - 1.00000)).*(exp(ALGEBRAIC(:,10)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,11)./( CONSTANTS(:,2).*CONSTANTS(:,3)))));
    ALGEBRAIC(:,7) =  CONSTANTS(:,14).*CONSTANTS(:,4).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,3) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,8).*STATES(:,4));
    ALGEBRAIC(:,12) = ALGEBRAIC(:,9)+ALGEBRAIC(:,3)+ALGEBRAIC(:,7);
    ALGEBRAIC(:,4) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,9).*STATES(:,3));
    ALGEBRAIC(:,13) = ALGEBRAIC(:,9)+ALGEBRAIC(:,4);
    ALGEBRAIC(:,15) = piecewise({ALGEBRAIC(:,7)==0.00000,  1.00000.*CONSTANTS(:,5).*(exp(ALGEBRAIC(:,12)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,13)./( CONSTANTS(:,2).*CONSTANTS(:,3)))) },  ((( 1.00000.*CONSTANTS(:,5).*ALGEBRAIC(:,7))./( CONSTANTS(:,2).*CONSTANTS(:,3)))./(exp(ALGEBRAIC(:,7)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - 1.00000)).*(exp(ALGEBRAIC(:,12)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,13)./( CONSTANTS(:,2).*CONSTANTS(:,3)))));
    ALGEBRAIC(:,8) =  CONSTANTS(:,2).*CONSTANTS(:,3).*log( CONSTANTS(:,12).*STATES(:,5));
    ALGEBRAIC(:,16) = ALGEBRAIC(:,8)+ CONSTANTS(:,16).*CONSTANTS(:,4).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,17) = ALGEBRAIC(:,9)+ CONSTANTS(:,17).*CONSTANTS(:,4).*ALGEBRAIC(:,5);
    ALGEBRAIC(:,18) =  CONSTANTS(:,7).*(exp(ALGEBRAIC(:,16)./( CONSTANTS(:,2).*CONSTANTS(:,3))) - exp(ALGEBRAIC(:,17)./( CONSTANTS(:,2).*CONSTANTS(:,3))));
    ALGEBRAIC(:,19) =  CONSTANTS(:,4).*((  - CONSTANTS(:,15).*ALGEBRAIC(:,14)+ CONSTANTS(:,14).*ALGEBRAIC(:,15)+ CONSTANTS(:,17).*ALGEBRAIC(:,18)) -  CONSTANTS(:,16).*ALGEBRAIC(:,18));
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