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 =7; end % There are a total of 11 entries in each of the rate and state variable arrays. % There are a total of 57 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_STATES(:,1) = strpad('Q_Ca_p in component Q_Ca_p (millimole)'); LEGEND_CONSTANTS(:,1) = strpad('J_bp_Ca in component model_fluxes (millimole_per_hour)'); LEGEND_ALGEBRAIC(:,1) = strpad('J_pb_Ca in component model_fluxes (millimole_per_hour)'); LEGEND_CONSTANTS(:,2) = strpad('J_pu_Ca in component model_fluxes (millimole_per_hour)'); LEGEND_ALGEBRAIC(:,4) = strpad('J_ip_Ca in component model_fluxes (millimole_per_hour)'); LEGEND_STATES(:,2) = strpad('Q_P_p in component Q_P_p (millimole)'); LEGEND_CONSTANTS(:,39) = strpad('J_bp_P in component model_fluxes (millimole_per_hour)'); LEGEND_ALGEBRAIC(:,5) = strpad('J_pb_P in component model_fluxes (millimole_per_hour)'); LEGEND_CONSTANTS(:,3) = strpad('J_pu_P in component model_fluxes (millimole_per_hour)'); LEGEND_CONSTANTS(:,49) = strpad('J_ip_P in component model_fluxes (millimole_per_hour)'); LEGEND_CONSTANTS(:,56) = strpad('J_pc_P in component model_fluxes (millimole_per_hour)'); LEGEND_ALGEBRAIC(:,7) = strpad('J_cp_P in component model_fluxes (millimole_per_hour)'); LEGEND_STATES(:,3) = strpad('Q_P_c in component Q_P_c (millimole)'); LEGEND_STATES(:,4) = strpad('Q_PTH_p in component Q_PTH_p (millimole)'); LEGEND_ALGEBRAIC(:,2) = strpad('S_PTH in component model_fluxes (millimole_per_hour)'); LEGEND_CONSTANTS(:,4) = strpad('C_PTH_p in component model_parameters (millimolar)'); LEGEND_CONSTANTS(:,5) = strpad('k_PTH in component model_parameters (first_order_rate_constant)'); LEGEND_STATES(:,5) = strpad('Q_Ca_b in component Q_Ca_b (millimole)'); LEGEND_STATES(:,6) = strpad('Q_P_b in component Q_P_b (millimole)'); LEGEND_STATES(:,7) = strpad('Q_E_k in component Q_E_k (millimole)'); LEGEND_CONSTANTS(:,6) = strpad('S_E in component model_parameters (millimole_per_hour)'); LEGEND_CONSTANTS(:,7) = strpad('k_E in component model_parameters (first_order_rate_constant)'); LEGEND_STATES(:,8) = strpad('Q_D_p in component Q_D_p (millimole)'); LEGEND_CONSTANTS(:,8) = strpad('k_D in component model_parameters (first_order_rate_constant)'); LEGEND_STATES(:,9) = strpad('Q_TCa_i in component Q_TCa_i (millimole)'); LEGEND_CONSTANTS(:,50) = strpad('k1_D in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,51) = strpad('k2_D in component model_parameters (first_order_rate_constant)'); LEGEND_STATES(:,10) = strpad('Q_C_PT in component Q_C_PT (millimole)'); LEGEND_CONSTANTS(:,52) = strpad('k3_D in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,53) = strpad('k4_D in component model_parameters (first_order_rate_constant)'); LEGEND_STATES(:,11) = strpad('Q_TP_k in component Q_TP_k (millimole)'); LEGEND_CONSTANTS(:,54) = strpad('k1_P in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,9) = strpad('k2_P in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,10) = strpad('J_P_ing in component model_fluxes (millimole_per_hour)'); LEGEND_CONSTANTS(:,11) = strpad('Stoic_Ca_P in component model_fluxes (dimensionless)'); LEGEND_CONSTANTS(:,12) = strpad('k_Ca_i in component model_fluxes (first_order_rate_constant)'); LEGEND_CONSTANTS(:,13) = strpad('C_Ca_i in component model_parameters (millimolar)'); LEGEND_CONSTANTS(:,14) = strpad('C_Ca_p in component model_parameters (millimolar)'); LEGEND_CONSTANTS(:,15) = strpad('C_P_p in component model_parameters (millimolar)'); LEGEND_CONSTANTS(:,16) = strpad('k_Ca_b in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,17) = strpad('k3_P in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,18) = strpad('k4_P in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,40) = strpad('Y_Ca_i_2plus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,41) = strpad('Y_Ca_p_1minus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,19) = strpad('C_D_p in component model_parameters (picomolar)'); LEGEND_CONSTANTS(:,55) = strpad('Ca_thr in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,57) = strpad('Ca_T in component model_parameters (second_order_rate_constant)'); LEGEND_ALGEBRAIC(:,3) = strpad('P_thr in component model_parameters (millimole_per_hour)'); LEGEND_ALGEBRAIC(:,6) = strpad('P_T in component model_parameters (litre_per_hour)'); LEGEND_CONSTANTS(:,42) = strpad('Y_i_D_p_1plus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,43) = strpad('Y_PT_D_p_1plus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,44) = strpad('Y_k_Jext_1plus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,45) = strpad('Y_PTH_p_2plus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,46) = strpad('Y_PTH_p_2minus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,47) = strpad('Y_i_D_p_1minus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,48) = strpad('Y_PT_D_p_1minus in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,20) = strpad('Y_Ca_i_2plus_Max in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,21) = strpad('Y_PTH_p_2plus_Max in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,22) = strpad('Y_PTH_p_2minus_Max in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,23) = strpad('Y_Ca_p_1plus_minus_Max in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,24) = strpad('Y_i_D_p_1plus_minus_Max in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,25) = strpad('Y_PT_D_p_1plus_minus_Max in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,26) = strpad('Y_k_Jext_1plus_minus_Max in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,27) = strpad('X_R_PTH_Ca in component model_parameters (millimolar)'); LEGEND_CONSTANTS(:,28) = strpad('X_R_i_Ca in component model_parameters (millimolar)'); LEGEND_CONSTANTS(:,29) = strpad('X_R_PTH_D in component model_parameters (picomolar)'); LEGEND_CONSTANTS(:,30) = strpad('X_R_E_PTH in component model_parameters (picomolar)'); LEGEND_CONSTANTS(:,31) = strpad('X_R_i_D in component model_parameters (picomolar)'); LEGEND_CONSTANTS(:,32) = strpad('X_R_P_k_PTH in component model_parameters (picomolar)'); LEGEND_CONSTANTS(:,33) = strpad('b_PTH_Ca in component model_parameters (litre_per_millimole)'); LEGEND_CONSTANTS(:,34) = strpad('b_PTH_D in component model_parameters (litre_per_picomole)'); LEGEND_CONSTANTS(:,35) = strpad('b_E_PTH in component model_parameters (litre_per_picomole)'); LEGEND_CONSTANTS(:,36) = strpad('b_i_D in component model_parameters (litre_per_picomole)'); LEGEND_CONSTANTS(:,37) = strpad('a in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,38) = strpad('d in component model_parameters (dimensionless)'); LEGEND_RATES(:,1) = strpad('d/dt Q_Ca_p in component Q_Ca_p (millimole)'); LEGEND_RATES(:,2) = strpad('d/dt Q_P_p in component Q_P_p (millimole)'); LEGEND_RATES(:,3) = strpad('d/dt Q_P_c in component Q_P_c (millimole)'); LEGEND_RATES(:,4) = strpad('d/dt Q_PTH_p in component Q_PTH_p (millimole)'); LEGEND_RATES(:,5) = strpad('d/dt Q_Ca_b in component Q_Ca_b (millimole)'); LEGEND_RATES(:,6) = strpad('d/dt Q_P_b in component Q_P_b (millimole)'); LEGEND_RATES(:,7) = strpad('d/dt Q_E_k in component Q_E_k (millimole)'); LEGEND_RATES(:,8) = strpad('d/dt Q_D_p in component Q_D_p (millimole)'); LEGEND_RATES(:,9) = strpad('d/dt Q_TCa_i in component Q_TCa_i (millimole)'); LEGEND_RATES(:,10) = strpad('d/dt Q_C_PT in component Q_C_PT (millimole)'); LEGEND_RATES(:,11) = strpad('d/dt Q_TP_k in component Q_TP_k (millimole)'); 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) = 1.000; CONSTANTS(:,1) = 3.3; CONSTANTS(:,2) = 0.21; STATES(:,2) = 1.000; CONSTANTS(:,3) = 1.27; STATES(:,3) = 3226.0; STATES(:,4) = 1.000; CONSTANTS(:,4) = 3.85; CONSTANTS(:,5) = 100.0; STATES(:,5) = 100.0; STATES(:,6) = 1.000; STATES(:,7) = 1.000; CONSTANTS(:,6) = 1.000; CONSTANTS(:,7) = 0.05; STATES(:,8) = 1.000; CONSTANTS(:,8) = 0.1; STATES(:,9) = 1.000; STATES(:,10) = 1.000; STATES(:,11) = 1.000; CONSTANTS(:,9) = 1.000; CONSTANTS(:,10) = 1.000; CONSTANTS(:,11) = 0.464; CONSTANTS(:,12) = 1.000; CONSTANTS(:,13) = 1.000; CONSTANTS(:,14) = 2.4; CONSTANTS(:,15) = 1.2; CONSTANTS(:,16) = 1.000; CONSTANTS(:,17) = 1.000; CONSTANTS(:,18) = 51.8; CONSTANTS(:,19) = 90.0; CONSTANTS(:,20) = 1.000; CONSTANTS(:,21) = 1.000; CONSTANTS(:,22) = 1.000; CONSTANTS(:,23) = 0.02; CONSTANTS(:,24) = 0.02; CONSTANTS(:,25) = 0.01; CONSTANTS(:,26) = 0.01; CONSTANTS(:,27) = 1.0; CONSTANTS(:,28) = 1.0; CONSTANTS(:,29) = 90.0; CONSTANTS(:,30) = 3.85; CONSTANTS(:,31) = 90.0; CONSTANTS(:,32) = 90.0; CONSTANTS(:,33) = 0.05; CONSTANTS(:,34) = 0.03; CONSTANTS(:,35) = 0.55; CONSTANTS(:,36) = 0.03; CONSTANTS(:,37) = 0.85; CONSTANTS(:,38) = 0.15; CONSTANTS(:,39) = CONSTANTS(:,11).*CONSTANTS(:,1); CONSTANTS(:,40) = CONSTANTS(:,20).*(CONSTANTS(:,13)./(CONSTANTS(:,13)+CONSTANTS(:,28))); CONSTANTS(:,41) = CONSTANTS(:,23).*( CONSTANTS(:,37).*(1.00000 - tanh( CONSTANTS(:,33).*(CONSTANTS(:,14) - CONSTANTS(:,27))))+CONSTANTS(:,38)); CONSTANTS(:,42) = CONSTANTS(:,24).*( CONSTANTS(:,37).*(1.00000+ tanh( CONSTANTS(:,36).*(CONSTANTS(:,19) - CONSTANTS(:,31))))+CONSTANTS(:,38)); CONSTANTS(:,43) = CONSTANTS(:,25).*( CONSTANTS(:,37).*(1.00000+ tanh( CONSTANTS(:,34).*(CONSTANTS(:,19) - CONSTANTS(:,29))))+CONSTANTS(:,38)); CONSTANTS(:,44) = CONSTANTS(:,26).*( CONSTANTS(:,37).*(1.00000+ tanh( CONSTANTS(:,35).*( 1.00000.*CONSTANTS(:,15) - CONSTANTS(:,30))))+CONSTANTS(:,38)); CONSTANTS(:,45) = CONSTANTS(:,21).*(CONSTANTS(:,4)./(CONSTANTS(:,4)+ 1.00000.*CONSTANTS(:,32))); CONSTANTS(:,46) = CONSTANTS(:,22).*(CONSTANTS(:,32)./( 1.00000.*CONSTANTS(:,4)+CONSTANTS(:,32))); CONSTANTS(:,47) = CONSTANTS(:,24).*( CONSTANTS(:,37).*(1.00000 - tanh( CONSTANTS(:,36).*(CONSTANTS(:,19) - CONSTANTS(:,31))))+CONSTANTS(:,38)); CONSTANTS(:,48) = CONSTANTS(:,25).*( CONSTANTS(:,37).*(1.00000 - tanh( CONSTANTS(:,34).*(CONSTANTS(:,19) - CONSTANTS(:,29))))+CONSTANTS(:,38)); CONSTANTS(:,49) = CONSTANTS(:,10); CONSTANTS(:,50) = CONSTANTS(:,42); CONSTANTS(:,51) = CONSTANTS(:,47); CONSTANTS(:,52) = CONSTANTS(:,43); CONSTANTS(:,53) = CONSTANTS(:,48); CONSTANTS(:,54) = CONSTANTS(:,44); CONSTANTS(:,55) = 1.95000+CONSTANTS(:,45); CONSTANTS(:,56) = CONSTANTS(:,18).*1.00000.*CONSTANTS(:,15); CONSTANTS(:,57) = CONSTANTS(:,55)./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 RATES(:,7) = CONSTANTS(:,6) - CONSTANTS(:,7).*STATES(:,7); RATES(:,8) = 1.00000.*STATES(:,7) - CONSTANTS(:,8).*STATES(:,8); RATES(:,9) = (1.00000 - STATES(:,9)).*CONSTANTS(:,50) - STATES(:,9).*CONSTANTS(:,51); RATES(:,10) = (1.00000 - STATES(:,10)).*CONSTANTS(:,52) - STATES(:,10).*CONSTANTS(:,53); RATES(:,11) = (1.00000 - STATES(:,11)).*CONSTANTS(:,54) - STATES(:,11).*CONSTANTS(:,9); ALGEBRAIC(:,2) = 1.00000.*CONSTANTS(:,41).*( CONSTANTS(:,52).*(1.00000 - STATES(:,10)) - CONSTANTS(:,53).*STATES(:,10)); RATES(:,4) = ALGEBRAIC(:,2) - CONSTANTS(:,5).*1.00000.*CONSTANTS(:,4); ALGEBRAIC(:,1) = CONSTANTS(:,16).*STATES(:,5); RATES(:,5) = ALGEBRAIC(:,1) - CONSTANTS(:,1); ALGEBRAIC(:,4) = ( CONSTANTS(:,40).*(1.00000 - STATES(:,9)).*1.00000.*CONSTANTS(:,50)+ CONSTANTS(:,12).*1.00000.*(CONSTANTS(:,13) - CONSTANTS(:,14))) - STATES(:,9).*CONSTANTS(:,51); RATES(:,1) = (CONSTANTS(:,1)+ALGEBRAIC(:,4)) - (ALGEBRAIC(:,1)+CONSTANTS(:,2)); ALGEBRAIC(:,5) = CONSTANTS(:,11).*ALGEBRAIC(:,1); RATES(:,6) = ALGEBRAIC(:,5) - CONSTANTS(:,39); ALGEBRAIC(:,7) = CONSTANTS(:,17).*STATES(:,3); RATES(:,2) = (CONSTANTS(:,39)+CONSTANTS(:,49)+ALGEBRAIC(:,7)) - (ALGEBRAIC(:,5)+CONSTANTS(:,3)+CONSTANTS(:,56)); RATES(:,3) = CONSTANTS(:,56) - ALGEBRAIC(:,7); 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(:,2) = 1.00000.*CONSTANTS(:,41).*( CONSTANTS(:,52).*(1.00000 - STATES(:,10)) - CONSTANTS(:,53).*STATES(:,10)); ALGEBRAIC(:,1) = CONSTANTS(:,16).*STATES(:,5); ALGEBRAIC(:,4) = ( CONSTANTS(:,40).*(1.00000 - STATES(:,9)).*1.00000.*CONSTANTS(:,50)+ CONSTANTS(:,12).*1.00000.*(CONSTANTS(:,13) - CONSTANTS(:,14))) - STATES(:,9).*CONSTANTS(:,51); ALGEBRAIC(:,5) = CONSTANTS(:,11).*ALGEBRAIC(:,1); ALGEBRAIC(:,7) = CONSTANTS(:,17).*STATES(:,3); ALGEBRAIC(:,3) = CONSTANTS(:,46).*STATES(:,11); ALGEBRAIC(:,6) = ALGEBRAIC(:,3)./CONSTANTS(:,15); 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