Generated Code
The following is matlab code generated by the CellML API from this CellML file. (Back to language selection)
The raw code is available.
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 =20; end % There are a total of 17 entries in each of the rate and state variable arrays. % There are a total of 33 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_ALGEBRAIC(:,1) = strpad('v1 in component v1 (flux)'); LEGEND_STATES(:,1) = strpad('pM9 in component pM9 (molar)'); LEGEND_STATES(:,2) = strpad('M3 in component M3 (molar)'); LEGEND_STATES(:,3) = strpad('M3_pM9 in component M3_pM9 (molar)'); LEGEND_CONSTANTS(:,1) = strpad('kon_M3pM9 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,2) = strpad('koff_M3pM9 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,2) = strpad('v2 in component v2 (flux)'); LEGEND_CONSTANTS(:,3) = strpad('kact_M3pM9 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,3) = strpad('v3 in component v3 (flux)'); LEGEND_STATES(:,4) = strpad('M3_T1 in component M3_T1 (molar)'); LEGEND_STATES(:,5) = strpad('T1 in component T1 (molar)'); LEGEND_CONSTANTS(:,4) = strpad('kon_M3T1 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,5) = strpad('koff_M3T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,4) = strpad('v4 in component v4 (flux)'); LEGEND_STATES(:,6) = strpad('pM9_T1 in component pM9_T1 (molar)'); LEGEND_CONSTANTS(:,6) = strpad('kon_pM9T1 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,7) = strpad('koff_pM9T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,5) = strpad('v5 in component v5 (flux)'); LEGEND_STATES(:,7) = strpad('M3_pM9_T1 in component M3_pM9_T1 (molar)'); LEGEND_CONSTANTS(:,8) = strpad('kon_M3pM9T1 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,9) = strpad('koff_M3pM9T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,6) = strpad('v6 in component v6 (flux)'); LEGEND_CONSTANTS(:,10) = strpad('kact_M3pM9T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,7) = strpad('v7 in component v7 (flux)'); LEGEND_STATES(:,8) = strpad('M9_T1_star in component M9_T1_star (molar)'); LEGEND_STATES(:,9) = strpad('M9 in component M9 (molar)'); LEGEND_CONSTANTS(:,11) = strpad('kon_M9T1_star in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,12) = strpad('koff_M9T1_star in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,8) = strpad('v8 in component v8 (flux)'); LEGEND_STATES(:,10) = strpad('M9_T1 in component M9_T1 (molar)'); LEGEND_CONSTANTS(:,13) = strpad('kon_M9T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,14) = strpad('koff_M9T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,9) = strpad('v9 in component v9 (flux)'); LEGEND_STATES(:,11) = strpad('M9_T1_M3 in component M9_T1_M3 (molar)'); LEGEND_CONSTANTS(:,15) = strpad('kon_M9_M3T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_CONSTANTS(:,16) = strpad('koff_M9_M3T1 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_ALGEBRAIC(:,10) = strpad('v10 in component v10 (flux)'); LEGEND_STATES(:,12) = strpad('pM9_T1_M3 in component pM9_T1_M3 (molar)'); LEGEND_CONSTANTS(:,17) = strpad('kon_M3_pM9T1 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,18) = strpad('koff_M3_pM9T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,11) = strpad('v11 in component v11 (flux)'); LEGEND_CONSTANTS(:,19) = strpad('kon_pM9T1M3 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,20) = strpad('koff_pM9T1M3 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,12) = strpad('v12 in component v12 (flux)'); LEGEND_STATES(:,13) = strpad('M3_pM9_T1_M3 in component M3_pM9_T1_M3 (molar)'); LEGEND_CONSTANTS(:,21) = strpad('kon_M3pM9T1M3 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,22) = strpad('koff_M3pM9T1M3 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,13) = strpad('v13 in component v13 (flux)'); LEGEND_CONSTANTS(:,23) = strpad('kact_M3pM9T1M3 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,14) = strpad('v14 in component v14 (flux)'); LEGEND_CONSTANTS(:,24) = strpad('kact_M9T1M3 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,15) = strpad('v15 in component v15 (flux)'); LEGEND_STATES(:,14) = strpad('Tr in component Tr (molar)'); LEGEND_STATES(:,15) = strpad('Tr_pM9 in component Tr_pM9 (molar)'); LEGEND_CONSTANTS(:,25) = strpad('kon_TrpM9 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,26) = strpad('koff_TrpM9 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,16) = strpad('v16 in component v16 (flux)'); LEGEND_CONSTANTS(:,27) = strpad('kact_TrpM9 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,17) = strpad('v17 in component v17 (flux)'); LEGEND_STATES(:,16) = strpad('Tr_pM9_T1 in component Tr_pM9_T1 (molar)'); LEGEND_CONSTANTS(:,28) = strpad('kon_TrpM9T1 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,29) = strpad('koff_TrpM9T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,18) = strpad('v18 in component v18 (flux)'); LEGEND_CONSTANTS(:,30) = strpad('kact_TrpM9T1 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,19) = strpad('v19 in component v19 (flux)'); LEGEND_STATES(:,17) = strpad('Tr_pM9_T1_M3 in component Tr_pM9_T1_M3 (molar)'); LEGEND_CONSTANTS(:,31) = strpad('kon_TrpM9T1M3 in component kinetic_parameters (second_order_rate_constant)'); LEGEND_CONSTANTS(:,32) = strpad('koff_TrpM9T1M3 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_ALGEBRAIC(:,20) = strpad('v20 in component v20 (flux)'); LEGEND_CONSTANTS(:,33) = strpad('kact_TrpM9T1M3 in component kinetic_parameters (first_order_rate_constant)'); LEGEND_RATES(:,1) = strpad('d/dt pM9 in component pM9 (molar)'); LEGEND_RATES(:,2) = strpad('d/dt M3 in component M3 (molar)'); LEGEND_RATES(:,3) = strpad('d/dt M3_pM9 in component M3_pM9 (molar)'); LEGEND_RATES(:,5) = strpad('d/dt T1 in component T1 (molar)'); LEGEND_RATES(:,4) = strpad('d/dt M3_T1 in component M3_T1 (molar)'); LEGEND_RATES(:,6) = strpad('d/dt pM9_T1 in component pM9_T1 (molar)'); LEGEND_RATES(:,7) = strpad('d/dt M3_pM9_T1 in component M3_pM9_T1 (molar)'); LEGEND_RATES(:,8) = strpad('d/dt M9_T1_star in component M9_T1_star (molar)'); LEGEND_RATES(:,9) = strpad('d/dt M9 in component M9 (molar)'); LEGEND_RATES(:,10) = strpad('d/dt M9_T1 in component M9_T1 (molar)'); LEGEND_RATES(:,12) = strpad('d/dt pM9_T1_M3 in component pM9_T1_M3 (molar)'); LEGEND_RATES(:,13) = strpad('d/dt M3_pM9_T1_M3 in component M3_pM9_T1_M3 (molar)'); LEGEND_RATES(:,11) = strpad('d/dt M9_T1_M3 in component M9_T1_M3 (molar)'); LEGEND_RATES(:,14) = strpad('d/dt Tr in component Tr (molar)'); LEGEND_RATES(:,15) = strpad('d/dt Tr_pM9 in component Tr_pM9 (molar)'); LEGEND_RATES(:,16) = strpad('d/dt Tr_pM9_T1 in component Tr_pM9_T1 (molar)'); LEGEND_RATES(:,17) = strpad('d/dt Tr_pM9_T1_M3 in component Tr_pM9_T1_M3 (molar)'); 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) = 3e-7; STATES(:,2) = 5e-8; STATES(:,3) = 0; CONSTANTS(:,1) = 1e4; CONSTANTS(:,2) = 0.001; CONSTANTS(:,3) = 0.0019; STATES(:,4) = 0; STATES(:,5) = 3e-7; CONSTANTS(:,4) = 1.9e6; CONSTANTS(:,5) = 1.26e-4; STATES(:,6) = 0; CONSTANTS(:,6) = 3.4e4; CONSTANTS(:,7) = 7.48e-5; STATES(:,7) = 0; CONSTANTS(:,8) = 1e4; CONSTANTS(:,9) = 0.001; CONSTANTS(:,10) = 0.00057; STATES(:,8) = 0; STATES(:,9) = 0; CONSTANTS(:,11) = 5.6e6; CONSTANTS(:,12) = 1.6e-5; STATES(:,10) = 0; CONSTANTS(:,13) = 6.3e-4; CONSTANTS(:,14) = 2.5e-4; STATES(:,11) = 0; CONSTANTS(:,15) = 7.48e-5; CONSTANTS(:,16) = 3.4e4; STATES(:,12) = 0; CONSTANTS(:,17) = 1.9e6; CONSTANTS(:,18) = 1.26e-4; CONSTANTS(:,19) = 3.4e4; CONSTANTS(:,20) = 7.48e-5; STATES(:,13) = 0; CONSTANTS(:,21) = 1e4; CONSTANTS(:,22) = 0.001; CONSTANTS(:,23) = 0.00057; CONSTANTS(:,24) = 1.26e-4; STATES(:,14) = 1e-7; STATES(:,15) = 0; CONSTANTS(:,25) = 1e4; CONSTANTS(:,26) = 0.007; CONSTANTS(:,27) = 0.002; STATES(:,16) = 0; CONSTANTS(:,28) = 1e4; CONSTANTS(:,29) = 0.007; CONSTANTS(:,30) = 0.0006; STATES(:,17) = 0; CONSTANTS(:,31) = 1e4; CONSTANTS(:,32) = 0.007; CONSTANTS(:,33) = 0.0006; 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).*STATES(:,2).*STATES(:,1) - CONSTANTS(:,2).*STATES(:,3); ALGEBRAIC(:,2) = CONSTANTS(:,3).*STATES(:,3); RATES(:,3) = ALGEBRAIC(:,1) - ALGEBRAIC(:,2); ALGEBRAIC(:,5) = CONSTANTS(:,8).*STATES(:,6).*STATES(:,2) - CONSTANTS(:,9).*STATES(:,7); ALGEBRAIC(:,6) = CONSTANTS(:,10).*STATES(:,7); RATES(:,7) = ALGEBRAIC(:,5) - ALGEBRAIC(:,6); ALGEBRAIC(:,3) = CONSTANTS(:,4).*STATES(:,2).*STATES(:,5) - CONSTANTS(:,5).*STATES(:,4); ALGEBRAIC(:,4) = CONSTANTS(:,6).*STATES(:,1).*STATES(:,5) - CONSTANTS(:,7).*STATES(:,6); ALGEBRAIC(:,7) = CONSTANTS(:,11).*STATES(:,5).*STATES(:,9) - CONSTANTS(:,12).*STATES(:,8); RATES(:,5) = - (ALGEBRAIC(:,3)+ALGEBRAIC(:,4)+ALGEBRAIC(:,7)); ALGEBRAIC(:,9) = CONSTANTS(:,15).*STATES(:,11) - CONSTANTS(:,16).*STATES(:,9).*STATES(:,4); ALGEBRAIC(:,11) = CONSTANTS(:,19).*STATES(:,1).*STATES(:,4) - CONSTANTS(:,20).*STATES(:,12); RATES(:,4) = (ALGEBRAIC(:,3)+ALGEBRAIC(:,9)) - ALGEBRAIC(:,11); ALGEBRAIC(:,12) = CONSTANTS(:,21).*STATES(:,2).*STATES(:,12) - CONSTANTS(:,22).*STATES(:,13); ALGEBRAIC(:,13) = CONSTANTS(:,23).*STATES(:,13); RATES(:,13) = ALGEBRAIC(:,12) - ALGEBRAIC(:,13); ALGEBRAIC(:,10) = CONSTANTS(:,17).*STATES(:,6).*STATES(:,2) - CONSTANTS(:,18).*STATES(:,12); ALGEBRAIC(:,14) = CONSTANTS(:,24).*STATES(:,11); RATES(:,2) = (ALGEBRAIC(:,2)+ALGEBRAIC(:,6)+ALGEBRAIC(:,13)+ALGEBRAIC(:,14)) - (ALGEBRAIC(:,1)+ALGEBRAIC(:,3)+ALGEBRAIC(:,5)+ALGEBRAIC(:,10)+ALGEBRAIC(:,12)); ALGEBRAIC(:,8) = CONSTANTS(:,13).*STATES(:,8) - CONSTANTS(:,14).*STATES(:,10); RATES(:,8) = (ALGEBRAIC(:,7)+ALGEBRAIC(:,14)) - ALGEBRAIC(:,8); ALGEBRAIC(:,15) = CONSTANTS(:,25).*STATES(:,14).*STATES(:,1) - CONSTANTS(:,26).*STATES(:,15); RATES(:,1) = - (ALGEBRAIC(:,1)+ALGEBRAIC(:,4)+ALGEBRAIC(:,15)+ALGEBRAIC(:,11)); ALGEBRAIC(:,16) = CONSTANTS(:,27).*STATES(:,15); RATES(:,9) = (ALGEBRAIC(:,2)+ALGEBRAIC(:,9)+ALGEBRAIC(:,16)) - ALGEBRAIC(:,7); RATES(:,15) = ALGEBRAIC(:,15) - ALGEBRAIC(:,16); ALGEBRAIC(:,17) = CONSTANTS(:,28).*STATES(:,6).*STATES(:,14) - CONSTANTS(:,29).*STATES(:,16); RATES(:,6) = ALGEBRAIC(:,4) - (ALGEBRAIC(:,5)+ALGEBRAIC(:,10)+ALGEBRAIC(:,17)); ALGEBRAIC(:,18) = CONSTANTS(:,30).*STATES(:,16); RATES(:,10) = ALGEBRAIC(:,8)+ALGEBRAIC(:,18); RATES(:,16) = ALGEBRAIC(:,17) - ALGEBRAIC(:,18); ALGEBRAIC(:,19) = CONSTANTS(:,31).*STATES(:,14).*STATES(:,12) - CONSTANTS(:,32).*STATES(:,17); RATES(:,12) = (ALGEBRAIC(:,10)+ALGEBRAIC(:,11)) - (ALGEBRAIC(:,12)+ALGEBRAIC(:,19)); ALGEBRAIC(:,20) = CONSTANTS(:,33).*STATES(:,17); RATES(:,11) = (ALGEBRAIC(:,13)+ALGEBRAIC(:,20)) - (ALGEBRAIC(:,14)+ALGEBRAIC(:,9)); RATES(:,14) = (ALGEBRAIC(:,16)+ALGEBRAIC(:,18)+ALGEBRAIC(:,20)) - (ALGEBRAIC(:,15)+ALGEBRAIC(:,17)+ALGEBRAIC(:,19)); RATES(:,17) = ALGEBRAIC(:,19) - ALGEBRAIC(:,20); 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).*STATES(:,2).*STATES(:,1) - CONSTANTS(:,2).*STATES(:,3); ALGEBRAIC(:,2) = CONSTANTS(:,3).*STATES(:,3); ALGEBRAIC(:,5) = CONSTANTS(:,8).*STATES(:,6).*STATES(:,2) - CONSTANTS(:,9).*STATES(:,7); ALGEBRAIC(:,6) = CONSTANTS(:,10).*STATES(:,7); ALGEBRAIC(:,3) = CONSTANTS(:,4).*STATES(:,2).*STATES(:,5) - CONSTANTS(:,5).*STATES(:,4); ALGEBRAIC(:,4) = CONSTANTS(:,6).*STATES(:,1).*STATES(:,5) - CONSTANTS(:,7).*STATES(:,6); ALGEBRAIC(:,7) = CONSTANTS(:,11).*STATES(:,5).*STATES(:,9) - CONSTANTS(:,12).*STATES(:,8); ALGEBRAIC(:,9) = CONSTANTS(:,15).*STATES(:,11) - CONSTANTS(:,16).*STATES(:,9).*STATES(:,4); ALGEBRAIC(:,11) = CONSTANTS(:,19).*STATES(:,1).*STATES(:,4) - CONSTANTS(:,20).*STATES(:,12); ALGEBRAIC(:,12) = CONSTANTS(:,21).*STATES(:,2).*STATES(:,12) - CONSTANTS(:,22).*STATES(:,13); ALGEBRAIC(:,13) = CONSTANTS(:,23).*STATES(:,13); ALGEBRAIC(:,10) = CONSTANTS(:,17).*STATES(:,6).*STATES(:,2) - CONSTANTS(:,18).*STATES(:,12); ALGEBRAIC(:,14) = CONSTANTS(:,24).*STATES(:,11); ALGEBRAIC(:,8) = CONSTANTS(:,13).*STATES(:,8) - CONSTANTS(:,14).*STATES(:,10); ALGEBRAIC(:,15) = CONSTANTS(:,25).*STATES(:,14).*STATES(:,1) - CONSTANTS(:,26).*STATES(:,15); ALGEBRAIC(:,16) = CONSTANTS(:,27).*STATES(:,15); ALGEBRAIC(:,17) = CONSTANTS(:,28).*STATES(:,6).*STATES(:,14) - CONSTANTS(:,29).*STATES(:,16); ALGEBRAIC(:,18) = CONSTANTS(:,30).*STATES(:,16); ALGEBRAIC(:,19) = CONSTANTS(:,31).*STATES(:,14).*STATES(:,12) - CONSTANTS(:,32).*STATES(:,17); ALGEBRAIC(:,20) = CONSTANTS(:,33).*STATES(:,17); 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