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 =11; end % There are a total of 2 entries in each of the rate and state variable arrays. % There are a total of 11 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('VPF in component pulmonary_O2_uptake (litre)'); LEGEND_CONSTANTS(:,2) = strpad('DOB in component pulmonary_O2_uptake (mL_per_minute)'); LEGEND_CONSTANTS(:,3) = strpad('QRO in component pulmonary_O2_uptake (L_per_minute)'); LEGEND_CONSTANTS(:,4) = strpad('RMO in component pulmonary_O2_uptake (mL_per_minute)'); LEGEND_CONSTANTS(:,5) = strpad('HM in component pulmonary_O2_uptake (dimensionless)'); LEGEND_CONSTANTS(:,10) = strpad('O2UTIL in component total_O2_utilization (mL_per_minute)'); LEGEND_ALGEBRAIC(:,6) = strpad('O2VAD2 in component progressive_chemoreceptor_adaptation_of_alveolar_ventilation (dimensionless)'); LEGEND_ALGEBRAIC(:,5) = strpad('O2VTS2 in component acute_chemoreceptor_adaptation_of_alveolar_ventilation (dimensionless)'); LEGEND_ALGEBRAIC(:,8) = strpad('ALVENT in component alveolar_ventilation (L_per_minute)'); LEGEND_CONSTANTS(:,6) = strpad('VNTSTM in component parameter_values (dimensionless)'); LEGEND_ALGEBRAIC(:,9) = strpad('PO2ALV in component alveolar_PO2 (mmHg)'); LEGEND_CONSTANTS(:,7) = strpad('PO2AMB in component parameter_values (mmHg)'); LEGEND_ALGEBRAIC(:,2) = strpad('PO2ART in component arterial_PO2 (mmHg)'); LEGEND_ALGEBRAIC(:,10) = strpad('O2DFS in component respiratory_O2_diffusion_into_capillaries (mL_per_minute)'); LEGEND_CONSTANTS(:,8) = strpad('PL2 in component parameter_values (L_mL_per_minute_per_mmHg)'); LEGEND_CONSTANTS(:,9) = strpad('VPTISS in component parameter_values (litre)'); LEGEND_CONSTANTS(:,11) = strpad('RSPDFC in component respiratory_O2_diffusion_into_capillaries (mL_per_minute_per_mmHg)'); LEGEND_STATES(:,1) = strpad('OVA in component O2_volume_of_arterial_blood (mL_per_L)'); LEGEND_ALGEBRAIC(:,11) = strpad('DOVA in component O2_volume_of_arterial_blood (mL_per_L_per_minute)'); LEGEND_ALGEBRAIC(:,1) = strpad('OSA in component arterial_PO2 (dimensionless)'); LEGEND_ALGEBRAIC(:,4) = strpad('O2VTST in component acute_chemoreceptor_adaptation_of_alveolar_ventilation (dimensionless)'); LEGEND_ALGEBRAIC(:,3) = strpad('O2VTST1 in component acute_chemoreceptor_adaptation_of_alveolar_ventilation (dimensionless)'); LEGEND_ALGEBRAIC(:,7) = strpad('DO2VAD in component progressive_chemoreceptor_adaptation_of_alveolar_ventilation (per_minute)'); LEGEND_STATES(:,2) = strpad('O2VAD1 in component progressive_chemoreceptor_adaptation_of_alveolar_ventilation (dimensionless)'); LEGEND_RATES(:,1) = strpad('d/dt OVA in component O2_volume_of_arterial_blood (mL_per_L)'); LEGEND_RATES(:,2) = strpad('d/dt O2VAD1 in component progressive_chemoreceptor_adaptation_of_alveolar_ventilation (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.0123238; CONSTANTS(:,2) = 163.508; CONSTANTS(:,3) = 4.97838; CONSTANTS(:,4) = 56.8057; CONSTANTS(:,5) = 40.0381; CONSTANTS(:,6) = 1; CONSTANTS(:,7) = 150; CONSTANTS(:,8) = 1.8; CONSTANTS(:,9) = 0.0175; STATES(:,1) = 204.497; STATES(:,2) = 2.368e-07; CONSTANTS(:,10) = CONSTANTS(:,2)+CONSTANTS(:,4); CONSTANTS(:,11) = CONSTANTS(:,8)./(CONSTANTS(:,9)+CONSTANTS(:,1)); 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) = (STATES(:,1)./CONSTANTS(:,5))./5.25000; ALGEBRAIC(:,2) = piecewise({ALGEBRAIC(:,1)>1.00000, 114.000+ (ALGEBRAIC(:,1) - 1.00000).*6667.00 , ALGEBRAIC(:,1)>0.936000&ALGEBRAIC(:,1)<=1.00000, 74.0000+ (ALGEBRAIC(:,1) - 0.936000).*625.000 , ALGEBRAIC(:,1)>0.800000&ALGEBRAIC(:,1)<=0.936000, 46.0000+ (ALGEBRAIC(:,1) - 0.800000).*205.882 }, ALGEBRAIC(:,1).*57.5000); ALGEBRAIC(:,3) = (ALGEBRAIC(:,2) - 67.0000)./30.0000; ALGEBRAIC(:,4) = piecewise({ALGEBRAIC(:,3)>1.00000, 1.00000 , ALGEBRAIC(:,3)<0.600000, 0.600000 }, ALGEBRAIC(:,3)); ALGEBRAIC(:,5) = 1.00000./ALGEBRAIC(:,4); ALGEBRAIC(:,7) = ( (ALGEBRAIC(:,5) - 1.00000).*3.00000 - STATES(:,2)).*0.000500000; RATES(:,2) = ALGEBRAIC(:,7); ALGEBRAIC(:,6) = STATES(:,2)+1.00000; ALGEBRAIC(:,8) = CONSTANTS(:,10).*CONSTANTS(:,6).*0.0266670.*ALGEBRAIC(:,5).*ALGEBRAIC(:,6); ALGEBRAIC(:,9) = CONSTANTS(:,7) - (CONSTANTS(:,10)./ALGEBRAIC(:,8))./0.761000; ALGEBRAIC(:,10) = (ALGEBRAIC(:,9) - ALGEBRAIC(:,2)).*CONSTANTS(:,11); ALGEBRAIC(:,11) = (ALGEBRAIC(:,10) - CONSTANTS(:,10))./( CONSTANTS(:,3).*1.00000); RATES(:,1) = ALGEBRAIC(:,11); 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) = (STATES(:,1)./CONSTANTS(:,5))./5.25000; ALGEBRAIC(:,2) = piecewise({ALGEBRAIC(:,1)>1.00000, 114.000+ (ALGEBRAIC(:,1) - 1.00000).*6667.00 , ALGEBRAIC(:,1)>0.936000&ALGEBRAIC(:,1)<=1.00000, 74.0000+ (ALGEBRAIC(:,1) - 0.936000).*625.000 , ALGEBRAIC(:,1)>0.800000&ALGEBRAIC(:,1)<=0.936000, 46.0000+ (ALGEBRAIC(:,1) - 0.800000).*205.882 }, ALGEBRAIC(:,1).*57.5000); ALGEBRAIC(:,3) = (ALGEBRAIC(:,2) - 67.0000)./30.0000; ALGEBRAIC(:,4) = piecewise({ALGEBRAIC(:,3)>1.00000, 1.00000 , ALGEBRAIC(:,3)<0.600000, 0.600000 }, ALGEBRAIC(:,3)); ALGEBRAIC(:,5) = 1.00000./ALGEBRAIC(:,4); ALGEBRAIC(:,7) = ( (ALGEBRAIC(:,5) - 1.00000).*3.00000 - STATES(:,2)).*0.000500000; ALGEBRAIC(:,6) = STATES(:,2)+1.00000; ALGEBRAIC(:,8) = CONSTANTS(:,10).*CONSTANTS(:,6).*0.0266670.*ALGEBRAIC(:,5).*ALGEBRAIC(:,6); ALGEBRAIC(:,9) = CONSTANTS(:,7) - (CONSTANTS(:,10)./ALGEBRAIC(:,8))./0.761000; ALGEBRAIC(:,10) = (ALGEBRAIC(:,9) - ALGEBRAIC(:,2)).*CONSTANTS(:,11); ALGEBRAIC(:,11) = (ALGEBRAIC(:,10) - CONSTANTS(:,10))./( CONSTANTS(:,3).*1.00000); 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