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 8 entries in each of the rate and state variable arrays. % There are a total of 20 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 (day)'); LEGEND_CONSTANTS(:,1) = strpad('mu_T in component uninfected_CD4 (per_day)'); LEGEND_CONSTANTS(:,2) = strpad('r in component uninfected_CD4 (per_day)'); LEGEND_STATES(:,1) = strpad('T_1 in component latently_infected_CD4 (per_micro_L)'); LEGEND_CONSTANTS(:,3) = strpad('T_max in component uninfected_CD4 (per_micro_L)'); LEGEND_CONSTANTS(:,4) = strpad('k_1 in component HIV1 (micro_L_per_day)'); LEGEND_STATES(:,2) = strpad('V in component HIV1 (per_micro_L)'); LEGEND_CONSTANTS(:,5) = strpad('k_1_ in component hybrid_HIV1 (micro_L_per_day)'); LEGEND_STATES(:,3) = strpad('V_ in component hybrid_HIV1 (per_micro_L)'); LEGEND_CONSTANTS(:,6) = strpad('s_0 in component uninfected_CD4 (per_micro_L_day)'); LEGEND_CONSTANTS(:,7) = strpad('theta in component uninfected_CD4 (per_micro_L)'); LEGEND_ALGEBRAIC(:,1) = strpad('s_V in component uninfected_CD4 (per_micro_L_day)'); LEGEND_STATES(:,4) = strpad('T in component uninfected_CD4 (per_micro_L)'); LEGEND_CONSTANTS(:,8) = strpad('k_2 in component latently_infected_CD4 (per_day)'); LEGEND_CONSTANTS(:,9) = strpad('k_1D in component actively_infected_CD4 (micro_L_per_day)'); LEGEND_ALGEBRAIC(:,2) = strpad('D in component DIV (per_micro_L)'); LEGEND_CONSTANTS(:,10) = strpad('mu_b in component actively_infected_CD4 (per_day)'); LEGEND_STATES(:,5) = strpad('T_2 in component actively_infected_CD4 (per_micro_L)'); LEGEND_CONSTANTS(:,11) = strpad('k_s in component actively_coinfected_CD4 (per_day)'); LEGEND_CONSTANTS(:,12) = strpad('mu_bD in component actively_coinfected_CD4 (per_day)'); LEGEND_STATES(:,6) = strpad('T_D2 in component actively_coinfected_CD4 (per_micro_L)'); LEGEND_CONSTANTS(:,13) = strpad('mu_TD in component stably_coinfected_CD4 (per_day)'); LEGEND_STATES(:,7) = strpad('T_D1 in component stably_coinfected_CD4 (per_micro_L)'); LEGEND_ALGEBRAIC(:,5) = strpad('N_D_t in component DIV (dimensionless)'); LEGEND_ALGEBRAIC(:,8) = strpad('pi_D_t in component DIV (per_day)'); LEGEND_CONSTANTS(:,14) = strpad('mu_D in component DIV (per_day)'); LEGEND_ALGEBRAIC(:,3) = strpad('N_t in component production_function (dimensionless)'); LEGEND_CONSTANTS(:,15) = strpad('t_DIV in component DIV (day)'); LEGEND_STATES(:,8) = strpad('D_ode in component DIV (per_micro_L)'); LEGEND_CONSTANTS(:,16) = strpad('D_0 in component DIV (per_micro_L)'); LEGEND_ALGEBRAIC(:,6) = strpad('N_2_t in component HIV1 (dimensionless)'); LEGEND_CONSTANTS(:,17) = strpad('mu_V in component HIV1 (per_day)'); LEGEND_ALGEBRAIC(:,7) = strpad('N_t_ in component hybrid_HIV1 (dimensionless)'); LEGEND_ALGEBRAIC(:,9) = strpad('pi_t_ in component hybrid_HIV1 (per_day)'); LEGEND_CONSTANTS(:,18) = strpad('N_0 in component production_function (dimensionless)'); LEGEND_CONSTANTS(:,19) = strpad('gamma in component production_function (dimensionless)'); LEGEND_CONSTANTS(:,20) = strpad('t_c in component production_function (day)'); LEGEND_ALGEBRAIC(:,4) = strpad('T_tot in component cell_population (per_micro_L)'); LEGEND_RATES(:,4) = strpad('d/dt T in component uninfected_CD4 (per_micro_L)'); LEGEND_RATES(:,1) = strpad('d/dt T_1 in component latently_infected_CD4 (per_micro_L)'); LEGEND_RATES(:,5) = strpad('d/dt T_2 in component actively_infected_CD4 (per_micro_L)'); LEGEND_RATES(:,6) = strpad('d/dt T_D2 in component actively_coinfected_CD4 (per_micro_L)'); LEGEND_RATES(:,7) = strpad('d/dt T_D1 in component stably_coinfected_CD4 (per_micro_L)'); LEGEND_RATES(:,8) = strpad('d/dt D_ode in component DIV (per_micro_L)'); LEGEND_RATES(:,2) = strpad('d/dt V in component HIV1 (per_micro_L)'); LEGEND_RATES(:,3) = strpad('d/dt V_ in component hybrid_HIV1 (per_micro_L)'); 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.02; CONSTANTS(:,2) = 0.03; STATES(:,1) = 0; CONSTANTS(:,3) = 1500; CONSTANTS(:,4) = 2.4E-5; STATES(:,2) = 1E-3; CONSTANTS(:,5) = 2.4E-7; STATES(:,3) = 0; CONSTANTS(:,6) = 10; CONSTANTS(:,7) = 1; STATES(:,4) = 1000; CONSTANTS(:,8) = 0.017; CONSTANTS(:,9) = 2.4E-5; CONSTANTS(:,10) = 0.24; STATES(:,5) = 0; CONSTANTS(:,11) = 0.48; CONSTANTS(:,12) = 0.12; STATES(:,6) = 0; CONSTANTS(:,13) = 0.02; STATES(:,7) = 0; CONSTANTS(:,14) = 0.76; CONSTANTS(:,15) = 2557; STATES(:,8) = 0; CONSTANTS(:,16) = 0.1; CONSTANTS(:,17) = 2.4; CONSTANTS(:,18) = 300; CONSTANTS(:,19) = 25; CONSTANTS(:,20) = 7305; 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(:,4).*STATES(:,2).*STATES(:,4)+ CONSTANTS(:,5).*STATES(:,3).*STATES(:,4)) - CONSTANTS(:,1).*STATES(:,1)) - CONSTANTS(:,8).*STATES(:,1); RATES(:,7) = CONSTANTS(:,11).*STATES(:,6) - CONSTANTS(:,13).*STATES(:,7); ALGEBRAIC(:,1) = ( CONSTANTS(:,6).*CONSTANTS(:,7))./(CONSTANTS(:,7)+STATES(:,2)); RATES(:,4) = (((ALGEBRAIC(:,1) - CONSTANTS(:,1).*STATES(:,4))+ CONSTANTS(:,2).*STATES(:,4).*(1.00000 - (STATES(:,4)+STATES(:,1))./CONSTANTS(:,3))) - CONSTANTS(:,4).*STATES(:,2).*STATES(:,4)) - CONSTANTS(:,5).*STATES(:,3).*STATES(:,4); ALGEBRAIC(:,2) = piecewise({VOI req_length strout = strin(1:req_length); else strout = [strin, blanks(req_length - insize)]; end end