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 =0;
end
% There are a total of 12 entries in each of the rate and state variable arrays.
% There are a total of 38 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('E in component E (millimolar)');
    LEGEND_CONSTANTS(:,1) = strpad('k23 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,2) = strpad('k24 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,3) = strpad('k17 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,4) = strpad('k18 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,5) = strpad('k1 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,6) = strpad('k2 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,7) = strpad('k3 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,8) = strpad('k4 in component reaction_constants (first_order_rate_constant)');
    LEGEND_STATES(:,2) = strpad('ED in component ED (millimolar)');
    LEGEND_STATES(:,3) = strpad('E_ in component E_ (millimolar)');
    LEGEND_STATES(:,4) = strpad('ENa in component ENa (millimolar)');
    LEGEND_STATES(:,5) = strpad('ECl in component ECl (millimolar)');
    LEGEND_CONSTANTS(:,9) = strpad('D in component reaction_constants (millimolar)');
    LEGEND_CONSTANTS(:,10) = strpad('Na in component reaction_constants (millimolar)');
    LEGEND_CONSTANTS(:,11) = strpad('Cl in component reaction_constants (millimolar)');
    LEGEND_CONSTANTS(:,12) = strpad('k29 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('k30 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('k11 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,15) = strpad('k12 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('k9 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('k10 in component reaction_constants (first_order_rate_constant)');
    LEGEND_STATES(:,6) = strpad('ED_ in component ED_ (millimolar)');
    LEGEND_STATES(:,7) = strpad('ENa_ in component ENa_ (millimolar)');
    LEGEND_STATES(:,8) = strpad('ECl_ in component ECl_ (millimolar)');
    LEGEND_CONSTANTS(:,18) = strpad('D_ in component reaction_constants (millimolar)');
    LEGEND_CONSTANTS(:,19) = strpad('Na_ in component reaction_constants (millimolar)');
    LEGEND_CONSTANTS(:,20) = strpad('Cl_ in component reaction_constants (millimolar)');
    LEGEND_CONSTANTS(:,21) = strpad('k21 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,22) = strpad('k22 in component reaction_constants (first_order_rate_constant)');
    LEGEND_STATES(:,9) = strpad('ENaD in component ENaD (millimolar)');
    LEGEND_CONSTANTS(:,23) = strpad('k27 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,24) = strpad('k28 in component reaction_constants (first_order_rate_constant)');
    LEGEND_STATES(:,10) = strpad('ENaD_ in component ENaD_ (millimolar)');
    LEGEND_CONSTANTS(:,25) = strpad('k25 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,26) = strpad('k26 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,27) = strpad('k31 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,28) = strpad('k32 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,29) = strpad('k5 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,30) = strpad('k6 in component reaction_constants (first_order_rate_constant)');
    LEGEND_STATES(:,11) = strpad('ENaCl in component ENaCl (millimolar)');
    LEGEND_CONSTANTS(:,31) = strpad('k13 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,32) = strpad('k14 in component reaction_constants (first_order_rate_constant)');
    LEGEND_STATES(:,12) = strpad('ENaCl_ in component ENaCl_ (millimolar)');
    LEGEND_CONSTANTS(:,33) = strpad('k7 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,34) = strpad('k8 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,35) = strpad('k15 in component reaction_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,36) = strpad('k16 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,37) = strpad('k19 in component reaction_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,38) = strpad('k20 in component reaction_constants (first_order_rate_constant)');
    LEGEND_RATES(:,1) = strpad('d/dt E in component E (millimolar)');
    LEGEND_RATES(:,3) = strpad('d/dt E_ in component E_ (millimolar)');
    LEGEND_RATES(:,2) = strpad('d/dt ED in component ED (millimolar)');
    LEGEND_RATES(:,6) = strpad('d/dt ED_ in component ED_ (millimolar)');
    LEGEND_RATES(:,9) = strpad('d/dt ENaD in component ENaD (millimolar)');
    LEGEND_RATES(:,10) = strpad('d/dt ENaD_ in component ENaD_ (millimolar)');
    LEGEND_RATES(:,4) = strpad('d/dt ENa in component ENa (millimolar)');
    LEGEND_RATES(:,7) = strpad('d/dt ENa_ in component ENa_ (millimolar)');
    LEGEND_RATES(:,5) = strpad('d/dt ECl in component ECl (millimolar)');
    LEGEND_RATES(:,8) = strpad('d/dt ECl_ in component ECl_ (millimolar)');
    LEGEND_RATES(:,11) = strpad('d/dt ENaCl in component ENaCl (millimolar)');
    LEGEND_RATES(:,12) = strpad('d/dt ENaCl_ in component ENaCl_ (millimolar)');
    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) = 0.08333;
    CONSTANTS(:,1) = 1.0E5;
    CONSTANTS(:,2) = 3.192E1;
    CONSTANTS(:,3) = 4.587E5;
    CONSTANTS(:,4) = 1.0E5;
    CONSTANTS(:,5) = 1.0E5;
    CONSTANTS(:,6) = 4.183E5;
    CONSTANTS(:,7) = 1.0E5;
    CONSTANTS(:,8) = 4.928E6;
    STATES(:,2) = 0.08333;
    STATES(:,3) = 0.08333;
    STATES(:,4) = 0.08333;
    STATES(:,5) = 0.08333;
    CONSTANTS(:,9) = 1.0E-6;
    CONSTANTS(:,10) = 50.0;
    CONSTANTS(:,11) = 96.0;
    CONSTANTS(:,12) = 1.0E5;
    CONSTANTS(:,13) = 3.514E-1;
    CONSTANTS(:,14) = 1.0E5;
    CONSTANTS(:,15) = 4.982E6;
    CONSTANTS(:,16) = 1.0E5;
    CONSTANTS(:,17) = 4.183E5;
    STATES(:,6) = 0.08333;
    STATES(:,7) = 0.08333;
    STATES(:,8) = 0.08333;
    CONSTANTS(:,18) = 1.0E-6;
    CONSTANTS(:,19) = 10.0;
    CONSTANTS(:,20) = 40.0;
    CONSTANTS(:,21) = 1.0E5;
    CONSTANTS(:,22) = 4.183E5;
    STATES(:,9) = 0.08333;
    CONSTANTS(:,23) = 1.0E5;
    CONSTANTS(:,24) = 1.389E5;
    STATES(:,10) = 0.08333;
    CONSTANTS(:,25) = 1.0E5;
    CONSTANTS(:,26) = 3.192E1;
    CONSTANTS(:,27) = 1.0E5;
    CONSTANTS(:,28) = 1.166E-1;
    CONSTANTS(:,29) = 1.0E5;
    CONSTANTS(:,30) = 1.065E6;
    STATES(:,11) = 0.08333;
    CONSTANTS(:,31) = 1.0E5;
    CONSTANTS(:,32) = 1.065E6;
    STATES(:,12) = 0.08333;
    CONSTANTS(:,33) = 1.0E5;
    CONSTANTS(:,34) = 8.940E4;
    CONSTANTS(:,35) = 1.0E5;
    CONSTANTS(:,36) = 8.940E4;
    CONSTANTS(:,37) = 1.0E3;
    CONSTANTS(:,38) = 2.180E2;
    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(:,1) = ( CONSTANTS(:,2).*STATES(:,2)+ CONSTANTS(:,4).*STATES(:,3)+ CONSTANTS(:,6).*STATES(:,4)+ CONSTANTS(:,8).*STATES(:,5)) - ( CONSTANTS(:,1).*CONSTANTS(:,9).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,1)+ CONSTANTS(:,5).*CONSTANTS(:,10).*STATES(:,1)+ CONSTANTS(:,7).*CONSTANTS(:,11).*STATES(:,1));
    RATES(:,3) = ( CONSTANTS(:,13).*STATES(:,6)+ CONSTANTS(:,3).*STATES(:,1)+ CONSTANTS(:,17).*STATES(:,7)+ CONSTANTS(:,15).*STATES(:,8)) - ( CONSTANTS(:,12).*CONSTANTS(:,18).*STATES(:,3)+ CONSTANTS(:,4).*STATES(:,3)+ CONSTANTS(:,16).*CONSTANTS(:,19).*STATES(:,3)+ CONSTANTS(:,14).*CONSTANTS(:,20).*STATES(:,3));
    RATES(:,2) = ( CONSTANTS(:,1).*STATES(:,1).*CONSTANTS(:,9)+ CONSTANTS(:,22).*STATES(:,9)) - ( CONSTANTS(:,2).*STATES(:,2)+ CONSTANTS(:,21).*CONSTANTS(:,10).*STATES(:,2));
    RATES(:,6) = ( CONSTANTS(:,12).*STATES(:,3).*CONSTANTS(:,18)+ CONSTANTS(:,24).*STATES(:,10)) - ( CONSTANTS(:,13).*STATES(:,6)+ CONSTANTS(:,23).*CONSTANTS(:,19).*STATES(:,6));
    RATES(:,9) = ( CONSTANTS(:,21).*CONSTANTS(:,10).*STATES(:,2)+ CONSTANTS(:,25).*CONSTANTS(:,9).*STATES(:,4)) - ( CONSTANTS(:,22).*STATES(:,9)+ CONSTANTS(:,26).*STATES(:,9));
    RATES(:,10) = ( CONSTANTS(:,23).*CONSTANTS(:,19).*STATES(:,6)+ CONSTANTS(:,27).*CONSTANTS(:,18).*STATES(:,7)) - ( CONSTANTS(:,24).*STATES(:,10)+ CONSTANTS(:,28).*STATES(:,10));
    RATES(:,4) = ( CONSTANTS(:,5).*CONSTANTS(:,10).*STATES(:,1)+ CONSTANTS(:,26).*STATES(:,9)+ CONSTANTS(:,30).*STATES(:,11)) - ( CONSTANTS(:,6).*STATES(:,4)+ CONSTANTS(:,29).*CONSTANTS(:,11).*STATES(:,4)+ CONSTANTS(:,25).*CONSTANTS(:,9).*STATES(:,4));
    RATES(:,7) = ( CONSTANTS(:,16).*CONSTANTS(:,19).*STATES(:,3)+ CONSTANTS(:,28).*STATES(:,10)+ CONSTANTS(:,32).*STATES(:,12)) - ( CONSTANTS(:,17).*STATES(:,7)+ CONSTANTS(:,31).*CONSTANTS(:,20).*STATES(:,7)+ CONSTANTS(:,27).*CONSTANTS(:,18).*STATES(:,7));
    RATES(:,5) = ( CONSTANTS(:,7).*CONSTANTS(:,11).*STATES(:,1)+ CONSTANTS(:,34).*STATES(:,11)) - ( CONSTANTS(:,33).*CONSTANTS(:,10).*STATES(:,5)+ CONSTANTS(:,8).*STATES(:,5));
    RATES(:,8) = ( CONSTANTS(:,14).*CONSTANTS(:,20).*STATES(:,3)+ CONSTANTS(:,36).*STATES(:,12)) - ( CONSTANTS(:,35).*CONSTANTS(:,19).*STATES(:,8)+ CONSTANTS(:,15).*STATES(:,8));
    RATES(:,11) = ( CONSTANTS(:,29).*CONSTANTS(:,11).*STATES(:,4)+ CONSTANTS(:,33).*CONSTANTS(:,10).*STATES(:,5)+ CONSTANTS(:,38).*STATES(:,12)) - ( CONSTANTS(:,30).*STATES(:,11)+ CONSTANTS(:,34).*STATES(:,11)+ CONSTANTS(:,37).*STATES(:,11));
    RATES(:,12) = ( CONSTANTS(:,31).*CONSTANTS(:,20).*STATES(:,7)+ CONSTANTS(:,35).*CONSTANTS(:,19).*STATES(:,8)+ CONSTANTS(:,37).*STATES(:,11)) - ( CONSTANTS(:,32).*STATES(:,12)+ CONSTANTS(:,36).*STATES(:,12)+ CONSTANTS(:,38).*STATES(:,12));
   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
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