MATLAB ( ) abs(#expr1) #exprs[&] acos(#expr1) acosh(#expr1) acot(#expr1) acoth(#expr1) acsc(#expr1) acsch(#expr1) asec(#expr1) asech(#expr1) asin(#expr1) asinh(#expr1) atan(#expr1) atanh(#expr1) ceil(#expr1) cos(#expr1) cosh(#expr1) cot(#expr1) coth(#expr1) csc(#expr1) csch(#expr1) #lookupDiffVariable #expr1./#expr2 #exprs[==] exp(#expr1) factorial(#expr1) mod(#expr1, #expr2) == 0 floor(#expr1) gcd_multi([#exprs[, ]]) #exprs[>=] #exprs[>] ~#expr1 || #expr2 defint(func#unique1, BOUND, CONSTANTS, RATES, VARIABLES, #bvarIndex)#supplement double func#unique1(double* BOUND, double* CONSTANTS, double* RATES, double* VARIABLES) { return #expr1; } lcm_multi([#exprs[, ]]) #exprs[<=] log(#expr1) arbitrary_log(#expr1, #logbase) #exprs[<] max([utilOnes.*(#exprs[), utilOnes.*( ])],[],2) min([utilOnes.*(#exprs[), utilOnes.*( ])],[],2) #expr1 - #expr2 #expr1 ~= #expr2 ~#expr1 #exprs[||] #exprs[+] power(#expr1, #expr2) floor(#expr1 ./ #expr2) rem(#expr1, #expr2) power(#expr1, 1.0 ./ #degree) sec(#expr1) sech(#expr1) sin(#expr1) sinh(#expr1) tan(#expr1) tanh(#expr1) #exprs[.*] - #expr1 #expr1.*#expr2 + #expr3 #expr1.*#expr2 #expr1+#expr2 xor(#expr1 , #expr2) piecewise({#expr1, #expr2 , #expr1, #expr2 }, #expr1) }, NaN) pi 0.577215664901533 Inf CONSTANTS(:,%) STATES(:,%) ALGEBRAIC(:,%) RATES(:,%) VOI 1 <LHS> = <RHS>; <LHS> = <RHS>; (VOI, CONSTANTS, STATES, ALGEBRAIC); % Functions required for solving differential algebraic equation function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN) CONSTANTS = CONSTANTS_IN; STATES = STATES_IN; ALGEBRAIC = ALGEBRAIC_IN; global initialGuess_; if (length(initialGuess_) ~= 1), initialGuess_ = ;, end options = optimset('Display', 'off', 'TolX', 1E-6); if length(VOI) == 1 residualfn = @(algebraicCandidate)residualSN_(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES); = fsolve(residualfn, initialGuess_, options); initialGuess_ = ; else SET_ = logical(1); for i=1:length(VOI) residualfn = @(algebraicCandidate)residualSN_(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:)); TEMP_ = fsolve(residualfn, initialGuess_, options); ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC); initialGuess_ = TEMP_; end end end function resid = residualSN_(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES) = algebraicCandidate; resid = () - (); end ]]> (VOI, CONSTANTS, STATES, ALGEBRAIC); % Functions required for solving differential algebraic equation function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN) ALGEBRAIC = ALGEBRAIC_IN; CONSTANTS = CONSTANTS_IN; STATES = STATES_IN; global initialGuess_; if (length(initialGuess_) ~= ), initialGuess_ = [,];, end options = optimset('Display', 'off', 'TolX', 1E-6); if length(VOI) == 1 residualfn = @(algebraicCandidate)residualSN_(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES); soln = fsolve(residualfn, initialGuess_, options); initialGuess_ = soln; = soln(); else SET_ = logical(1); for i=1:length(VOI) residualfn = @(algebraicCandidate)residualSN_(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:)); soln = fsolve(residualfn, initialGuess_, options); initialGuess_ = soln; TEMP_ = soln(); ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC); end end end function resid = residualSN_(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES) = algebraicCandidate(); resid() = ; end ]]> 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 = ; end % There are a total of entries in each of the rate and state variable arrays. % There are a total of entries in the constant variable array. % , ]; % Set numerical accuracy options for ODE solver options = odeset('RelTol', , 'AbsTol', , 'MaxStep', ); % 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 [STATES, CONSTANTS] = initConsts() VOI = 0; CONSTANTS = []; STATES = []; ALGEBRAIC = []; 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 = 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 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 function [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends() LEGEND_STATES = ''; LEGEND_ALGEBRAIC = ''; LEGEND_VOI = ''; LEGEND_CONSTANTS = ''; LEGEND_STATES = LEGEND_STATES'; LEGEND_ALGEBRAIC = LEGEND_ALGEBRAIC'; LEGEND_RATES = LEGEND_RATES'; LEGEND_CONSTANTS = LEGEND_CONSTANTS'; end LEGEND_ = strpad(' ');