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 =34;
end
% There are a total of 9 entries in each of the rate and state variable arrays.
% There are a total of 54 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('isotonic in component parameters (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('alpha_1 in component parameters (per_micrometre)');
    LEGEND_CONSTANTS(:,3) = strpad('beta_1 in component parameters (millinewton)');
    LEGEND_CONSTANTS(:,4) = strpad('alpha_2 in component parameters (per_micrometre)');
    LEGEND_CONSTANTS(:,5) = strpad('beta_2 in component parameters (millinewton)');
    LEGEND_CONSTANTS(:,6) = strpad('alpha_3 in component parameters (per_micrometre)');
    LEGEND_CONSTANTS(:,7) = strpad('beta_3 in component parameters (millinewton)');
    LEGEND_CONSTANTS(:,8) = strpad('lambda in component parameters (millinewton)');
    LEGEND_CONSTANTS(:,9) = strpad('A_half in component parameters (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('mu in component parameters (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('chi in component parameters (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('chi_0 in component parameters (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('m_0 in component parameters (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('v_max in component parameters (micrometre_per_second)');
    LEGEND_CONSTANTS(:,15) = strpad('a in component parameters (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('d_h in component parameters (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('alpha_P in component parameters (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('l in component length (micrometre)');
    LEGEND_ALGEBRAIC(:,6) = strpad('F_muscle in component force (millinewton)');
    LEGEND_ALGEBRAIC(:,12) = strpad('flag in component isotonic (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('F_afterload in component isotonic (millinewton)');
    LEGEND_ALGEBRAIC(:,14) = strpad('isotonic_mode in component isotonic (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('l_0 in component isotonic (micrometre)');
    LEGEND_CONSTANTS(:,20) = strpad('S_0 in component parameters_izakov_et_al_1991 (micrometre)');
    LEGEND_ALGEBRAIC(:,1) = strpad('q_v in component parameters_izakov_et_al_1991 (per_second)');
    LEGEND_CONSTANTS(:,21) = strpad('q_1 in component parameters_izakov_et_al_1991 (per_second)');
    LEGEND_CONSTANTS(:,22) = strpad('q_2 in component parameters_izakov_et_al_1991 (per_second)');
    LEGEND_CONSTANTS(:,23) = strpad('q_3 in component parameters_izakov_et_al_1991 (per_second)');
    LEGEND_CONSTANTS(:,24) = strpad('q_4 in component parameters_izakov_et_al_1991 (per_second)');
    LEGEND_CONSTANTS(:,25) = strpad('v_star in component parameters_izakov_et_al_1991 (micrometre_per_second)');
    LEGEND_CONSTANTS(:,51) = strpad('v_1 in component parameters_izakov_et_al_1991 (micrometre_per_second)');
    LEGEND_CONSTANTS(:,26) = strpad('alpha_G in component parameters_izakov_et_al_1991 (dimensionless)');
    LEGEND_CONSTANTS(:,27) = strpad('a_on in component parameters_izakov_et_al_1991 (per_second)');
    LEGEND_CONSTANTS(:,28) = strpad('a_off in component parameters_izakov_et_al_1991 (per_second)');
    LEGEND_CONSTANTS(:,29) = strpad('k_A in component parameters_izakov_et_al_1991 (dimensionless)');
    LEGEND_STATES(:,1) = strpad('v in component CE_velocity (micrometre_per_second)');
    LEGEND_CONSTANTS(:,30) = strpad('alpha_Q in component parameters_izakov_et_al_1991 (dimensionless)');
    LEGEND_CONSTANTS(:,31) = strpad('beta_Q in component parameters_izakov_et_al_1991 (dimensionless)');
    LEGEND_ALGEBRAIC(:,32) = strpad('F_CE in component force (millinewton)');
    LEGEND_ALGEBRAIC(:,5) = strpad('F_XSE in component force (millinewton)');
    LEGEND_ALGEBRAIC(:,2) = strpad('F_SE in component force (millinewton)');
    LEGEND_ALGEBRAIC(:,3) = strpad('F_PE in component force (millinewton)');
    LEGEND_STATES(:,2) = strpad('N in component crossbridge_kinetics (dimensionless)');
    LEGEND_ALGEBRAIC(:,18) = strpad('k_P_vis in component CE_velocity (millinewton_second_per_micrometre)');
    LEGEND_ALGEBRAIC(:,21) = strpad('k_S_vis in component PE_velocity (millinewton_second_per_micrometre)');
    LEGEND_STATES(:,3) = strpad('w in component PE_velocity (micrometre_per_second)');
    LEGEND_STATES(:,4) = strpad('l_1 in component length (micrometre)');
    LEGEND_STATES(:,5) = strpad('l_2 in component length (micrometre)');
    LEGEND_STATES(:,6) = strpad('l_3 in component length (micrometre)');
    LEGEND_ALGEBRAIC(:,31) = strpad('p_v in component average_crossbridge_force (dimensionless)');
    LEGEND_ALGEBRAIC(:,28) = strpad('K_chi in component crossbridge_kinetics (per_second)');
    LEGEND_ALGEBRAIC(:,7) = strpad('M_A in component crossbridge_kinetics (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('n_1 in component crossbridge_kinetics (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('L_oz in component crossbridge_kinetics (dimensionless)');
    LEGEND_ALGEBRAIC(:,26) = strpad('k_p_v in component crossbridge_kinetics (per_second)');
    LEGEND_ALGEBRAIC(:,27) = strpad('k_m_v in component crossbridge_kinetics (per_second)');
    LEGEND_STATES(:,7) = strpad('A in component calcium_handling (dimensionless)');
    LEGEND_ALGEBRAIC(:,25) = strpad('G_star in component average_crossbridge_force (dimensionless)');
    LEGEND_ALGEBRAIC(:,22) = strpad('P_star in component average_crossbridge_force (dimensionless)');
    LEGEND_CONSTANTS(:,32) = strpad('v_0 in component crossbridge_kinetics (micrometre_per_second)');
    LEGEND_CONSTANTS(:,33) = strpad('q_star in component crossbridge_kinetics (per_second)');
    LEGEND_ALGEBRAIC(:,4) = strpad('dl_1_dt in component length (micrometre_per_second)');
    LEGEND_ALGEBRAIC(:,23) = strpad('dl_2_dt in component length (micrometre_per_second)');
    LEGEND_ALGEBRAIC(:,24) = strpad('dl_3_dt in component length (micrometre_per_second)');
    LEGEND_ALGEBRAIC(:,19) = strpad('phi_chi_2 in component CE_velocity (micrometre_per_second)');
    LEGEND_ALGEBRAIC(:,34) = strpad('phi_chi in component CE_velocity (micrometre_per_second2)');
    LEGEND_ALGEBRAIC(:,33) = strpad('p_prime_v in component average_crossbridge_force (second_per_micrometre)');
    LEGEND_CONSTANTS(:,34) = strpad('alpha_P_lengthening in component CE_velocity (per_micrometre)');
    LEGEND_CONSTANTS(:,35) = strpad('beta_P_lengthening in component CE_velocity (millinewton_second_per_micrometre)');
    LEGEND_CONSTANTS(:,36) = strpad('alpha_P_shortening in component CE_velocity (per_micrometre)');
    LEGEND_CONSTANTS(:,37) = strpad('beta_P_shortening in component CE_velocity (millinewton_second_per_micrometre)');
    LEGEND_ALGEBRAIC(:,16) = strpad('alp_p in component CE_velocity (per_micrometre)');
    LEGEND_CONSTANTS(:,38) = strpad('alpha_S_lengthening in component PE_velocity (per_micrometre)');
    LEGEND_CONSTANTS(:,39) = strpad('beta_S_lengthening in component PE_velocity (millinewton_second_per_micrometre)');
    LEGEND_CONSTANTS(:,40) = strpad('alpha_S_shortening in component PE_velocity (per_micrometre)');
    LEGEND_CONSTANTS(:,41) = strpad('beta_S_shortening in component PE_velocity (millinewton_second_per_micrometre)');
    LEGEND_ALGEBRAIC(:,20) = strpad('alp_s in component PE_velocity (per_micrometre)');
    LEGEND_CONSTANTS(:,54) = strpad('gamma in component average_crossbridge_force (dimensionless)');
    LEGEND_CONSTANTS(:,52) = strpad('case_1 in component average_crossbridge_force (second_per_micrometre)');
    LEGEND_ALGEBRAIC(:,29) = strpad('case_2 in component average_crossbridge_force (second_per_micrometre)');
    LEGEND_CONSTANTS(:,53) = strpad('case_3 in component average_crossbridge_force (second_per_micrometre)');
    LEGEND_ALGEBRAIC(:,30) = strpad('case_4 in component average_crossbridge_force (second_per_micrometre)');
    LEGEND_ALGEBRAIC(:,15) = strpad('dA_dt in component calcium_handling (per_second)');
    LEGEND_ALGEBRAIC(:,11) = strpad('N_A in component calcium_handling (dimensionless)');
    LEGEND_ALGEBRAIC(:,13) = strpad('pi_N_A in component calcium_handling (dimensionless)');
    LEGEND_STATES(:,8) = strpad('B in component calcium_handling (dimensionless)');
    LEGEND_ALGEBRAIC(:,17) = strpad('dB_dt in component calcium_handling (per_second)');
    LEGEND_STATES(:,9) = strpad('Ca_C in component calcium_handling (dimensionless)');
    LEGEND_CONSTANTS(:,42) = strpad('A_tot in component calcium_handling (dimensionless)');
    LEGEND_CONSTANTS(:,43) = strpad('B_tot in component calcium_handling (dimensionless)');
    LEGEND_CONSTANTS(:,44) = strpad('b_on in component calcium_handling (per_second)');
    LEGEND_CONSTANTS(:,45) = strpad('b_off in component calcium_handling (per_second)');
    LEGEND_CONSTANTS(:,46) = strpad('a_c in component calcium_handling (per_second2)');
    LEGEND_CONSTANTS(:,47) = strpad('r_Ca in component calcium_handling (per_second)');
    LEGEND_CONSTANTS(:,48) = strpad('q_Ca in component calcium_handling (dimensionless)');
    LEGEND_CONSTANTS(:,49) = strpad('t_d in component calcium_handling (second)');
    LEGEND_CONSTANTS(:,50) = strpad('Ca_m in component calcium_handling (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt N in component crossbridge_kinetics (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt l_1 in component length (micrometre)');
    LEGEND_RATES(:,5) = strpad('d/dt l_2 in component length (micrometre)');
    LEGEND_RATES(:,6) = strpad('d/dt l_3 in component length (micrometre)');
    LEGEND_RATES(:,1) = strpad('d/dt v in component CE_velocity (micrometre_per_second)');
    LEGEND_RATES(:,3) = strpad('d/dt w in component PE_velocity (micrometre_per_second)');
    LEGEND_RATES(:,7) = strpad('d/dt A in component calcium_handling (dimensionless)');
    LEGEND_RATES(:,8) = strpad('d/dt B in component calcium_handling (dimensionless)');
    LEGEND_RATES(:,9) = strpad('d/dt Ca_C in component calcium_handling (dimensionless)');
    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;
    CONSTANTS(:,2) = 19;
    CONSTANTS(:,3) = 0.29;
    CONSTANTS(:,4) = 14.6;
    CONSTANTS(:,5) = 0.000924;
    CONSTANTS(:,6) = 48;
    CONSTANTS(:,7) = 0.01;
    CONSTANTS(:,8) = 96;
    CONSTANTS(:,9) = 0.6;
    CONSTANTS(:,10) = 3;
    CONSTANTS(:,11) = 0.705;
    CONSTANTS(:,12) = 3;
    CONSTANTS(:,13) = 0.9;
    CONSTANTS(:,14) = 5.6;
    CONSTANTS(:,15) = 0.25;
    CONSTANTS(:,16) = 0.5;
    CONSTANTS(:,17) = 4;
    CONSTANTS(:,18) = 2;
    CONSTANTS(:,19) = 0.527;
    CONSTANTS(:,20) = 1.14;
    CONSTANTS(:,21) = 17.3;
    CONSTANTS(:,22) = 259;
    CONSTANTS(:,23) = 17.3;
    CONSTANTS(:,24) = 15;
    CONSTANTS(:,25) = 5.3035675;
    CONSTANTS(:,26) = 1;
    CONSTANTS(:,27) = 2300;
    CONSTANTS(:,28) = 290;
    CONSTANTS(:,29) = 2.8;
    STATES(:,1) = 0;
    CONSTANTS(:,30) = 10;
    CONSTANTS(:,31) = 5000;
    STATES(:,2) = 0.0001;
    STATES(:,3) = 0;
    STATES(:,4) = 0.437;
    STATES(:,5) = 0.439;
    STATES(:,6) = 0.089;
    STATES(:,7) = 0.01;
    CONSTANTS(:,32) = 560;
    CONSTANTS(:,33) = 1000;
    CONSTANTS(:,34) = 16;
    CONSTANTS(:,35) = 0.0015;
    CONSTANTS(:,36) = 16;
    CONSTANTS(:,37) = 0.0015;
    CONSTANTS(:,38) = 39;
    CONSTANTS(:,39) = 0.008;
    CONSTANTS(:,40) = 46;
    CONSTANTS(:,41) = 0.006;
    STATES(:,8) = 0;
    STATES(:,9) = 0;
    CONSTANTS(:,42) = 1;
    CONSTANTS(:,43) = 0.4;
    CONSTANTS(:,44) = 2600;
    CONSTANTS(:,45) = 182;
    CONSTANTS(:,46) = 5200;
    CONSTANTS(:,47) = 650;
    CONSTANTS(:,48) = 50;
    CONSTANTS(:,49) = 0.033;
    CONSTANTS(:,50) = 0.03;
    CONSTANTS(:,51) = CONSTANTS(:,14)./10.0000;
    CONSTANTS(:,52) = ( CONSTANTS(:,15).*(0.400000+ 0.400000.*CONSTANTS(:,15)))./( CONSTANTS(:,14).*power( (CONSTANTS(:,15)+1.00000).*0.400000, 2.00000));
    CONSTANTS(:,53) = ( 0.400000.*CONSTANTS(:,15)+1.00000)./( CONSTANTS(:,15).*CONSTANTS(:,14));
    CONSTANTS(:,54) = ( CONSTANTS(:,15).*CONSTANTS(:,16).*power(CONSTANTS(:,51)./CONSTANTS(:,14), 2.00000))./( 3.00000.*CONSTANTS(:,15).*CONSTANTS(:,16) - ( (CONSTANTS(:,15)+1.00000).*CONSTANTS(:,51))./CONSTANTS(:,14));
    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(:,4) = STATES(:,1);
    RATES(:,4) = ALGEBRAIC(:,4);
    ALGEBRAIC(:,9) = (STATES(:,4)+CONSTANTS(:,20))./(0.460000+CONSTANTS(:,20));
    ALGEBRAIC(:,11) = STATES(:,2)./( ALGEBRAIC(:,9).*STATES(:,7));
    ALGEBRAIC(:,13) = piecewise({ALGEBRAIC(:,11)>=1.00000, 1.00000 }, power(0.0200000, ALGEBRAIC(:,11)));
    ALGEBRAIC(:,15) =  CONSTANTS(:,27).*(CONSTANTS(:,42) - STATES(:,7)).*STATES(:,9) -  CONSTANTS(:,28).*exp(  - CONSTANTS(:,29).*STATES(:,7)).*ALGEBRAIC(:,13).*STATES(:,7);
    RATES(:,7) = ALGEBRAIC(:,15);
    ALGEBRAIC(:,17) =  CONSTANTS(:,44).*(CONSTANTS(:,43) - STATES(:,8)).*STATES(:,9) -  CONSTANTS(:,45).*STATES(:,8);
    RATES(:,8) = ALGEBRAIC(:,17);
    RATES(:,9) = piecewise({VOI<CONSTANTS(:,49),  4.00000.*CONSTANTS(:,46).*CONSTANTS(:,50).*VOI.*(1.00000 - exp(  - CONSTANTS(:,46).*power(VOI, 2.00000))).*exp(  - CONSTANTS(:,46).*power(VOI, 2.00000)) }, ( - ALGEBRAIC(:,15) - ALGEBRAIC(:,17)) -  CONSTANTS(:,47).*exp(  - CONSTANTS(:,48).*STATES(:,9)).*STATES(:,9));
    ALGEBRAIC(:,21) = piecewise({STATES(:,3)<=STATES(:,1),  CONSTANTS(:,39).*exp( CONSTANTS(:,38).*(STATES(:,5) - STATES(:,4))) },  CONSTANTS(:,41).*exp( CONSTANTS(:,40).*(STATES(:,5) - STATES(:,4))));
    ALGEBRAIC(:,10) = STATES(:,5)+STATES(:,6);
    ALGEBRAIC(:,5) =  CONSTANTS(:,7).*(exp( CONSTANTS(:,6).*STATES(:,6)) - 1.00000);
    ALGEBRAIC(:,6) = ALGEBRAIC(:,5);
    ALGEBRAIC(:,12) = piecewise({ALGEBRAIC(:,10)>=CONSTANTS(:,19)&VOI<0.150000, 0.00000 }, 1.00000);
    ALGEBRAIC(:,14) = piecewise({CONSTANTS(:,1)==0.00000, 0.00000 , CONSTANTS(:,1)==1.00000&ALGEBRAIC(:,6)>=CONSTANTS(:,18), 1.00000 , CONSTANTS(:,1)==1.00000&ALGEBRAIC(:,10)>=CONSTANTS(:,19)&ALGEBRAIC(:,12)==1.00000, 0.00000 }, NaN);
    ALGEBRAIC(:,19) = piecewise({ALGEBRAIC(:,14)==1.00000, ( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4))).*STATES(:,1))./( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4)))+ CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5))) }, ( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4))).*STATES(:,1))./( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4)))+ CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5))+ CONSTANTS(:,6).*CONSTANTS(:,7).*exp( CONSTANTS(:,6).*STATES(:,6))));
    ALGEBRAIC(:,23) = piecewise({ALGEBRAIC(:,21)==0.00000, ALGEBRAIC(:,19) }, STATES(:,3));
    RATES(:,5) = ALGEBRAIC(:,23);
    ALGEBRAIC(:,24) = piecewise({ALGEBRAIC(:,14)==1.00000, 0.00000 , ALGEBRAIC(:,14)==0.00000&ALGEBRAIC(:,21)==0.00000,  - ALGEBRAIC(:,19) },  - STATES(:,3));
    RATES(:,6) = ALGEBRAIC(:,24);
    ALGEBRAIC(:,7) = power(STATES(:,7), CONSTANTS(:,10))./(power(STATES(:,7), CONSTANTS(:,10))+power(CONSTANTS(:,9), CONSTANTS(:,10)));
    ALGEBRAIC(:,8) =  0.600000.*STATES(:,4)+0.500000;
    ALGEBRAIC(:,1) = piecewise({STATES(:,1)<=0.00000, CONSTANTS(:,21) - ( CONSTANTS(:,22).*STATES(:,1))./CONSTANTS(:,14) , STATES(:,1)<=CONSTANTS(:,25)&0.00000<STATES(:,1), ( (CONSTANTS(:,24) - CONSTANTS(:,23)).*STATES(:,1))./CONSTANTS(:,25)+CONSTANTS(:,23) }, CONSTANTS(:,24)./power(1.00000+( CONSTANTS(:,31).*(STATES(:,1) - CONSTANTS(:,25)))./CONSTANTS(:,14), CONSTANTS(:,30)));
    ALGEBRAIC(:,22) = piecewise({STATES(:,1)<=0.00000, ( CONSTANTS(:,15).*(1.00000+STATES(:,1)./CONSTANTS(:,14)))./(CONSTANTS(:,15) - STATES(:,1)./CONSTANTS(:,14)) }, (1.00000+CONSTANTS(:,16)) - ( power(CONSTANTS(:,16), 2.00000).*CONSTANTS(:,15))./( (( CONSTANTS(:,15).*CONSTANTS(:,16))./CONSTANTS(:,54)).*power(STATES(:,1)./CONSTANTS(:,14), 2.00000)+( (CONSTANTS(:,15)+1.00000).*STATES(:,1))./CONSTANTS(:,14)+ CONSTANTS(:,15).*CONSTANTS(:,16)));
    ALGEBRAIC(:,25) = piecewise({ - CONSTANTS(:,14)<=STATES(:,1)&STATES(:,1)<=0.00000, 1.00000+( 0.600000.*STATES(:,1))./CONSTANTS(:,14) , 0.00000<STATES(:,1)&STATES(:,1)<=CONSTANTS(:,51), ALGEBRAIC(:,22)./(( ( 0.400000.*CONSTANTS(:,15)+1.00000).*STATES(:,1))./( CONSTANTS(:,15).*CONSTANTS(:,14))+1.00000) }, ( ALGEBRAIC(:,22).*exp(  - CONSTANTS(:,26).*power((STATES(:,1) - CONSTANTS(:,51))./CONSTANTS(:,14), CONSTANTS(:,17))))./(( ( 0.400000.*CONSTANTS(:,15)+1.00000).*STATES(:,1))./( CONSTANTS(:,15).*CONSTANTS(:,14))+1.00000));
    ALGEBRAIC(:,26) =  CONSTANTS(:,11).*CONSTANTS(:,12).*ALGEBRAIC(:,1).*CONSTANTS(:,13).*ALGEBRAIC(:,25);
    ALGEBRAIC(:,27) = piecewise({STATES(:,1)<=CONSTANTS(:,25),  CONSTANTS(:,12).*ALGEBRAIC(:,1).*(1.00000 -  CONSTANTS(:,11).*CONSTANTS(:,13).*ALGEBRAIC(:,25)) },  CONSTANTS(:,12).*( CONSTANTS(:,24).*(1.00000 -  CONSTANTS(:,11).*CONSTANTS(:,13).*ALGEBRAIC(:,25))+( CONSTANTS(:,33).*(STATES(:,1) - CONSTANTS(:,25)))./(CONSTANTS(:,32) - CONSTANTS(:,25))));
    ALGEBRAIC(:,28) =  ALGEBRAIC(:,26).*ALGEBRAIC(:,7).*ALGEBRAIC(:,8).*ALGEBRAIC(:,9).*(1.00000 - STATES(:,2)) -  ALGEBRAIC(:,27).*STATES(:,2);
    RATES(:,2) = ALGEBRAIC(:,28);
    ALGEBRAIC(:,18) = piecewise({STATES(:,1)<=0.00000,  CONSTANTS(:,35).*exp( CONSTANTS(:,34).*STATES(:,4)) },  CONSTANTS(:,37).*exp( CONSTANTS(:,36).*STATES(:,4)));
    ALGEBRAIC(:,31) = ALGEBRAIC(:,22)./ALGEBRAIC(:,25);
    ALGEBRAIC(:,29) = ( CONSTANTS(:,15).*1.00000.*(1.00000+ 0.400000.*CONSTANTS(:,15)+( 1.20000.*STATES(:,1))./CONSTANTS(:,14)+ 0.600000.*power(STATES(:,1)./CONSTANTS(:,14), 2.00000)))./( CONSTANTS(:,14).*power( (CONSTANTS(:,15) - STATES(:,1)./CONSTANTS(:,14)).*(1.00000+( 0.600000.*STATES(:,1))./CONSTANTS(:,14)), 2.00000));
    ALGEBRAIC(:,30) =  (1.00000./CONSTANTS(:,14)).*exp(  - CONSTANTS(:,26).*power(STATES(:,1)./CONSTANTS(:,14) - CONSTANTS(:,51)./CONSTANTS(:,14), CONSTANTS(:,17))).*(( 0.400000.*CONSTANTS(:,15)+1.00000)./CONSTANTS(:,15)+ CONSTANTS(:,26).*CONSTANTS(:,17).*(1.00000+( ( 0.400000.*CONSTANTS(:,15)+1.00000).*STATES(:,1))./( CONSTANTS(:,15).*CONSTANTS(:,14))).*power(STATES(:,1)./CONSTANTS(:,14) - CONSTANTS(:,51)./CONSTANTS(:,14), CONSTANTS(:,17) - 1.00000));
    ALGEBRAIC(:,33) = piecewise({STATES(:,1)<= - CONSTANTS(:,14), CONSTANTS(:,52) ,  - CONSTANTS(:,14)<STATES(:,1)&STATES(:,1)<=0.00000, ALGEBRAIC(:,29) , 0.00000<STATES(:,1)&STATES(:,1)<=CONSTANTS(:,51), CONSTANTS(:,53) }, ALGEBRAIC(:,30));
    ALGEBRAIC(:,16) = piecewise({STATES(:,1)<=0.00000, CONSTANTS(:,34) }, CONSTANTS(:,36));
    ALGEBRAIC(:,34) = piecewise({ALGEBRAIC(:,14)==1.00000,  - ( CONSTANTS(:,8).*ALGEBRAIC(:,28).*ALGEBRAIC(:,31)+ ALGEBRAIC(:,16).*ALGEBRAIC(:,18).*power(STATES(:,1), 2.00000)+ CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5)).*STATES(:,3))./( CONSTANTS(:,8).*STATES(:,2).*ALGEBRAIC(:,33)+ALGEBRAIC(:,18)) },  - ( CONSTANTS(:,8).*ALGEBRAIC(:,28).*ALGEBRAIC(:,31)+ ALGEBRAIC(:,16).*ALGEBRAIC(:,18).*power(STATES(:,1), 2.00000)+ ( CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5))+ CONSTANTS(:,6).*CONSTANTS(:,7).*exp( CONSTANTS(:,6).*STATES(:,6))).*STATES(:,3))./( CONSTANTS(:,8).*STATES(:,2).*ALGEBRAIC(:,33)+ALGEBRAIC(:,18)));
    RATES(:,1) = piecewise({ALGEBRAIC(:,21)==0.00000, ( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4))).*(ALGEBRAIC(:,19) - STATES(:,1)) - ( CONSTANTS(:,8).*ALGEBRAIC(:,28).*ALGEBRAIC(:,31)+ ALGEBRAIC(:,16).*ALGEBRAIC(:,18).*power(STATES(:,1), 2.00000)))./( CONSTANTS(:,8).*STATES(:,2).*ALGEBRAIC(:,33)+ALGEBRAIC(:,18)) }, ALGEBRAIC(:,34));
    ALGEBRAIC(:,20) = piecewise({STATES(:,3)<=STATES(:,1), CONSTANTS(:,38) }, CONSTANTS(:,40));
    RATES(:,3) = piecewise({ALGEBRAIC(:,14)==1.00000, (( ALGEBRAIC(:,21).*(ALGEBRAIC(:,34) -  ALGEBRAIC(:,20).*power(STATES(:,3) - STATES(:,1), 2.00000)) -  CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4))).*(STATES(:,3) - STATES(:,1))) -  CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5)).*STATES(:,3))./ALGEBRAIC(:,21) }, (ALGEBRAIC(:,34) -  ALGEBRAIC(:,20).*power(STATES(:,3) - STATES(:,1), 2.00000)) - ( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4))).*(STATES(:,3) - STATES(:,1))+ ( CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5))+ CONSTANTS(:,6).*CONSTANTS(:,7).*exp( CONSTANTS(:,6).*STATES(:,6))).*STATES(:,3))./ALGEBRAIC(:,21));
   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(:,4) = STATES(:,1);
    ALGEBRAIC(:,9) = (STATES(:,4)+CONSTANTS(:,20))./(0.460000+CONSTANTS(:,20));
    ALGEBRAIC(:,11) = STATES(:,2)./( ALGEBRAIC(:,9).*STATES(:,7));
    ALGEBRAIC(:,13) = piecewise({ALGEBRAIC(:,11)>=1.00000, 1.00000 }, power(0.0200000, ALGEBRAIC(:,11)));
    ALGEBRAIC(:,15) =  CONSTANTS(:,27).*(CONSTANTS(:,42) - STATES(:,7)).*STATES(:,9) -  CONSTANTS(:,28).*exp(  - CONSTANTS(:,29).*STATES(:,7)).*ALGEBRAIC(:,13).*STATES(:,7);
    ALGEBRAIC(:,17) =  CONSTANTS(:,44).*(CONSTANTS(:,43) - STATES(:,8)).*STATES(:,9) -  CONSTANTS(:,45).*STATES(:,8);
    ALGEBRAIC(:,21) = piecewise({STATES(:,3)<=STATES(:,1),  CONSTANTS(:,39).*exp( CONSTANTS(:,38).*(STATES(:,5) - STATES(:,4))) },  CONSTANTS(:,41).*exp( CONSTANTS(:,40).*(STATES(:,5) - STATES(:,4))));
    ALGEBRAIC(:,10) = STATES(:,5)+STATES(:,6);
    ALGEBRAIC(:,5) =  CONSTANTS(:,7).*(exp( CONSTANTS(:,6).*STATES(:,6)) - 1.00000);
    ALGEBRAIC(:,6) = ALGEBRAIC(:,5);
    ALGEBRAIC(:,12) = piecewise({ALGEBRAIC(:,10)>=CONSTANTS(:,19)&VOI<0.150000, 0.00000 }, 1.00000);
    ALGEBRAIC(:,14) = piecewise({CONSTANTS(:,1)==0.00000, 0.00000 , CONSTANTS(:,1)==1.00000&ALGEBRAIC(:,6)>=CONSTANTS(:,18), 1.00000 , CONSTANTS(:,1)==1.00000&ALGEBRAIC(:,10)>=CONSTANTS(:,19)&ALGEBRAIC(:,12)==1.00000, 0.00000 }, NaN);
    ALGEBRAIC(:,19) = piecewise({ALGEBRAIC(:,14)==1.00000, ( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4))).*STATES(:,1))./( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4)))+ CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5))) }, ( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4))).*STATES(:,1))./( CONSTANTS(:,2).*CONSTANTS(:,3).*exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4)))+ CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5))+ CONSTANTS(:,6).*CONSTANTS(:,7).*exp( CONSTANTS(:,6).*STATES(:,6))));
    ALGEBRAIC(:,23) = piecewise({ALGEBRAIC(:,21)==0.00000, ALGEBRAIC(:,19) }, STATES(:,3));
    ALGEBRAIC(:,24) = piecewise({ALGEBRAIC(:,14)==1.00000, 0.00000 , ALGEBRAIC(:,14)==0.00000&ALGEBRAIC(:,21)==0.00000,  - ALGEBRAIC(:,19) },  - STATES(:,3));
    ALGEBRAIC(:,7) = power(STATES(:,7), CONSTANTS(:,10))./(power(STATES(:,7), CONSTANTS(:,10))+power(CONSTANTS(:,9), CONSTANTS(:,10)));
    ALGEBRAIC(:,8) =  0.600000.*STATES(:,4)+0.500000;
    ALGEBRAIC(:,1) = piecewise({STATES(:,1)<=0.00000, CONSTANTS(:,21) - ( CONSTANTS(:,22).*STATES(:,1))./CONSTANTS(:,14) , STATES(:,1)<=CONSTANTS(:,25)&0.00000<STATES(:,1), ( (CONSTANTS(:,24) - CONSTANTS(:,23)).*STATES(:,1))./CONSTANTS(:,25)+CONSTANTS(:,23) }, CONSTANTS(:,24)./power(1.00000+( CONSTANTS(:,31).*(STATES(:,1) - CONSTANTS(:,25)))./CONSTANTS(:,14), CONSTANTS(:,30)));
    ALGEBRAIC(:,22) = piecewise({STATES(:,1)<=0.00000, ( CONSTANTS(:,15).*(1.00000+STATES(:,1)./CONSTANTS(:,14)))./(CONSTANTS(:,15) - STATES(:,1)./CONSTANTS(:,14)) }, (1.00000+CONSTANTS(:,16)) - ( power(CONSTANTS(:,16), 2.00000).*CONSTANTS(:,15))./( (( CONSTANTS(:,15).*CONSTANTS(:,16))./CONSTANTS(:,54)).*power(STATES(:,1)./CONSTANTS(:,14), 2.00000)+( (CONSTANTS(:,15)+1.00000).*STATES(:,1))./CONSTANTS(:,14)+ CONSTANTS(:,15).*CONSTANTS(:,16)));
    ALGEBRAIC(:,25) = piecewise({ - CONSTANTS(:,14)<=STATES(:,1)&STATES(:,1)<=0.00000, 1.00000+( 0.600000.*STATES(:,1))./CONSTANTS(:,14) , 0.00000<STATES(:,1)&STATES(:,1)<=CONSTANTS(:,51), ALGEBRAIC(:,22)./(( ( 0.400000.*CONSTANTS(:,15)+1.00000).*STATES(:,1))./( CONSTANTS(:,15).*CONSTANTS(:,14))+1.00000) }, ( ALGEBRAIC(:,22).*exp(  - CONSTANTS(:,26).*power((STATES(:,1) - CONSTANTS(:,51))./CONSTANTS(:,14), CONSTANTS(:,17))))./(( ( 0.400000.*CONSTANTS(:,15)+1.00000).*STATES(:,1))./( CONSTANTS(:,15).*CONSTANTS(:,14))+1.00000));
    ALGEBRAIC(:,26) =  CONSTANTS(:,11).*CONSTANTS(:,12).*ALGEBRAIC(:,1).*CONSTANTS(:,13).*ALGEBRAIC(:,25);
    ALGEBRAIC(:,27) = piecewise({STATES(:,1)<=CONSTANTS(:,25),  CONSTANTS(:,12).*ALGEBRAIC(:,1).*(1.00000 -  CONSTANTS(:,11).*CONSTANTS(:,13).*ALGEBRAIC(:,25)) },  CONSTANTS(:,12).*( CONSTANTS(:,24).*(1.00000 -  CONSTANTS(:,11).*CONSTANTS(:,13).*ALGEBRAIC(:,25))+( CONSTANTS(:,33).*(STATES(:,1) - CONSTANTS(:,25)))./(CONSTANTS(:,32) - CONSTANTS(:,25))));
    ALGEBRAIC(:,28) =  ALGEBRAIC(:,26).*ALGEBRAIC(:,7).*ALGEBRAIC(:,8).*ALGEBRAIC(:,9).*(1.00000 - STATES(:,2)) -  ALGEBRAIC(:,27).*STATES(:,2);
    ALGEBRAIC(:,18) = piecewise({STATES(:,1)<=0.00000,  CONSTANTS(:,35).*exp( CONSTANTS(:,34).*STATES(:,4)) },  CONSTANTS(:,37).*exp( CONSTANTS(:,36).*STATES(:,4)));
    ALGEBRAIC(:,31) = ALGEBRAIC(:,22)./ALGEBRAIC(:,25);
    ALGEBRAIC(:,29) = ( CONSTANTS(:,15).*1.00000.*(1.00000+ 0.400000.*CONSTANTS(:,15)+( 1.20000.*STATES(:,1))./CONSTANTS(:,14)+ 0.600000.*power(STATES(:,1)./CONSTANTS(:,14), 2.00000)))./( CONSTANTS(:,14).*power( (CONSTANTS(:,15) - STATES(:,1)./CONSTANTS(:,14)).*(1.00000+( 0.600000.*STATES(:,1))./CONSTANTS(:,14)), 2.00000));
    ALGEBRAIC(:,30) =  (1.00000./CONSTANTS(:,14)).*exp(  - CONSTANTS(:,26).*power(STATES(:,1)./CONSTANTS(:,14) - CONSTANTS(:,51)./CONSTANTS(:,14), CONSTANTS(:,17))).*(( 0.400000.*CONSTANTS(:,15)+1.00000)./CONSTANTS(:,15)+ CONSTANTS(:,26).*CONSTANTS(:,17).*(1.00000+( ( 0.400000.*CONSTANTS(:,15)+1.00000).*STATES(:,1))./( CONSTANTS(:,15).*CONSTANTS(:,14))).*power(STATES(:,1)./CONSTANTS(:,14) - CONSTANTS(:,51)./CONSTANTS(:,14), CONSTANTS(:,17) - 1.00000));
    ALGEBRAIC(:,33) = piecewise({STATES(:,1)<= - CONSTANTS(:,14), CONSTANTS(:,52) ,  - CONSTANTS(:,14)<STATES(:,1)&STATES(:,1)<=0.00000, ALGEBRAIC(:,29) , 0.00000<STATES(:,1)&STATES(:,1)<=CONSTANTS(:,51), CONSTANTS(:,53) }, ALGEBRAIC(:,30));
    ALGEBRAIC(:,16) = piecewise({STATES(:,1)<=0.00000, CONSTANTS(:,34) }, CONSTANTS(:,36));
    ALGEBRAIC(:,34) = piecewise({ALGEBRAIC(:,14)==1.00000,  - ( CONSTANTS(:,8).*ALGEBRAIC(:,28).*ALGEBRAIC(:,31)+ ALGEBRAIC(:,16).*ALGEBRAIC(:,18).*power(STATES(:,1), 2.00000)+ CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5)).*STATES(:,3))./( CONSTANTS(:,8).*STATES(:,2).*ALGEBRAIC(:,33)+ALGEBRAIC(:,18)) },  - ( CONSTANTS(:,8).*ALGEBRAIC(:,28).*ALGEBRAIC(:,31)+ ALGEBRAIC(:,16).*ALGEBRAIC(:,18).*power(STATES(:,1), 2.00000)+ ( CONSTANTS(:,4).*CONSTANTS(:,5).*exp( CONSTANTS(:,4).*STATES(:,5))+ CONSTANTS(:,6).*CONSTANTS(:,7).*exp( CONSTANTS(:,6).*STATES(:,6))).*STATES(:,3))./( CONSTANTS(:,8).*STATES(:,2).*ALGEBRAIC(:,33)+ALGEBRAIC(:,18)));
    ALGEBRAIC(:,20) = piecewise({STATES(:,3)<=STATES(:,1), CONSTANTS(:,38) }, CONSTANTS(:,40));
    ALGEBRAIC(:,2) =  CONSTANTS(:,3).*(exp( CONSTANTS(:,2).*(STATES(:,5) - STATES(:,4))) - 1.00000);
    ALGEBRAIC(:,3) =  CONSTANTS(:,5).*(exp( CONSTANTS(:,4).*STATES(:,5)) - 1.00000);
    ALGEBRAIC(:,32) =  CONSTANTS(:,8).*ALGEBRAIC(:,31).*STATES(:,2);
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