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 =16; end % There are a total of 8 entries in each of the rate and state variable arrays. % There are a total of 37 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 Environmental (second)'); LEGEND_CONSTANTS(:,1) = strpad('S in component Parameters (hertz)'); LEGEND_CONSTANTS(:,2) = strpad('Zeta in component Parameters (dimensionless)'); LEGEND_CONSTANTS(:,3) = strpad('R0 in component Parameters (mm)'); LEGEND_CONSTANTS(:,4) = strpad('Cwall in component Parameters (mm_per_mmHg)'); LEGEND_CONSTANTS(:,5) = strpad('Kne in component Parameters (mmHg_per_mm)'); LEGEND_CONSTANTS(:,6) = strpad('K1 in component Parameters (mmHg_per_mm)'); LEGEND_CONSTANTS(:,7) = strpad('K2 in component Parameters (mmHg_per_mm)'); LEGEND_CONSTANTS(:,8) = strpad('K3 in component Parameters (mmHg_per_mm)'); LEGEND_CONSTANTS(:,9) = strpad('Bwall in component Parameters (mmHg_s_per_sq_mm)'); LEGEND_CONSTANTS(:,10) = strpad('B1 in component Parameters (mmHg_s_per_mm)'); LEGEND_CONSTANTS(:,11) = strpad('B2 in component Parameters (mmHg_s_per_mm)'); LEGEND_CONSTANTS(:,12) = strpad('B3 in component Parameters (mmHg_s_per_mm)'); LEGEND_CONSTANTS(:,13) = strpad('Tsmax in component Parameters (AU)'); LEGEND_CONSTANTS(:,14) = strpad('Tpmax in component Parameters (AU)'); LEGEND_CONSTANTS(:,15) = strpad('Tsmin in component Parameters (AU)'); LEGEND_CONSTANTS(:,16) = strpad('Tpmin in component Parameters (AU)'); LEGEND_CONSTANTS(:,17) = strpad('Gcns in component Parameters (dimensionless)'); LEGEND_CONSTANTS(:,18) = strpad('Gs in component Parameters (per_Hertz)'); LEGEND_CONSTANTS(:,19) = strpad('Gp in component Parameters (per_Hertz)'); LEGEND_CONSTANTS(:,20) = strpad('tau_nor in component Parameters (second)'); LEGEND_CONSTANTS(:,21) = strpad('tau_ach in component Parameters (second)'); LEGEND_CONSTANTS(:,22) = strpad('tau_HR_nor in component Parameters (second)'); LEGEND_CONSTANTS(:,23) = strpad('tau_HR_ach in component Parameters (second)'); LEGEND_CONSTANTS(:,24) = strpad('HRo in component Parameters (Beats_per_min)'); LEGEND_CONSTANTS(:,25) = strpad('HRmax in component Parameters (Beats_per_min)'); LEGEND_CONSTANTS(:,26) = strpad('HRmin in component Parameters (Beats_per_min)'); LEGEND_CONSTANTS(:,27) = strpad('Beta in component Parameters (dimensionless)'); LEGEND_CONSTANTS(:,28) = strpad('delta_th in component Parameters (dimensionless)'); LEGEND_CONSTANTS(:,29) = strpad('q_nor in component Parameters (per_s)'); LEGEND_CONSTANTS(:,30) = strpad('q_ach in component Parameters (per_s)'); LEGEND_CONSTANTS(:,31) = strpad('K_nor in component Parameters (AU)'); LEGEND_CONSTANTS(:,32) = strpad('K_ach in component Parameters (AU)'); LEGEND_CONSTANTS(:,33) = strpad('Gamma in component Parameters (dimensionless)'); LEGEND_ALGEBRAIC(:,11) = strpad('alpha_cns in component Nervous_System (hertz)'); LEGEND_ALGEBRAIC(:,9) = strpad('n in component Nervous_System (hertz)'); LEGEND_ALGEBRAIC(:,7) = strpad('Delta in component Coupling_Dynamics (dimensionless)'); LEGEND_CONSTANTS(:,34) = strpad('alpha_s0 in component Nervous_System (hertz)'); LEGEND_CONSTANTS(:,35) = strpad('alpha_p0 in component Nervous_System (hertz)'); LEGEND_STATES(:,1) = strpad('A in component Aortic_Wall (mm_sq)'); LEGEND_ALGEBRAIC(:,1) = strpad('P in component Aortic_Wall (mmHg)'); LEGEND_ALGEBRAIC(:,2) = strpad('R in component Aortic_Wall (mm)'); LEGEND_STATES(:,2) = strpad('Eps_1 in component Coupling_Dynamics (dimensionless)'); LEGEND_STATES(:,3) = strpad('Eps_2 in component Coupling_Dynamics (dimensionless)'); LEGEND_STATES(:,4) = strpad('Eps_3 in component Coupling_Dynamics (dimensionless)'); LEGEND_ALGEBRAIC(:,5) = strpad('Eps_wall in component Coupling_Dynamics (dimensionless)'); LEGEND_ALGEBRAIC(:,13) = strpad('Ts in component PNS_tones (AU)'); LEGEND_ALGEBRAIC(:,14) = strpad('Tp in component PNS_tones (AU)'); LEGEND_STATES(:,5) = strpad('c_nor in component Norepinephrine (AU)'); LEGEND_STATES(:,6) = strpad('C_ach in component Acetylcholine (AU)'); LEGEND_ALGEBRAIC(:,3) = strpad('delta_HR_ss in component Heart_Response_Nor (Beats_per_min)'); LEGEND_CONSTANTS(:,36) = strpad('delta_HR_smax in component Heart_Response_Nor (Beats_per_min)'); LEGEND_STATES(:,7) = strpad('delta_HR_s in component Heart_Response_Nor (Beats_per_min)'); LEGEND_ALGEBRAIC(:,4) = strpad('delta_HR_ps in component HR_ach (Beats_per_min)'); LEGEND_CONSTANTS(:,37) = strpad('delta_HR_pmax in component HR_ach (Beats_per_min)'); LEGEND_ALGEBRAIC(:,6) = strpad('delta_HR_pfast in component HR_ach (Beats_per_min)'); LEGEND_STATES(:,8) = strpad('delta_HR_pslow in component HR_ach (Beats_per_min)'); LEGEND_ALGEBRAIC(:,8) = strpad('delta_HR_p in component HR_ach (Beats_per_min)'); LEGEND_ALGEBRAIC(:,15) = strpad('HR in component HR_Combined (Beats_per_min)'); LEGEND_ALGEBRAIC(:,12) = strpad('HR_p in component HR_Combined (Beats_per_min)'); LEGEND_ALGEBRAIC(:,10) = strpad('HR_s in component HR_Combined (Beats_per_min)'); LEGEND_ALGEBRAIC(:,16) = strpad('Period in component HR_Combined (Sec_per_Beat)'); LEGEND_RATES(:,1) = strpad('d/dt A in component Aortic_Wall (mm_sq)'); LEGEND_RATES(:,2) = strpad('d/dt Eps_1 in component Coupling_Dynamics (dimensionless)'); LEGEND_RATES(:,3) = strpad('d/dt Eps_2 in component Coupling_Dynamics (dimensionless)'); LEGEND_RATES(:,4) = strpad('d/dt Eps_3 in component Coupling_Dynamics (dimensionless)'); LEGEND_RATES(:,5) = strpad('d/dt c_nor in component Norepinephrine (AU)'); LEGEND_RATES(:,6) = strpad('d/dt C_ach in component Acetylcholine (AU)'); LEGEND_RATES(:,7) = strpad('d/dt delta_HR_s in component Heart_Response_Nor (Beats_per_min)'); LEGEND_RATES(:,8) = strpad('d/dt delta_HR_pslow in component HR_ach (Beats_per_min)'); 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) = 480; CONSTANTS(:,2) = 1; CONSTANTS(:,3) = 1.6; CONSTANTS(:,4) = 0.006; CONSTANTS(:,5) = 1; CONSTANTS(:,6) = 1.5; CONSTANTS(:,7) = 3.75; CONSTANTS(:,8) = 1.05; CONSTANTS(:,9) = 1; CONSTANTS(:,10) = 1; CONSTANTS(:,11) = 10; CONSTANTS(:,12) = 206.973; CONSTANTS(:,13) = 4.12; CONSTANTS(:,14) = 4.994; CONSTANTS(:,15) = 0.5; CONSTANTS(:,16) = 1.6; CONSTANTS(:,17) = 1; CONSTANTS(:,18) = 0.178; CONSTANTS(:,19) = 0.492; CONSTANTS(:,20) = 9.1; CONSTANTS(:,21) = 0.2; CONSTANTS(:,22) = 2.1; CONSTANTS(:,23) = 2.5; CONSTANTS(:,24) = 282.648; CONSTANTS(:,25) = 483.218; CONSTANTS(:,26) = 226.238; CONSTANTS(:,27) = 0.175; CONSTANTS(:,28) = 0; CONSTANTS(:,29) = 0.1099; CONSTANTS(:,30) = 5; CONSTANTS(:,31) = 1.12; CONSTANTS(:,32) = 0.65; CONSTANTS(:,33) = 0.75; CONSTANTS(:,34) = 58.6; CONSTANTS(:,35) = 76.019; STATES(:,1) = 15.20531; STATES(:,2) = 0.2042; STATES(:,3) = 0.183; STATES(:,4) = 0.161; STATES(:,5) = 1.441; STATES(:,6) = 1.0; STATES(:,7) = 0; STATES(:,8) = 0; CONSTANTS(:,36) = CONSTANTS(:,25) - CONSTANTS(:,24); CONSTANTS(:,37) = CONSTANTS(:,24) - CONSTANTS(:,26); 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) = 100.000+ 10.0000.* sin( 5.00000.*VOI); RATES(:,1) = ( - (power((STATES(:,1)./ pi), 1.0 ./ 2) - CONSTANTS(:,3))./CONSTANTS(:,4)+ALGEBRAIC(:,1))./CONSTANTS(:,9); ALGEBRAIC(:,3) = ( CONSTANTS(:,36).*power(STATES(:,5), 2.00000))./(power(CONSTANTS(:,31), 2.00000)+power(STATES(:,5), 2.00000)); RATES(:,7) = ( - STATES(:,7)+ALGEBRAIC(:,3))./CONSTANTS(:,22); ALGEBRAIC(:,4) = ( CONSTANTS(:,37).*power(STATES(:,6), 2.00000))./(power(CONSTANTS(:,32), 2.00000)+power(STATES(:,6), 2.00000)); RATES(:,8) = ( - STATES(:,8)+ (1.00000 - CONSTANTS(:,33)).*ALGEBRAIC(:,4))./CONSTANTS(:,23); ALGEBRAIC(:,2) = power(STATES(:,1)./ pi, 0.500000); ALGEBRAIC(:,5) = (ALGEBRAIC(:,2) - CONSTANTS(:,3))./CONSTANTS(:,3); [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC); ALGEBRAIC(:,7) = ALGEBRAIC(:,5) - STATES(:,2); ALGEBRAIC(:,9) = CONSTANTS(:,1).*(ALGEBRAIC(:,7) - CONSTANTS(:,2).*CONSTANTS(:,28)); ALGEBRAIC(:,11) = CONSTANTS(:,17).*ALGEBRAIC(:,9); ALGEBRAIC(:,13) = CONSTANTS(:,15)+(CONSTANTS(:,13) - CONSTANTS(:,15))./(exp( CONSTANTS(:,18).*(ALGEBRAIC(:,11) - CONSTANTS(:,34)))+1.00000); RATES(:,5) = - (STATES(:,5)./CONSTANTS(:,20))+ CONSTANTS(:,29).*ALGEBRAIC(:,13); ALGEBRAIC(:,14) = CONSTANTS(:,16)+(CONSTANTS(:,14) - CONSTANTS(:,16))./(exp( - CONSTANTS(:,19).*(ALGEBRAIC(:,11) - CONSTANTS(:,35)))+1.00000); RATES(:,6) = - (STATES(:,6)./CONSTANTS(:,21))+ CONSTANTS(:,30).*ALGEBRAIC(:,14); 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) = 100.000+ 10.0000.* sin( 5.00000.*VOI); ALGEBRAIC(:,3) = ( CONSTANTS(:,36).*power(STATES(:,5), 2.00000))./(power(CONSTANTS(:,31), 2.00000)+power(STATES(:,5), 2.00000)); ALGEBRAIC(:,4) = ( CONSTANTS(:,37).*power(STATES(:,6), 2.00000))./(power(CONSTANTS(:,32), 2.00000)+power(STATES(:,6), 2.00000)); ALGEBRAIC(:,2) = power(STATES(:,1)./ pi, 0.500000); ALGEBRAIC(:,5) = (ALGEBRAIC(:,2) - CONSTANTS(:,3))./CONSTANTS(:,3); ALGEBRAIC(:,7) = ALGEBRAIC(:,5) - STATES(:,2); ALGEBRAIC(:,9) = CONSTANTS(:,1).*(ALGEBRAIC(:,7) - CONSTANTS(:,2).*CONSTANTS(:,28)); ALGEBRAIC(:,11) = CONSTANTS(:,17).*ALGEBRAIC(:,9); ALGEBRAIC(:,13) = CONSTANTS(:,15)+(CONSTANTS(:,13) - CONSTANTS(:,15))./(exp( CONSTANTS(:,18).*(ALGEBRAIC(:,11) - CONSTANTS(:,34)))+1.00000); ALGEBRAIC(:,14) = CONSTANTS(:,16)+(CONSTANTS(:,14) - CONSTANTS(:,16))./(exp( - CONSTANTS(:,19).*(ALGEBRAIC(:,11) - CONSTANTS(:,35)))+1.00000); ALGEBRAIC(:,6) = CONSTANTS(:,33).*ALGEBRAIC(:,4); ALGEBRAIC(:,8) = ALGEBRAIC(:,6)+STATES(:,8); ALGEBRAIC(:,10) = CONSTANTS(:,24)+STATES(:,7); ALGEBRAIC(:,12) = CONSTANTS(:,24) - ALGEBRAIC(:,8); ALGEBRAIC(:,15) = ALGEBRAIC(:,12)+( (ALGEBRAIC(:,10) - CONSTANTS(:,24)).*(ALGEBRAIC(:,12) - CONSTANTS(:,27).*CONSTANTS(:,26)))./(CONSTANTS(:,24) - CONSTANTS(:,27).*CONSTANTS(:,26)); ALGEBRAIC(:,16) = 60.0000./ALGEBRAIC(:,15); end % Functions required for solving differential algebraic equation function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN) ALGEBRAIC = ALGEBRAIC_IN; CONSTANTS = CONSTANTS_IN; STATES = STATES_IN; global initialGuess_0; if (length(initialGuess_0) ~= 3), initialGuess_0 = [0.1,0.1,0.1];, end options = optimset('Display', 'off', 'TolX', 1E-6); if length(VOI) == 1 residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES); soln = fsolve(residualfn, initialGuess_0, options); initialGuess_0 = soln; RATES(:,2) = soln(1); RATES(:,3) = soln(2); RATES(:,4) = soln(3); else SET_RATES(:,2) = logical(1); SET_RATES(:,3) = logical(1); SET_RATES(:,4) = logical(1); for i=1:length(VOI) residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:)); soln = fsolve(residualfn, initialGuess_0, options); initialGuess_0 = soln; TEMP_RATES(:,2) = soln(1); TEMP_RATES(:,3) = soln(2); TEMP_RATES(:,4) = soln(3); ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC); end end end function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES) RATES(:,2) = algebraicCandidate(1); RATES(:,3) = algebraicCandidate(2); RATES(:,4) = algebraicCandidate(3); resid(1) = RATES(:,2) - (( CONSTANTS(:,5).*(ALGEBRAIC(:,5) - STATES(:,2)) - CONSTANTS(:,6).*(STATES(:,2) - STATES(:,3)))./CONSTANTS(:,10)+RATES(:,3)); resid(2) = RATES(:,3) - (( CONSTANTS(:,6).*(STATES(:,2) - STATES(:,3)) - CONSTANTS(:,7).*(STATES(:,3) - STATES(:,4)))+ CONSTANTS(:,10).*RATES(:,2)+ CONSTANTS(:,11).*RATES(:,4))./(CONSTANTS(:,10)+CONSTANTS(:,11)); resid(3) = RATES(:,4) - (( CONSTANTS(:,7).*(STATES(:,3) - STATES(:,4)) - CONSTANTS(:,8).*STATES(:,4))+ CONSTANTS(:,11).*RATES(:,3))./(CONSTANTS(:,11)+CONSTANTS(:,12)); 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