
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 =32;
end
% There are a total of 14 entries in each of the rate and state variable arrays.
% There are a total of 29 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_CONSTANTS(:,1) = strpad('kappa_sig1 in component muscarinic_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,2) = strpad('kappa_sig2 in component muscarinic_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,3) = strpad('kappa_sig3 in component muscarinic_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,4) = strpad('kappa_sig4 in component muscarinic_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,5) = strpad('kappa_act1 in component muscarinic_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,6) = strpad('kappa_act2 in component muscarinic_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,7) = strpad('kappa_hyd in component muscarinic_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,8) = strpad('kappa_reassoc in component muscarinic_parameters (fmol_per_sec)');
    LEGEND_CONSTANTS(:,9) = strpad('K_L in component muscarinic_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,10) = strpad('K_R in component muscarinic_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,11) = strpad('K_G in component muscarinic_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,12) = strpad('K_LR in component muscarinic_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,13) = strpad('K_RG in component muscarinic_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,14) = strpad('K_LRG in component muscarinic_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,15) = strpad('K_a_GTP in component muscarinic_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,16) = strpad('K_beta_gamma in component muscarinic_parameters (per_fmol)');
    LEGEND_CONSTANTS(:,17) = strpad('K_a_GDP in component muscarinic_parameters (per_fmol)');
    LEGEND_VOI = strpad('time in component environment (second)');
    LEGEND_ALGEBRAIC(:,1) = strpad('q_L_init in component environment (fmol)');
    LEGEND_CONSTANTS(:,18) = strpad('q_LRG_init in component environment (fmol)');
    LEGEND_CONSTANTS(:,19) = strpad('q_LR_init in component environment (fmol)');
    LEGEND_CONSTANTS(:,20) = strpad('q_R_init in component environment (fmol)');
    LEGEND_CONSTANTS(:,21) = strpad('q_G_init in component environment (fmol)');
    LEGEND_CONSTANTS(:,22) = strpad('q_RG_init in component environment (fmol)');
    LEGEND_CONSTANTS(:,23) = strpad('q_a_GTP_init in component environment (fmol)');
    LEGEND_CONSTANTS(:,24) = strpad('q_beta_gamma_init in component environment (fmol)');
    LEGEND_CONSTANTS(:,25) = strpad('q_a_GDP_init in component environment (fmol)');
    LEGEND_STATES(:,1) = strpad('q_R in component GiProtein (fmol)');
    LEGEND_STATES(:,2) = strpad('q_G in component GiProtein (fmol)');
    LEGEND_STATES(:,3) = strpad('q_RG in component GiProtein (fmol)');
    LEGEND_STATES(:,4) = strpad('q_LR in component GiProtein (fmol)');
    LEGEND_STATES(:,5) = strpad('q_LRG in component GiProtein (fmol)');
    LEGEND_STATES(:,6) = strpad('q_a_GTP in component GiProtein (fmol)');
    LEGEND_STATES(:,7) = strpad('q_beta_gamma in component GiProtein (fmol)');
    LEGEND_STATES(:,8) = strpad('q_a_GDP in component GiProtein (fmol)');
    LEGEND_STATES(:,9) = strpad('q_L in component LRGbinding (fmol)');
    LEGEND_STATES(:,10) = strpad('q_R in component LRGbinding (fmol)');
    LEGEND_STATES(:,11) = strpad('q_G in component LRGbinding (fmol)');
    LEGEND_STATES(:,12) = strpad('q_LR in component LRGbinding (fmol)');
    LEGEND_STATES(:,13) = strpad('q_RG in component LRGbinding (fmol)');
    LEGEND_STATES(:,14) = strpad('q_LRG in component LRGbinding (fmol)');
    LEGEND_ALGEBRAIC(:,2) = strpad('q_L in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,7) = strpad('q_LRG in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,5) = strpad('q_LR in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,3) = strpad('q_R in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,4) = strpad('q_G in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,6) = strpad('q_RG in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,8) = strpad('q_a_GTP in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,10) = strpad('q_beta_gamma in component environment (fmol)');
    LEGEND_ALGEBRAIC(:,12) = strpad('q_a_GDP in component environment (fmol)');
    LEGEND_CONSTANTS(:,26) = strpad('R in component constants (J_per_K_per_mol)');
    LEGEND_CONSTANTS(:,27) = strpad('T in component constants (kelvin)');
    LEGEND_CONSTANTS(:,28) = strpad('F in component constants (C_per_mol)');
    LEGEND_ALGEBRAIC(:,29) = strpad('vact1 in component GiProtein (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,30) = strpad('vact2 in component GiProtein (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,31) = strpad('vhyd in component GiProtein (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,32) = strpad('vreassoc in component GiProtein (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,14) = strpad('mu_R in component GiProtein (J_per_mol)');
    LEGEND_ALGEBRAIC(:,16) = strpad('mu_G in component GiProtein (J_per_mol)');
    LEGEND_ALGEBRAIC(:,18) = strpad('mu_RG in component GiProtein (J_per_mol)');
    LEGEND_ALGEBRAIC(:,20) = strpad('mu_LR in component GiProtein (J_per_mol)');
    LEGEND_ALGEBRAIC(:,22) = strpad('mu_LRG in component GiProtein (J_per_mol)');
    LEGEND_ALGEBRAIC(:,24) = strpad('mu_a_GTP in component GiProtein (J_per_mol)');
    LEGEND_ALGEBRAIC(:,26) = strpad('mu_beta_gamma in component GiProtein (J_per_mol)');
    LEGEND_ALGEBRAIC(:,28) = strpad('mu_a_GDP in component GiProtein (J_per_mol)');
    LEGEND_ALGEBRAIC(:,9) = strpad('mu_L in component LRGbinding (J_per_mol)');
    LEGEND_ALGEBRAIC(:,11) = strpad('mu_R in component LRGbinding (J_per_mol)');
    LEGEND_ALGEBRAIC(:,13) = strpad('mu_G in component LRGbinding (J_per_mol)');
    LEGEND_ALGEBRAIC(:,15) = strpad('mu_LR in component LRGbinding (J_per_mol)');
    LEGEND_ALGEBRAIC(:,17) = strpad('mu_RG in component LRGbinding (J_per_mol)');
    LEGEND_ALGEBRAIC(:,19) = strpad('mu_LRG in component LRGbinding (J_per_mol)');
    LEGEND_ALGEBRAIC(:,21) = strpad('vsig1 in component LRGbinding (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,23) = strpad('vsig2 in component LRGbinding (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,25) = strpad('vsig3 in component LRGbinding (fmol_per_sec)');
    LEGEND_ALGEBRAIC(:,27) = strpad('vsig4 in component LRGbinding (fmol_per_sec)');
    LEGEND_CONSTANTS(:,29) = strpad('vol in component LRGbinding (pL)');
    LEGEND_RATES(:,1) = strpad('d/dt q_R in component GiProtein (fmol)');
    LEGEND_RATES(:,3) = strpad('d/dt q_RG in component GiProtein (fmol)');
    LEGEND_RATES(:,2) = strpad('d/dt q_G in component GiProtein (fmol)');
    LEGEND_RATES(:,4) = strpad('d/dt q_LR in component GiProtein (fmol)');
    LEGEND_RATES(:,5) = strpad('d/dt q_LRG in component GiProtein (fmol)');
    LEGEND_RATES(:,6) = strpad('d/dt q_a_GTP in component GiProtein (fmol)');
    LEGEND_RATES(:,7) = strpad('d/dt q_beta_gamma in component GiProtein (fmol)');
    LEGEND_RATES(:,8) = strpad('d/dt q_a_GDP in component GiProtein (fmol)');
    LEGEND_RATES(:,9) = strpad('d/dt q_L in component LRGbinding (fmol)');
    LEGEND_RATES(:,10) = strpad('d/dt q_R in component LRGbinding (fmol)');
    LEGEND_RATES(:,11) = strpad('d/dt q_G in component LRGbinding (fmol)');
    LEGEND_RATES(:,12) = strpad('d/dt q_LR in component LRGbinding (fmol)');
    LEGEND_RATES(:,13) = strpad('d/dt q_RG in component LRGbinding (fmol)');
    LEGEND_RATES(:,14) = strpad('d/dt q_LRG in component LRGbinding (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) = 238142;
    CONSTANTS(:,2) = 266.437;
    CONSTANTS(:,3) = 3304.28;
    CONSTANTS(:,4) = 53920.1;
    CONSTANTS(:,5) = 0.00103925;
    CONSTANTS(:,6) = 1.69088e-05;
    CONSTANTS(:,7) = 0.0682376;
    CONSTANTS(:,8) = 76.6955;
    CONSTANTS(:,9) = 0.190462;
    CONSTANTS(:,10) = 0.0844987;
    CONSTANTS(:,11) = 0.00948347;
    CONSTANTS(:,12) = 5.77509;
    CONSTANTS(:,13) = 16.2165;
    CONSTANTS(:,14) = 17.9264;
    CONSTANTS(:,15) = 0.0164815;
    CONSTANTS(:,16) = 0.0731915;
    CONSTANTS(:,17) = 0.00880903;
    CONSTANTS(:,18) = 1e-16;
    CONSTANTS(:,19) = 1e-16;
    CONSTANTS(:,20) = 0.00072;
    CONSTANTS(:,21) = 0.00836;
    CONSTANTS(:,22) = 1e-16;
    CONSTANTS(:,23) = 1e-16;
    CONSTANTS(:,24) = 1e-16;
    CONSTANTS(:,25) = 1e-16;
    STATES(:,1) = 1e-16;
    STATES(:,2) = 1e-16;
    STATES(:,3) = 1e-16;
    STATES(:,4) = 1e-16;
    STATES(:,5) = 1e-16;
    STATES(:,6) = 1e-16;
    STATES(:,7) = 1e-16;
    STATES(:,8) = 1e-16;
    STATES(:,9) = 1e-16;
    STATES(:,10) = 1e-16;
    STATES(:,11) = 1e-16;
    STATES(:,12) = 1e-16;
    STATES(:,13) = 1e-16;
    STATES(:,14) = 1e-16;
    CONSTANTS(:,26) = 8.31;
    CONSTANTS(:,27) = 310;
    CONSTANTS(:,28) = 96485;
    CONSTANTS(:,29) = 34.4;
    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(:,3) = STATES(:,10)+STATES(:,1)+CONSTANTS(:,20);
    ALGEBRAIC(:,11) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,10).*ALGEBRAIC(:,3));
    ALGEBRAIC(:,4) = STATES(:,11)+STATES(:,2)+CONSTANTS(:,21);
    ALGEBRAIC(:,13) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,11).*ALGEBRAIC(:,4));
    ALGEBRAIC(:,6) = STATES(:,13)+STATES(:,3)+CONSTANTS(:,22);
    ALGEBRAIC(:,17) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,13).*ALGEBRAIC(:,6));
    ALGEBRAIC(:,21) =  CONSTANTS(:,1).*exp((ALGEBRAIC(:,11)+ALGEBRAIC(:,13))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,17)./( CONSTANTS(:,26).*CONSTANTS(:,27)));
    ALGEBRAIC(:,1) = piecewise({VOI>0.100000&VOI<=1.00000, 0.380000 }, 1.00000e-16);
    ALGEBRAIC(:,2) = STATES(:,9)+ALGEBRAIC(:,1);
    ALGEBRAIC(:,9) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,9).*ALGEBRAIC(:,2));
    ALGEBRAIC(:,7) = STATES(:,14)+STATES(:,5)+CONSTANTS(:,18);
    ALGEBRAIC(:,19) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,14).*ALGEBRAIC(:,7));
    ALGEBRAIC(:,23) =  CONSTANTS(:,2).*exp((ALGEBRAIC(:,17)+ALGEBRAIC(:,9))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,19)./( CONSTANTS(:,26).*CONSTANTS(:,27)));
    RATES(:,13) = ALGEBRAIC(:,21) - ALGEBRAIC(:,23);
    ALGEBRAIC(:,5) = STATES(:,12)+STATES(:,4)+CONSTANTS(:,19);
    ALGEBRAIC(:,15) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,12).*ALGEBRAIC(:,5));
    ALGEBRAIC(:,25) =  CONSTANTS(:,3).*exp((ALGEBRAIC(:,15)+ALGEBRAIC(:,13))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,19)./( CONSTANTS(:,26).*CONSTANTS(:,27)));
    RATES(:,11) =  - ALGEBRAIC(:,21) - ALGEBRAIC(:,25);
    RATES(:,14) = ALGEBRAIC(:,23)+ALGEBRAIC(:,25);
    ALGEBRAIC(:,27) =  CONSTANTS(:,4).*exp((ALGEBRAIC(:,11)+ALGEBRAIC(:,9))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,15)./( CONSTANTS(:,26).*CONSTANTS(:,27)));
    RATES(:,9) =  - ALGEBRAIC(:,23) - ALGEBRAIC(:,27);
    RATES(:,10) =  - ALGEBRAIC(:,21) - ALGEBRAIC(:,27);
    RATES(:,12) =  - ALGEBRAIC(:,25)+ALGEBRAIC(:,27);
    ALGEBRAIC(:,14) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,10).*ALGEBRAIC(:,3));
    ALGEBRAIC(:,18) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,13).*ALGEBRAIC(:,6));
    ALGEBRAIC(:,8) = STATES(:,6)+CONSTANTS(:,23);
    ALGEBRAIC(:,24) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,15).*ALGEBRAIC(:,8));
    ALGEBRAIC(:,10) = STATES(:,7)+CONSTANTS(:,24);
    ALGEBRAIC(:,26) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,16).*ALGEBRAIC(:,10));
    ALGEBRAIC(:,29) =  CONSTANTS(:,5).*(exp(ALGEBRAIC(:,18)./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp((ALGEBRAIC(:,24)+ALGEBRAIC(:,26)+ALGEBRAIC(:,14))./( CONSTANTS(:,26).*CONSTANTS(:,27))));
    RATES(:,1) = ALGEBRAIC(:,29);
    RATES(:,3) =  - ALGEBRAIC(:,29);
    ALGEBRAIC(:,20) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,12).*ALGEBRAIC(:,5));
    ALGEBRAIC(:,22) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,14).*ALGEBRAIC(:,7));
    ALGEBRAIC(:,30) =  CONSTANTS(:,6).*(exp(ALGEBRAIC(:,22)./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp((ALGEBRAIC(:,24)+ALGEBRAIC(:,26)+ALGEBRAIC(:,20))./( CONSTANTS(:,26).*CONSTANTS(:,27))));
    RATES(:,4) = ALGEBRAIC(:,30);
    RATES(:,5) =  - ALGEBRAIC(:,30);
    ALGEBRAIC(:,12) = STATES(:,8)+CONSTANTS(:,25);
    ALGEBRAIC(:,28) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,17).*ALGEBRAIC(:,12));
    ALGEBRAIC(:,31) =  CONSTANTS(:,7).*(exp(ALGEBRAIC(:,24)./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,28)./( CONSTANTS(:,26).*CONSTANTS(:,27))));
    RATES(:,6) = (ALGEBRAIC(:,29)+ALGEBRAIC(:,30)) - ALGEBRAIC(:,31);
    ALGEBRAIC(:,16) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,11).*ALGEBRAIC(:,4));
    ALGEBRAIC(:,32) =  CONSTANTS(:,8).*(exp((ALGEBRAIC(:,28)+ALGEBRAIC(:,26))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,16)./( CONSTANTS(:,26).*CONSTANTS(:,27))));
    RATES(:,2) = ALGEBRAIC(:,32);
    RATES(:,7) = (ALGEBRAIC(:,29)+ALGEBRAIC(:,30)) - ALGEBRAIC(:,32);
    RATES(:,8) = ALGEBRAIC(:,31) - ALGEBRAIC(:,32);
   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(:,3) = STATES(:,10)+STATES(:,1)+CONSTANTS(:,20);
    ALGEBRAIC(:,11) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,10).*ALGEBRAIC(:,3));
    ALGEBRAIC(:,4) = STATES(:,11)+STATES(:,2)+CONSTANTS(:,21);
    ALGEBRAIC(:,13) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,11).*ALGEBRAIC(:,4));
    ALGEBRAIC(:,6) = STATES(:,13)+STATES(:,3)+CONSTANTS(:,22);
    ALGEBRAIC(:,17) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,13).*ALGEBRAIC(:,6));
    ALGEBRAIC(:,21) =  CONSTANTS(:,1).*exp((ALGEBRAIC(:,11)+ALGEBRAIC(:,13))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,17)./( CONSTANTS(:,26).*CONSTANTS(:,27)));
    ALGEBRAIC(:,1) = piecewise({VOI>0.100000&VOI<=1.00000, 0.380000 }, 1.00000e-16);
    ALGEBRAIC(:,2) = STATES(:,9)+ALGEBRAIC(:,1);
    ALGEBRAIC(:,9) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,9).*ALGEBRAIC(:,2));
    ALGEBRAIC(:,7) = STATES(:,14)+STATES(:,5)+CONSTANTS(:,18);
    ALGEBRAIC(:,19) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,14).*ALGEBRAIC(:,7));
    ALGEBRAIC(:,23) =  CONSTANTS(:,2).*exp((ALGEBRAIC(:,17)+ALGEBRAIC(:,9))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,19)./( CONSTANTS(:,26).*CONSTANTS(:,27)));
    ALGEBRAIC(:,5) = STATES(:,12)+STATES(:,4)+CONSTANTS(:,19);
    ALGEBRAIC(:,15) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,12).*ALGEBRAIC(:,5));
    ALGEBRAIC(:,25) =  CONSTANTS(:,3).*exp((ALGEBRAIC(:,15)+ALGEBRAIC(:,13))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,19)./( CONSTANTS(:,26).*CONSTANTS(:,27)));
    ALGEBRAIC(:,27) =  CONSTANTS(:,4).*exp((ALGEBRAIC(:,11)+ALGEBRAIC(:,9))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,15)./( CONSTANTS(:,26).*CONSTANTS(:,27)));
    ALGEBRAIC(:,14) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,10).*ALGEBRAIC(:,3));
    ALGEBRAIC(:,18) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,13).*ALGEBRAIC(:,6));
    ALGEBRAIC(:,8) = STATES(:,6)+CONSTANTS(:,23);
    ALGEBRAIC(:,24) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,15).*ALGEBRAIC(:,8));
    ALGEBRAIC(:,10) = STATES(:,7)+CONSTANTS(:,24);
    ALGEBRAIC(:,26) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,16).*ALGEBRAIC(:,10));
    ALGEBRAIC(:,29) =  CONSTANTS(:,5).*(exp(ALGEBRAIC(:,18)./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp((ALGEBRAIC(:,24)+ALGEBRAIC(:,26)+ALGEBRAIC(:,14))./( CONSTANTS(:,26).*CONSTANTS(:,27))));
    ALGEBRAIC(:,20) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,12).*ALGEBRAIC(:,5));
    ALGEBRAIC(:,22) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,14).*ALGEBRAIC(:,7));
    ALGEBRAIC(:,30) =  CONSTANTS(:,6).*(exp(ALGEBRAIC(:,22)./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp((ALGEBRAIC(:,24)+ALGEBRAIC(:,26)+ALGEBRAIC(:,20))./( CONSTANTS(:,26).*CONSTANTS(:,27))));
    ALGEBRAIC(:,12) = STATES(:,8)+CONSTANTS(:,25);
    ALGEBRAIC(:,28) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,17).*ALGEBRAIC(:,12));
    ALGEBRAIC(:,31) =  CONSTANTS(:,7).*(exp(ALGEBRAIC(:,24)./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,28)./( CONSTANTS(:,26).*CONSTANTS(:,27))));
    ALGEBRAIC(:,16) =  CONSTANTS(:,26).*CONSTANTS(:,27).*log( CONSTANTS(:,11).*ALGEBRAIC(:,4));
    ALGEBRAIC(:,32) =  CONSTANTS(:,8).*(exp((ALGEBRAIC(:,28)+ALGEBRAIC(:,26))./( CONSTANTS(:,26).*CONSTANTS(:,27))) - exp(ALGEBRAIC(:,16)./( CONSTANTS(:,26).*CONSTANTS(:,27))));
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

