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 =2; end % There are a total of 9 entries in each of the rate and state variable arrays. % There are a total of 30 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 (hour)'); LEGEND_CONSTANTS(:,30) = strpad('F_c_dopa in component gastrointestinal_compartment_L_dopa (dimensionless)'); LEGEND_STATES(:,1) = strpad('A_dopa_c in component gastrointestinal_compartment_L_dopa (umole)'); LEGEND_CONSTANTS(:,1) = strpad('ka_c_dopa in component gastrointestinal_compartment_L_dopa (per_hour)'); LEGEND_CONSTANTS(:,22) = strpad('F_G in component gastrointestinal_compartment_L_dopa (dimensionless)'); LEGEND_CONSTANTS(:,29) = strpad('F_H in component gastrointestinal_compartment_L_dopa (dimensionless)'); LEGEND_CONSTANTS(:,26) = strpad('CL_H in component gastrointestinal_compartment_L_dopa (liter_per_hour)'); LEGEND_CONSTANTS(:,2) = strpad('Q in component gastrointestinal_compartment_L_dopa (liter_per_hour)'); LEGEND_CONSTANTS(:,3) = strpad('f_H in component gastrointestinal_compartment_L_dopa (dimensionless)'); LEGEND_CONSTANTS(:,4) = strpad('CL_dopa_0 in component L_dopa_clearance (liter_per_hour)'); LEGEND_STATES(:,2) = strpad('C_dopa_c in component body_compartment_L_dopa (uM)'); LEGEND_CONSTANTS(:,5) = strpad('V_dopa in component body_compartment_L_dopa (liter)'); LEGEND_ALGEBRAIC(:,2) = strpad('CL_dopa in component L_dopa_clearance (liter_per_hour)'); LEGEND_STATES(:,3) = strpad('C_OMD_c in component body_compartment_3_OMD (uM)'); LEGEND_CONSTANTS(:,6) = strpad('CL_OMD_c in component body_compartment_3_OMD (liter_per_hour)'); LEGEND_CONSTANTS(:,7) = strpad('V_OMD_c in component body_compartment_3_OMD (liter)'); LEGEND_CONSTANTS(:,23) = strpad('CL_COMT in component L_dopa_clearance (liter_per_hour)'); LEGEND_ALGEBRAIC(:,1) = strpad('CL_AADC in component L_dopa_clearance (liter_per_hour)'); LEGEND_CONSTANTS(:,24) = strpad('CL_AADC0 in component L_dopa_clearance (liter_per_hour)'); LEGEND_STATES(:,4) = strpad('C_Ro_central in component central_compartment_Ro (uM)'); LEGEND_CONSTANTS(:,27) = strpad('CL_REST in component L_dopa_clearance (liter_per_hour)'); LEGEND_CONSTANTS(:,8) = strpad('ki in component L_dopa_clearance (uM)'); LEGEND_STATES(:,5) = strpad('A_bens in component gastrointestinal_compartment_benserazide (umole)'); LEGEND_CONSTANTS(:,9) = strpad('ka_B in component gastrointestinal_compartment_benserazide (per_hour)'); LEGEND_CONSTANTS(:,10) = strpad('F_B in component gastrointestinal_compartment_benserazide (dimensionless)'); LEGEND_STATES(:,6) = strpad('C_bens_central in component central_compartment_benserazide (uM)'); LEGEND_CONSTANTS(:,11) = strpad('V1_B in component central_compartment_benserazide (liter)'); LEGEND_CONSTANTS(:,12) = strpad('CLd_B in component central_compartment_benserazide (liter_per_hour)'); LEGEND_STATES(:,7) = strpad('C_bens_peripheral in component peripheral_compartment_benserazide (uM)'); LEGEND_CONSTANTS(:,25) = strpad('CL_bens_total in component benserazide_clearance (liter_per_hour)'); LEGEND_CONSTANTS(:,13) = strpad('V2_B in component peripheral_compartment_benserazide (liter)'); LEGEND_CONSTANTS(:,28) = strpad('CL_Ro in component benserazide_clearance (liter_per_hour)'); LEGEND_CONSTANTS(:,14) = strpad('CL_B in component benserazide_clearance (liter_per_hour)'); LEGEND_CONSTANTS(:,15) = strpad('fm in component benserazide_clearance (dimensionless)'); LEGEND_STATES(:,8) = strpad('A_Ro in component gastrointestinal_compartment_Ro (umole)'); LEGEND_CONSTANTS(:,16) = strpad('ka_M in component gastrointestinal_compartment_Ro (per_hour)'); LEGEND_CONSTANTS(:,17) = strpad('F_Ro in component gastrointestinal_compartment_Ro (dimensionless)'); LEGEND_CONSTANTS(:,18) = strpad('V1_M in component central_compartment_Ro (liter)'); LEGEND_CONSTANTS(:,19) = strpad('CLd_M in component central_compartment_Ro (liter_per_hour)'); LEGEND_CONSTANTS(:,20) = strpad('CL_M in component central_compartment_Ro (liter_per_hour)'); LEGEND_STATES(:,9) = strpad('C_Ro_peripheral in component peripheral_compartment_Ro (uM)'); LEGEND_CONSTANTS(:,21) = strpad('V2_M in component peripheral_compartment_Ro (liter)'); LEGEND_RATES(:,1) = strpad('d/dt A_dopa_c in component gastrointestinal_compartment_L_dopa (umole)'); LEGEND_RATES(:,2) = strpad('d/dt C_dopa_c in component body_compartment_L_dopa (uM)'); LEGEND_RATES(:,3) = strpad('d/dt C_OMD_c in component body_compartment_3_OMD (uM)'); LEGEND_RATES(:,5) = strpad('d/dt A_bens in component gastrointestinal_compartment_benserazide (umole)'); LEGEND_RATES(:,6) = strpad('d/dt C_bens_central in component central_compartment_benserazide (uM)'); LEGEND_RATES(:,7) = strpad('d/dt C_bens_peripheral in component peripheral_compartment_benserazide (uM)'); LEGEND_RATES(:,8) = strpad('d/dt A_Ro in component gastrointestinal_compartment_Ro (umole)'); LEGEND_RATES(:,4) = strpad('d/dt C_Ro_central in component central_compartment_Ro (uM)'); LEGEND_RATES(:,9) = strpad('d/dt C_Ro_peripheral in component peripheral_compartment_Ro (uM)'); 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 = []; STATES(:,1) = 101; CONSTANTS(:,1) = 1.29; CONSTANTS(:,2) = 0.828; CONSTANTS(:,3) = 0.13; CONSTANTS(:,4) = 0.823; STATES(:,2) = 0; CONSTANTS(:,5) = 0.496; STATES(:,3) = 0; CONSTANTS(:,6) = 0.00895; CONSTANTS(:,7) = 0.128; STATES(:,4) = 0; CONSTANTS(:,8) = 0.00246; STATES(:,5) = 19.51; CONSTANTS(:,9) = 0.94; CONSTANTS(:,10) = 0.022; STATES(:,6) = 0; CONSTANTS(:,11) = 0.202; CONSTANTS(:,12) = 0.072; STATES(:,7) = 0; CONSTANTS(:,13) = 0.127; CONSTANTS(:,14) = 1.67; CONSTANTS(:,15) = 0.15; STATES(:,8) = 1.3658; CONSTANTS(:,16) = 2.47; CONSTANTS(:,17) = 1; CONSTANTS(:,18) = 0.0691; CONSTANTS(:,19) = 1.06; CONSTANTS(:,20) = 4.29; STATES(:,9) = 0; CONSTANTS(:,21) = 3.2; CONSTANTS(:,22) = 1.00000; CONSTANTS(:,23) = CONSTANTS(:,4).*0.100000; CONSTANTS(:,24) = CONSTANTS(:,4).*0.690000; CONSTANTS(:,25) = CONSTANTS(:,14)./(1.00000 - CONSTANTS(:,15)); CONSTANTS(:,26) = CONSTANTS(:,3).*CONSTANTS(:,4); CONSTANTS(:,27) = CONSTANTS(:,4).*0.210000; CONSTANTS(:,28) = CONSTANTS(:,25).*CONSTANTS(:,15); CONSTANTS(:,29) = 1.00000 - CONSTANTS(:,26)./CONSTANTS(:,2); CONSTANTS(:,30) = CONSTANTS(:,29).*CONSTANTS(:,22); 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(:,1) = - CONSTANTS(:,1).*STATES(:,1); RATES(:,3) = (1.00000./CONSTANTS(:,7)).*( CONSTANTS(:,23).*STATES(:,2) - CONSTANTS(:,6).*STATES(:,3)); RATES(:,5) = - CONSTANTS(:,9).*STATES(:,5); RATES(:,6) = (1.00000./CONSTANTS(:,11)).*(( CONSTANTS(:,9).*STATES(:,5).*CONSTANTS(:,10) - CONSTANTS(:,25).*STATES(:,6))+ CONSTANTS(:,12).*(STATES(:,7) - STATES(:,6))); RATES(:,7) = (1.00000./CONSTANTS(:,13)).*CONSTANTS(:,12).*(STATES(:,6) - STATES(:,7)); RATES(:,8) = - CONSTANTS(:,16).*STATES(:,8); RATES(:,4) = (1.00000./CONSTANTS(:,18)).*(( CONSTANTS(:,16).*STATES(:,8).*CONSTANTS(:,17) - CONSTANTS(:,20).*STATES(:,4))+ CONSTANTS(:,28).*STATES(:,6)+ CONSTANTS(:,19).*(STATES(:,9) - STATES(:,4))); RATES(:,9) = (1.00000./CONSTANTS(:,21)).*CONSTANTS(:,19).*(STATES(:,4) - STATES(:,9)); ALGEBRAIC(:,1) = CONSTANTS(:,24)./(1.00000+STATES(:,4)./CONSTANTS(:,8)); ALGEBRAIC(:,2) = ALGEBRAIC(:,1)+CONSTANTS(:,23)+CONSTANTS(:,27); RATES(:,2) = (1.00000./CONSTANTS(:,5)).*( CONSTANTS(:,1).*STATES(:,1).*CONSTANTS(:,30) - ALGEBRAIC(:,2).*STATES(:,2)); 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(:,24)./(1.00000+STATES(:,4)./CONSTANTS(:,8)); ALGEBRAIC(:,2) = ALGEBRAIC(:,1)+CONSTANTS(:,23)+CONSTANTS(:,27); 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