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 =13;
end
% There are a total of 8 entries in each of the rate and state variable arrays.
% There are a total of 55 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_ALGEBRAIC(:,1) = strpad('time in component environment (second)');
    LEGEND_STATES(:,1) = strpad('p in component p (dimensionless)');
    LEGEND_CONSTANTS(:,31) = strpad('v1 in component v1 (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('v2 in component v2 (dimensionless)');
    LEGEND_ALGEBRAIC(:,7) = strpad('v7 in component v7 (dimensionless)');
    LEGEND_VOI = strpad('tau in component normalised_constants (dimensionless)');
    LEGEND_STATES(:,2) = strpad('a in component a (dimensionless)');
    LEGEND_ALGEBRAIC(:,3) = strpad('v3 in component v3 (dimensionless)');
    LEGEND_CONSTANTS(:,49) = strpad('epsilon1 in component normalised_constants (dimensionless)');
    LEGEND_STATES(:,3) = strpad('c in component c (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('v4 in component v4 (dimensionless)');
    LEGEND_CONSTANTS(:,50) = strpad('epsilon2 in component normalised_constants (dimensionless)');
    LEGEND_STATES(:,4) = strpad('k in component k (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('v5 in component v5 (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('v6 in component v6 (dimensionless)');
    LEGEND_CONSTANTS(:,51) = strpad('epsilon3 in component normalised_constants (dimensionless)');
    LEGEND_STATES(:,5) = strpad('o in component o (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('v8 in component v8 (dimensionless)');
    LEGEND_CONSTANTS(:,52) = strpad('epsilon4 in component normalised_constants (dimensionless)');
    LEGEND_STATES(:,6) = strpad('n in component n (dimensionless)');
    LEGEND_ALGEBRAIC(:,11) = strpad('vresp in component vresp (dimensionless)');
    LEGEND_CONSTANTS(:,53) = strpad('epsilon5 in component normalised_constants (dimensionless)');
    LEGEND_STATES(:,7) = strpad('en in component en (dimensionless)');
    LEGEND_ALGEBRAIC(:,13) = strpad('vATP in component vATP (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('vANT in component vANT (dimensionless)');
    LEGEND_CONSTANTS(:,54) = strpad('epsilon6 in component normalised_constants (dimensionless)');
    LEGEND_STATES(:,8) = strpad('s in component s (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('vleak in component vleak (dimensionless)');
    LEGEND_CONSTANTS(:,55) = strpad('epsilon7 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,32) = strpad('beta2 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,33) = strpad('beta3 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,34) = strpad('beta4 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,35) = strpad('beta5 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,36) = strpad('delta_6 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,37) = strpad('beta6 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,38) = strpad('beta7 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,39) = strpad('beta8 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,40) = strpad('beta_ANT in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,41) = strpad('beta_leak in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,42) = strpad('beta_resp in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,43) = strpad('delta_r1 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,44) = strpad('delta_r2 in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,45) = strpad('beta_ATP in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,46) = strpad('delta_atp in component normalised_constants (dimensionless)');
    LEGEND_ALGEBRAIC(:,12) = strpad('en_crit in component en_crit (dimensionless)');
    LEGEND_CONSTANTS(:,47) = strpad('Kapp_dash in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,48) = strpad('delta_crit in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,1) = strpad('At in component normalised_constants (millimolar)');
    LEGEND_CONSTANTS(:,2) = strpad('Nt in component normalised_constants (millimolar)');
    LEGEND_CONSTANTS(:,3) = strpad('Pyr_bar in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('Cit_bar in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('AcCoA_bar in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('KG_bar in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('OAA_bar in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('k1 in component normalised_constants (micromolar_per_second)');
    LEGEND_CONSTANTS(:,9) = strpad('k2 in component normalised_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,10) = strpad('k3 in component normalised_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('k4 in component normalised_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('k5 in component normalised_constants (third_order_rate_constant)');
    LEGEND_CONSTANTS(:,13) = strpad('k6 in component normalised_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,14) = strpad('k7 in component normalised_constants (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,15) = strpad('k8 in component normalised_constants (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,16) = strpad('kresp in component normalised_constants (millimolar_per_second)');
    LEGEND_CONSTANTS(:,17) = strpad('kATP in component normalised_constants (millimolar_per_second)');
    LEGEND_CONSTANTS(:,18) = strpad('kANT in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('kleak in component normalised_constants (molar_per_millivolt_per_second)');
    LEGEND_CONSTANTS(:,20) = strpad('Keq in component normalised_constants (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('K in component normalised_constants (millimolar)');
    LEGEND_CONSTANTS(:,22) = strpad('alpha in component normalised_constants (per_millivolt)');
    LEGEND_CONSTANTS(:,23) = strpad('b in component normalised_constants (per_millimolar)');
    LEGEND_CONSTANTS(:,24) = strpad('delta_psi_m in component normalised_constants (millivolt)');
    LEGEND_CONSTANTS(:,25) = strpad('R in component normalised_constants (joule_per_mole_kelvin)');
    LEGEND_CONSTANTS(:,26) = strpad('T in component normalised_constants (kelvin)');
    LEGEND_CONSTANTS(:,27) = strpad('F in component normalised_constants (coulomb_per_mole)');
    LEGEND_CONSTANTS(:,28) = strpad('C in component normalised_constants (millimolar_per_millivolt)');
    LEGEND_CONSTANTS(:,29) = strpad('Kapp in component normalised_constants (per_millimolar)');
    LEGEND_CONSTANTS(:,30) = strpad('Pi in component normalised_constants (millimolar)');
    LEGEND_RATES(:,1) = strpad('d/dt p in component p (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt a in component a (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt c in component c (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt k in component k (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt o in component o (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt n in component n (dimensionless)');
    LEGEND_RATES(:,7) = strpad('d/dt en in component en (dimensionless)');
    LEGEND_RATES(:,8) = strpad('d/dt s in component s (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) = 0.154;
    STATES(:,2) = 0.063;
    STATES(:,3) = 0.44;
    STATES(:,4) = 0.225;
    STATES(:,5) = 0.005;
    STATES(:,6) = 0.856;
    STATES(:,7) = 3.536;
    STATES(:,8) = 150.0;
    CONSTANTS(:,1) = 4.160;
    CONSTANTS(:,2) = 1.070;
    CONSTANTS(:,3) = 0.161;
    CONSTANTS(:,4) = 0.460;
    CONSTANTS(:,5) = 0.105;
    CONSTANTS(:,6) = 0.146;
    CONSTANTS(:,7) = 0.004;
    CONSTANTS(:,8) = 38;
    CONSTANTS(:,9) = 152;
    CONSTANTS(:,10) = 57142;
    CONSTANTS(:,11) = 53;
    CONSTANTS(:,12) = 40;
    CONSTANTS(:,13) = 82361;
    CONSTANTS(:,14) = 3.2e-3;
    CONSTANTS(:,15) = 3.6;
    CONSTANTS(:,16) = 2.5;
    CONSTANTS(:,17) = 131.9;
    CONSTANTS(:,18) = 0.1;
    CONSTANTS(:,19) = 0.426;
    CONSTANTS(:,20) = 0.3975;
    CONSTANTS(:,21) = 2;
    CONSTANTS(:,22) = 0.100;
    CONSTANTS(:,23) = 0.004;
    CONSTANTS(:,24) = 150.0;
    CONSTANTS(:,25) = 8.314;
    CONSTANTS(:,26) = 298;
    CONSTANTS(:,27) = 96485;
    CONSTANTS(:,28) = 6.75e-06;
    CONSTANTS(:,29) = 4.4e-6;
    CONSTANTS(:,30) = 2.440;
    CONSTANTS(:,31) = 1.00000;
    CONSTANTS(:,32) =  (CONSTANTS(:,9)./CONSTANTS(:,8)).*CONSTANTS(:,2).*CONSTANTS(:,3);
    CONSTANTS(:,33) =  (CONSTANTS(:,10)./CONSTANTS(:,8)).*CONSTANTS(:,7).*CONSTANTS(:,5);
    CONSTANTS(:,34) =  (CONSTANTS(:,11)./CONSTANTS(:,8)).*CONSTANTS(:,2).*CONSTANTS(:,4);
    CONSTANTS(:,35) =  (CONSTANTS(:,12)./CONSTANTS(:,8)).*CONSTANTS(:,2).*CONSTANTS(:,1).*CONSTANTS(:,6);
    CONSTANTS(:,36) = CONSTANTS(:,6)./( CONSTANTS(:,7).*CONSTANTS(:,20));
    CONSTANTS(:,37) =  (CONSTANTS(:,13)./CONSTANTS(:,8)).*CONSTANTS(:,7);
    CONSTANTS(:,38) =  (CONSTANTS(:,14)./CONSTANTS(:,8)).*CONSTANTS(:,1).*CONSTANTS(:,3);
    CONSTANTS(:,39) =  (CONSTANTS(:,15)./CONSTANTS(:,8)).*CONSTANTS(:,7);
    CONSTANTS(:,40) =  (CONSTANTS(:,18)./CONSTANTS(:,8)).*CONSTANTS(:,1);
    CONSTANTS(:,41) =  (CONSTANTS(:,19)./CONSTANTS(:,8)).*CONSTANTS(:,24);
    CONSTANTS(:,42) = CONSTANTS(:,16)./CONSTANTS(:,8);
    CONSTANTS(:,43) = CONSTANTS(:,21)./CONSTANTS(:,2);
    CONSTANTS(:,44) =  CONSTANTS(:,22).*CONSTANTS(:,24);
    CONSTANTS(:,45) = CONSTANTS(:,17)./CONSTANTS(:,8);
    CONSTANTS(:,46) =  CONSTANTS(:,23).*CONSTANTS(:,1);
    CONSTANTS(:,47) =  CONSTANTS(:,29).*CONSTANTS(:,30);
    CONSTANTS(:,48) =  3.00000.*(( 1.20000.*CONSTANTS(:,27).*CONSTANTS(:,24))./( CONSTANTS(:,25).*CONSTANTS(:,26)));
    CONSTANTS(:,49) = CONSTANTS(:,5)./CONSTANTS(:,3);
    CONSTANTS(:,50) = CONSTANTS(:,4)./CONSTANTS(:,3);
    CONSTANTS(:,51) = CONSTANTS(:,6)./CONSTANTS(:,3);
    CONSTANTS(:,52) = CONSTANTS(:,7)./CONSTANTS(:,3);
    CONSTANTS(:,53) = CONSTANTS(:,2)./CONSTANTS(:,3);
    CONSTANTS(:,54) = CONSTANTS(:,1)./CONSTANTS(:,3);
    CONSTANTS(:,55) =  (CONSTANTS(:,24)./CONSTANTS(:,3)).*CONSTANTS(:,28);
    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) =  CONSTANTS(:,32).*STATES(:,1).*STATES(:,6);
    ALGEBRAIC(:,3) =  CONSTANTS(:,33).*STATES(:,5).*STATES(:,2);
    RATES(:,2) = (ALGEBRAIC(:,2) - ALGEBRAIC(:,3))./CONSTANTS(:,49);
    ALGEBRAIC(:,4) =  CONSTANTS(:,34).*STATES(:,3).*STATES(:,6);
    RATES(:,3) = (ALGEBRAIC(:,3) - ALGEBRAIC(:,4))./CONSTANTS(:,50);
    ALGEBRAIC(:,5) =  CONSTANTS(:,35).*STATES(:,4).*STATES(:,6).*(1.00000 - STATES(:,7));
    ALGEBRAIC(:,6) =  CONSTANTS(:,37).*(STATES(:,5) -  CONSTANTS(:,36).*STATES(:,4));
    RATES(:,4) = ((ALGEBRAIC(:,4)+ALGEBRAIC(:,6)) - ALGEBRAIC(:,5))./CONSTANTS(:,51);
    ALGEBRAIC(:,7) =  CONSTANTS(:,38).*STATES(:,1).*STATES(:,7);
    RATES(:,1) = CONSTANTS(:,31) - (ALGEBRAIC(:,2)+ALGEBRAIC(:,7));
    ALGEBRAIC(:,8) =  CONSTANTS(:,39).*STATES(:,5);
    RATES(:,5) = ((ALGEBRAIC(:,5)+ALGEBRAIC(:,7)) - (ALGEBRAIC(:,3)+ALGEBRAIC(:,8)+ALGEBRAIC(:,6)))./CONSTANTS(:,52);
    ALGEBRAIC(:,11) =  CONSTANTS(:,42).*((1.00000 - STATES(:,6))./((CONSTANTS(:,43)+1.00000) - STATES(:,6))).*(1.00000./(1.00000+exp( CONSTANTS(:,44).*(STATES(:,8) - 1.00000))));
    RATES(:,6) = (ALGEBRAIC(:,11) - (ALGEBRAIC(:,2)+ALGEBRAIC(:,4)+ 2.00000.*ALGEBRAIC(:,5)))./CONSTANTS(:,53);
    ALGEBRAIC(:,12) = CONSTANTS(:,47)./(CONSTANTS(:,47)+exp( -1.00000.*CONSTANTS(:,48).*STATES(:,8)));
    ALGEBRAIC(:,13) =  CONSTANTS(:,45).*(2.00000./(1.00000+exp( CONSTANTS(:,46).*(STATES(:,7) -  ALGEBRAIC(:,12).*STATES(:,8)))) - 1.00000);
    ALGEBRAIC(:,9) =  CONSTANTS(:,40).*STATES(:,7);
    RATES(:,7) = ((ALGEBRAIC(:,13)+ALGEBRAIC(:,5)) - (ALGEBRAIC(:,9)+ALGEBRAIC(:,7)))./CONSTANTS(:,54);
    ALGEBRAIC(:,10) =  CONSTANTS(:,41).*STATES(:,8);
    RATES(:,8) = ( 10.0000.*ALGEBRAIC(:,11) - ( 3.00000.*ALGEBRAIC(:,13)+ALGEBRAIC(:,10)+ALGEBRAIC(:,9)))./CONSTANTS(:,55);
   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) =  CONSTANTS(:,32).*STATES(:,1).*STATES(:,6);
    ALGEBRAIC(:,3) =  CONSTANTS(:,33).*STATES(:,5).*STATES(:,2);
    ALGEBRAIC(:,4) =  CONSTANTS(:,34).*STATES(:,3).*STATES(:,6);
    ALGEBRAIC(:,5) =  CONSTANTS(:,35).*STATES(:,4).*STATES(:,6).*(1.00000 - STATES(:,7));
    ALGEBRAIC(:,6) =  CONSTANTS(:,37).*(STATES(:,5) -  CONSTANTS(:,36).*STATES(:,4));
    ALGEBRAIC(:,7) =  CONSTANTS(:,38).*STATES(:,1).*STATES(:,7);
    ALGEBRAIC(:,8) =  CONSTANTS(:,39).*STATES(:,5);
    ALGEBRAIC(:,11) =  CONSTANTS(:,42).*((1.00000 - STATES(:,6))./((CONSTANTS(:,43)+1.00000) - STATES(:,6))).*(1.00000./(1.00000+exp( CONSTANTS(:,44).*(STATES(:,8) - 1.00000))));
    ALGEBRAIC(:,12) = CONSTANTS(:,47)./(CONSTANTS(:,47)+exp( -1.00000.*CONSTANTS(:,48).*STATES(:,8)));
    ALGEBRAIC(:,13) =  CONSTANTS(:,45).*(2.00000./(1.00000+exp( CONSTANTS(:,46).*(STATES(:,7) -  ALGEBRAIC(:,12).*STATES(:,8)))) - 1.00000);
    ALGEBRAIC(:,9) =  CONSTANTS(:,40).*STATES(:,7);
    ALGEBRAIC(:,10) =  CONSTANTS(:,41).*STATES(:,8);
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
    CONSTANTS = CONSTANTS_IN;
    STATES = STATES_IN;
    ALGEBRAIC = ALGEBRAIC_IN;
    global initialGuess_0;
    if (length(initialGuess_0) ~= 1), initialGuess_0 = 0.1;, end
    options = optimset('Display', 'off', 'TolX', 1E-6);
    if length(VOI) == 1
        residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
        ALGEBRAIC(:,1) = fsolve(residualfn, initialGuess_0, options);
        initialGuess_0 = ALGEBRAIC(:,1);
    else
        SET_ALGEBRAIC(:,1) = logical(1);
        for i=1:length(VOI)
            residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
            TEMP_ALGEBRAIC(:,1) = fsolve(residualfn, initialGuess_0, options);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
            initialGuess_0 = TEMP_ALGEBRAIC(:,1);
        end
    end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    ALGEBRAIC(:,1) = algebraicCandidate;
    resid = (VOI) - ( (CONSTANTS(:,8)./CONSTANTS(:,3)).*ALGEBRAIC(:,1));
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