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 4 entries in each of the rate and state variable arrays. % There are a total of 6 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('t in component main (second)'); LEGEND_CONSTANTS(:,1) = strpad('RT in component main (J_per_mol)'); LEGEND_STATES(:,1) = strpad('q_1 in component main (mole)'); LEGEND_STATES(:,2) = strpad('q_2 in component main (mole)'); LEGEND_STATES(:,3) = strpad('q_3 in component main (mole)'); LEGEND_STATES(:,4) = strpad('q_4 in component main (mole)'); LEGEND_ALGEBRAIC(:,7) = strpad('v_r in component main (mol_per_s)'); LEGEND_ALGEBRAIC(:,1) = strpad('u_1 in component main (J_per_mol)'); LEGEND_ALGEBRAIC(:,2) = strpad('u_2 in component main (J_per_mol)'); LEGEND_ALGEBRAIC(:,4) = strpad('u_3 in component main (J_per_mol)'); LEGEND_ALGEBRAIC(:,5) = strpad('u_4 in component main (J_per_mol)'); LEGEND_ALGEBRAIC(:,3) = strpad('u_f in component main (J_per_mol)'); LEGEND_ALGEBRAIC(:,6) = strpad('u_r in component main (J_per_mol)'); LEGEND_CONSTANTS(:,2) = strpad('K_q_1 in component main (per_mol)'); LEGEND_CONSTANTS(:,3) = strpad('K_q_2 in component main (per_mol)'); LEGEND_CONSTANTS(:,4) = strpad('K_q_3 in component main (per_mol)'); LEGEND_CONSTANTS(:,5) = strpad('K_q_4 in component main (per_mol)'); LEGEND_CONSTANTS(:,6) = strpad('kappa_r in component main (mol_per_s)'); LEGEND_RATES(:,1) = strpad('d/dt q_1 in component main (mole)'); LEGEND_RATES(:,2) = strpad('d/dt q_2 in component main (mole)'); LEGEND_RATES(:,3) = strpad('d/dt q_3 in component main (mole)'); LEGEND_RATES(:,4) = strpad('d/dt q_4 in component main (mole)'); 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) = 2578.73058; STATES(:,1) = 1; STATES(:,2) = 1; STATES(:,3) = 0; STATES(:,4) = 0; CONSTANTS(:,2) = 2; CONSTANTS(:,3) = 2; CONSTANTS(:,4) = 2; CONSTANTS(:,5) = 2; CONSTANTS(:,6) = 0.1; 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(:,1) = CONSTANTS(:,1).*log( CONSTANTS(:,2).*STATES(:,1)); ALGEBRAIC(:,2) = CONSTANTS(:,1).*log( CONSTANTS(:,3).*STATES(:,2)); ALGEBRAIC(:,3) = ALGEBRAIC(:,1)+ 2.00000.*ALGEBRAIC(:,2); ALGEBRAIC(:,4) = CONSTANTS(:,1).*log( CONSTANTS(:,4).*STATES(:,3)); ALGEBRAIC(:,5) = CONSTANTS(:,1).*log( CONSTANTS(:,5).*STATES(:,4)); ALGEBRAIC(:,6) = ALGEBRAIC(:,4)+ 2.00000.*ALGEBRAIC(:,5); ALGEBRAIC(:,7) = CONSTANTS(:,6).*(exp(ALGEBRAIC(:,3)./CONSTANTS(:,1)) - exp(ALGEBRAIC(:,6)./CONSTANTS(:,1))); RATES(:,1) = - ALGEBRAIC(:,7); RATES(:,2) = - 2.00000.*ALGEBRAIC(:,7); RATES(:,3) = ALGEBRAIC(:,7); RATES(:,4) = 2.00000.*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(:,1) = CONSTANTS(:,1).*log( CONSTANTS(:,2).*STATES(:,1)); ALGEBRAIC(:,2) = CONSTANTS(:,1).*log( CONSTANTS(:,3).*STATES(:,2)); ALGEBRAIC(:,3) = ALGEBRAIC(:,1)+ 2.00000.*ALGEBRAIC(:,2); ALGEBRAIC(:,4) = CONSTANTS(:,1).*log( CONSTANTS(:,4).*STATES(:,3)); ALGEBRAIC(:,5) = CONSTANTS(:,1).*log( CONSTANTS(:,5).*STATES(:,4)); ALGEBRAIC(:,6) = ALGEBRAIC(:,4)+ 2.00000.*ALGEBRAIC(:,5); ALGEBRAIC(:,7) = CONSTANTS(:,6).*(exp(ALGEBRAIC(:,3)./CONSTANTS(:,1)) - exp(ALGEBRAIC(:,6)./CONSTANTS(:,1))); 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