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 =11; end % There are a total of 4 entries in each of the rate and state variable arrays. % There are a total of 35 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 time_s (second)'); LEGEND_ALGEBRAIC(:,3) = strpad('Cai in component Cai (mM)'); LEGEND_CONSTANTS(:,1) = strpad('V in component control_para (mV)'); LEGEND_CONSTANTS(:,2) = strpad('M_init in component control_para (dimensionless)'); LEGEND_CONSTANTS(:,3) = strpad('Mp_init in component control_para (dimensionless)'); LEGEND_ALGEBRAIC(:,10) = strpad('J_VOCC in component J_VOCC (mM_per_s)'); LEGEND_ALGEBRAIC(:,4) = strpad('J_CaPump in component J_CaPump (mM_per_s)'); LEGEND_ALGEBRAIC(:,11) = strpad('J_NaCa in component J_NaCa (mM_per_s)'); LEGEND_CONSTANTS(:,34) = strpad('rho_vCa in component J_VOCC (dimensionless)'); LEGEND_ALGEBRAIC(:,7) = strpad('K_1 in component K_1 (per_s)'); LEGEND_ALGEBRAIC(:,1) = strpad('stress in component CB4HM (dimensionless)'); LEGEND_ALGEBRAIC(:,2) = strpad('phosphorylation in component CB4HM (dimensionless)'); LEGEND_CONSTANTS(:,4) = strpad('R in component constants (J_per_K_mol)'); LEGEND_CONSTANTS(:,5) = strpad('F in component constants (C_per_mmol)'); LEGEND_CONSTANTS(:,6) = strpad('T in component model_para (kelvin)'); LEGEND_CONSTANTS(:,7) = strpad('Nai in component model_para (mM)'); LEGEND_CONSTANTS(:,8) = strpad('Nao in component model_para (mM)'); LEGEND_CONSTANTS(:,9) = strpad('Cao in component model_para (mM)'); LEGEND_CONSTANTS(:,10) = strpad('V_cell in component model_para (fm3)'); LEGEND_CONSTANTS(:,11) = strpad('V_Cahalf in component model_para (mV)'); LEGEND_CONSTANTS(:,12) = strpad('K_Cahalf in component model_para (mV)'); LEGEND_CONSTANTS(:,13) = strpad('g_mCa in component model_para (nS)'); LEGEND_CONSTANTS(:,14) = strpad('V_pmax in component model_para (mM_per_s)'); LEGEND_CONSTANTS(:,15) = strpad('n in component model_para (dimensionless)'); LEGEND_CONSTANTS(:,16) = strpad('K_ph in component model_para (mM)'); LEGEND_CONSTANTS(:,17) = strpad('K_NaCa in component model_para (mM)'); LEGEND_CONSTANTS(:,18) = strpad('G_NaCa in component model_para (mM_per_s_mV)'); LEGEND_CONSTANTS(:,19) = strpad('n_M in component model_para (dimensionless)'); LEGEND_CONSTANTS(:,20) = strpad('Ca_halfMLCK in component model_para (mM)'); LEGEND_CONSTANTS(:,21) = strpad('inhPump in component model_para (dimensionless)'); LEGEND_CONSTANTS(:,22) = strpad('AM_init in component initials (dimensionless)'); LEGEND_CONSTANTS(:,23) = strpad('AMp_init in component initials (dimensionless)'); LEGEND_CONSTANTS(:,24) = strpad('K_7 in component model_para (per_s)'); LEGEND_CONSTANTS(:,25) = strpad('K_2 in component model_para (per_s)'); LEGEND_CONSTANTS(:,26) = strpad('K_3 in component model_para (per_s)'); LEGEND_CONSTANTS(:,27) = strpad('K_4 in component model_para (per_s)'); LEGEND_CONSTANTS(:,28) = strpad('K_5 in component model_para (per_s)'); LEGEND_CONSTANTS(:,29) = strpad('M_init in component initials (dimensionless)'); LEGEND_CONSTANTS(:,30) = strpad('Mp_init in component initials (dimensionless)'); LEGEND_CONSTANTS(:,31) = strpad('Cai_init in component initials (mM)'); LEGEND_CONSTANTS(:,32) = strpad('z_Ca in component E_Ca (dimensionless)'); LEGEND_ALGEBRAIC(:,6) = strpad('E in component Nernst_potential (mV)'); LEGEND_CONSTANTS(:,33) = strpad('z_Na in component E_Na (dimensionless)'); LEGEND_CONSTANTS(:,35) = strpad('E in component Nernst_potential (mV)'); LEGEND_ALGEBRAIC(:,8) = strpad('I in component Ionic_currents (pA)'); LEGEND_ALGEBRAIC(:,9) = strpad('V_mNaCa in component J_NaCa (mV)'); LEGEND_ALGEBRAIC(:,5) = strpad('norm in component CB4HM (dimensionless)'); LEGEND_STATES(:,1) = strpad('M in component CB4HM (dimensionless)'); LEGEND_STATES(:,2) = strpad('Mp in component CB4HM (dimensionless)'); LEGEND_STATES(:,3) = strpad('AM in component CB4HM (dimensionless)'); LEGEND_STATES(:,4) = strpad('AMp in component CB4HM (dimensionless)'); LEGEND_RATES(:,1) = strpad('d/dt M in component CB4HM (dimensionless)'); LEGEND_RATES(:,2) = strpad('d/dt Mp in component CB4HM (dimensionless)'); LEGEND_RATES(:,3) = strpad('d/dt AM in component CB4HM (dimensionless)'); LEGEND_RATES(:,4) = strpad('d/dt AMp in component CB4HM (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 = []; CONSTANTS(:,1) = -80; CONSTANTS(:,2) = 1; CONSTANTS(:,3) = 0; CONSTANTS(:,4) = 8.314; CONSTANTS(:,5) = 96.48534; CONSTANTS(:,6) = 310; CONSTANTS(:,7) = 2.9836; CONSTANTS(:,8) = 140; CONSTANTS(:,9) = 2; CONSTANTS(:,10) = 21; CONSTANTS(:,11) = -27; CONSTANTS(:,12) = 11; CONSTANTS(:,13) = 0.046842; CONSTANTS(:,14) = 5.1449e-4; CONSTANTS(:,15) = 1.9015; CONSTANTS(:,16) = 0.6e-3; CONSTANTS(:,17) = 7e-3; CONSTANTS(:,18) = 5.7297e-5; CONSTANTS(:,19) = 8.7613; CONSTANTS(:,20) = 256.98e-6; CONSTANTS(:,21) = 1; CONSTANTS(:,22) = 0; CONSTANTS(:,23) = 0; CONSTANTS(:,24) = 0.0378; CONSTANTS(:,25) = 1.2387; CONSTANTS(:,26) = 0.1419; CONSTANTS(:,27) = 0.035475; CONSTANTS(:,28) = 1.2387; CONSTANTS(:,29) = 1; CONSTANTS(:,30) = 0; CONSTANTS(:,31) = 0.1e-6; CONSTANTS(:,32) = 2; CONSTANTS(:,33) = 1; CONSTANTS(:,34) = 1.00000./(1.00000+exp((CONSTANTS(:,11) - CONSTANTS(:,1))./CONSTANTS(:,12))); CONSTANTS(:,35) = (( CONSTANTS(:,4).*CONSTANTS(:,6))./( CONSTANTS(:,33).*CONSTANTS(:,5))).*log(CONSTANTS(:,8)./CONSTANTS(:,7)); STATES(:,1) = CONSTANTS(:,2); STATES(:,2) = CONSTANTS(:,3); STATES(:,3) = CONSTANTS(:,22); STATES(:,4) = CONSTANTS(:,23); 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(:,3) = 340.700.*1.00000e-06+ 71.3100.*1.00000e-06.*log(VOI+0.0500000); ALGEBRAIC(:,7) = (power(ALGEBRAIC(:,3), CONSTANTS(:,19))./(power(CONSTANTS(:,20), CONSTANTS(:,19))+power(ALGEBRAIC(:,3), CONSTANTS(:,19)))).*1.00000; ALGEBRAIC(:,5) = STATES(:,1)+STATES(:,2)+STATES(:,3)+STATES(:,4); RATES(:,1) = ( - ALGEBRAIC(:,7).*STATES(:,1))./ALGEBRAIC(:,5)+( CONSTANTS(:,25).*STATES(:,2))./ALGEBRAIC(:,5)+( CONSTANTS(:,24).*STATES(:,3))./ALGEBRAIC(:,5); RATES(:,2) = (( CONSTANTS(:,27).*STATES(:,4))./ALGEBRAIC(:,5)+( ALGEBRAIC(:,7).*STATES(:,1))./ALGEBRAIC(:,5)) - ( (CONSTANTS(:,25)+CONSTANTS(:,26)).*STATES(:,2))./ALGEBRAIC(:,5); RATES(:,3) = ( CONSTANTS(:,28).*STATES(:,4))./ALGEBRAIC(:,5) - ( (ALGEBRAIC(:,7)+CONSTANTS(:,24)).*STATES(:,3))./ALGEBRAIC(:,5); RATES(:,4) = (( CONSTANTS(:,26).*STATES(:,2))./ALGEBRAIC(:,5)+( ALGEBRAIC(:,7).*STATES(:,3))./ALGEBRAIC(:,5)) - ( (CONSTANTS(:,27)+CONSTANTS(:,28)).*STATES(:,4))./ALGEBRAIC(:,5); 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(:,3) = 340.700.*1.00000e-06+ 71.3100.*1.00000e-06.*log(VOI+0.0500000); ALGEBRAIC(:,7) = (power(ALGEBRAIC(:,3), CONSTANTS(:,19))./(power(CONSTANTS(:,20), CONSTANTS(:,19))+power(ALGEBRAIC(:,3), CONSTANTS(:,19)))).*1.00000; ALGEBRAIC(:,5) = STATES(:,1)+STATES(:,2)+STATES(:,3)+STATES(:,4); ALGEBRAIC(:,1) = STATES(:,4)+STATES(:,3); ALGEBRAIC(:,2) = STATES(:,4)+STATES(:,2); ALGEBRAIC(:,4) = ( - CONSTANTS(:,14).*power(ALGEBRAIC(:,3), CONSTANTS(:,15)))./(power(CONSTANTS(:,16), CONSTANTS(:,15))+power(ALGEBRAIC(:,3), CONSTANTS(:,15))); ALGEBRAIC(:,6) = (( CONSTANTS(:,4).*CONSTANTS(:,6))./( CONSTANTS(:,32).*CONSTANTS(:,5))).*log(CONSTANTS(:,9)./ALGEBRAIC(:,3)); ALGEBRAIC(:,8) = CONSTANTS(:,13).*CONSTANTS(:,34).*(CONSTANTS(:,1) - ALGEBRAIC(:,6)); ALGEBRAIC(:,9) = 3.00000.*CONSTANTS(:,35) - 2.00000.*ALGEBRAIC(:,6); ALGEBRAIC(:,10) = - ALGEBRAIC(:,8)./( 2.00000.*CONSTANTS(:,10).*CONSTANTS(:,5)); ALGEBRAIC(:,11) = (( CONSTANTS(:,18).*ALGEBRAIC(:,3))./(ALGEBRAIC(:,3)+CONSTANTS(:,17))).*(CONSTANTS(:,1) - ALGEBRAIC(:,9)); 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