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 =1; end % There are a total of 17 entries in each of the rate and state variable arrays. % There are a total of 16 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_STATES(:,1) = strpad('Py in component Py (nanomolar)'); LEGEND_CONSTANTS(:,1) = strpad('Zn in component model_parameters (nanomolar)'); LEGEND_STATES(:,2) = strpad('Py1 in component Py1 (nanomolar)'); LEGEND_CONSTANTS(:,2) = strpad('r3 in component model_parameters (third_order_rate_constant)'); LEGEND_CONSTANTS(:,3) = strpad('r4 in component model_parameters (first_order_rate_constant)'); LEGEND_STATES(:,3) = strpad('Dw in component Dw (nanomolar)'); LEGEND_STATES(:,4) = strpad('Qw2 in component Qw2 (nanomolar)'); LEGEND_CONSTANTS(:,4) = strpad('k_1 in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,5) = strpad('k1a in component model_parameters (second_order_rate_constant)'); LEGEND_STATES(:,5) = strpad('Qw1 in component Qw1 (nanomolar)'); LEGEND_STATES(:,6) = strpad('Rw in component Rw (nanomolar)'); LEGEND_CONSTANTS(:,6) = strpad('k2 in component model_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,7) = strpad('k_2 in component model_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,1) = strpad('k3 in component model_parameters (first_order_rate_constant)'); LEGEND_STATES(:,7) = strpad('Mw in component Mw (nanomolar)'); LEGEND_STATES(:,8) = strpad('Px in component Px (nanomolar)'); LEGEND_STATES(:,9) = strpad('Px1 in component Px1 (nanomolar)'); LEGEND_STATES(:,10) = strpad('Dz in component Dz (nanomolar)'); LEGEND_STATES(:,11) = strpad('Qz4 in component Qz4 (nanomolar)'); LEGEND_CONSTANTS(:,8) = strpad('r1 in component model_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,9) = strpad('r2 in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,10) = strpad('k1b in component model_parameters (second_order_rate_constant)'); LEGEND_STATES(:,12) = strpad('Qz2 in component Qz2 (nanomolar)'); LEGEND_CONSTANTS(:,11) = strpad('k1 in component model_parameters (second_order_rate_constant)'); LEGEND_STATES(:,13) = strpad('Qz1 in component Qz1 (nanomolar)'); LEGEND_STATES(:,14) = strpad('Rz in component Rz (nanomolar)'); LEGEND_CONSTANTS(:,12) = strpad('k2a in component model_parameters (second_order_rate_constant)'); LEGEND_STATES(:,15) = strpad('Qz3 in component Qz3 (nanomolar)'); LEGEND_STATES(:,16) = strpad('Qz5 in component Qz5 (nanomolar)'); LEGEND_CONSTANTS(:,13) = strpad('k2b in component model_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,14) = strpad('k2c in component model_parameters (second_order_rate_constant)'); LEGEND_STATES(:,17) = strpad('Mz in component Mz (nanomolar)'); LEGEND_CONSTANTS(:,15) = strpad('td0 in component model_parameters (second)'); LEGEND_CONSTANTS(:,16) = strpad('td in component model_parameters (second)'); LEGEND_RATES(:,1) = strpad('d/dt Py in component Py (nanomolar)'); LEGEND_RATES(:,2) = strpad('d/dt Py1 in component Py1 (nanomolar)'); LEGEND_RATES(:,3) = strpad('d/dt Dw in component Dw (nanomolar)'); LEGEND_RATES(:,6) = strpad('d/dt Rw in component Rw (nanomolar)'); LEGEND_RATES(:,5) = strpad('d/dt Qw1 in component Qw1 (nanomolar)'); LEGEND_RATES(:,4) = strpad('d/dt Qw2 in component Qw2 (nanomolar)'); LEGEND_RATES(:,7) = strpad('d/dt Mw in component Mw (nanomolar)'); LEGEND_RATES(:,8) = strpad('d/dt Px in component Px (nanomolar)'); LEGEND_RATES(:,9) = strpad('d/dt Px1 in component Px1 (nanomolar)'); LEGEND_RATES(:,10) = strpad('d/dt Dz in component Dz (nanomolar)'); LEGEND_RATES(:,14) = strpad('d/dt Rz in component Rz (nanomolar)'); LEGEND_RATES(:,13) = strpad('d/dt Qz1 in component Qz1 (nanomolar)'); LEGEND_RATES(:,12) = strpad('d/dt Qz2 in component Qz2 (nanomolar)'); LEGEND_RATES(:,15) = strpad('d/dt Qz3 in component Qz3 (nanomolar)'); LEGEND_RATES(:,11) = strpad('d/dt Qz4 in component Qz4 (nanomolar)'); LEGEND_RATES(:,16) = strpad('d/dt Qz5 in component Qz5 (nanomolar)'); LEGEND_RATES(:,17) = strpad('d/dt Mz in component Mz (nanomolar)'); 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) = 25.0; CONSTANTS(:,1) = 1E-5; STATES(:,2) = 0.0; CONSTANTS(:,2) = 4.41E10; CONSTANTS(:,3) = 9E-3; STATES(:,3) = 4.0; STATES(:,4) = 0.0; CONSTANTS(:,4) = 0.9; CONSTANTS(:,5) = 1.0; STATES(:,5) = 0.0; STATES(:,6) = 50.0; CONSTANTS(:,6) = 0.02; CONSTANTS(:,7) = 0.3; STATES(:,7) = 0.0; STATES(:,8) = 25.0; STATES(:,9) = 0.0; STATES(:,10) = 4.0; STATES(:,11) = 0.0; CONSTANTS(:,8) = 2.73E2; CONSTANTS(:,9) = 3.437E-4; CONSTANTS(:,10) = 1.253E-2; STATES(:,12) = 0.0; CONSTANTS(:,11) = 0.025; STATES(:,13) = 0.0; STATES(:,14) = 50.0; CONSTANTS(:,12) = 0.00005; STATES(:,15) = 0.0; STATES(:,16) = 0.0; CONSTANTS(:,13) = 0.0002; CONSTANTS(:,14) = 0.0037; STATES(:,17) = 0.0; CONSTANTS(:,15) = 1800.0; CONSTANTS(:,16) = 2700; 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(:,3).*STATES(:,2) - CONSTANTS(:,2).*power(CONSTANTS(:,1), 2.00000).*STATES(:,1); RATES(:,2) = ( CONSTANTS(:,2).*power(CONSTANTS(:,1), 2.00000).*STATES(:,1)+ CONSTANTS(:,4).*STATES(:,4)) - ( CONSTANTS(:,3).*STATES(:,2)+ CONSTANTS(:,5).*STATES(:,3).*STATES(:,2)); RATES(:,4) = CONSTANTS(:,5).*STATES(:,3).*STATES(:,2) - CONSTANTS(:,4).*STATES(:,4); RATES(:,8) = ( CONSTANTS(:,9).*STATES(:,9)+ CONSTANTS(:,4).*STATES(:,11)) - ( CONSTANTS(:,8).*CONSTANTS(:,1).*STATES(:,8)+ CONSTANTS(:,10).*STATES(:,10).*STATES(:,8)); RATES(:,9) = ( CONSTANTS(:,8).*CONSTANTS(:,1).*STATES(:,8)+ CONSTANTS(:,4).*STATES(:,12)) - ( CONSTANTS(:,9).*STATES(:,9)+ CONSTANTS(:,11).*STATES(:,10).*STATES(:,9)); ALGEBRAIC(:,1) = piecewise({VOI>=0.00000&VOI=CONSTANTS(:,15)&VOI=0.00000&VOI=CONSTANTS(:,15)&VOI req_length strout = strin(1:req_length); else strout = [strin, blanks(req_length - insize)]; end end