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 =62;
end
% There are a total of 5 entries in each of the rate and state variable arrays.
% There are a total of 45 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 (minute)');
    LEGEND_CONSTANTS(:,1) = strpad('ADHMV in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('AMM in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('ANU in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('ANUVN in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('ARM in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('ATRRFB in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('ATRVFB in component circulatory_dynamics (litre)');
    LEGEND_CONSTANTS(:,8) = strpad('AU in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,9) = strpad('AUH in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('AUM in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('AVE in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('HMD in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('HPL in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('HPR in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('MYOGRS in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('OSA in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('PAMK in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('PC in component circulatory_dynamics (mmHg)');
    LEGEND_CONSTANTS(:,19) = strpad('RBF in component circulatory_dynamics (L_per_minute)');
    LEGEND_CONSTANTS(:,20) = strpad('VIM in component circulatory_dynamics (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('VP in component circulatory_dynamics (litre)');
    LEGEND_CONSTANTS(:,22) = strpad('VRC in component circulatory_dynamics (litre)');
    LEGEND_CONSTANTS(:,23) = strpad('VV6 in component circulatory_dynamics (litre)');
    LEGEND_CONSTANTS(:,24) = strpad('VV7 in component circulatory_dynamics (litre)');
    LEGEND_CONSTANTS(:,25) = strpad('VVR in component circulatory_dynamics (litre)');
    LEGEND_STATES(:,1) = strpad('VVS1 in component venous_blood_volume (litre)');
    LEGEND_STATES(:,2) = strpad('VAS1 in component arterial_blood_volume (litre)');
    LEGEND_STATES(:,3) = strpad('VLA1 in component left_atrial_blood_volume (litre)');
    LEGEND_STATES(:,4) = strpad('VPA1 in component pulmonary_vasculature_blood_volume (litre)');
    LEGEND_STATES(:,5) = strpad('VRA1 in component right_atrial_blood_volume (litre)');
    LEGEND_ALGEBRAIC(:,1) = strpad('VBD in component total_blood_volume_change (litre)');
    LEGEND_ALGEBRAIC(:,34) = strpad('QVO in component rate_of_blood_flow_from_veins_to_right_atrium (L_per_minute)');
    LEGEND_ALGEBRAIC(:,46) = strpad('QRO in component right_ventricular_output (L_per_minute)');
    LEGEND_ALGEBRAIC(:,2) = strpad('VRA in component right_atrial_blood_volume (litre)');
    LEGEND_ALGEBRAIC(:,48) = strpad('DRA in component right_atrial_blood_volume (L_per_minute)');
    LEGEND_ALGEBRAIC(:,4) = strpad('PRA in component right_atrial_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,3) = strpad('VRE in component right_atrial_pressure (litre)');
    LEGEND_ALGEBRAIC(:,5) = strpad('PRA1 in component autonomic_stimulation_effect_on_right_atrial_pressure (mmHg)');
    LEGEND_CONSTANTS(:,26) = strpad('HTAUML in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('PPA in component pulmonary_vasculature_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,11) = strpad('RVM in component pressure_effect_on_right_ventricular_pumping (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('PP2 in component pressure_effect_on_right_ventricular_pumping (mmHg)');
    LEGEND_ALGEBRAIC(:,42) = strpad('QLO in component left_ventricular_output (L_per_minute)');
    LEGEND_ALGEBRAIC(:,25) = strpad('QLN in component left_ventricular_output (L_per_minute)');
    LEGEND_ALGEBRAIC(:,43) = strpad('HPEF in component pumping_effectiveness_of_right_ventricle (L_per_minute)');
    LEGEND_CONSTANTS(:,27) = strpad('QRF in component parameter_values (L_per_minute)');
    LEGEND_CONSTANTS(:,28) = strpad('HSR in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('QRN in component right_ventricular_output (dimensionless)');
    LEGEND_ALGEBRAIC(:,23) = strpad('QPO in component rate_of_blood_flow_from_pulmonary_veins_to_left_atrium (L_per_minute)');
    LEGEND_ALGEBRAIC(:,7) = strpad('VPA in component pulmonary_vasculature_blood_volume (litre)');
    LEGEND_ALGEBRAIC(:,49) = strpad('DPA in component pulmonary_vasculature_blood_volume (L_per_minute)');
    LEGEND_ALGEBRAIC(:,8) = strpad('VPE in component pulmonary_vasculature_pressure (litre)');
    LEGEND_ALGEBRAIC(:,15) = strpad('RPA in component pulmonary_arterial_resistance (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,12) = strpad('PP1T in component pulmonary_arterial_resistance (L_per_minute_per_mmHg)');
    LEGEND_ALGEBRAIC(:,13) = strpad('PP1 in component pulmonary_arterial_resistance (L_per_minute_per_mmHg)');
    LEGEND_ALGEBRAIC(:,14) = strpad('CPA in component pulmonary_arterial_resistance (L_per_minute_per_mmHg)');
    LEGEND_ALGEBRAIC(:,18) = strpad('PLA in component left_atrial_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,20) = strpad('RPV in component pulmonary_venous_resistance (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,19) = strpad('PL1 in component pulmonary_venous_resistance (mmHg)');
    LEGEND_ALGEBRAIC(:,21) = strpad('RPT in component total_pulmonary_vascular_resistance (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,22) = strpad('PGL in component pressure_gradient_through_the_lungs (mmHg)');
    LEGEND_ALGEBRAIC(:,16) = strpad('VLA in component left_atrial_blood_volume (litre)');
    LEGEND_ALGEBRAIC(:,44) = strpad('DLA in component left_atrial_blood_volume (L_per_minute)');
    LEGEND_ALGEBRAIC(:,17) = strpad('VLE in component left_atrial_pressure (litre)');
    LEGEND_ALGEBRAIC(:,24) = strpad('PLA1 in component autonomic_stimulation_effect_on_left_atrial_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,37) = strpad('PA in component arterial_pressure_and_pressure_gradient (mmHg)');
    LEGEND_ALGEBRAIC(:,39) = strpad('LVM in component pumping_effectiveness_of_left_ventricle (dimensionless)');
    LEGEND_ALGEBRAIC(:,38) = strpad('PA2 in component pumping_effectiveness_of_left_ventricle (mmHg)');
    LEGEND_ALGEBRAIC(:,40) = strpad('QLOT in component left_ventricular_output (L_per_minute)');
    LEGEND_CONSTANTS(:,29) = strpad('HSL in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,41) = strpad('QLO1 in component left_ventricular_output (L_per_minute)');
    LEGEND_ALGEBRAIC(:,59) = strpad('QAO in component systemic_blood_flow (L_per_minute)');
    LEGEND_ALGEBRAIC(:,26) = strpad('VVS in component venous_blood_volume (litre)');
    LEGEND_ALGEBRAIC(:,60) = strpad('DVS in component venous_blood_volume (L_per_minute)');
    LEGEND_CONSTANTS(:,41) = strpad('VVA in component angiotensin_induced_venous_constriction (litre)');
    LEGEND_CONSTANTS(:,30) = strpad('ANY in component parameter_values (litre)');
    LEGEND_ALGEBRAIC(:,28) = strpad('VVE in component venous_excess_volume (litre)');
    LEGEND_ALGEBRAIC(:,27) = strpad('VVE1 in component venous_excess_volume (litre)');
    LEGEND_ALGEBRAIC(:,30) = strpad('PVS in component venous_average_pressure (mmHg)');
    LEGEND_CONSTANTS(:,31) = strpad('CV in component parameter_values (L_per_mmHg)');
    LEGEND_ALGEBRAIC(:,29) = strpad('PVS1 in component venous_average_pressure (mmHg)');
    LEGEND_ALGEBRAIC(:,31) = strpad('PR1 in component venous_outflow_pressure_into_heart (mmHg)');
    LEGEND_CONSTANTS(:,32) = strpad('PR1LL in component parameter_values (mmHg)');
    LEGEND_ALGEBRAIC(:,32) = strpad('RVG in component resistance_from_veins_to_right_atrium (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,33) = strpad('PGV in component rate_of_blood_flow_from_veins_to_right_atrium (mmHg)');
    LEGEND_CONSTANTS(:,44) = strpad('RVS in component venous_resistance (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,33) = strpad('CN7 in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,34) = strpad('CN2 in component parameter_values (per_mmHg)');
    LEGEND_CONSTANTS(:,35) = strpad('RVSM in component parameter_values (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,42) = strpad('CN3 in component venous_resistance (dimensionless)');
    LEGEND_CONSTANTS(:,43) = strpad('RV1 in component venous_resistance (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,45) = strpad('NNRVR in component NM_NR_venous_resistance (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,35) = strpad('VAS in component arterial_blood_volume (litre)');
    LEGEND_ALGEBRAIC(:,61) = strpad('DAS in component arterial_blood_volume (L_per_minute)');
    LEGEND_ALGEBRAIC(:,45) = strpad('PAG in component arterial_pressure_and_pressure_gradient (mmHg)');
    LEGEND_ALGEBRAIC(:,36) = strpad('VAE in component arterial_pressure_and_pressure_gradient (litre)');
    LEGEND_ALGEBRAIC(:,47) = strpad('PAM in component pressure_effect_on_arterial_distention (dimensionless)');
    LEGEND_CONSTANTS(:,36) = strpad('PAEX in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,50) = strpad('R1 in component non_renal_systemic_arterial_resistance_multiplier (dimensionless)');
    LEGEND_ALGEBRAIC(:,51) = strpad('NNRAR in component NM_NR_arterial_resistance (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,37) = strpad('RAR in component parameter_values (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,38) = strpad('RMULT1 in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,52) = strpad('PGS in component pressure_gradient_from_arteries_to_veins (mmHg)');
    LEGEND_ALGEBRAIC(:,53) = strpad('RSM in component M_systemic_resistance (mmHg_minute_per_L)');
    LEGEND_CONSTANTS(:,39) = strpad('RAM in component parameter_values (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,54) = strpad('RSN in component total_NM_NR_systemic_resistance (mmHg_minute_per_L)');
    LEGEND_ALGEBRAIC(:,55) = strpad('BFM in component blood_flow_through_M_tissues (L_per_minute)');
    LEGEND_ALGEBRAIC(:,56) = strpad('BFN in component blood_flow_through_NM_NR_tissues (L_per_minute)');
    LEGEND_ALGEBRAIC(:,57) = strpad('FISFLO in component blood_flow_through_AV_fistulas (L_per_minute)');
    LEGEND_CONSTANTS(:,40) = strpad('FIS in component parameter_values (L_per_minute_per_mmHg)');
    LEGEND_ALGEBRAIC(:,58) = strpad('SYSFLO in component systemic_blood_flow (L_per_minute)');
    LEGEND_ALGEBRAIC(:,62) = strpad('RTP in component total_peripheral_resistance (mmHg_minute_per_L)');
    LEGEND_RATES(:,5) = strpad('d/dt VRA1 in component right_atrial_blood_volume (litre)');
    LEGEND_RATES(:,4) = strpad('d/dt VPA1 in component pulmonary_vasculature_blood_volume (litre)');
    LEGEND_RATES(:,3) = strpad('d/dt VLA1 in component left_atrial_blood_volume (litre)');
    LEGEND_RATES(:,1) = strpad('d/dt VVS1 in component venous_blood_volume (litre)');
    LEGEND_RATES(:,2) = strpad('d/dt VAS1 in component arterial_blood_volume (litre)');
    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) = 1.0;
    CONSTANTS(:,2) = 1.0;
    CONSTANTS(:,3) = 0.925271;
    CONSTANTS(:,4) = 1.0;
    CONSTANTS(:,5) = 1.16463;
    CONSTANTS(:,6) = 1.0;
    CONSTANTS(:,7) = 0.0;
    CONSTANTS(:,8) = 1.00022;
    CONSTANTS(:,9) = 1.00012;
    CONSTANTS(:,10) = 1.00066;
    CONSTANTS(:,11) = 1.0;
    CONSTANTS(:,12) = 1.0;
    CONSTANTS(:,13) = 1.00163;
    CONSTANTS(:,14) = 1.00237;
    CONSTANTS(:,15) = 1.0;
    CONSTANTS(:,16) = 0.97287;
    CONSTANTS(:,17) = 1.0;
    CONSTANTS(:,18) = 16.9144;
    CONSTANTS(:,19) = 1.22057;
    CONSTANTS(:,20) = 1.00076;
    CONSTANTS(:,21) = 3.00449;
    CONSTANTS(:,22) = 2.00439;
    CONSTANTS(:,23) = 0.0101913;
    CONSTANTS(:,24) = 0.00366525;
    CONSTANTS(:,25) = 2.50967;
    STATES(:,1) = 3.28246;
    STATES(:,2) = 0.862514;
    STATES(:,3) = 0.379883;
    STATES(:,4) = 0.38131;
    STATES(:,5) = 0.100043;
    CONSTANTS(:,26) = 0.4;
    CONSTANTS(:,27) = 0.15;
    CONSTANTS(:,28) = 1;
    CONSTANTS(:,29) = 1;
    CONSTANTS(:,30) = -0.2;
    CONSTANTS(:,31) = 0.1;
    CONSTANTS(:,32) = 0;
    CONSTANTS(:,33) = 0.2;
    CONSTANTS(:,34) = 0.0212;
    CONSTANTS(:,35) = 1;
    CONSTANTS(:,36) = 2;
    CONSTANTS(:,37) = 30.52;
    CONSTANTS(:,38) = 1;
    CONSTANTS(:,39) = 96.3;
    CONSTANTS(:,40) = 0;
    CONSTANTS(:,41) =  (CONSTANTS(:,3) - 1.00000).*CONSTANTS(:,30);
    CONSTANTS(:,42) =  ( (CONSTANTS(:,18) - 17.0000).*CONSTANTS(:,33)+17.0000).*CONSTANTS(:,34);
    CONSTANTS(:,43) = CONSTANTS(:,35)./CONSTANTS(:,42);
    CONSTANTS(:,44) =  CONSTANTS(:,11).*CONSTANTS(:,43).*CONSTANTS(:,20).*CONSTANTS(:,4);
    CONSTANTS(:,45) =  CONSTANTS(:,44).*1.79000;
    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(:,21)+CONSTANTS(:,22)) - STATES(:,1)) - STATES(:,2)) - STATES(:,3)) - STATES(:,4)) - STATES(:,5))./2.00000;
    ALGEBRAIC(:,16) = STATES(:,3)+ ALGEBRAIC(:,1).*0.128000;
    ALGEBRAIC(:,17) = ALGEBRAIC(:,16) - 0.380000;
    ALGEBRAIC(:,18) = ALGEBRAIC(:,17)./0.0100000;
    ALGEBRAIC(:,24) =  (ALGEBRAIC(:,18)+4.00000).*( CONSTANTS(:,26).*(CONSTANTS(:,8) - 1.00000)+1.00000) - 4.00000;
    ALGEBRAIC(:,25) = piecewise({ALGEBRAIC(:,24)<= - 2.00000, 0.0100000 , ALGEBRAIC(:,24)> - 2.00000&ALGEBRAIC(:,24)<=1.00000, 0.0100000+( (3.60000 - 0.0100000).*(ALGEBRAIC(:,24) -  - 2.00000))./(1.00000 -  - 2.00000) , ALGEBRAIC(:,24)>1.00000&ALGEBRAIC(:,24)<=5.00000, 3.60000+( (9.40000 - 3.60000).*(ALGEBRAIC(:,24) - 1.00000))./(5.00000 - 1.00000) , ALGEBRAIC(:,24)>5.00000&ALGEBRAIC(:,24)<=8.00000, 9.40000+( (11.6000 - 9.40000).*(ALGEBRAIC(:,24) - 5.00000))./(8.00000 - 5.00000) , ALGEBRAIC(:,24)>8.00000&ALGEBRAIC(:,24)<=12.0000, 11.6000+( (13.5000 - 11.6000).*(ALGEBRAIC(:,24) - 8.00000))./(12.0000 - 8.00000) }, 13.5000);
    ALGEBRAIC(:,35) = STATES(:,2)+ ALGEBRAIC(:,1).*0.261000;
    ALGEBRAIC(:,36) = ALGEBRAIC(:,35) - 0.495000;
    ALGEBRAIC(:,37) = ALGEBRAIC(:,36)./0.00355000;
    ALGEBRAIC(:,38) = ALGEBRAIC(:,37)./( CONSTANTS(:,9).*CONSTANTS(:,16));
    ALGEBRAIC(:,39) = piecewise({ALGEBRAIC(:,38)<=0.00000, 1.04000 , ALGEBRAIC(:,38)>0.00000&ALGEBRAIC(:,38)<=60.0000, 1.04000+( (1.02500 - 1.04000).*(ALGEBRAIC(:,38) - 0.00000))./(60.0000 - 0.00000) , ALGEBRAIC(:,38)>60.0000&ALGEBRAIC(:,38)<=125.000, 1.02500+( (0.970000 - 1.02500).*(ALGEBRAIC(:,38) - 60.0000))./(125.000 - 60.0000) , ALGEBRAIC(:,38)>125.000&ALGEBRAIC(:,38)<=160.000, 0.970000+( (0.880000 - 0.970000).*(ALGEBRAIC(:,38) - 125.000))./(160.000 - 125.000) , ALGEBRAIC(:,38)>160.000&ALGEBRAIC(:,38)<=200.000, 0.880000+( (0.590000 - 0.880000).*(ALGEBRAIC(:,38) - 160.000))./(200.000 - 160.000) , ALGEBRAIC(:,38)>200.000&ALGEBRAIC(:,38)<=240.000, 0.590000+( (0.00000 - 0.590000).*(ALGEBRAIC(:,38) - 200.000))./(240.000 - 200.000) }, 0.00000);
    ALGEBRAIC(:,40) =  ALGEBRAIC(:,39).*ALGEBRAIC(:,25).*CONSTANTS(:,9).*CONSTANTS(:,29).*CONSTANTS(:,12).*CONSTANTS(:,13);
    ALGEBRAIC(:,41) = (ALGEBRAIC(:,18) - ALGEBRAIC(:,37))./3.00000;
    ALGEBRAIC(:,42) = piecewise({ALGEBRAIC(:,41)>0.00000, ALGEBRAIC(:,40)+ALGEBRAIC(:,41) }, ALGEBRAIC(:,40));
    ALGEBRAIC(:,7) = STATES(:,4)+ ALGEBRAIC(:,1).*0.155000;
    ALGEBRAIC(:,8) = ALGEBRAIC(:,7) - 0.306250;
    ALGEBRAIC(:,9) = ALGEBRAIC(:,8)./0.00480000;
    ALGEBRAIC(:,12) =  0.0260000.*ALGEBRAIC(:,9);
    ALGEBRAIC(:,13) = piecewise({ALGEBRAIC(:,12)<1.00000e-05, 1.00000e-05 }, ALGEBRAIC(:,12));
    ALGEBRAIC(:,14) = power(ALGEBRAIC(:,13), 0.500000);
    ALGEBRAIC(:,15) = 1.00000./ALGEBRAIC(:,14);
    ALGEBRAIC(:,19) = ALGEBRAIC(:,18)+18.0000;
    ALGEBRAIC(:,20) = 1.00000./( ALGEBRAIC(:,19).*0.0357000);
    ALGEBRAIC(:,21) = ALGEBRAIC(:,20)+ALGEBRAIC(:,15);
    ALGEBRAIC(:,22) = ALGEBRAIC(:,9) - ALGEBRAIC(:,18);
    ALGEBRAIC(:,23) = ALGEBRAIC(:,22)./ALGEBRAIC(:,21);
    ALGEBRAIC(:,44) = ALGEBRAIC(:,23) - ALGEBRAIC(:,42);
    RATES(:,3) = ALGEBRAIC(:,44);
    ALGEBRAIC(:,26) = STATES(:,1)+ ALGEBRAIC(:,1).*0.398600;
    ALGEBRAIC(:,27) = ((((ALGEBRAIC(:,26) - CONSTANTS(:,25)) - CONSTANTS(:,41)) - CONSTANTS(:,24)) - CONSTANTS(:,23)) - CONSTANTS(:,7);
    ALGEBRAIC(:,28) = piecewise({ALGEBRAIC(:,27)<0.000100000, 0.000100000 }, ALGEBRAIC(:,27));
    ALGEBRAIC(:,29) = 3.70000+(ALGEBRAIC(:,28) - 0.740000)./CONSTANTS(:,31);
    ALGEBRAIC(:,30) = piecewise({ALGEBRAIC(:,29)<0.000100000, 0.000100000 }, ALGEBRAIC(:,29));
    ALGEBRAIC(:,32) = 0.740000./power(ALGEBRAIC(:,30)./( CONSTANTS(:,20).*3.70000), 0.500000);
    ALGEBRAIC(:,2) = STATES(:,5)+ ALGEBRAIC(:,1).*0.0574000;
    ALGEBRAIC(:,3) = ALGEBRAIC(:,2) - 0.100000;
    ALGEBRAIC(:,4) = ALGEBRAIC(:,3)./0.00500000;
    ALGEBRAIC(:,31) = piecewise({ALGEBRAIC(:,4)<CONSTANTS(:,32), CONSTANTS(:,32) }, ALGEBRAIC(:,4));
    ALGEBRAIC(:,33) = ALGEBRAIC(:,30) - ALGEBRAIC(:,31);
    ALGEBRAIC(:,34) = ALGEBRAIC(:,33)./ALGEBRAIC(:,32);
    ALGEBRAIC(:,10) = (ALGEBRAIC(:,9)./CONSTANTS(:,9))./CONSTANTS(:,16);
    ALGEBRAIC(:,11) = piecewise({ALGEBRAIC(:,10)<=0.00000, 1.06000 , ALGEBRAIC(:,10)>0.00000&ALGEBRAIC(:,10)<=32.0000, 1.06000+( (0.970000 - 1.06000).*(ALGEBRAIC(:,10) - 0.00000))./(32.0000 - 0.00000) , ALGEBRAIC(:,10)>32.0000&ALGEBRAIC(:,10)<=38.4000, 0.970000+( (0.930000 - 0.970000).*(ALGEBRAIC(:,10) - 32.0000))./(38.4000 - 32.0000) , ALGEBRAIC(:,10)>38.4000&ALGEBRAIC(:,10)<=48.0000, 0.930000+( (0.800000 - 0.930000).*(ALGEBRAIC(:,10) - 38.4000))./(48.0000 - 38.4000) , ALGEBRAIC(:,10)>48.0000&ALGEBRAIC(:,10)<=60.8000, 0.800000+( (0.460000 - 0.800000).*(ALGEBRAIC(:,10) - 48.0000))./(60.8000 - 48.0000) , ALGEBRAIC(:,10)>60.8000&ALGEBRAIC(:,10)<=72.0000, 0.460000+( (0.00000 - 0.460000).*(ALGEBRAIC(:,10) - 60.8000))./(72.0000 - 60.8000) }, 0.00000);
    ALGEBRAIC(:,43) =  (1.00000 - CONSTANTS(:,27)).*CONSTANTS(:,9).*ALGEBRAIC(:,11).*CONSTANTS(:,28).*CONSTANTS(:,12).*CONSTANTS(:,14)+( CONSTANTS(:,27).*ALGEBRAIC(:,42))./ALGEBRAIC(:,25);
    ALGEBRAIC(:,5) =  (ALGEBRAIC(:,4)+8.00000).*( CONSTANTS(:,26).*(CONSTANTS(:,8) - 1.00000)+1.00000) - 8.00000;
    ALGEBRAIC(:,6) = piecewise({ALGEBRAIC(:,5)<= - 8.00000, 0.00000 , ALGEBRAIC(:,5)> - 8.00000&ALGEBRAIC(:,5)<= - 6.00000, 0.00000+( (0.750000 - 0.00000).*(ALGEBRAIC(:,5) -  - 8.00000))./( - 6.00000 -  - 8.00000) , ALGEBRAIC(:,5)> - 6.00000&ALGEBRAIC(:,5)<= - 2.00000, 0.750000+( (2.60000 - 0.750000).*(ALGEBRAIC(:,5) -  - 6.00000))./( - 2.00000 -  - 6.00000) , ALGEBRAIC(:,5)> - 2.00000&ALGEBRAIC(:,5)<=4.00000, 2.60000+( (9.80000 - 2.60000).*(ALGEBRAIC(:,5) -  - 2.00000))./(4.00000 -  - 2.00000) , ALGEBRAIC(:,5)>4.00000&ALGEBRAIC(:,5)<=12.0000, 9.80000+( (13.5000 - 9.80000).*(ALGEBRAIC(:,5) - 4.00000))./(12.0000 - 4.00000) }, 13.5000);
    ALGEBRAIC(:,46) =  ALGEBRAIC(:,6).*ALGEBRAIC(:,43);
    ALGEBRAIC(:,48) = ALGEBRAIC(:,34) - ALGEBRAIC(:,46);
    RATES(:,5) = ALGEBRAIC(:,48);
    ALGEBRAIC(:,49) = ALGEBRAIC(:,46) - ALGEBRAIC(:,23);
    RATES(:,4) = ALGEBRAIC(:,49);
    ALGEBRAIC(:,45) = ALGEBRAIC(:,37) - ALGEBRAIC(:,4);
    ALGEBRAIC(:,57) =  ALGEBRAIC(:,45).*CONSTANTS(:,40);
    ALGEBRAIC(:,52) = ALGEBRAIC(:,37) - ALGEBRAIC(:,30);
    ALGEBRAIC(:,47) = power(ALGEBRAIC(:,37)./100.000, CONSTANTS(:,36));
    ALGEBRAIC(:,50) = (( CONSTANTS(:,3).*CONSTANTS(:,1).*CONSTANTS(:,10).*CONSTANTS(:,20).*CONSTANTS(:,17))./ALGEBRAIC(:,47))./CONSTANTS(:,6);
    ALGEBRAIC(:,53) =  CONSTANTS(:,39).*CONSTANTS(:,2).*ALGEBRAIC(:,50).*CONSTANTS(:,15).*CONSTANTS(:,38);
    ALGEBRAIC(:,55) = ALGEBRAIC(:,52)./ALGEBRAIC(:,53);
    ALGEBRAIC(:,51) =  CONSTANTS(:,37).*CONSTANTS(:,5).*ALGEBRAIC(:,50).*CONSTANTS(:,15).*CONSTANTS(:,38);
    ALGEBRAIC(:,54) = ALGEBRAIC(:,51)+CONSTANTS(:,45);
    ALGEBRAIC(:,56) = ALGEBRAIC(:,52)./ALGEBRAIC(:,54);
    ALGEBRAIC(:,58) = ALGEBRAIC(:,55)+ALGEBRAIC(:,56)+CONSTANTS(:,19);
    ALGEBRAIC(:,59) = ALGEBRAIC(:,58)+ALGEBRAIC(:,57);
    ALGEBRAIC(:,60) = ALGEBRAIC(:,59) - ALGEBRAIC(:,34);
    RATES(:,1) = ALGEBRAIC(:,60);
    ALGEBRAIC(:,61) = ALGEBRAIC(:,42) - ALGEBRAIC(:,59);
    RATES(:,2) = ALGEBRAIC(:,61);
   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(:,21)+CONSTANTS(:,22)) - STATES(:,1)) - STATES(:,2)) - STATES(:,3)) - STATES(:,4)) - STATES(:,5))./2.00000;
    ALGEBRAIC(:,16) = STATES(:,3)+ ALGEBRAIC(:,1).*0.128000;
    ALGEBRAIC(:,17) = ALGEBRAIC(:,16) - 0.380000;
    ALGEBRAIC(:,18) = ALGEBRAIC(:,17)./0.0100000;
    ALGEBRAIC(:,24) =  (ALGEBRAIC(:,18)+4.00000).*( CONSTANTS(:,26).*(CONSTANTS(:,8) - 1.00000)+1.00000) - 4.00000;
    ALGEBRAIC(:,25) = piecewise({ALGEBRAIC(:,24)<= - 2.00000, 0.0100000 , ALGEBRAIC(:,24)> - 2.00000&ALGEBRAIC(:,24)<=1.00000, 0.0100000+( (3.60000 - 0.0100000).*(ALGEBRAIC(:,24) -  - 2.00000))./(1.00000 -  - 2.00000) , ALGEBRAIC(:,24)>1.00000&ALGEBRAIC(:,24)<=5.00000, 3.60000+( (9.40000 - 3.60000).*(ALGEBRAIC(:,24) - 1.00000))./(5.00000 - 1.00000) , ALGEBRAIC(:,24)>5.00000&ALGEBRAIC(:,24)<=8.00000, 9.40000+( (11.6000 - 9.40000).*(ALGEBRAIC(:,24) - 5.00000))./(8.00000 - 5.00000) , ALGEBRAIC(:,24)>8.00000&ALGEBRAIC(:,24)<=12.0000, 11.6000+( (13.5000 - 11.6000).*(ALGEBRAIC(:,24) - 8.00000))./(12.0000 - 8.00000) }, 13.5000);
    ALGEBRAIC(:,35) = STATES(:,2)+ ALGEBRAIC(:,1).*0.261000;
    ALGEBRAIC(:,36) = ALGEBRAIC(:,35) - 0.495000;
    ALGEBRAIC(:,37) = ALGEBRAIC(:,36)./0.00355000;
    ALGEBRAIC(:,38) = ALGEBRAIC(:,37)./( CONSTANTS(:,9).*CONSTANTS(:,16));
    ALGEBRAIC(:,39) = piecewise({ALGEBRAIC(:,38)<=0.00000, 1.04000 , ALGEBRAIC(:,38)>0.00000&ALGEBRAIC(:,38)<=60.0000, 1.04000+( (1.02500 - 1.04000).*(ALGEBRAIC(:,38) - 0.00000))./(60.0000 - 0.00000) , ALGEBRAIC(:,38)>60.0000&ALGEBRAIC(:,38)<=125.000, 1.02500+( (0.970000 - 1.02500).*(ALGEBRAIC(:,38) - 60.0000))./(125.000 - 60.0000) , ALGEBRAIC(:,38)>125.000&ALGEBRAIC(:,38)<=160.000, 0.970000+( (0.880000 - 0.970000).*(ALGEBRAIC(:,38) - 125.000))./(160.000 - 125.000) , ALGEBRAIC(:,38)>160.000&ALGEBRAIC(:,38)<=200.000, 0.880000+( (0.590000 - 0.880000).*(ALGEBRAIC(:,38) - 160.000))./(200.000 - 160.000) , ALGEBRAIC(:,38)>200.000&ALGEBRAIC(:,38)<=240.000, 0.590000+( (0.00000 - 0.590000).*(ALGEBRAIC(:,38) - 200.000))./(240.000 - 200.000) }, 0.00000);
    ALGEBRAIC(:,40) =  ALGEBRAIC(:,39).*ALGEBRAIC(:,25).*CONSTANTS(:,9).*CONSTANTS(:,29).*CONSTANTS(:,12).*CONSTANTS(:,13);
    ALGEBRAIC(:,41) = (ALGEBRAIC(:,18) - ALGEBRAIC(:,37))./3.00000;
    ALGEBRAIC(:,42) = piecewise({ALGEBRAIC(:,41)>0.00000, ALGEBRAIC(:,40)+ALGEBRAIC(:,41) }, ALGEBRAIC(:,40));
    ALGEBRAIC(:,7) = STATES(:,4)+ ALGEBRAIC(:,1).*0.155000;
    ALGEBRAIC(:,8) = ALGEBRAIC(:,7) - 0.306250;
    ALGEBRAIC(:,9) = ALGEBRAIC(:,8)./0.00480000;
    ALGEBRAIC(:,12) =  0.0260000.*ALGEBRAIC(:,9);
    ALGEBRAIC(:,13) = piecewise({ALGEBRAIC(:,12)<1.00000e-05, 1.00000e-05 }, ALGEBRAIC(:,12));
    ALGEBRAIC(:,14) = power(ALGEBRAIC(:,13), 0.500000);
    ALGEBRAIC(:,15) = 1.00000./ALGEBRAIC(:,14);
    ALGEBRAIC(:,19) = ALGEBRAIC(:,18)+18.0000;
    ALGEBRAIC(:,20) = 1.00000./( ALGEBRAIC(:,19).*0.0357000);
    ALGEBRAIC(:,21) = ALGEBRAIC(:,20)+ALGEBRAIC(:,15);
    ALGEBRAIC(:,22) = ALGEBRAIC(:,9) - ALGEBRAIC(:,18);
    ALGEBRAIC(:,23) = ALGEBRAIC(:,22)./ALGEBRAIC(:,21);
    ALGEBRAIC(:,44) = ALGEBRAIC(:,23) - ALGEBRAIC(:,42);
    ALGEBRAIC(:,26) = STATES(:,1)+ ALGEBRAIC(:,1).*0.398600;
    ALGEBRAIC(:,27) = ((((ALGEBRAIC(:,26) - CONSTANTS(:,25)) - CONSTANTS(:,41)) - CONSTANTS(:,24)) - CONSTANTS(:,23)) - CONSTANTS(:,7);
    ALGEBRAIC(:,28) = piecewise({ALGEBRAIC(:,27)<0.000100000, 0.000100000 }, ALGEBRAIC(:,27));
    ALGEBRAIC(:,29) = 3.70000+(ALGEBRAIC(:,28) - 0.740000)./CONSTANTS(:,31);
    ALGEBRAIC(:,30) = piecewise({ALGEBRAIC(:,29)<0.000100000, 0.000100000 }, ALGEBRAIC(:,29));
    ALGEBRAIC(:,32) = 0.740000./power(ALGEBRAIC(:,30)./( CONSTANTS(:,20).*3.70000), 0.500000);
    ALGEBRAIC(:,2) = STATES(:,5)+ ALGEBRAIC(:,1).*0.0574000;
    ALGEBRAIC(:,3) = ALGEBRAIC(:,2) - 0.100000;
    ALGEBRAIC(:,4) = ALGEBRAIC(:,3)./0.00500000;
    ALGEBRAIC(:,31) = piecewise({ALGEBRAIC(:,4)<CONSTANTS(:,32), CONSTANTS(:,32) }, ALGEBRAIC(:,4));
    ALGEBRAIC(:,33) = ALGEBRAIC(:,30) - ALGEBRAIC(:,31);
    ALGEBRAIC(:,34) = ALGEBRAIC(:,33)./ALGEBRAIC(:,32);
    ALGEBRAIC(:,10) = (ALGEBRAIC(:,9)./CONSTANTS(:,9))./CONSTANTS(:,16);
    ALGEBRAIC(:,11) = piecewise({ALGEBRAIC(:,10)<=0.00000, 1.06000 , ALGEBRAIC(:,10)>0.00000&ALGEBRAIC(:,10)<=32.0000, 1.06000+( (0.970000 - 1.06000).*(ALGEBRAIC(:,10) - 0.00000))./(32.0000 - 0.00000) , ALGEBRAIC(:,10)>32.0000&ALGEBRAIC(:,10)<=38.4000, 0.970000+( (0.930000 - 0.970000).*(ALGEBRAIC(:,10) - 32.0000))./(38.4000 - 32.0000) , ALGEBRAIC(:,10)>38.4000&ALGEBRAIC(:,10)<=48.0000, 0.930000+( (0.800000 - 0.930000).*(ALGEBRAIC(:,10) - 38.4000))./(48.0000 - 38.4000) , ALGEBRAIC(:,10)>48.0000&ALGEBRAIC(:,10)<=60.8000, 0.800000+( (0.460000 - 0.800000).*(ALGEBRAIC(:,10) - 48.0000))./(60.8000 - 48.0000) , ALGEBRAIC(:,10)>60.8000&ALGEBRAIC(:,10)<=72.0000, 0.460000+( (0.00000 - 0.460000).*(ALGEBRAIC(:,10) - 60.8000))./(72.0000 - 60.8000) }, 0.00000);
    ALGEBRAIC(:,43) =  (1.00000 - CONSTANTS(:,27)).*CONSTANTS(:,9).*ALGEBRAIC(:,11).*CONSTANTS(:,28).*CONSTANTS(:,12).*CONSTANTS(:,14)+( CONSTANTS(:,27).*ALGEBRAIC(:,42))./ALGEBRAIC(:,25);
    ALGEBRAIC(:,5) =  (ALGEBRAIC(:,4)+8.00000).*( CONSTANTS(:,26).*(CONSTANTS(:,8) - 1.00000)+1.00000) - 8.00000;
    ALGEBRAIC(:,6) = piecewise({ALGEBRAIC(:,5)<= - 8.00000, 0.00000 , ALGEBRAIC(:,5)> - 8.00000&ALGEBRAIC(:,5)<= - 6.00000, 0.00000+( (0.750000 - 0.00000).*(ALGEBRAIC(:,5) -  - 8.00000))./( - 6.00000 -  - 8.00000) , ALGEBRAIC(:,5)> - 6.00000&ALGEBRAIC(:,5)<= - 2.00000, 0.750000+( (2.60000 - 0.750000).*(ALGEBRAIC(:,5) -  - 6.00000))./( - 2.00000 -  - 6.00000) , ALGEBRAIC(:,5)> - 2.00000&ALGEBRAIC(:,5)<=4.00000, 2.60000+( (9.80000 - 2.60000).*(ALGEBRAIC(:,5) -  - 2.00000))./(4.00000 -  - 2.00000) , ALGEBRAIC(:,5)>4.00000&ALGEBRAIC(:,5)<=12.0000, 9.80000+( (13.5000 - 9.80000).*(ALGEBRAIC(:,5) - 4.00000))./(12.0000 - 4.00000) }, 13.5000);
    ALGEBRAIC(:,46) =  ALGEBRAIC(:,6).*ALGEBRAIC(:,43);
    ALGEBRAIC(:,48) = ALGEBRAIC(:,34) - ALGEBRAIC(:,46);
    ALGEBRAIC(:,49) = ALGEBRAIC(:,46) - ALGEBRAIC(:,23);
    ALGEBRAIC(:,45) = ALGEBRAIC(:,37) - ALGEBRAIC(:,4);
    ALGEBRAIC(:,57) =  ALGEBRAIC(:,45).*CONSTANTS(:,40);
    ALGEBRAIC(:,52) = ALGEBRAIC(:,37) - ALGEBRAIC(:,30);
    ALGEBRAIC(:,47) = power(ALGEBRAIC(:,37)./100.000, CONSTANTS(:,36));
    ALGEBRAIC(:,50) = (( CONSTANTS(:,3).*CONSTANTS(:,1).*CONSTANTS(:,10).*CONSTANTS(:,20).*CONSTANTS(:,17))./ALGEBRAIC(:,47))./CONSTANTS(:,6);
    ALGEBRAIC(:,53) =  CONSTANTS(:,39).*CONSTANTS(:,2).*ALGEBRAIC(:,50).*CONSTANTS(:,15).*CONSTANTS(:,38);
    ALGEBRAIC(:,55) = ALGEBRAIC(:,52)./ALGEBRAIC(:,53);
    ALGEBRAIC(:,51) =  CONSTANTS(:,37).*CONSTANTS(:,5).*ALGEBRAIC(:,50).*CONSTANTS(:,15).*CONSTANTS(:,38);
    ALGEBRAIC(:,54) = ALGEBRAIC(:,51)+CONSTANTS(:,45);
    ALGEBRAIC(:,56) = ALGEBRAIC(:,52)./ALGEBRAIC(:,54);
    ALGEBRAIC(:,58) = ALGEBRAIC(:,55)+ALGEBRAIC(:,56)+CONSTANTS(:,19);
    ALGEBRAIC(:,59) = ALGEBRAIC(:,58)+ALGEBRAIC(:,57);
    ALGEBRAIC(:,60) = ALGEBRAIC(:,59) - ALGEBRAIC(:,34);
    ALGEBRAIC(:,61) = ALGEBRAIC(:,42) - ALGEBRAIC(:,59);
    ALGEBRAIC(:,62) = ALGEBRAIC(:,45)./ALGEBRAIC(:,59);
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