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 =22;
end
% There are a total of 4 entries in each of the rate and state variable arrays.
% There are a total of 26 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('t in component GLUT2_BG (second)');
    LEGEND_CONSTANTS(:,1) = strpad('R in component params_BG (J_per_K_mol)');
    LEGEND_CONSTANTS(:,2) = strpad('T in component params_BG (kelvin)');
    LEGEND_CONSTANTS(:,3) = strpad('K_Ai in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,24) = strpad('q_init_Ai in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,4) = strpad('K_Ao in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,21) = strpad('q_init_Ao in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,5) = strpad('K_1 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,6) = strpad('q_init_1 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,7) = strpad('K_2 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,8) = strpad('q_init_2 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,9) = strpad('K_3 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,10) = strpad('q_init_3 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,11) = strpad('K_4 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,12) = strpad('q_init_4 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,13) = strpad('kappa_r1 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,14) = strpad('kappa_r2 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,15) = strpad('kappa_r3 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,16) = strpad('kappa_r4 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,26) = strpad('mu_Ai in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,17) = strpad('v_Ai in component GLUT2_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,23) = strpad('mu_Ao in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,18) = strpad('v_Ao in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,1) = strpad('mu_1 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,19) = strpad('v_1 in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,2) = strpad('mu_2 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,20) = strpad('v_2 in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,3) = strpad('mu_3 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,21) = strpad('v_3 in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,4) = strpad('mu_4 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,22) = strpad('v_4 in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,5) = strpad('A_f_r1 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,7) = strpad('v_r1 in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,6) = strpad('A_r_r1 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,8) = strpad('A_f_r2 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,10) = strpad('v_r2 in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,9) = strpad('A_r_r2 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,11) = strpad('A_f_r3 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,15) = strpad('v_r3 in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,13) = strpad('A_r_r3 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,12) = strpad('A_f_r4 in component GLUT2_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,16) = strpad('v_r4 in component GLUT2_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,14) = strpad('A_r_r4 in component GLUT2_BG (J_per_mol)');
    LEGEND_CONSTANTS(:,25) = strpad('q_Ai in component GLUT2_BG (fmol)');
    LEGEND_CONSTANTS(:,22) = strpad('q_Ao in component GLUT2_BG (fmol)');
    LEGEND_STATES(:,1) = strpad('q_1 in component GLUT2_BG (fmol)');
    LEGEND_STATES(:,2) = strpad('q_2 in component GLUT2_BG (fmol)');
    LEGEND_STATES(:,3) = strpad('q_3 in component GLUT2_BG (fmol)');
    LEGEND_STATES(:,4) = strpad('q_4 in component GLUT2_BG (fmol)');
    LEGEND_CONSTANTS(:,17) = strpad('V_i in component params_BG (pL)');
    LEGEND_CONSTANTS(:,18) = strpad('g_i in component params_BG (mM)');
    LEGEND_CONSTANTS(:,19) = strpad('g_o in component params_BG (mM)');
    LEGEND_CONSTANTS(:,20) = strpad('V_o in component params_BG (pL)');
    LEGEND_RATES(:,1) = strpad('d/dt q_1 in component GLUT2_BG (fmol)');
    LEGEND_RATES(:,2) = strpad('d/dt q_2 in component GLUT2_BG (fmol)');
    LEGEND_RATES(:,3) = strpad('d/dt q_3 in component GLUT2_BG (fmol)');
    LEGEND_RATES(:,4) = strpad('d/dt q_4 in component GLUT2_BG (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) = 8.31;
    CONSTANTS(:,2) = 273.15;
    CONSTANTS(:,3) = 149.65;
    CONSTANTS(:,4) = 149.65;
    CONSTANTS(:,5) = 33.20;
    CONSTANTS(:,6) = 0.0017;
    CONSTANTS(:,7) = 4.25e+03;
    CONSTANTS(:,8) = 0.0017;
    CONSTANTS(:,9) = 344.59;
    CONSTANTS(:,10) = 0.0017;
    CONSTANTS(:,11) = 1.99;
    CONSTANTS(:,12) = 0.0017;
    CONSTANTS(:,13) = 0.36;
    CONSTANTS(:,14) = 0.26;
    CONSTANTS(:,15) = 1.01e+05;
    CONSTANTS(:,16) = 1.01e+04;
    CONSTANTS(:,17) = 0.09;
    CONSTANTS(:,18) = 10;
    CONSTANTS(:,19) = 1e-5;
    CONSTANTS(:,20) = 0.09;
    CONSTANTS(:,21) =  CONSTANTS(:,19).*CONSTANTS(:,20);
    CONSTANTS(:,22) = CONSTANTS(:,21);
    CONSTANTS(:,23) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,4).*CONSTANTS(:,22));
    CONSTANTS(:,24) =  CONSTANTS(:,18).*CONSTANTS(:,17);
    CONSTANTS(:,25) = CONSTANTS(:,24);
    CONSTANTS(:,26) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,3).*CONSTANTS(:,25));
    STATES(:,1) = CONSTANTS(:,6);
    STATES(:,2) = CONSTANTS(:,8);
    STATES(:,3) = CONSTANTS(:,10);
    STATES(:,4) = CONSTANTS(:,12);
    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(:,4) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,11).*STATES(:,4));
    ALGEBRAIC(:,5) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,1) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,5).*STATES(:,1));
    ALGEBRAIC(:,6) = ALGEBRAIC(:,1);
    ALGEBRAIC(:,7) =  CONSTANTS(:,13).*(exp(ALGEBRAIC(:,5)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,6)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,11) = CONSTANTS(:,23)+ALGEBRAIC(:,1);
    ALGEBRAIC(:,2) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,7).*STATES(:,2));
    ALGEBRAIC(:,13) = ALGEBRAIC(:,2);
    ALGEBRAIC(:,15) =  CONSTANTS(:,15).*(exp(ALGEBRAIC(:,11)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,13)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,19) = ALGEBRAIC(:,7) - ALGEBRAIC(:,15);
    RATES(:,1) = ALGEBRAIC(:,19);
    ALGEBRAIC(:,8) = ALGEBRAIC(:,2);
    ALGEBRAIC(:,3) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,9).*STATES(:,3));
    ALGEBRAIC(:,9) = ALGEBRAIC(:,3);
    ALGEBRAIC(:,10) =  CONSTANTS(:,14).*(exp(ALGEBRAIC(:,8)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,9)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,20) = ALGEBRAIC(:,15) - ALGEBRAIC(:,10);
    RATES(:,2) = ALGEBRAIC(:,20);
    ALGEBRAIC(:,12) = ALGEBRAIC(:,3);
    ALGEBRAIC(:,14) = CONSTANTS(:,26)+ALGEBRAIC(:,4);
    ALGEBRAIC(:,16) =  CONSTANTS(:,16).*(exp(ALGEBRAIC(:,12)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,14)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,21) = ALGEBRAIC(:,10) - ALGEBRAIC(:,16);
    RATES(:,3) = ALGEBRAIC(:,21);
    ALGEBRAIC(:,22) = ALGEBRAIC(:,16) - ALGEBRAIC(:,7);
    RATES(:,4) = ALGEBRAIC(:,22);
   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(:,4) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,11).*STATES(:,4));
    ALGEBRAIC(:,5) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,1) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,5).*STATES(:,1));
    ALGEBRAIC(:,6) = ALGEBRAIC(:,1);
    ALGEBRAIC(:,7) =  CONSTANTS(:,13).*(exp(ALGEBRAIC(:,5)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,6)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,11) = CONSTANTS(:,23)+ALGEBRAIC(:,1);
    ALGEBRAIC(:,2) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,7).*STATES(:,2));
    ALGEBRAIC(:,13) = ALGEBRAIC(:,2);
    ALGEBRAIC(:,15) =  CONSTANTS(:,15).*(exp(ALGEBRAIC(:,11)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,13)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,19) = ALGEBRAIC(:,7) - ALGEBRAIC(:,15);
    ALGEBRAIC(:,8) = ALGEBRAIC(:,2);
    ALGEBRAIC(:,3) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,9).*STATES(:,3));
    ALGEBRAIC(:,9) = ALGEBRAIC(:,3);
    ALGEBRAIC(:,10) =  CONSTANTS(:,14).*(exp(ALGEBRAIC(:,8)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,9)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,20) = ALGEBRAIC(:,15) - ALGEBRAIC(:,10);
    ALGEBRAIC(:,12) = ALGEBRAIC(:,3);
    ALGEBRAIC(:,14) = CONSTANTS(:,26)+ALGEBRAIC(:,4);
    ALGEBRAIC(:,16) =  CONSTANTS(:,16).*(exp(ALGEBRAIC(:,12)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,14)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,21) = ALGEBRAIC(:,10) - ALGEBRAIC(:,16);
    ALGEBRAIC(:,22) = ALGEBRAIC(:,16) - ALGEBRAIC(:,7);
    ALGEBRAIC(:,17) = ALGEBRAIC(:,16);
    ALGEBRAIC(:,18) =  - ALGEBRAIC(:,15);
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