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 =24;
end
% There are a total of 7 entries in each of the rate and state variable arrays.
% There are a total of 58 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 environment (second)');
    LEGEND_ALGEBRAIC(:,11) = strpad('Pi in component TempCDa (UnitP)');
    LEGEND_STATES(:,1) = strpad('Pi in component TempRLC (UnitP)');
    LEGEND_ALGEBRAIC(:,19) = strpad('Qo in component TempRC (UnitQ)');
    LEGEND_ALGEBRAIC(:,22) = strpad('Qvi in component TempIPump (UnitQ)');
    LEGEND_ALGEBRAIC(:,6) = strpad('Qo in component TempCDv (UnitQ)');
    LEGEND_ALGEBRAIC(:,7) = strpad('Pi in component TJoint (UnitP)');
    LEGEND_ALGEBRAIC(:,20) = strpad('Qvo in component TempIPump (UnitQ)');
    LEGEND_ALGEBRAIC(:,15) = strpad('Pi in component TJoint (UnitP)');
    LEGEND_ALGEBRAIC(:,4) = strpad('Pi in component TempCDv (UnitP)');
    LEGEND_ALGEBRAIC(:,24) = strpad('Qo in component TJoint (UnitQ)');
    LEGEND_CONSTANTS(:,1) = strpad('CVao in component ParaHeartP (UnitCV)');
    LEGEND_ALGEBRAIC(:,3) = strpad('E in component EVentricle (UnitE)');
    LEGEND_STATES(:,2) = strpad('V in component TempCDv (UnitV)');
    LEGEND_CONSTANTS(:,2) = strpad('PlvIni in component ParaHeartP (UnitP)');
    LEGEND_CONSTANTS(:,3) = strpad('VlvIni in component ParaHeartP (UnitV)');
    LEGEND_ALGEBRAIC(:,5) = strpad('Tao in component TempCDv (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('Vlv0 in component ParaHeartP (UnitV)');
    LEGEND_ALGEBRAIC(:,13) = strpad('Qo in component TempCDa (UnitQ)');
    LEGEND_CONSTANTS(:,5) = strpad('CVmi in component ParaHeartP (UnitCV)');
    LEGEND_ALGEBRAIC(:,10) = strpad('E in component EAtrium (UnitE)');
    LEGEND_STATES(:,3) = strpad('V in component TempCDa (UnitV)');
    LEGEND_CONSTANTS(:,6) = strpad('PlaIni in component ParaHeartP (UnitP)');
    LEGEND_CONSTANTS(:,7) = strpad('VlaIni in component ParaHeartP (UnitV)');
    LEGEND_ALGEBRAIC(:,12) = strpad('Tao in component TempCDa (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('Vla0 in component ParaHeartP (UnitV)');
    LEGEND_CONSTANTS(:,9) = strpad('ElvMax in component ParaHeartP (UnitE)');
    LEGEND_CONSTANTS(:,10) = strpad('ElvMin in component ParaHeartP (UnitE)');
    LEGEND_CONSTANTS(:,11) = strpad('T in component ParaHeartP (second)');
    LEGEND_CONSTANTS(:,12) = strpad('Ts1 in component ParaHeartP (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('Ts2 in component ParaHeartP (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('mt in component EVentricle (second)');
    LEGEND_ALGEBRAIC(:,2) = strpad('et in component EVentricle (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('ElaMax in component ParaHeartP (UnitE)');
    LEGEND_CONSTANTS(:,15) = strpad('ElaMin in component ParaHeartP (UnitE)');
    LEGEND_CONSTANTS(:,16) = strpad('Tpwb in component ParaHeartP (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('Tpww in component ParaHeartP (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('mt in component EAtrium (second)');
    LEGEND_ALGEBRAIC(:,9) = strpad('et in component EAtrium (dimensionless)');
    LEGEND_STATES(:,4) = strpad('Qo in component TempRLC (UnitQ)');
    LEGEND_CONSTANTS(:,18) = strpad('Rsas in component ParaSys (UnitR)');
    LEGEND_CONSTANTS(:,19) = strpad('Csas in component ParaSys (UnitC)');
    LEGEND_CONSTANTS(:,20) = strpad('Lsas in component ParaSys (UnitL)');
    LEGEND_CONSTANTS(:,21) = strpad('P0sas in component ParaSys (UnitP)');
    LEGEND_CONSTANTS(:,22) = strpad('Q0sas in component ParaSys (UnitQ)');
    LEGEND_STATES(:,5) = strpad('Pi in component TempRLC (UnitP)');
    LEGEND_ALGEBRAIC(:,23) = strpad('Qo in component TJoint (UnitQ)');
    LEGEND_ALGEBRAIC(:,21) = strpad('Pi in component TempR (UnitP)');
    LEGEND_STATES(:,6) = strpad('Qo in component TempRLC (UnitQ)');
    LEGEND_CONSTANTS(:,23) = strpad('Rsat in component ParaSys (UnitR)');
    LEGEND_CONSTANTS(:,24) = strpad('Csat in component ParaSys (UnitC)');
    LEGEND_CONSTANTS(:,25) = strpad('Lsat in component ParaSys (UnitL)');
    LEGEND_CONSTANTS(:,26) = strpad('P0sat in component ParaSys (UnitP)');
    LEGEND_CONSTANTS(:,27) = strpad('Q0sat in component ParaSys (UnitQ)');
    LEGEND_ALGEBRAIC(:,18) = strpad('Pi in component TempR (UnitP)');
    LEGEND_ALGEBRAIC(:,14) = strpad('Qo in component TempR (UnitQ)');
    LEGEND_CONSTANTS(:,28) = strpad('Rsar in component ParaSys (UnitR)');
    LEGEND_STATES(:,7) = strpad('Pi in component TempRC (UnitP)');
    LEGEND_ALGEBRAIC(:,16) = strpad('Qo in component TempR (UnitQ)');
    LEGEND_CONSTANTS(:,29) = strpad('Rscp in component ParaSys (UnitR)');
    LEGEND_CONSTANTS(:,30) = strpad('Rsvn in component ParaSys (UnitR)');
    LEGEND_CONSTANTS(:,31) = strpad('Csvn in component ParaSys (UnitC)');
    LEGEND_CONSTANTS(:,32) = strpad('P0svn in component ParaSys (UnitP)');
    LEGEND_CONSTANTS(:,58) = strpad('Wn in component TempIPump (UnitRPM)');
    LEGEND_CONSTANTS(:,33) = strpad('Ts2 in component ParaHeartP (dimensionless)');
    LEGEND_CONSTANTS(:,34) = strpad('T in component ParaHeartP (second)');
    LEGEND_CONSTANTS(:,35) = strpad('Kp0 in component ParaIPump (UnitKp0)');
    LEGEND_CONSTANTS(:,36) = strpad('Kp1 in component ParaIPump (UnitKp1)');
    LEGEND_CONSTANTS(:,37) = strpad('Kp2 in component ParaIPump (UnitKp2)');
    LEGEND_CONSTANTS(:,38) = strpad('Kp3 in component ParaIPump (UnitKp3)');
    LEGEND_CONSTANTS(:,39) = strpad('Kp4 in component ParaIPump (UnitKp4)');
    LEGEND_CONSTANTS(:,40) = strpad('Kp5 in component ParaIPump (UnitKp5)');
    LEGEND_CONSTANTS(:,41) = strpad('Kp6 in component ParaIPump (UnitKp6)');
    LEGEND_CONSTANTS(:,42) = strpad('W in component ParaIPump (UnitRPM)');
    LEGEND_ALGEBRAIC(:,17) = strpad('dPv in component TempIPump (UnitP)');
    LEGEND_CONSTANTS(:,43) = strpad('ElaMax in component ParaHeartP (UnitE)');
    LEGEND_CONSTANTS(:,44) = strpad('ElaMin in component ParaHeartP (UnitE)');
    LEGEND_CONSTANTS(:,45) = strpad('PlaIni in component ParaHeartP (UnitP)');
    LEGEND_CONSTANTS(:,46) = strpad('VlaIni in component ParaHeartP (UnitV)');
    LEGEND_CONSTANTS(:,47) = strpad('ElvMax in component ParaHeartP (UnitE)');
    LEGEND_CONSTANTS(:,48) = strpad('ElvMin in component ParaHeartP (UnitE)');
    LEGEND_CONSTANTS(:,49) = strpad('PlvIni in component ParaHeartP (UnitP)');
    LEGEND_CONSTANTS(:,50) = strpad('VlvIni in component ParaHeartP (UnitV)');
    LEGEND_CONSTANTS(:,51) = strpad('Tpwb in component ParaHeartP (dimensionless)');
    LEGEND_CONSTANTS(:,52) = strpad('Tpww in component ParaHeartP (dimensionless)');
    LEGEND_CONSTANTS(:,53) = strpad('Ts1 in component ParaHeartP (dimensionless)');
    LEGEND_CONSTANTS(:,54) = strpad('CVao in component ParaHeartP (UnitCV)');
    LEGEND_CONSTANTS(:,55) = strpad('CVmi in component ParaHeartP (UnitCV)');
    LEGEND_CONSTANTS(:,56) = strpad('Vlv0 in component ParaHeartP (UnitV)');
    LEGEND_CONSTANTS(:,57) = strpad('Vla0 in component ParaHeartP (UnitV)');
    LEGEND_RATES(:,2) = strpad('d/dt V in component TempCDv (UnitV)');
    LEGEND_RATES(:,3) = strpad('d/dt V in component TempCDa (UnitV)');
    LEGEND_RATES(:,1) = strpad('d/dt Pi in component TempRLC (UnitP)');
    LEGEND_RATES(:,4) = strpad('d/dt Qo in component TempRLC (UnitQ)');
    LEGEND_RATES(:,5) = strpad('d/dt Pi in component TempRLC (UnitP)');
    LEGEND_RATES(:,6) = strpad('d/dt Qo in component TempRLC (UnitQ)');
    LEGEND_RATES(:,7) = strpad('d/dt Pi in component TempRC (UnitP)');
    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) = 350.;
    CONSTANTS(:,2) = 1.0;
    CONSTANTS(:,3) = 5.0;
    CONSTANTS(:,4) = 500;
    CONSTANTS(:,5) = 400.;
    CONSTANTS(:,6) = 1.0;
    CONSTANTS(:,7) = 4.0;
    CONSTANTS(:,8) = 20;
    CONSTANTS(:,9) = 0.5;
    CONSTANTS(:,10) = 0.1;
    CONSTANTS(:,11) = 1.0;
    CONSTANTS(:,12) = 0.3;
    CONSTANTS(:,13) = 0.45;
    CONSTANTS(:,14) = 0.25;
    CONSTANTS(:,15) = 0.15;
    CONSTANTS(:,16) = 0.92;
    CONSTANTS(:,17) = 0.09;
    CONSTANTS(:,18) = 0.003;
    CONSTANTS(:,19) = 0.08;
    CONSTANTS(:,20) = 0.000062;
    CONSTANTS(:,21) = 100.;
    CONSTANTS(:,22) = 0.;
    CONSTANTS(:,23) = 0.05;
    CONSTANTS(:,24) = 1.6;
    CONSTANTS(:,25) = 0.0017;
    CONSTANTS(:,26) = 100.;
    CONSTANTS(:,27) = 0.;
    CONSTANTS(:,28) = 0.5;
    CONSTANTS(:,29) = 0.52;
    CONSTANTS(:,30) = 0.075;
    CONSTANTS(:,31) = 20.5;
    CONSTANTS(:,32) = 0.;
    CONSTANTS(:,33) = 0.45;
    CONSTANTS(:,34) = 1.0;
    CONSTANTS(:,35) = 19.51840;
    CONSTANTS(:,36) = -3.03610e-3;
    CONSTANTS(:,37) = -1.23045;
    CONSTANTS(:,38) = 5.78974e-4;
    CONSTANTS(:,39) = -5.8777e-8;
    CONSTANTS(:,40) = -1.27539e-6;
    CONSTANTS(:,41) = 2.04834e-10;
    CONSTANTS(:,42) = 3600;
    CONSTANTS(:,43) = 0.25;
    CONSTANTS(:,44) = 0.15;
    CONSTANTS(:,45) = 1.0;
    CONSTANTS(:,46) = 4.0;
    CONSTANTS(:,47) = 0.5;
    CONSTANTS(:,48) = 0.1;
    CONSTANTS(:,49) = 1.0;
    CONSTANTS(:,50) = 5.0;
    CONSTANTS(:,51) = 0.92;
    CONSTANTS(:,52) = 0.09;
    CONSTANTS(:,53) = 0.3;
    CONSTANTS(:,54) = 350.;
    CONSTANTS(:,55) = 400.;
    CONSTANTS(:,56) = 500;
    CONSTANTS(:,57) = 20;
    CONSTANTS(:,58) =  1.00000.*CONSTANTS(:,42);
    STATES(:,1) = CONSTANTS(:,21);
    STATES(:,2) = CONSTANTS(:,4);
    STATES(:,3) = CONSTANTS(:,8);
    STATES(:,4) = CONSTANTS(:,22);
    STATES(:,5) = CONSTANTS(:,26);
    STATES(:,6) = CONSTANTS(:,27);
    STATES(:,7) = CONSTANTS(:,32);
    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) = VOI -  CONSTANTS(:,11).*floor(VOI./CONSTANTS(:,11));
    ALGEBRAIC(:,2) = piecewise({ALGEBRAIC(:,1)>=0.00000&ALGEBRAIC(:,1)<= CONSTANTS(:,12).*CONSTANTS(:,11), 1.00000 - cos(( 3.14159.*ALGEBRAIC(:,1))./( CONSTANTS(:,12).*CONSTANTS(:,11))) , ALGEBRAIC(:,1)> CONSTANTS(:,12).*CONSTANTS(:,11)&ALGEBRAIC(:,1)<= CONSTANTS(:,13).*CONSTANTS(:,11), 1.00000+cos(( 3.14159.*(ALGEBRAIC(:,1) -  CONSTANTS(:,12).*CONSTANTS(:,11)))./( (CONSTANTS(:,13) - CONSTANTS(:,12)).*CONSTANTS(:,11))) , ALGEBRAIC(:,1)> CONSTANTS(:,13).*CONSTANTS(:,11)&ALGEBRAIC(:,1)<CONSTANTS(:,11), 0.00000 }, NaN);
    ALGEBRAIC(:,3) = CONSTANTS(:,10)+( ALGEBRAIC(:,2).*(CONSTANTS(:,9) - CONSTANTS(:,10)))./2.00000;
    ALGEBRAIC(:,4) = CONSTANTS(:,2)+ ALGEBRAIC(:,3).*(STATES(:,2) - CONSTANTS(:,3));
    ALGEBRAIC(:,5) = piecewise({ALGEBRAIC(:,4)>=STATES(:,1), 1.00000 , ALGEBRAIC(:,4)<STATES(:,1), 0.00000 }, NaN);
    ALGEBRAIC(:,6) = piecewise({ALGEBRAIC(:,4)>=STATES(:,1),  CONSTANTS(:,1).*ALGEBRAIC(:,5).*power(abs(ALGEBRAIC(:,4) - STATES(:,1)), 0.500000) , ALGEBRAIC(:,4)<STATES(:,1),  -1.00000.*CONSTANTS(:,1).*ALGEBRAIC(:,5).*power(abs(STATES(:,1) - ALGEBRAIC(:,4)), 0.500000) }, NaN);
    RATES(:,1) = (ALGEBRAIC(:,6) - STATES(:,4))./CONSTANTS(:,19);
    ALGEBRAIC(:,15) = STATES(:,5);
    RATES(:,4) = ((STATES(:,1) - ALGEBRAIC(:,15)) -  CONSTANTS(:,18).*STATES(:,4))./CONSTANTS(:,20);
    ALGEBRAIC(:,8) = VOI -  CONSTANTS(:,11).*floor(VOI./CONSTANTS(:,11));
    ALGEBRAIC(:,9) = piecewise({ALGEBRAIC(:,8)>=0.00000&ALGEBRAIC(:,8)<= ((CONSTANTS(:,16)+CONSTANTS(:,17)) - 1.00000).*CONSTANTS(:,11), 1.00000 - cos(( 2.00000.*3.14159.*(ALGEBRAIC(:,8) -  (CONSTANTS(:,16) - 1.00000).*CONSTANTS(:,11)))./( CONSTANTS(:,17).*CONSTANTS(:,11))) , ALGEBRAIC(:,8)> ((CONSTANTS(:,16)+CONSTANTS(:,17)) - 1.00000).*CONSTANTS(:,11)&ALGEBRAIC(:,8)<= CONSTANTS(:,16).*CONSTANTS(:,11), 0.00000 , ALGEBRAIC(:,8)> CONSTANTS(:,16).*CONSTANTS(:,11)&ALGEBRAIC(:,8)<=CONSTANTS(:,11), 1.00000 - cos(( 2.00000.*3.14159.*(ALGEBRAIC(:,8) -  CONSTANTS(:,16).*CONSTANTS(:,11)))./( CONSTANTS(:,17).*CONSTANTS(:,11))) }, NaN);
    ALGEBRAIC(:,10) = CONSTANTS(:,15)+( ALGEBRAIC(:,9).*(CONSTANTS(:,14) - CONSTANTS(:,15)))./2.00000;
    ALGEBRAIC(:,11) = CONSTANTS(:,6)+ ALGEBRAIC(:,10).*(STATES(:,3) - CONSTANTS(:,7));
    ALGEBRAIC(:,19) = (STATES(:,7) - ALGEBRAIC(:,11))./CONSTANTS(:,30);
    ALGEBRAIC(:,7) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,12) = piecewise({ALGEBRAIC(:,11)>=ALGEBRAIC(:,7), 1.00000 , ALGEBRAIC(:,11)<ALGEBRAIC(:,7), 0.00000 }, NaN);
    ALGEBRAIC(:,13) = piecewise({ALGEBRAIC(:,11)>=ALGEBRAIC(:,7),  CONSTANTS(:,5).*ALGEBRAIC(:,12).*power(abs(ALGEBRAIC(:,11) - ALGEBRAIC(:,7)), 0.500000) , ALGEBRAIC(:,11)<ALGEBRAIC(:,7),  -1.00000.*CONSTANTS(:,5).*ALGEBRAIC(:,12).*power(abs(ALGEBRAIC(:,7) - ALGEBRAIC(:,11)), 0.500000) }, NaN);
    RATES(:,3) = ALGEBRAIC(:,19) - ALGEBRAIC(:,13);
    ALGEBRAIC(:,14) = STATES(:,6);
    ALGEBRAIC(:,18) = STATES(:,7)+ CONSTANTS(:,29).*ALGEBRAIC(:,14);
    ALGEBRAIC(:,21) = ALGEBRAIC(:,18)+ CONSTANTS(:,28).*STATES(:,6);
    RATES(:,6) = ((STATES(:,5) - ALGEBRAIC(:,21)) -  CONSTANTS(:,23).*STATES(:,6))./CONSTANTS(:,25);
    ALGEBRAIC(:,16) = ALGEBRAIC(:,14);
    RATES(:,7) = (ALGEBRAIC(:,16) - ALGEBRAIC(:,19))./CONSTANTS(:,31);
    ALGEBRAIC(:,17) = ALGEBRAIC(:,15) - ALGEBRAIC(:,7);
    ALGEBRAIC(:,20) =  (CONSTANTS(:,35)+ CONSTANTS(:,36).*CONSTANTS(:,58)+ CONSTANTS(:,37).*ALGEBRAIC(:,17)+ CONSTANTS(:,38).*CONSTANTS(:,58).*ALGEBRAIC(:,17)+ CONSTANTS(:,39).*power(CONSTANTS(:,58), 2.00000).*ALGEBRAIC(:,17)+ CONSTANTS(:,40).*CONSTANTS(:,58).*power(ALGEBRAIC(:,17), 2.00000)+ CONSTANTS(:,41).*power(CONSTANTS(:,58), 2.00000).*power(ALGEBRAIC(:,17), 2.00000)).*16.6667;
    ALGEBRAIC(:,23) = STATES(:,4)+ALGEBRAIC(:,20);
    RATES(:,5) = (ALGEBRAIC(:,23) - STATES(:,6))./CONSTANTS(:,24);
    ALGEBRAIC(:,22) =  -1.00000.*ALGEBRAIC(:,20);
    ALGEBRAIC(:,24) = ALGEBRAIC(:,13)+ALGEBRAIC(:,22);
    RATES(:,2) = ALGEBRAIC(:,24) - ALGEBRAIC(:,6);
   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) = VOI -  CONSTANTS(:,11).*floor(VOI./CONSTANTS(:,11));
    ALGEBRAIC(:,2) = piecewise({ALGEBRAIC(:,1)>=0.00000&ALGEBRAIC(:,1)<= CONSTANTS(:,12).*CONSTANTS(:,11), 1.00000 - cos(( 3.14159.*ALGEBRAIC(:,1))./( CONSTANTS(:,12).*CONSTANTS(:,11))) , ALGEBRAIC(:,1)> CONSTANTS(:,12).*CONSTANTS(:,11)&ALGEBRAIC(:,1)<= CONSTANTS(:,13).*CONSTANTS(:,11), 1.00000+cos(( 3.14159.*(ALGEBRAIC(:,1) -  CONSTANTS(:,12).*CONSTANTS(:,11)))./( (CONSTANTS(:,13) - CONSTANTS(:,12)).*CONSTANTS(:,11))) , ALGEBRAIC(:,1)> CONSTANTS(:,13).*CONSTANTS(:,11)&ALGEBRAIC(:,1)<CONSTANTS(:,11), 0.00000 }, NaN);
    ALGEBRAIC(:,3) = CONSTANTS(:,10)+( ALGEBRAIC(:,2).*(CONSTANTS(:,9) - CONSTANTS(:,10)))./2.00000;
    ALGEBRAIC(:,4) = CONSTANTS(:,2)+ ALGEBRAIC(:,3).*(STATES(:,2) - CONSTANTS(:,3));
    ALGEBRAIC(:,5) = piecewise({ALGEBRAIC(:,4)>=STATES(:,1), 1.00000 , ALGEBRAIC(:,4)<STATES(:,1), 0.00000 }, NaN);
    ALGEBRAIC(:,6) = piecewise({ALGEBRAIC(:,4)>=STATES(:,1),  CONSTANTS(:,1).*ALGEBRAIC(:,5).*power(abs(ALGEBRAIC(:,4) - STATES(:,1)), 0.500000) , ALGEBRAIC(:,4)<STATES(:,1),  -1.00000.*CONSTANTS(:,1).*ALGEBRAIC(:,5).*power(abs(STATES(:,1) - ALGEBRAIC(:,4)), 0.500000) }, NaN);
    ALGEBRAIC(:,15) = STATES(:,5);
    ALGEBRAIC(:,8) = VOI -  CONSTANTS(:,11).*floor(VOI./CONSTANTS(:,11));
    ALGEBRAIC(:,9) = piecewise({ALGEBRAIC(:,8)>=0.00000&ALGEBRAIC(:,8)<= ((CONSTANTS(:,16)+CONSTANTS(:,17)) - 1.00000).*CONSTANTS(:,11), 1.00000 - cos(( 2.00000.*3.14159.*(ALGEBRAIC(:,8) -  (CONSTANTS(:,16) - 1.00000).*CONSTANTS(:,11)))./( CONSTANTS(:,17).*CONSTANTS(:,11))) , ALGEBRAIC(:,8)> ((CONSTANTS(:,16)+CONSTANTS(:,17)) - 1.00000).*CONSTANTS(:,11)&ALGEBRAIC(:,8)<= CONSTANTS(:,16).*CONSTANTS(:,11), 0.00000 , ALGEBRAIC(:,8)> CONSTANTS(:,16).*CONSTANTS(:,11)&ALGEBRAIC(:,8)<=CONSTANTS(:,11), 1.00000 - cos(( 2.00000.*3.14159.*(ALGEBRAIC(:,8) -  CONSTANTS(:,16).*CONSTANTS(:,11)))./( CONSTANTS(:,17).*CONSTANTS(:,11))) }, NaN);
    ALGEBRAIC(:,10) = CONSTANTS(:,15)+( ALGEBRAIC(:,9).*(CONSTANTS(:,14) - CONSTANTS(:,15)))./2.00000;
    ALGEBRAIC(:,11) = CONSTANTS(:,6)+ ALGEBRAIC(:,10).*(STATES(:,3) - CONSTANTS(:,7));
    ALGEBRAIC(:,19) = (STATES(:,7) - ALGEBRAIC(:,11))./CONSTANTS(:,30);
    ALGEBRAIC(:,7) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,12) = piecewise({ALGEBRAIC(:,11)>=ALGEBRAIC(:,7), 1.00000 , ALGEBRAIC(:,11)<ALGEBRAIC(:,7), 0.00000 }, NaN);
    ALGEBRAIC(:,13) = piecewise({ALGEBRAIC(:,11)>=ALGEBRAIC(:,7),  CONSTANTS(:,5).*ALGEBRAIC(:,12).*power(abs(ALGEBRAIC(:,11) - ALGEBRAIC(:,7)), 0.500000) , ALGEBRAIC(:,11)<ALGEBRAIC(:,7),  -1.00000.*CONSTANTS(:,5).*ALGEBRAIC(:,12).*power(abs(ALGEBRAIC(:,7) - ALGEBRAIC(:,11)), 0.500000) }, NaN);
    ALGEBRAIC(:,14) = STATES(:,6);
    ALGEBRAIC(:,18) = STATES(:,7)+ CONSTANTS(:,29).*ALGEBRAIC(:,14);
    ALGEBRAIC(:,21) = ALGEBRAIC(:,18)+ CONSTANTS(:,28).*STATES(:,6);
    ALGEBRAIC(:,16) = ALGEBRAIC(:,14);
    ALGEBRAIC(:,17) = ALGEBRAIC(:,15) - ALGEBRAIC(:,7);
    ALGEBRAIC(:,20) =  (CONSTANTS(:,35)+ CONSTANTS(:,36).*CONSTANTS(:,58)+ CONSTANTS(:,37).*ALGEBRAIC(:,17)+ CONSTANTS(:,38).*CONSTANTS(:,58).*ALGEBRAIC(:,17)+ CONSTANTS(:,39).*power(CONSTANTS(:,58), 2.00000).*ALGEBRAIC(:,17)+ CONSTANTS(:,40).*CONSTANTS(:,58).*power(ALGEBRAIC(:,17), 2.00000)+ CONSTANTS(:,41).*power(CONSTANTS(:,58), 2.00000).*power(ALGEBRAIC(:,17), 2.00000)).*16.6667;
    ALGEBRAIC(:,23) = STATES(:,4)+ALGEBRAIC(:,20);
    ALGEBRAIC(:,22) =  -1.00000.*ALGEBRAIC(:,20);
    ALGEBRAIC(:,24) = ALGEBRAIC(:,13)+ALGEBRAIC(:,22);
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