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 =9;
end
% There are a total of 7 entries in each of the rate and state variable arrays.
% There are a total of 26 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_ALGEBRAIC(:,1) = strpad('P_L in component PluralPressureFunction (mmHg)');
    LEGEND_ALGEBRAIC(:,2) = strpad('dP_Ldt in component PluralPressureFunction (mmHg_per_second)');
    LEGEND_CONSTANTS(:,1) = strpad('P_m in component PluralPressureFunction (mmHg)');
    LEGEND_CONSTANTS(:,24) = strpad('R in component PluralPressureFunction (mmHg_second_per_litre)');
    LEGEND_CONSTANTS(:,2) = strpad('omega in component PluralPressureFunction (radian_per_second)');
    LEGEND_CONSTANTS(:,3) = strpad('V_T in component PluralPressureFunction (litre)');
    LEGEND_CONSTANTS(:,4) = strpad('E in component PluralPressureFunction (mmHg_per_second)');
    LEGEND_CONSTANTS(:,5) = strpad('P_m in component lungMechanics (mmHg)');
    LEGEND_STATES(:,1) = strpad('V_A in component lungMechanics (litre)');
    LEGEND_CONSTANTS(:,6) = strpad('E in component lungMechanics (mmHg_per_second)');
    LEGEND_STATES(:,2) = strpad('P_A in component lungMechanics (mmHg)');
    LEGEND_CONSTANTS(:,7) = strpad('R in component lungMechanics (mmHg_second_per_litre)');
    LEGEND_ALGEBRAIC(:,7) = strpad('Q_A in component gasExchange (litre_per_second)');
    LEGEND_ALGEBRAIC(:,4) = strpad('q in component lungMechanics (litre_per_second)');
    LEGEND_CONSTANTS(:,8) = strpad('D_o in component gasExchange (mole_per_second_mmHg)');
    LEGEND_STATES(:,3) = strpad('f_o in component gasExchange (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('f_oi in component gasExchange (dimensionless)');
    LEGEND_CONSTANTS(:,9) = strpad('D_c in component gasExchange (mole_per_second_mmHg)');
    LEGEND_STATES(:,4) = strpad('f_c in component gasExchange (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('f_ci in component gasExchange (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('P_w in component gasExchange (mmHg)');
    LEGEND_ALGEBRAIC(:,5) = strpad('p_ao in component gasExchange (mmHg)');
    LEGEND_STATES(:,5) = strpad('p_o in component gasTransport (mmHg)');
    LEGEND_ALGEBRAIC(:,6) = strpad('p_ac in component gasExchange (mmHg)');
    LEGEND_CONSTANTS(:,11) = strpad('f_om in component gasExchange (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('f_cm in component gasExchange (dimensionless)');
    LEGEND_STATES(:,6) = strpad('p_c in component gasTransport (mmHg)');
    LEGEND_CONSTANTS(:,13) = strpad('V_D in component gasExchange (litre)');
    LEGEND_CONSTANTS(:,14) = strpad('V_T in component gasExchange (litre)');
    LEGEND_ALGEBRAIC(:,3) = strpad('df_satdp in component gasTransport (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('L in component gasTransport (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('K_T in component gasTransport (litre_per_mole)');
    LEGEND_CONSTANTS(:,17) = strpad('K_R in component gasTransport (litre_per_mole)');
    LEGEND_CONSTANTS(:,18) = strpad('sigma in component gasTransport (mole_per_litre_mmHg)');
    LEGEND_CONSTANTS(:,19) = strpad('V_c in component gasTransport (litre)');
    LEGEND_CONSTANTS(:,20) = strpad('T_h in component gasTransport (mole_per_litre)');
    LEGEND_CONSTANTS(:,25) = strpad('delta in component gasTransport (dimensionless)');
    LEGEND_CONSTANTS(:,26) = strpad('h in component gasTransport (mole_per_litre)');
    LEGEND_CONSTANTS(:,21) = strpad('l_2 in component gasTransport (litre_per_second_mole)');
    LEGEND_CONSTANTS(:,22) = strpad('r_2 in component gasTransport (per_second)');
    LEGEND_CONSTANTS(:,23) = strpad('sigma_c in component gasTransport (mole_per_litre_mmHg)');
    LEGEND_STATES(:,7) = strpad('z in component gasTransport (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt P_A in component lungMechanics (mmHg)');
    LEGEND_RATES(:,1) = strpad('d/dt V_A in component lungMechanics (litre)');
    LEGEND_RATES(:,3) = strpad('d/dt f_o in component gasExchange (dimensionless)');
    LEGEND_RATES(:,4) = strpad('d/dt f_c in component gasExchange (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt p_o in component gasTransport (mmHg)');
    LEGEND_RATES(:,6) = strpad('d/dt p_c in component gasTransport (mmHg)');
    LEGEND_RATES(:,7) = strpad('d/dt z in component gasTransport (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) = 760;
    CONSTANTS(:,2) = 1.256637;
    CONSTANTS(:,3) = 0.41;
    CONSTANTS(:,4) = 2.5;
    CONSTANTS(:,5) = 760;
    STATES(:,1) = 1;
    CONSTANTS(:,6) = 2.5;
    STATES(:,2) = 760;
    CONSTANTS(:,7) = 1;
    CONSTANTS(:,8) = 0.0000156;
    STATES(:,3) = 0.1368;
    CONSTANTS(:,9) = 0.0000316;
    STATES(:,4) = 0.05263;
    CONSTANTS(:,10) = 47;
    STATES(:,5) = 40;
    CONSTANTS(:,11) = 0.21;
    CONSTANTS(:,12) = 0;
    STATES(:,6) = 46;
    CONSTANTS(:,13) = 0.151;
    CONSTANTS(:,14) = 0.41;
    CONSTANTS(:,15) = 171200000;
    CONSTANTS(:,16) = 10000;
    CONSTANTS(:,17) = 3600000;
    CONSTANTS(:,18) = 0.0000014;
    CONSTANTS(:,19) = 0.071;
    CONSTANTS(:,20) = 0.002;
    CONSTANTS(:,21) = 164000;
    CONSTANTS(:,22) = 0.12;
    CONSTANTS(:,23) = 0.000033;
    STATES(:,7) = 0.00000044219;
    CONSTANTS(:,24) = ( 2.00000.* pi.*1.00000)./5.00000;
    CONSTANTS(:,25) = power(10.0000, 1.90000);
    CONSTANTS(:,26) =  1.00000.*power(10.0000,  - 7.40000);
    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
    RATES(:,7) = ( CONSTANTS(:,25).*CONSTANTS(:,22).*CONSTANTS(:,23).*STATES(:,6))./1.00000 -  CONSTANTS(:,25).*CONSTANTS(:,21).*CONSTANTS(:,26).*STATES(:,7);
    ALGEBRAIC(:,1) = (CONSTANTS(:,1) -  (( CONSTANTS(:,24).*CONSTANTS(:,2).*CONSTANTS(:,3))./2.00000).* sin(( CONSTANTS(:,2).*VOI)./1.00000)) -  CONSTANTS(:,4).*(2.50000 -  (( CONSTANTS(:,3).*1.00000)./2.00000).*cos(( CONSTANTS(:,2).*VOI)./1.00000));
    RATES(:,1) = ((CONSTANTS(:,5) - ALGEBRAIC(:,1)) - ( STATES(:,1).*CONSTANTS(:,6))./1.00000)./CONSTANTS(:,7);
    ALGEBRAIC(:,3) = ( ( CONSTANTS(:,15).*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 4.00000)+power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 4.00000)).*( 3.00000.*CONSTANTS(:,15).*power(CONSTANTS(:,16), 2.00000).*power(CONSTANTS(:,18), 2.00000).*STATES(:,5).*1.00000.*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 2.00000)+ CONSTANTS(:,15).*CONSTANTS(:,16).*CONSTANTS(:,18).*1.00000.*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 3.00000)+ 3.00000.*power(CONSTANTS(:,17), 2.00000).*power(CONSTANTS(:,18), 2.00000).*STATES(:,5).*1.00000.*power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 2.00000)+ CONSTANTS(:,17).*CONSTANTS(:,18).*1.00000.*power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 3.00000)) -  ( CONSTANTS(:,15).*CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5).*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 3.00000)+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5).*power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 3.00000)).*( 4.00000.*CONSTANTS(:,15).*CONSTANTS(:,16).*CONSTANTS(:,18).*1.00000.*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 3.00000)+ 4.00000.*CONSTANTS(:,17).*CONSTANTS(:,18).*1.00000.*power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 3.00000)))./power( CONSTANTS(:,15).*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 4.00000)+power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 4.00000), 2.00000);
    RATES(:,5) =  (CONSTANTS(:,8)./( CONSTANTS(:,18).*CONSTANTS(:,19))).*power(1.00000+ (( 4.00000.*CONSTANTS(:,20))./CONSTANTS(:,18)).*ALGEBRAIC(:,3),  - 1.00000).*( STATES(:,3).*(STATES(:,2) - CONSTANTS(:,10)) - STATES(:,5));
    ALGEBRAIC(:,6) =  STATES(:,4).*(STATES(:,2) - CONSTANTS(:,10));
    RATES(:,6) = ( (CONSTANTS(:,9)./( CONSTANTS(:,23).*CONSTANTS(:,19))).*(ALGEBRAIC(:,6) - STATES(:,6))+ (( 1.00000.*CONSTANTS(:,25).*CONSTANTS(:,21))./CONSTANTS(:,23)).*CONSTANTS(:,26).*STATES(:,7)) -  CONSTANTS(:,25).*CONSTANTS(:,22).*STATES(:,6);
    ALGEBRAIC(:,2) =  ((  - CONSTANTS(:,24).*power(CONSTANTS(:,2), 2.00000).*CONSTANTS(:,3))./( 2.00000.*1.00000)).*cos(( CONSTANTS(:,2).*VOI)./1.00000) -  CONSTANTS(:,4).*(2.50000 -  (CONSTANTS(:,3)./2.00000).* sin(( CONSTANTS(:,2).*VOI)./1.00000));
    ALGEBRAIC(:,4) = (CONSTANTS(:,5) - STATES(:,2))./CONSTANTS(:,7);
    ALGEBRAIC(:,5) =  STATES(:,3).*(STATES(:,2) - CONSTANTS(:,10));
    ALGEBRAIC(:,7) = ALGEBRAIC(:,4)+ 1.00000.*CONSTANTS(:,9).*(STATES(:,6) - ALGEBRAIC(:,6))+ 1.00000.*CONSTANTS(:,8).*(STATES(:,5) - ALGEBRAIC(:,5));
    RATES(:,2) = ( CONSTANTS(:,5).*CONSTANTS(:,6).*ALGEBRAIC(:,7))./( STATES(:,2).*1.00000)+ALGEBRAIC(:,2);
    ALGEBRAIC(:,8) = piecewise({CONSTANTS(:,14)>=CONSTANTS(:,13), ( STATES(:,3).*CONSTANTS(:,13)+ CONSTANTS(:,11).*(CONSTANTS(:,14) - CONSTANTS(:,13)))./CONSTANTS(:,14) }, STATES(:,3));
    RATES(:,3) =  (1.00000./STATES(:,1)).*(( 1.00000.*CONSTANTS(:,8).*(STATES(:,5) - ALGEBRAIC(:,5))+ (ALGEBRAIC(:,8) - STATES(:,3)).*ALGEBRAIC(:,4)) -  STATES(:,3).*( 1.00000.*CONSTANTS(:,9).*(STATES(:,6) - ALGEBRAIC(:,6))+ 1.00000.*CONSTANTS(:,8).*(STATES(:,5) - ALGEBRAIC(:,5))));
    ALGEBRAIC(:,9) = piecewise({CONSTANTS(:,14)>=CONSTANTS(:,13), ( STATES(:,4).*CONSTANTS(:,13)+ CONSTANTS(:,12).*(CONSTANTS(:,14) - CONSTANTS(:,13)))./CONSTANTS(:,14) }, STATES(:,4));
    RATES(:,4) =  (1.00000./STATES(:,1)).*(( 1.00000.*CONSTANTS(:,9).*(STATES(:,6) - ALGEBRAIC(:,6))+ (ALGEBRAIC(:,9) - STATES(:,4)).*ALGEBRAIC(:,4)) -  STATES(:,4).*( 1.00000.*CONSTANTS(:,8).*(STATES(:,5) - ALGEBRAIC(:,5))+ 1.00000.*CONSTANTS(:,9).*(STATES(:,6) - 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) = (CONSTANTS(:,1) -  (( CONSTANTS(:,24).*CONSTANTS(:,2).*CONSTANTS(:,3))./2.00000).* sin(( CONSTANTS(:,2).*VOI)./1.00000)) -  CONSTANTS(:,4).*(2.50000 -  (( CONSTANTS(:,3).*1.00000)./2.00000).*cos(( CONSTANTS(:,2).*VOI)./1.00000));
    ALGEBRAIC(:,3) = ( ( CONSTANTS(:,15).*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 4.00000)+power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 4.00000)).*( 3.00000.*CONSTANTS(:,15).*power(CONSTANTS(:,16), 2.00000).*power(CONSTANTS(:,18), 2.00000).*STATES(:,5).*1.00000.*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 2.00000)+ CONSTANTS(:,15).*CONSTANTS(:,16).*CONSTANTS(:,18).*1.00000.*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 3.00000)+ 3.00000.*power(CONSTANTS(:,17), 2.00000).*power(CONSTANTS(:,18), 2.00000).*STATES(:,5).*1.00000.*power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 2.00000)+ CONSTANTS(:,17).*CONSTANTS(:,18).*1.00000.*power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 3.00000)) -  ( CONSTANTS(:,15).*CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5).*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 3.00000)+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5).*power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 3.00000)).*( 4.00000.*CONSTANTS(:,15).*CONSTANTS(:,16).*CONSTANTS(:,18).*1.00000.*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 3.00000)+ 4.00000.*CONSTANTS(:,17).*CONSTANTS(:,18).*1.00000.*power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 3.00000)))./power( CONSTANTS(:,15).*power(1.00000+ CONSTANTS(:,16).*CONSTANTS(:,18).*STATES(:,5), 4.00000)+power(1.00000+ CONSTANTS(:,17).*CONSTANTS(:,18).*STATES(:,5), 4.00000), 2.00000);
    ALGEBRAIC(:,6) =  STATES(:,4).*(STATES(:,2) - CONSTANTS(:,10));
    ALGEBRAIC(:,2) =  ((  - CONSTANTS(:,24).*power(CONSTANTS(:,2), 2.00000).*CONSTANTS(:,3))./( 2.00000.*1.00000)).*cos(( CONSTANTS(:,2).*VOI)./1.00000) -  CONSTANTS(:,4).*(2.50000 -  (CONSTANTS(:,3)./2.00000).* sin(( CONSTANTS(:,2).*VOI)./1.00000));
    ALGEBRAIC(:,4) = (CONSTANTS(:,5) - STATES(:,2))./CONSTANTS(:,7);
    ALGEBRAIC(:,5) =  STATES(:,3).*(STATES(:,2) - CONSTANTS(:,10));
    ALGEBRAIC(:,7) = ALGEBRAIC(:,4)+ 1.00000.*CONSTANTS(:,9).*(STATES(:,6) - ALGEBRAIC(:,6))+ 1.00000.*CONSTANTS(:,8).*(STATES(:,5) - ALGEBRAIC(:,5));
    ALGEBRAIC(:,8) = piecewise({CONSTANTS(:,14)>=CONSTANTS(:,13), ( STATES(:,3).*CONSTANTS(:,13)+ CONSTANTS(:,11).*(CONSTANTS(:,14) - CONSTANTS(:,13)))./CONSTANTS(:,14) }, STATES(:,3));
    ALGEBRAIC(:,9) = piecewise({CONSTANTS(:,14)>=CONSTANTS(:,13), ( STATES(:,4).*CONSTANTS(:,13)+ CONSTANTS(:,12).*(CONSTANTS(:,14) - CONSTANTS(:,13)))./CONSTANTS(:,14) }, STATES(:,4));
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