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 =15; end % There are a total of 5 entries in each of the rate and state variable arrays. % There are a total of 24 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_STATES(:,1) = strpad('y0 in component y0 (dimensionless)'); LEGEND_ALGEBRAIC(:,11) = strpad('g0 in component y0 (dimensionless)'); LEGEND_ALGEBRAIC(:,6) = strpad('r0 in component y0 (dimensionless)'); LEGEND_ALGEBRAIC(:,1) = strpad('h0 in component y0 (dimensionless)'); LEGEND_CONSTANTS(:,1) = strpad('S0 in component y0 (dimensionless)'); LEGEND_STATES(:,2) = strpad('y2 in component y2 (dimensionless)'); LEGEND_CONSTANTS(:,2) = strpad('a00 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,3) = strpad('a02 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,4) = strpad('c0 in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,5) = strpad('e0 in component model_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,6) = strpad('epsilon in component model_parameters (dimensionless)'); LEGEND_STATES(:,3) = strpad('y1 in component y1 (dimensionless)'); LEGEND_ALGEBRAIC(:,12) = strpad('g1 in component y1 (dimensionless)'); LEGEND_ALGEBRAIC(:,7) = strpad('r1 in component y1 (dimensionless)'); LEGEND_ALGEBRAIC(:,2) = strpad('h1 in component y1 (dimensionless)'); LEGEND_CONSTANTS(:,7) = strpad('S1 in component y1 (dimensionless)'); LEGEND_CONSTANTS(:,8) = strpad('a10 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,9) = strpad('a12 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,10) = strpad('a11 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,11) = strpad('e1 in component model_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,13) = strpad('g2 in component y2 (dimensionless)'); LEGEND_ALGEBRAIC(:,8) = strpad('r2 in component y2 (dimensionless)'); LEGEND_ALGEBRAIC(:,3) = strpad('h2 in component y2 (dimensionless)'); LEGEND_CONSTANTS(:,12) = strpad('S2 in component y2 (dimensionless)'); LEGEND_STATES(:,4) = strpad('y3 in component y3 (dimensionless)'); LEGEND_STATES(:,5) = strpad('y4 in component y4 (dimensionless)'); LEGEND_CONSTANTS(:,13) = strpad('a23 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,14) = strpad('a24 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,15) = strpad('a20 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,16) = strpad('a21 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,17) = strpad('a22 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,18) = strpad('e2 in component model_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,14) = strpad('g3 in component y3 (dimensionless)'); LEGEND_ALGEBRAIC(:,9) = strpad('r3 in component y3 (dimensionless)'); LEGEND_ALGEBRAIC(:,4) = strpad('h3 in component y3 (dimensionless)'); LEGEND_CONSTANTS(:,19) = strpad('S3 in component y3 (dimensionless)'); LEGEND_CONSTANTS(:,20) = strpad('a32 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,21) = strpad('a33 in component model_parameters (dimensionless)'); LEGEND_ALGEBRAIC(:,15) = strpad('g4 in component y4 (dimensionless)'); LEGEND_ALGEBRAIC(:,10) = strpad('r4 in component y4 (dimensionless)'); LEGEND_ALGEBRAIC(:,5) = strpad('h4 in component y4 (dimensionless)'); LEGEND_CONSTANTS(:,22) = strpad('S4 in component y4 (dimensionless)'); LEGEND_CONSTANTS(:,23) = strpad('a42 in component model_parameters (dimensionless)'); LEGEND_CONSTANTS(:,24) = strpad('a44 in component model_parameters (dimensionless)'); LEGEND_RATES(:,1) = strpad('d/dt y0 in component y0 (dimensionless)'); LEGEND_RATES(:,3) = strpad('d/dt y1 in component y1 (dimensionless)'); LEGEND_RATES(:,2) = strpad('d/dt y2 in component y2 (dimensionless)'); LEGEND_RATES(:,4) = strpad('d/dt y3 in component y3 (dimensionless)'); LEGEND_RATES(:,5) = strpad('d/dt y4 in component y4 (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 = []; STATES(:,1) = 0.4; CONSTANTS(:,1) = 0.010; STATES(:,2) = 1.17; CONSTANTS(:,2) = -0.00843; CONSTANTS(:,3) = -0.440; CONSTANTS(:,4) = 0.443; CONSTANTS(:,5) = 0.0; CONSTANTS(:,6) = 0.50; STATES(:,3) = 1.4; CONSTANTS(:,7) = 0.010; CONSTANTS(:,8) = 0.082; CONSTANTS(:,9) = -0.0668; CONSTANTS(:,10) = -0.0040; CONSTANTS(:,11) = 0.0; CONSTANTS(:,12) = 0.010; STATES(:,4) = 0.95; STATES(:,5) = 0.65; CONSTANTS(:,13) = 0.0576; CONSTANTS(:,14) = 3.25E-4; CONSTANTS(:,15) = 0.0; CONSTANTS(:,16) = 0.0310; CONSTANTS(:,17) = -0.0957; CONSTANTS(:,18) = 0.0; CONSTANTS(:,19) = 0.010; CONSTANTS(:,20) = 0.00869; CONSTANTS(:,21) = -0.00857; CONSTANTS(:,22) = 0.010; CONSTANTS(:,23) = 1.39E-4; CONSTANTS(:,24) = -1.43E-4; 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(:,6) = piecewise({STATES(:,1)0.00000, ( CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2))./(1.00000+ (( CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2))./CONSTANTS(:,1)).*(1.00000 - exp( - (( CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2))./CONSTANTS(:,1))))) , CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2)<=0.00000, CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2) }, NaN); ALGEBRAIC(:,11) = ALGEBRAIC(:,1).*ALGEBRAIC(:,6); RATES(:,1) = 1.00000.*ALGEBRAIC(:,11)+CONSTANTS(:,4)+CONSTANTS(:,5); ALGEBRAIC(:,7) = piecewise({STATES(:,3)0.00000, ( CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2))./(1.00000+ (( CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2))./CONSTANTS(:,7)).*(1.00000 - exp( - (( CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2))./CONSTANTS(:,7))))) , CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2)<=0.00000, CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2) }, NaN); ALGEBRAIC(:,12) = ALGEBRAIC(:,2).*ALGEBRAIC(:,7); RATES(:,3) = 1.00000.*ALGEBRAIC(:,12)+CONSTANTS(:,11); ALGEBRAIC(:,8) = piecewise({STATES(:,2)0.00000, ( CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5))./(1.00000+ (( CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5))./CONSTANTS(:,12)).*(1.00000 - exp( - (( CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5))./CONSTANTS(:,12))))) , CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5)<=0.00000, CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5) }, NaN); ALGEBRAIC(:,13) = ALGEBRAIC(:,3).*ALGEBRAIC(:,8); RATES(:,2) = 1.00000.*ALGEBRAIC(:,13)+CONSTANTS(:,18); ALGEBRAIC(:,9) = piecewise({STATES(:,4)0.00000, ( CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4))./(1.00000+ (( CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4))./CONSTANTS(:,19)).*(1.00000 - exp( - (( CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4))./CONSTANTS(:,19))))) , CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4)<=0.00000, CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4) }, NaN); ALGEBRAIC(:,14) = ALGEBRAIC(:,4).*ALGEBRAIC(:,9); RATES(:,4) = 1.00000.*ALGEBRAIC(:,14); ALGEBRAIC(:,10) = piecewise({STATES(:,5)0.00000, ( CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5))./(1.00000+ (( CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5))./CONSTANTS(:,22)).*(1.00000 - exp( - (( CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5))./CONSTANTS(:,22))))) , CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5)<=0.00000, CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5) }, NaN); ALGEBRAIC(:,15) = ALGEBRAIC(:,5).*ALGEBRAIC(:,10); RATES(:,5) = 1.00000.*ALGEBRAIC(:,15); 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(:,6) = piecewise({STATES(:,1)0.00000, ( CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2))./(1.00000+ (( CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2))./CONSTANTS(:,1)).*(1.00000 - exp( - (( CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2))./CONSTANTS(:,1))))) , CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2)<=0.00000, CONSTANTS(:,2).*STATES(:,1)+ CONSTANTS(:,3).*STATES(:,2) }, NaN); ALGEBRAIC(:,11) = ALGEBRAIC(:,1).*ALGEBRAIC(:,6); ALGEBRAIC(:,7) = piecewise({STATES(:,3)0.00000, ( CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2))./(1.00000+ (( CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2))./CONSTANTS(:,7)).*(1.00000 - exp( - (( CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2))./CONSTANTS(:,7))))) , CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2)<=0.00000, CONSTANTS(:,8).*STATES(:,1)+ CONSTANTS(:,10).*STATES(:,3)+ CONSTANTS(:,9).*STATES(:,2) }, NaN); ALGEBRAIC(:,12) = ALGEBRAIC(:,2).*ALGEBRAIC(:,7); ALGEBRAIC(:,8) = piecewise({STATES(:,2)0.00000, ( CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5))./(1.00000+ (( CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5))./CONSTANTS(:,12)).*(1.00000 - exp( - (( CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5))./CONSTANTS(:,12))))) , CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5)<=0.00000, CONSTANTS(:,15).*STATES(:,1)+ CONSTANTS(:,16).*STATES(:,3)+ CONSTANTS(:,17).*STATES(:,2)+ CONSTANTS(:,13).*STATES(:,4)+ CONSTANTS(:,14).*STATES(:,5) }, NaN); ALGEBRAIC(:,13) = ALGEBRAIC(:,3).*ALGEBRAIC(:,8); ALGEBRAIC(:,9) = piecewise({STATES(:,4)0.00000, ( CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4))./(1.00000+ (( CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4))./CONSTANTS(:,19)).*(1.00000 - exp( - (( CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4))./CONSTANTS(:,19))))) , CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4)<=0.00000, CONSTANTS(:,20).*STATES(:,2)+ CONSTANTS(:,21).*STATES(:,4) }, NaN); ALGEBRAIC(:,14) = ALGEBRAIC(:,4).*ALGEBRAIC(:,9); ALGEBRAIC(:,10) = piecewise({STATES(:,5)0.00000, ( CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5))./(1.00000+ (( CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5))./CONSTANTS(:,22)).*(1.00000 - exp( - (( CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5))./CONSTANTS(:,22))))) , CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5)<=0.00000, CONSTANTS(:,23).*STATES(:,2)+ CONSTANTS(:,24).*STATES(:,5) }, NaN); ALGEBRAIC(:,15) = ALGEBRAIC(:,5).*ALGEBRAIC(:,10); 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