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 =21;
end
% There are a total of 6 entries in each of the rate and state variable arrays.
% There are a total of 40 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('time in component environment (second)');
    LEGEND_CONSTANTS(:,1) = strpad('R_mt in component heart_parameters (kPa_second_per_liter)');
    LEGEND_CONSTANTS(:,2) = strpad('R_av in component heart_parameters (kPa_second_per_liter)');
    LEGEND_CONSTANTS(:,3) = strpad('R_tc in component heart_parameters (kPa_second_per_liter)');
    LEGEND_CONSTANTS(:,4) = strpad('R_pv in component heart_parameters (kPa_second_per_liter)');
    LEGEND_CONSTANTS(:,5) = strpad('R_pul in component heart_parameters (kPa_second_per_liter)');
    LEGEND_CONSTANTS(:,6) = strpad('R_sys in component heart_parameters (kPa_second_per_liter)');
    LEGEND_CONSTANTS(:,7) = strpad('HR in component heart_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('V_tot in component heart_parameters (liter)');
    LEGEND_CONSTANTS(:,9) = strpad('P_pl in component heart_parameters (kPa)');
    LEGEND_ALGEBRAIC(:,2) = strpad('e_t in component driver_function (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('A in component driver_function (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('B in component driver_function (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('C in component driver_function (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('tau in component driver_function (second)');
    LEGEND_CONSTANTS(:,13) = strpad('period in component driver_function (dimensionless)');
    LEGEND_ALGEBRAIC(:,3) = strpad('V_pcd in component pericardium (liter)');
    LEGEND_ALGEBRAIC(:,4) = strpad('P_pcd in component pericardium (kPa)');
    LEGEND_ALGEBRAIC(:,5) = strpad('P_peri in component pericardium (kPa)');
    LEGEND_STATES(:,1) = strpad('V_lv in component left_ventricle (liter)');
    LEGEND_STATES(:,2) = strpad('V_rv in component right_ventricle (liter)');
    LEGEND_CONSTANTS(:,14) = strpad('P_0_pcd in component pericardium (kPa)');
    LEGEND_CONSTANTS(:,15) = strpad('V_0_pcd in component pericardium (liter)');
    LEGEND_CONSTANTS(:,16) = strpad('lambda_pcd in component pericardium (per_liter)');
    LEGEND_ALGEBRAIC(:,10) = strpad('V_lvf in component left_ventricle (liter)');
    LEGEND_ALGEBRAIC(:,11) = strpad('P_lvf in component left_ventricle (kPa)');
    LEGEND_ALGEBRAIC(:,19) = strpad('P_lv in component left_ventricle (kPa)');
    LEGEND_ALGEBRAIC(:,12) = strpad('V_spt in component septum (liter)');
    LEGEND_ALGEBRAIC(:,13) = strpad('P_es_lvf in component lvf_calculator (kPa)');
    LEGEND_ALGEBRAIC(:,14) = strpad('P_ed_lvf in component lvf_calculator (kPa)');
    LEGEND_ALGEBRAIC(:,7) = strpad('P_pu in component pulmonary_vein (kPa)');
    LEGEND_ALGEBRAIC(:,8) = strpad('P_ao in component aorta (kPa)');
    LEGEND_CONSTANTS(:,17) = strpad('E_es_lvf in component lvf_calculator (kPa_per_liter)');
    LEGEND_CONSTANTS(:,18) = strpad('V_d_lvf in component lvf_calculator (liter)');
    LEGEND_CONSTANTS(:,19) = strpad('P_0_lvf in component lvf_calculator (kPa)');
    LEGEND_CONSTANTS(:,20) = strpad('lambda_lvf in component lvf_calculator (per_liter)');
    LEGEND_CONSTANTS(:,21) = strpad('V_0_lvf in component lvf_calculator (liter)');
    LEGEND_ALGEBRAIC(:,15) = strpad('V_rvf in component right_ventricle (liter)');
    LEGEND_ALGEBRAIC(:,16) = strpad('P_rvf in component right_ventricle (kPa)');
    LEGEND_ALGEBRAIC(:,20) = strpad('P_rv in component right_ventricle (kPa)');
    LEGEND_ALGEBRAIC(:,17) = strpad('P_es_rvf in component rvf_calculator (kPa)');
    LEGEND_ALGEBRAIC(:,18) = strpad('P_ed_rvf in component rvf_calculator (kPa)');
    LEGEND_ALGEBRAIC(:,6) = strpad('P_pa in component pulmonary_artery (kPa)');
    LEGEND_ALGEBRAIC(:,9) = strpad('P_vc in component vena_cava (kPa)');
    LEGEND_CONSTANTS(:,22) = strpad('E_es_rvf in component rvf_calculator (kPa_per_liter)');
    LEGEND_CONSTANTS(:,23) = strpad('V_d_rvf in component rvf_calculator (liter)');
    LEGEND_CONSTANTS(:,24) = strpad('P_0_rvf in component rvf_calculator (kPa)');
    LEGEND_CONSTANTS(:,25) = strpad('lambda_rvf in component rvf_calculator (per_liter)');
    LEGEND_CONSTANTS(:,26) = strpad('V_0_rvf in component rvf_calculator (liter)');
    LEGEND_ALGEBRAIC(:,21) = strpad('P_sept in component septum (kPa)');
    LEGEND_CONSTANTS(:,27) = strpad('E_es_spt in component septum (kPa_per_liter)');
    LEGEND_CONSTANTS(:,28) = strpad('V_d_spt in component septum (liter)');
    LEGEND_CONSTANTS(:,29) = strpad('P_0_spt in component septum (kPa)');
    LEGEND_CONSTANTS(:,30) = strpad('lambda_spt in component septum (per_liter)');
    LEGEND_CONSTANTS(:,31) = strpad('V_0_spt in component septum (liter)');
    LEGEND_CONSTANTS(:,32) = strpad('one in component septum (dimensionless)');
    LEGEND_CONSTANTS(:,33) = strpad('E_es_pa in component pulmonary_artery (kPa_per_liter)');
    LEGEND_STATES(:,3) = strpad('V_pa in component pulmonary_artery (liter)');
    LEGEND_CONSTANTS(:,34) = strpad('V_d_pa in component pulmonary_artery (liter)');
    LEGEND_CONSTANTS(:,35) = strpad('E_es_pu in component pulmonary_vein (kPa_per_liter)');
    LEGEND_STATES(:,4) = strpad('V_pu in component pulmonary_vein (liter)');
    LEGEND_CONSTANTS(:,36) = strpad('V_d_pu in component pulmonary_vein (liter)');
    LEGEND_CONSTANTS(:,37) = strpad('E_es_ao in component aorta (kPa_per_liter)');
    LEGEND_STATES(:,5) = strpad('V_ao in component aorta (liter)');
    LEGEND_CONSTANTS(:,38) = strpad('V_d_ao in component aorta (liter)');
    LEGEND_CONSTANTS(:,39) = strpad('E_es_vc in component vena_cava (kPa_per_liter)');
    LEGEND_STATES(:,6) = strpad('V_vc in component vena_cava (liter)');
    LEGEND_CONSTANTS(:,40) = strpad('V_d_vc in component vena_cava (liter)');
    LEGEND_RATES(:,1) = strpad('d/dt V_lv in component left_ventricle (liter)');
    LEGEND_RATES(:,2) = strpad('d/dt V_rv in component right_ventricle (liter)');
    LEGEND_RATES(:,3) = strpad('d/dt V_pa in component pulmonary_artery (liter)');
    LEGEND_RATES(:,4) = strpad('d/dt V_pu in component pulmonary_vein (liter)');
    LEGEND_RATES(:,5) = strpad('d/dt V_ao in component aorta (liter)');
    LEGEND_RATES(:,6) = strpad('d/dt V_vc in component vena_cava (liter)');
    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) = 0.06;
    CONSTANTS(:,2) = 1.4;
    CONSTANTS(:,3) = 0.18;
    CONSTANTS(:,4) = 0.48;
    CONSTANTS(:,5) = 19;
    CONSTANTS(:,6) = 140;
    CONSTANTS(:,7) = 80;
    CONSTANTS(:,8) = 5.5;
    CONSTANTS(:,9) = -0.533289474;
    CONSTANTS(:,10) = 1;
    CONSTANTS(:,11) = 80;
    CONSTANTS(:,12) = 0.27;
    CONSTANTS(:,13) = 0.405;
    STATES(:,1) = 0.005;
    STATES(:,2) = 0.005;
    CONSTANTS(:,14) = 0.067;
    CONSTANTS(:,15) = 0.2;
    CONSTANTS(:,16) = 30;
    CONSTANTS(:,17) = 454;
    CONSTANTS(:,18) = 0.005;
    CONSTANTS(:,19) = 0.17;
    CONSTANTS(:,20) = 15;
    CONSTANTS(:,21) = 0.005;
    CONSTANTS(:,22) = 87;
    CONSTANTS(:,23) = 0.005;
    CONSTANTS(:,24) = 0.16;
    CONSTANTS(:,25) = 15;
    CONSTANTS(:,26) = 0.005;
    CONSTANTS(:,27) = 6500;
    CONSTANTS(:,28) = 0.002;
    CONSTANTS(:,29) = 0.148;
    CONSTANTS(:,30) = 435;
    CONSTANTS(:,31) = 0.002;
    CONSTANTS(:,32) = 1;
    CONSTANTS(:,33) = 45;
    STATES(:,3) = 0.16;
    CONSTANTS(:,34) = 0.16;
    CONSTANTS(:,35) = 0.8;
    STATES(:,4) = 0.2;
    CONSTANTS(:,36) = 0.2;
    CONSTANTS(:,37) = 94;
    STATES(:,5) = 0.8;
    CONSTANTS(:,38) = 0.8;
    CONSTANTS(:,39) = 1.5;
    STATES(:,6) = 2.83;
    CONSTANTS(:,40) = 2.83;
    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(:,1)+STATES(:,2);
    ALGEBRAIC(:,4) =  CONSTANTS(:,14).*(exp( CONSTANTS(:,16).*(ALGEBRAIC(:,3) - CONSTANTS(:,15))) - 1.00000);
    ALGEBRAIC(:,5) = ALGEBRAIC(:,4)+CONSTANTS(:,9);
    ALGEBRAIC(:,1) =  rem(VOI, CONSTANTS(:,13));
    ALGEBRAIC(:,2) =  CONSTANTS(:,10).*exp(  - CONSTANTS(:,11).*power(( ALGEBRAIC(:,1).*CONSTANTS(:,7))./60.0000 - CONSTANTS(:,12), 2.00000));
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
    ALGEBRAIC(:,19) = ALGEBRAIC(:,11)+ALGEBRAIC(:,5);
    ALGEBRAIC(:,7) =  CONSTANTS(:,35).*(STATES(:,4) - CONSTANTS(:,36));
    ALGEBRAIC(:,8) =  CONSTANTS(:,37).*(STATES(:,5) - CONSTANTS(:,38));
    RATES(:,1) = piecewise({ALGEBRAIC(:,7) - ALGEBRAIC(:,19)<0.00000&ALGEBRAIC(:,19) - ALGEBRAIC(:,8)<0.00000, 0.00000 , ALGEBRAIC(:,7) - ALGEBRAIC(:,19)<0.00000,  - (ALGEBRAIC(:,19) - ALGEBRAIC(:,8))./CONSTANTS(:,2) , ALGEBRAIC(:,19) - ALGEBRAIC(:,8)<0.00000, (ALGEBRAIC(:,7) - ALGEBRAIC(:,19))./CONSTANTS(:,1) }, (ALGEBRAIC(:,7) - ALGEBRAIC(:,19))./CONSTANTS(:,1) - (ALGEBRAIC(:,19) - ALGEBRAIC(:,8))./CONSTANTS(:,2));
    ALGEBRAIC(:,6) =  CONSTANTS(:,33).*(STATES(:,3) - CONSTANTS(:,34));
    RATES(:,4) = piecewise({ALGEBRAIC(:,6) - ALGEBRAIC(:,7)<0.00000&ALGEBRAIC(:,7) - ALGEBRAIC(:,19)<0.00000, 0.00000 , ALGEBRAIC(:,6) - ALGEBRAIC(:,7)<0.00000,  - (ALGEBRAIC(:,7) - ALGEBRAIC(:,19))./CONSTANTS(:,1) , ALGEBRAIC(:,7) - ALGEBRAIC(:,19)<0.00000, (ALGEBRAIC(:,6) - ALGEBRAIC(:,7))./CONSTANTS(:,5) }, (ALGEBRAIC(:,6) - ALGEBRAIC(:,7))./CONSTANTS(:,5) - (ALGEBRAIC(:,7) - ALGEBRAIC(:,19))./CONSTANTS(:,1));
    ALGEBRAIC(:,9) =  CONSTANTS(:,39).*(STATES(:,6) - CONSTANTS(:,40));
    RATES(:,5) = piecewise({ALGEBRAIC(:,19) - ALGEBRAIC(:,8)<0.00000&ALGEBRAIC(:,8) - ALGEBRAIC(:,9)<0.00000, 0.00000 , ALGEBRAIC(:,19) - ALGEBRAIC(:,8)<0.00000,  - (ALGEBRAIC(:,8) - ALGEBRAIC(:,9))./CONSTANTS(:,6) , ALGEBRAIC(:,8) - ALGEBRAIC(:,9)<0.00000, (ALGEBRAIC(:,19) - ALGEBRAIC(:,8))./CONSTANTS(:,2) }, (ALGEBRAIC(:,19) - ALGEBRAIC(:,8))./CONSTANTS(:,2) - (ALGEBRAIC(:,8) - ALGEBRAIC(:,9))./CONSTANTS(:,6));
    ALGEBRAIC(:,20) = ALGEBRAIC(:,16)+ALGEBRAIC(:,5);
    RATES(:,2) = piecewise({ALGEBRAIC(:,9) - ALGEBRAIC(:,20)<0.00000&ALGEBRAIC(:,20) - ALGEBRAIC(:,6)<0.00000, 0.00000 , ALGEBRAIC(:,9) - ALGEBRAIC(:,20)<0.00000,  - (ALGEBRAIC(:,20) - ALGEBRAIC(:,6))./CONSTANTS(:,4) , ALGEBRAIC(:,20) - ALGEBRAIC(:,6)<0.00000, (ALGEBRAIC(:,9) - ALGEBRAIC(:,20))./CONSTANTS(:,3) }, (ALGEBRAIC(:,9) - ALGEBRAIC(:,20))./CONSTANTS(:,3) - (ALGEBRAIC(:,20) - ALGEBRAIC(:,6))./CONSTANTS(:,4));
    RATES(:,3) = piecewise({ALGEBRAIC(:,20) - ALGEBRAIC(:,6)<0.00000&ALGEBRAIC(:,6) - ALGEBRAIC(:,7)<0.00000, 0.00000 , ALGEBRAIC(:,20) - ALGEBRAIC(:,6)<0.00000,  - (ALGEBRAIC(:,6) - ALGEBRAIC(:,7))./CONSTANTS(:,5) , ALGEBRAIC(:,6) - ALGEBRAIC(:,7)<0.00000, (ALGEBRAIC(:,20) - ALGEBRAIC(:,6))./CONSTANTS(:,4) }, (ALGEBRAIC(:,20) - ALGEBRAIC(:,6))./CONSTANTS(:,4) - (ALGEBRAIC(:,6) - ALGEBRAIC(:,7))./CONSTANTS(:,5));
    RATES(:,6) = piecewise({ALGEBRAIC(:,8) - ALGEBRAIC(:,9)<0.00000&ALGEBRAIC(:,9) - ALGEBRAIC(:,20)<0.00000, 0.00000 , ALGEBRAIC(:,8) - ALGEBRAIC(:,9)<0.00000,  - (ALGEBRAIC(:,9) - ALGEBRAIC(:,20))./CONSTANTS(:,3) , ALGEBRAIC(:,9) - ALGEBRAIC(:,20)<0.00000, (ALGEBRAIC(:,8) - ALGEBRAIC(:,9))./CONSTANTS(:,6) }, (ALGEBRAIC(:,8) - ALGEBRAIC(:,9))./CONSTANTS(:,6) - (ALGEBRAIC(:,9) - ALGEBRAIC(:,20))./CONSTANTS(:,3));
   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(:,1)+STATES(:,2);
    ALGEBRAIC(:,4) =  CONSTANTS(:,14).*(exp( CONSTANTS(:,16).*(ALGEBRAIC(:,3) - CONSTANTS(:,15))) - 1.00000);
    ALGEBRAIC(:,5) = ALGEBRAIC(:,4)+CONSTANTS(:,9);
    ALGEBRAIC(:,1) =  rem(VOI, CONSTANTS(:,13));
    ALGEBRAIC(:,2) =  CONSTANTS(:,10).*exp(  - CONSTANTS(:,11).*power(( ALGEBRAIC(:,1).*CONSTANTS(:,7))./60.0000 - CONSTANTS(:,12), 2.00000));
    ALGEBRAIC(:,19) = ALGEBRAIC(:,11)+ALGEBRAIC(:,5);
    ALGEBRAIC(:,7) =  CONSTANTS(:,35).*(STATES(:,4) - CONSTANTS(:,36));
    ALGEBRAIC(:,8) =  CONSTANTS(:,37).*(STATES(:,5) - CONSTANTS(:,38));
    ALGEBRAIC(:,6) =  CONSTANTS(:,33).*(STATES(:,3) - CONSTANTS(:,34));
    ALGEBRAIC(:,9) =  CONSTANTS(:,39).*(STATES(:,6) - CONSTANTS(:,40));
    ALGEBRAIC(:,20) = ALGEBRAIC(:,16)+ALGEBRAIC(:,5);
    ALGEBRAIC(:,21) = ALGEBRAIC(:,19) - ALGEBRAIC(:,20);
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
    ALGEBRAIC = ALGEBRAIC_IN;
    CONSTANTS = CONSTANTS_IN;
    STATES = STATES_IN;
    global initialGuess_0;
    if (length(initialGuess_0) ~= 9), initialGuess_0 = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1];, end
    options = optimset('Display', 'off', 'TolX', 1E-6);
    if length(VOI) == 1
        residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
        soln = fsolve(residualfn, initialGuess_0, options);
        initialGuess_0 = soln;
        ALGEBRAIC(:,10) = soln(1);
        ALGEBRAIC(:,11) = soln(2);
        ALGEBRAIC(:,12) = soln(3);
        ALGEBRAIC(:,13) = soln(4);
        ALGEBRAIC(:,14) = soln(5);
        ALGEBRAIC(:,15) = soln(6);
        ALGEBRAIC(:,16) = soln(7);
        ALGEBRAIC(:,17) = soln(8);
        ALGEBRAIC(:,18) = soln(9);
    else
        SET_ALGEBRAIC(:,10) = logical(1);
        SET_ALGEBRAIC(:,11) = logical(1);
        SET_ALGEBRAIC(:,12) = logical(1);
        SET_ALGEBRAIC(:,13) = logical(1);
        SET_ALGEBRAIC(:,14) = logical(1);
        SET_ALGEBRAIC(:,15) = logical(1);
        SET_ALGEBRAIC(:,16) = logical(1);
        SET_ALGEBRAIC(:,17) = logical(1);
        SET_ALGEBRAIC(:,18) = logical(1);
        for i=1:length(VOI)
            residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
            soln = fsolve(residualfn, initialGuess_0, options);
            initialGuess_0 = soln;
            TEMP_ALGEBRAIC(:,10) = soln(1);
            TEMP_ALGEBRAIC(:,11) = soln(2);
            TEMP_ALGEBRAIC(:,12) = soln(3);
            TEMP_ALGEBRAIC(:,13) = soln(4);
            TEMP_ALGEBRAIC(:,14) = soln(5);
            TEMP_ALGEBRAIC(:,15) = soln(6);
            TEMP_ALGEBRAIC(:,16) = soln(7);
            TEMP_ALGEBRAIC(:,17) = soln(8);
            TEMP_ALGEBRAIC(:,18) = soln(9);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    ALGEBRAIC(:,10) = algebraicCandidate(1);
    ALGEBRAIC(:,11) = algebraicCandidate(2);
    ALGEBRAIC(:,12) = algebraicCandidate(3);
    ALGEBRAIC(:,13) = algebraicCandidate(4);
    ALGEBRAIC(:,14) = algebraicCandidate(5);
    ALGEBRAIC(:,15) = algebraicCandidate(6);
    ALGEBRAIC(:,16) = algebraicCandidate(7);
    ALGEBRAIC(:,17) = algebraicCandidate(8);
    ALGEBRAIC(:,18) = algebraicCandidate(9);
    resid(1) = ALGEBRAIC(:,10) - (STATES(:,1) - ALGEBRAIC(:,12));
    resid(2) = ALGEBRAIC(:,11) - ( ALGEBRAIC(:,2).*ALGEBRAIC(:,13)+ (1.00000 - ALGEBRAIC(:,2)).*ALGEBRAIC(:,14));
    resid(3) = ALGEBRAIC(:,13) -  CONSTANTS(:,17).*(ALGEBRAIC(:,10) - CONSTANTS(:,18));
    resid(4) = ALGEBRAIC(:,14) -  CONSTANTS(:,19).*(exp( CONSTANTS(:,20).*(ALGEBRAIC(:,10) - CONSTANTS(:,21))) - 1.00000);
    resid(5) = ALGEBRAIC(:,15) - (STATES(:,2)+ALGEBRAIC(:,12));
    resid(6) = ALGEBRAIC(:,16) - ( ALGEBRAIC(:,2).*ALGEBRAIC(:,17)+ (1.00000 - ALGEBRAIC(:,2)).*ALGEBRAIC(:,18));
    resid(7) = ALGEBRAIC(:,17) -  CONSTANTS(:,22).*(ALGEBRAIC(:,15) - CONSTANTS(:,23));
    resid(8) = ALGEBRAIC(:,18) -  CONSTANTS(:,24).*(exp( CONSTANTS(:,25).*(ALGEBRAIC(:,15) - CONSTANTS(:,26))) - 1.00000);
    resid(9) = ALGEBRAIC(:,11) - (( ALGEBRAIC(:,2).*CONSTANTS(:,27).*(ALGEBRAIC(:,12) - CONSTANTS(:,28))+ (CONSTANTS(:,32) - ALGEBRAIC(:,2)).*CONSTANTS(:,29).*(exp( CONSTANTS(:,30).*(ALGEBRAIC(:,12) - CONSTANTS(:,31))) - CONSTANTS(:,32))) - ALGEBRAIC(:,16));
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