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 =11;
end
% There are a total of 6 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_ALGEBRAIC(:,1) = strpad('N_x in component N_x (dimensionless)');
    LEGEND_CONSTANTS(:,1) = strpad('N_0 in component model_parameters (dimensionless)');
    LEGEND_VOI = strpad('x in component model_parameters (mm)');
    LEGEND_CONSTANTS(:,2) = strpad('ksh in component model_parameters (per_mm)');
    LEGEND_STATES(:,1) = strpad('F_DVR_v in component F_DVR_v (nl_min)');
    LEGEND_ALGEBRAIC(:,9) = strpad('Jv in component model_parameters (nl_min_mm)');
    LEGEND_STATES(:,2) = strpad('F_DVR_GLU in component F_DVR_GLU (pmol_min)');
    LEGEND_ALGEBRAIC(:,10) = strpad('JGLU in component JGLU (pmol_min_mm)');
    LEGEND_STATES(:,3) = strpad('F_DVR_LAC in component F_DVR_LAC (pmol_min)');
    LEGEND_ALGEBRAIC(:,11) = strpad('JLAC in component JLAC (pmol_min_mm)');
    LEGEND_STATES(:,4) = strpad('F_AVR_v in component F_AVR_v (nl_min)');
    LEGEND_ALGEBRAIC(:,8) = strpad('J_ABS_V in component J_ABS_V (nl_min_mm)');
    LEGEND_STATES(:,5) = strpad('F_AVR_GLU in component F_AVR_GLU (pmol_min)');
    LEGEND_ALGEBRAIC(:,7) = strpad('JGLY in component JGLY (pmol_min_mm)');
    LEGEND_STATES(:,6) = strpad('F_AVR_LAC in component F_AVR_LAC (pmol_min)');
    LEGEND_CONSTANTS(:,3) = strpad('PGLU in component JGLU (nl_min_mm)');
    LEGEND_CONSTANTS(:,4) = strpad('sigma_GLU in component JGLU (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('c_DVR_GLU in component c_DVR_GLU (millimolar)');
    LEGEND_ALGEBRAIC(:,5) = strpad('c_AVR_GLU in component c_AVR_GLU (millimolar)');
    LEGEND_CONSTANTS(:,5) = strpad('PLAC in component JLAC (nl_min_mm)');
    LEGEND_CONSTANTS(:,6) = strpad('sigma_LAC in component JLAC (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('c_AVR_LAC in component c_AVR_LAC (millimolar)');
    LEGEND_ALGEBRAIC(:,2) = strpad('c_DVR_LAC in component c_DVR_LAC (millimolar)');
    LEGEND_CONSTANTS(:,16) = strpad('Vmax in component JGLY (pmol_min_mm)');
    LEGEND_CONSTANTS(:,7) = strpad('Km in component JGLY (millimolar)');
    LEGEND_CONSTANTS(:,8) = strpad('GlyFract in component JGLY (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('F_DVR_G_0 in component model_parameters (pmol_min)');
    LEGEND_CONSTANTS(:,9) = strpad('L in component model_parameters (mm)');
    LEGEND_CONSTANTS(:,14) = strpad('kv in component kv (nl_min_mm)');
    LEGEND_CONSTANTS(:,10) = strpad('VolFract in component kv (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('F_DVR_V_0 in component model_parameters (nl_min)');
    LEGEND_CONSTANTS(:,11) = strpad('c_DVR_GLU_0 in component model_parameters (millimolar)');
    LEGEND_ALGEBRAIC(:,3) = strpad('x_L in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('b in component model_parameters (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt F_DVR_v in component F_DVR_v (nl_min)');
    LEGEND_RATES(:,2) = strpad('d/dt F_DVR_GLU in component F_DVR_GLU (pmol_min)');
    LEGEND_RATES(:,3) = strpad('d/dt F_DVR_LAC in component F_DVR_LAC (pmol_min)');
    LEGEND_RATES(:,4) = strpad('d/dt F_AVR_v in component F_AVR_v (nl_min)');
    LEGEND_RATES(:,5) = strpad('d/dt F_AVR_GLU in component F_AVR_GLU (pmol_min)');
    LEGEND_RATES(:,6) = strpad('d/dt F_AVR_LAC in component F_AVR_LAC (pmol_min)');
    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) = 128.0;
    CONSTANTS(:,2) = 1.213;
    STATES(:,1) = 3.75;
    STATES(:,2) = 0.01;
    STATES(:,3) = 0.01;
    STATES(:,4) = 0.01;
    STATES(:,5) = 0.01;
    STATES(:,6) = 0.01;
    CONSTANTS(:,3) = 1.2;
    CONSTANTS(:,4) = 0.5;
    CONSTANTS(:,5) = 33.93;
    CONSTANTS(:,6) = 0.5;
    CONSTANTS(:,7) = 0.1;
    CONSTANTS(:,8) = 0.2;
    CONSTANTS(:,9) = 4.0;
    CONSTANTS(:,10) = 0.3;
    CONSTANTS(:,11) = 10.0;
    CONSTANTS(:,12) = 4.0;
    CONSTANTS(:,13) =  3.75000.*CONSTANTS(:,1);
    CONSTANTS(:,14) =  (CONSTANTS(:,2)./( CONSTANTS(:,1).*(1.00000 - exp( - ( CONSTANTS(:,2).*CONSTANTS(:,9)))))).*CONSTANTS(:,10).*CONSTANTS(:,13);
    CONSTANTS(:,15) =  CONSTANTS(:,13).*CONSTANTS(:,11);
    CONSTANTS(:,16) =  (CONSTANTS(:,2)./( CONSTANTS(:,1).*(1.00000 - exp( - ( CONSTANTS(:,2).*CONSTANTS(:,9)))))).*( CONSTANTS(:,8).*CONSTANTS(:,15));
    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(:,1) =  CONSTANTS(:,1).*exp( - ( CONSTANTS(:,2).*VOI));
    ALGEBRAIC(:,9) =  0.300000.*(STATES(:,1)./( CONSTANTS(:,1).*CONSTANTS(:,12))).*ALGEBRAIC(:,1);
    RATES(:,1) =  - (ALGEBRAIC(:,9)+ CONSTANTS(:,2).*STATES(:,1));
    ALGEBRAIC(:,8) =  CONSTANTS(:,14).*ALGEBRAIC(:,1);
    RATES(:,4) = ALGEBRAIC(:,9)+ CONSTANTS(:,2).*STATES(:,1)+ALGEBRAIC(:,8);
    ALGEBRAIC(:,4) = STATES(:,2)./STATES(:,1);
    ALGEBRAIC(:,5) = STATES(:,5)./STATES(:,4);
    ALGEBRAIC(:,10) =  ALGEBRAIC(:,1).*CONSTANTS(:,3).*(ALGEBRAIC(:,4) - ALGEBRAIC(:,5))+ (1.00000 - CONSTANTS(:,4)).*ALGEBRAIC(:,9).*((ALGEBRAIC(:,4)+ALGEBRAIC(:,5))./2.00000);
    RATES(:,2) =  - (ALGEBRAIC(:,10)+ CONSTANTS(:,2).*STATES(:,2));
    ALGEBRAIC(:,6) = STATES(:,6)./STATES(:,4);
    ALGEBRAIC(:,11) =  ALGEBRAIC(:,1).*CONSTANTS(:,5).*(ALGEBRAIC(:,4) - ALGEBRAIC(:,6))+ (1.00000 - CONSTANTS(:,6)).*ALGEBRAIC(:,9).*((ALGEBRAIC(:,4)+ALGEBRAIC(:,6))./2.00000);
    RATES(:,3) =  - (ALGEBRAIC(:,11)+ CONSTANTS(:,2).*STATES(:,3));
    ALGEBRAIC(:,7) =  ALGEBRAIC(:,1).*(( CONSTANTS(:,16).*ALGEBRAIC(:,5))./(CONSTANTS(:,7)+ALGEBRAIC(:,5)));
    RATES(:,5) = (ALGEBRAIC(:,10)+ CONSTANTS(:,2).*STATES(:,2)) - ALGEBRAIC(:,7);
    RATES(:,6) = ALGEBRAIC(:,11)+ CONSTANTS(:,2).*STATES(:,3)+ 2.00000.*ALGEBRAIC(:,7);
   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(:,1) =  CONSTANTS(:,1).*exp( - ( CONSTANTS(:,2).*VOI));
    ALGEBRAIC(:,9) =  0.300000.*(STATES(:,1)./( CONSTANTS(:,1).*CONSTANTS(:,12))).*ALGEBRAIC(:,1);
    ALGEBRAIC(:,8) =  CONSTANTS(:,14).*ALGEBRAIC(:,1);
    ALGEBRAIC(:,4) = STATES(:,2)./STATES(:,1);
    ALGEBRAIC(:,5) = STATES(:,5)./STATES(:,4);
    ALGEBRAIC(:,10) =  ALGEBRAIC(:,1).*CONSTANTS(:,3).*(ALGEBRAIC(:,4) - ALGEBRAIC(:,5))+ (1.00000 - CONSTANTS(:,4)).*ALGEBRAIC(:,9).*((ALGEBRAIC(:,4)+ALGEBRAIC(:,5))./2.00000);
    ALGEBRAIC(:,6) = STATES(:,6)./STATES(:,4);
    ALGEBRAIC(:,11) =  ALGEBRAIC(:,1).*CONSTANTS(:,5).*(ALGEBRAIC(:,4) - ALGEBRAIC(:,6))+ (1.00000 - CONSTANTS(:,6)).*ALGEBRAIC(:,9).*((ALGEBRAIC(:,4)+ALGEBRAIC(:,6))./2.00000);
    ALGEBRAIC(:,7) =  ALGEBRAIC(:,1).*(( CONSTANTS(:,16).*ALGEBRAIC(:,5))./(CONSTANTS(:,7)+ALGEBRAIC(:,5)));
    ALGEBRAIC(:,2) = STATES(:,3)./STATES(:,1);
    ALGEBRAIC(:,3) = VOI./CONSTANTS(:,9);
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