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 =54;
end
% There are a total of 7 entries in each of the rate and state variable arrays.
% There are a total of 46 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 params_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('F in component params_BG (C_per_mol)');
    LEGEND_CONSTANTS(:,4) = strpad('K_Nai in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,39) = strpad('q_Nai in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,5) = strpad('K_Nao in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,41) = strpad('q_Nao in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,6) = strpad('K_Glci in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,43) = strpad('q_Glci in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,7) = strpad('K_Glco in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,45) = strpad('q_Glco in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,8) = strpad('K_1 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,9) = strpad('q_init_1 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,10) = strpad('K_2 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,11) = strpad('q_init_2 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,12) = strpad('K_3 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,13) = strpad('q_init_3 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,14) = strpad('K_4 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,15) = strpad('q_init_4 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,16) = strpad('K_5 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,17) = strpad('q_init_5 in component params_BG (fmol)');
    LEGEND_CONSTANTS(:,18) = strpad('K_6 in component params_BG (per_fmol)');
    LEGEND_CONSTANTS(:,19) = strpad('q_init_6 in component params_BG (fmol)');
    LEGEND_ALGEBRAIC(:,25) = strpad('V0_Vm in component params_BG (volt)');
    LEGEND_CONSTANTS(:,20) = strpad('z_zf1 in component params_BG (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('z_zr1 in component params_BG (dimensionless)');
    LEGEND_CONSTANTS(:,22) = strpad('z_zf6 in component params_BG (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('z_zr6 in component params_BG (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('kappa_r1 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,25) = strpad('kappa_r2 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,26) = strpad('kappa_r3 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,27) = strpad('kappa_r4 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,28) = strpad('kappa_r5 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,29) = strpad('kappa_r6 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,30) = strpad('kappa_r7 in component params_BG (fmol_per_s)');
    LEGEND_CONSTANTS(:,40) = strpad('mu_Nai in component SGLT1_BG (J_per_mol)');
    LEGEND_CONSTANTS(:,42) = strpad('mu_Nao in component SGLT1_BG (J_per_mol)');
    LEGEND_CONSTANTS(:,44) = strpad('mu_Glci in component SGLT1_BG (J_per_mol)');
    LEGEND_CONSTANTS(:,46) = strpad('mu_Glco in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,1) = strpad('mu_1 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,47) = strpad('v_1 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,3) = strpad('mu_2 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,39) = strpad('v_2 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,4) = strpad('mu_3 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,14) = strpad('v_3 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,5) = strpad('mu_4 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,18) = strpad('v_4 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,6) = strpad('mu_5 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,26) = strpad('v_5 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,7) = strpad('mu_6 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,48) = strpad('v_6 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,27) = strpad('V_Vm in component SGLT1_BG (volt)');
    LEGEND_ALGEBRAIC(:,28) = strpad('V_zf1 in component SGLT1_BG (volt)');
    LEGEND_ALGEBRAIC(:,41) = strpad('I_zf1 in component SGLT1_BG (fA)');
    LEGEND_ALGEBRAIC(:,29) = strpad('mu_zf1 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,40) = strpad('v_zf1 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,30) = strpad('V_zr1 in component SGLT1_BG (volt)');
    LEGEND_ALGEBRAIC(:,43) = strpad('I_zr1 in component SGLT1_BG (fA)');
    LEGEND_ALGEBRAIC(:,31) = strpad('mu_zr1 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,42) = strpad('v_zr1 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,32) = strpad('V_zf6 in component SGLT1_BG (volt)');
    LEGEND_ALGEBRAIC(:,51) = strpad('I_zf6 in component SGLT1_BG (fA)');
    LEGEND_ALGEBRAIC(:,33) = strpad('mu_zf6 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,49) = strpad('v_zf6 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,34) = strpad('V_zr6 in component SGLT1_BG (volt)');
    LEGEND_ALGEBRAIC(:,53) = strpad('I_zr6 in component SGLT1_BG (fA)');
    LEGEND_ALGEBRAIC(:,35) = strpad('mu_zr6 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,52) = strpad('v_zr6 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,36) = strpad('A_f_r1 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,38) = strpad('v_r1 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,37) = strpad('A_r_r1 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,8) = strpad('A_f_r2 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,10) = strpad('v_r2 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,9) = strpad('A_r_r2 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,11) = strpad('A_f_r3 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,13) = strpad('v_r3 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,12) = strpad('A_r_r3 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,15) = strpad('A_f_r4 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,17) = strpad('v_r4 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,16) = strpad('A_r_r4 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,19) = strpad('A_f_r5 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,21) = strpad('v_r5 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,20) = strpad('A_r_r5 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,44) = strpad('A_f_r6 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,46) = strpad('v_r6 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,45) = strpad('A_r_r6 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,22) = strpad('A_f_r7 in component SGLT1_BG (J_per_mol)');
    LEGEND_ALGEBRAIC(:,24) = strpad('v_r7 in component SGLT1_BG (fmol_per_s)');
    LEGEND_ALGEBRAIC(:,23) = strpad('A_r_r7 in component SGLT1_BG (J_per_mol)');
    LEGEND_STATES(:,1) = strpad('q_1 in component SGLT1_BG (fmol)');
    LEGEND_STATES(:,2) = strpad('q_2 in component SGLT1_BG (fmol)');
    LEGEND_STATES(:,3) = strpad('q_3 in component SGLT1_BG (fmol)');
    LEGEND_STATES(:,4) = strpad('q_4 in component SGLT1_BG (fmol)');
    LEGEND_STATES(:,5) = strpad('q_5 in component SGLT1_BG (fmol)');
    LEGEND_STATES(:,6) = strpad('q_6 in component SGLT1_BG (fmol)');
    LEGEND_ALGEBRAIC(:,54) = strpad('Ii in component SGLT1_BG (fA)');
    LEGEND_ALGEBRAIC(:,50) = strpad('I_ss in component SGLT1_BG (fA)');
    LEGEND_CONSTANTS(:,31) = strpad('Nai in component params_BG (mM)');
    LEGEND_CONSTANTS(:,32) = strpad('Nao in component params_BG (mM)');
    LEGEND_CONSTANTS(:,33) = strpad('Glci in component params_BG (mM)');
    LEGEND_CONSTANTS(:,34) = strpad('Glco in component params_BG (mM)');
    LEGEND_CONSTANTS(:,35) = strpad('V_i in component params_BG (pL)');
    LEGEND_CONSTANTS(:,36) = strpad('V_o in component params_BG (pL)');
    LEGEND_STATES(:,7) = strpad('V_E in component params_BG (volt)');
    LEGEND_CONSTANTS(:,37) = strpad('test_volt in component params_BG (volt)');
    LEGEND_ALGEBRAIC(:,2) = strpad('rate_VE in component params_BG (volt_per_s)');
    LEGEND_CONSTANTS(:,38) = strpad('dt in component params_BG (second)');
    LEGEND_RATES(:,1) = strpad('d/dt q_1 in component SGLT1_BG (fmol)');
    LEGEND_RATES(:,2) = strpad('d/dt q_2 in component SGLT1_BG (fmol)');
    LEGEND_RATES(:,3) = strpad('d/dt q_3 in component SGLT1_BG (fmol)');
    LEGEND_RATES(:,4) = strpad('d/dt q_4 in component SGLT1_BG (fmol)');
    LEGEND_RATES(:,5) = strpad('d/dt q_5 in component SGLT1_BG (fmol)');
    LEGEND_RATES(:,6) = strpad('d/dt q_6 in component SGLT1_BG (fmol)');
    LEGEND_RATES(:,7) = strpad('d/dt V_E in component params_BG (volt)');
    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) = 293;
    CONSTANTS(:,3) = 96485;
    CONSTANTS(:,4) = 3.216e-08;
    CONSTANTS(:,5) = 3.216e-08;
    CONSTANTS(:,6) = 4.848e-06;
    CONSTANTS(:,7) = 4.848e-06;
    CONSTANTS(:,8) = 2.235;
    CONSTANTS(:,9) = 13.7727015449461;
    CONSTANTS(:,10) = 10.437;
    CONSTANTS(:,11) = 72.295598813025;
    CONSTANTS(:,12) = 8.602;
    CONSTANTS(:,13) = 0.16701061319503;
    CONSTANTS(:,14) = 8.602;
    CONSTANTS(:,15) = 0.233806833618937;
    CONSTANTS(:,16) = 28.628;
    CONSTANTS(:,17) = 1.73527350102727;
    CONSTANTS(:,18) = 0.192;
    CONSTANTS(:,19) = 11.4302815603453;
    CONSTANTS(:,20) = 0.3;
    CONSTANTS(:,21) = 0.3;
    CONSTANTS(:,22) = 0.7;
    CONSTANTS(:,23) = 0.7;
    CONSTANTS(:,24) = 47.905;
    CONSTANTS(:,25) = 2.325;
    CONSTANTS(:,26) = 5.812;
    CONSTANTS(:,27) = 92.998;
    CONSTANTS(:,28) = 0.349;
    CONSTANTS(:,29) = 15.661;
    CONSTANTS(:,30) = 0.029;
    CONSTANTS(:,31) = 20;
    CONSTANTS(:,32) = 100;
    CONSTANTS(:,33) = 10e-3;
    CONSTANTS(:,34) = 1e-12;
    CONSTANTS(:,35) = 8.5e5;
    CONSTANTS(:,36) = 8.5e5;
    STATES(:,7) = -0.05;
    CONSTANTS(:,37) = 0;
    CONSTANTS(:,38) = 1e-3;
    CONSTANTS(:,39) =  CONSTANTS(:,31).*CONSTANTS(:,35);
    CONSTANTS(:,40) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,4).*CONSTANTS(:,39));
    CONSTANTS(:,41) =  CONSTANTS(:,32).*CONSTANTS(:,36);
    CONSTANTS(:,42) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,5).*CONSTANTS(:,41));
    CONSTANTS(:,43) =  CONSTANTS(:,33).*CONSTANTS(:,35);
    CONSTANTS(:,44) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,6).*CONSTANTS(:,43));
    CONSTANTS(:,45) =  CONSTANTS(:,34).*CONSTANTS(:,36);
    CONSTANTS(:,46) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,7).*CONSTANTS(:,45));
    STATES(:,1) = CONSTANTS(:,9);
    STATES(:,2) = CONSTANTS(:,11);
    STATES(:,3) = CONSTANTS(:,13);
    STATES(:,4) = CONSTANTS(:,15);
    STATES(:,5) = CONSTANTS(:,17);
    STATES(:,6) = CONSTANTS(:,19);
    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) = piecewise({VOI<1.20475, 0.00000 , VOI>=1.20475&VOI<1.20475+CONSTANTS(:,38), (CONSTANTS(:,37)+0.0500000)./CONSTANTS(:,38) , VOI>=1.20475+CONSTANTS(:,38)&VOI<2.98475, 0.00000 , VOI>=2.98475&VOI<2.98475+CONSTANTS(:,38),  - (CONSTANTS(:,37)+0.0500000)./CONSTANTS(:,38) }, 0.00000);
    RATES(:,7) = ALGEBRAIC(:,2);
    ALGEBRAIC(:,3) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,10).*STATES(:,2));
    ALGEBRAIC(:,8) = CONSTANTS(:,46)+ALGEBRAIC(:,3);
    ALGEBRAIC(:,4) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,12).*STATES(:,3));
    ALGEBRAIC(:,9) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,10) =  CONSTANTS(:,25).*(exp(ALGEBRAIC(:,8)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,9)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,11) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,5) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,14).*STATES(:,4));
    ALGEBRAIC(:,12) = ALGEBRAIC(:,5);
    ALGEBRAIC(:,13) =  CONSTANTS(:,26).*(exp(ALGEBRAIC(:,11)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,12)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,14) = ALGEBRAIC(:,10) - ALGEBRAIC(:,13);
    RATES(:,3) = ALGEBRAIC(:,14);
    ALGEBRAIC(:,15) = ALGEBRAIC(:,5);
    ALGEBRAIC(:,6) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,16).*STATES(:,5));
    ALGEBRAIC(:,16) = CONSTANTS(:,44)+ALGEBRAIC(:,6);
    ALGEBRAIC(:,17) =  CONSTANTS(:,27).*(exp(ALGEBRAIC(:,15)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,16)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,18) = ALGEBRAIC(:,13) - ALGEBRAIC(:,17);
    RATES(:,4) = ALGEBRAIC(:,18);
    ALGEBRAIC(:,19) = ALGEBRAIC(:,6);
    ALGEBRAIC(:,7) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,18).*STATES(:,6));
    ALGEBRAIC(:,20) =  2.00000.*CONSTANTS(:,40)+ALGEBRAIC(:,7);
    ALGEBRAIC(:,21) =  CONSTANTS(:,28).*(exp(ALGEBRAIC(:,19)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,20)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,22) = ALGEBRAIC(:,3);
    ALGEBRAIC(:,23) = ALGEBRAIC(:,6);
    ALGEBRAIC(:,24) =  CONSTANTS(:,30).*(exp(ALGEBRAIC(:,22)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,23)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,26) = (ALGEBRAIC(:,17) - ALGEBRAIC(:,21))+ALGEBRAIC(:,24);
    RATES(:,5) = ALGEBRAIC(:,26);
    ALGEBRAIC(:,1) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,8).*STATES(:,1));
    ALGEBRAIC(:,25) = STATES(:,7);
    ALGEBRAIC(:,27) = ALGEBRAIC(:,25);
    ALGEBRAIC(:,28) = ALGEBRAIC(:,27);
    ALGEBRAIC(:,29) =  CONSTANTS(:,20).*CONSTANTS(:,3).*ALGEBRAIC(:,28);
    ALGEBRAIC(:,36) = ( 2.00000.*CONSTANTS(:,42)+ALGEBRAIC(:,1)) - ALGEBRAIC(:,29);
    ALGEBRAIC(:,30) = ALGEBRAIC(:,27);
    ALGEBRAIC(:,31) =  CONSTANTS(:,21).*CONSTANTS(:,3).*ALGEBRAIC(:,30);
    ALGEBRAIC(:,37) = ALGEBRAIC(:,3)+ALGEBRAIC(:,31);
    ALGEBRAIC(:,38) =  CONSTANTS(:,24).*(exp(ALGEBRAIC(:,36)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,37)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,39) = (ALGEBRAIC(:,38) - ALGEBRAIC(:,10)) - ALGEBRAIC(:,24);
    RATES(:,2) = ALGEBRAIC(:,39);
    ALGEBRAIC(:,32) = ALGEBRAIC(:,27);
    ALGEBRAIC(:,33) =  CONSTANTS(:,22).*CONSTANTS(:,3).*ALGEBRAIC(:,32);
    ALGEBRAIC(:,44) = ALGEBRAIC(:,7) - ALGEBRAIC(:,33);
    ALGEBRAIC(:,34) = ALGEBRAIC(:,27);
    ALGEBRAIC(:,35) =  CONSTANTS(:,23).*CONSTANTS(:,3).*ALGEBRAIC(:,34);
    ALGEBRAIC(:,45) = ALGEBRAIC(:,1)+ALGEBRAIC(:,35);
    ALGEBRAIC(:,46) =  CONSTANTS(:,29).*(exp(ALGEBRAIC(:,44)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,45)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,47) =  - ALGEBRAIC(:,38)+ALGEBRAIC(:,46);
    RATES(:,1) = ALGEBRAIC(:,47);
    ALGEBRAIC(:,48) = ALGEBRAIC(:,21) - ALGEBRAIC(:,46);
    RATES(:,6) = ALGEBRAIC(:,48);
   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) = piecewise({VOI<1.20475, 0.00000 , VOI>=1.20475&VOI<1.20475+CONSTANTS(:,38), (CONSTANTS(:,37)+0.0500000)./CONSTANTS(:,38) , VOI>=1.20475+CONSTANTS(:,38)&VOI<2.98475, 0.00000 , VOI>=2.98475&VOI<2.98475+CONSTANTS(:,38),  - (CONSTANTS(:,37)+0.0500000)./CONSTANTS(:,38) }, 0.00000);
    ALGEBRAIC(:,3) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,10).*STATES(:,2));
    ALGEBRAIC(:,8) = CONSTANTS(:,46)+ALGEBRAIC(:,3);
    ALGEBRAIC(:,4) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,12).*STATES(:,3));
    ALGEBRAIC(:,9) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,10) =  CONSTANTS(:,25).*(exp(ALGEBRAIC(:,8)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,9)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,11) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,5) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,14).*STATES(:,4));
    ALGEBRAIC(:,12) = ALGEBRAIC(:,5);
    ALGEBRAIC(:,13) =  CONSTANTS(:,26).*(exp(ALGEBRAIC(:,11)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,12)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,14) = ALGEBRAIC(:,10) - ALGEBRAIC(:,13);
    ALGEBRAIC(:,15) = ALGEBRAIC(:,5);
    ALGEBRAIC(:,6) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,16).*STATES(:,5));
    ALGEBRAIC(:,16) = CONSTANTS(:,44)+ALGEBRAIC(:,6);
    ALGEBRAIC(:,17) =  CONSTANTS(:,27).*(exp(ALGEBRAIC(:,15)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,16)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,18) = ALGEBRAIC(:,13) - ALGEBRAIC(:,17);
    ALGEBRAIC(:,19) = ALGEBRAIC(:,6);
    ALGEBRAIC(:,7) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,18).*STATES(:,6));
    ALGEBRAIC(:,20) =  2.00000.*CONSTANTS(:,40)+ALGEBRAIC(:,7);
    ALGEBRAIC(:,21) =  CONSTANTS(:,28).*(exp(ALGEBRAIC(:,19)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,20)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,22) = ALGEBRAIC(:,3);
    ALGEBRAIC(:,23) = ALGEBRAIC(:,6);
    ALGEBRAIC(:,24) =  CONSTANTS(:,30).*(exp(ALGEBRAIC(:,22)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,23)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,26) = (ALGEBRAIC(:,17) - ALGEBRAIC(:,21))+ALGEBRAIC(:,24);
    ALGEBRAIC(:,1) =  CONSTANTS(:,1).*CONSTANTS(:,2).*log( CONSTANTS(:,8).*STATES(:,1));
    ALGEBRAIC(:,25) = STATES(:,7);
    ALGEBRAIC(:,27) = ALGEBRAIC(:,25);
    ALGEBRAIC(:,28) = ALGEBRAIC(:,27);
    ALGEBRAIC(:,29) =  CONSTANTS(:,20).*CONSTANTS(:,3).*ALGEBRAIC(:,28);
    ALGEBRAIC(:,36) = ( 2.00000.*CONSTANTS(:,42)+ALGEBRAIC(:,1)) - ALGEBRAIC(:,29);
    ALGEBRAIC(:,30) = ALGEBRAIC(:,27);
    ALGEBRAIC(:,31) =  CONSTANTS(:,21).*CONSTANTS(:,3).*ALGEBRAIC(:,30);
    ALGEBRAIC(:,37) = ALGEBRAIC(:,3)+ALGEBRAIC(:,31);
    ALGEBRAIC(:,38) =  CONSTANTS(:,24).*(exp(ALGEBRAIC(:,36)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,37)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,39) = (ALGEBRAIC(:,38) - ALGEBRAIC(:,10)) - ALGEBRAIC(:,24);
    ALGEBRAIC(:,32) = ALGEBRAIC(:,27);
    ALGEBRAIC(:,33) =  CONSTANTS(:,22).*CONSTANTS(:,3).*ALGEBRAIC(:,32);
    ALGEBRAIC(:,44) = ALGEBRAIC(:,7) - ALGEBRAIC(:,33);
    ALGEBRAIC(:,34) = ALGEBRAIC(:,27);
    ALGEBRAIC(:,35) =  CONSTANTS(:,23).*CONSTANTS(:,3).*ALGEBRAIC(:,34);
    ALGEBRAIC(:,45) = ALGEBRAIC(:,1)+ALGEBRAIC(:,35);
    ALGEBRAIC(:,46) =  CONSTANTS(:,29).*(exp(ALGEBRAIC(:,44)./( CONSTANTS(:,1).*CONSTANTS(:,2))) - exp(ALGEBRAIC(:,45)./( CONSTANTS(:,1).*CONSTANTS(:,2))));
    ALGEBRAIC(:,47) =  - ALGEBRAIC(:,38)+ALGEBRAIC(:,46);
    ALGEBRAIC(:,48) = ALGEBRAIC(:,21) - ALGEBRAIC(:,46);
    ALGEBRAIC(:,40) = ALGEBRAIC(:,38);
    ALGEBRAIC(:,41) =  CONSTANTS(:,20).*CONSTANTS(:,3).*ALGEBRAIC(:,40);
    ALGEBRAIC(:,42) = ALGEBRAIC(:,38);
    ALGEBRAIC(:,43) =  CONSTANTS(:,21).*CONSTANTS(:,3).*ALGEBRAIC(:,42);
    ALGEBRAIC(:,49) = ALGEBRAIC(:,46);
    ALGEBRAIC(:,50) =  2.00000.*CONSTANTS(:,3).* - ALGEBRAIC(:,46);
    ALGEBRAIC(:,51) =  CONSTANTS(:,22).*CONSTANTS(:,3).*ALGEBRAIC(:,49);
    ALGEBRAIC(:,52) = ALGEBRAIC(:,46);
    ALGEBRAIC(:,53) =  CONSTANTS(:,23).*CONSTANTS(:,3).*ALGEBRAIC(:,52);
    ALGEBRAIC(:,54) = ALGEBRAIC(:,41)+ALGEBRAIC(:,43)+ALGEBRAIC(:,51)+ALGEBRAIC(:,53);
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