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 =2; end % There are a total of 9 entries in each of the rate and state variable arrays. % There are a total of 38 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_CONSTANTS(:,1) = strpad('BW in component main (kilogram)'); LEGEND_CONSTANTS(:,2) = strpad('Qcc in component main (litre_per_hr_kg)'); LEGEND_CONSTANTS(:,3) = strpad('Qliv in component main (dimensionless)'); LEGEND_CONSTANTS(:,4) = strpad('Qfat in component main (dimensionless)'); LEGEND_CONSTANTS(:,5) = strpad('Qbrain in component main (dimensionless)'); LEGEND_CONSTANTS(:,6) = strpad('Qslow in component main (dimensionless)'); LEGEND_CONSTANTS(:,21) = strpad('Qc in component main (litre_per_hr)'); LEGEND_CONSTANTS(:,29) = strpad('Ql in component main (litre_per_hr)'); LEGEND_CONSTANTS(:,34) = strpad('Qf in component main (litre_per_hr)'); LEGEND_CONSTANTS(:,36) = strpad('Qbr in component main (litre_per_hr)'); LEGEND_CONSTANTS(:,37) = strpad('Qs in component main (litre_per_hr)'); LEGEND_CONSTANTS(:,38) = strpad('Qr in component main (litre_per_hr)'); LEGEND_CONSTANTS(:,7) = strpad('Vblood in component main (dimensionless)'); LEGEND_CONSTANTS(:,8) = strpad('Vbrain in component main (dimensionless)'); LEGEND_CONSTANTS(:,9) = strpad('Vliver in component main (dimensionless)'); LEGEND_CONSTANTS(:,22) = strpad('Vfat in component main (dimensionless)'); LEGEND_CONSTANTS(:,10) = strpad('Vslow in component main (dimensionless)'); LEGEND_CONSTANTS(:,30) = strpad('Vrap in component main (dimensionless)'); LEGEND_CONSTANTS(:,31) = strpad('Vf in component main (litre)'); LEGEND_CONSTANTS(:,23) = strpad('Vs in component main (litre)'); LEGEND_CONSTANTS(:,24) = strpad('Vl in component main (litre)'); LEGEND_CONSTANTS(:,35) = strpad('Vr in component main (litre)'); LEGEND_CONSTANTS(:,25) = strpad('Vbr in component main (litre)'); LEGEND_CONSTANTS(:,26) = strpad('Vb in component main (litre)'); LEGEND_CONSTANTS(:,32) = strpad('Vven in component main (litre)'); LEGEND_CONSTANTS(:,33) = strpad('Vart in component main (litre)'); LEGEND_CONSTANTS(:,11) = strpad('Pfat_bl in component main (dimensionless)'); LEGEND_CONSTANTS(:,12) = strpad('Pslow_bl in component main (dimensionless)'); LEGEND_CONSTANTS(:,13) = strpad('Pliv_bl in component main (dimensionless)'); LEGEND_CONSTANTS(:,14) = strpad('Prapid_bl in component main (dimensionless)'); LEGEND_CONSTANTS(:,15) = strpad('Pbrain_bl in component main (dimensionless)'); LEGEND_CONSTANTS(:,16) = strpad('dose in component main (mg_per_kg)'); LEGEND_CONSTANTS(:,17) = strpad('F in component main (dimensionless)'); LEGEND_CONSTANTS(:,27) = strpad('D0 in component main (mg)'); LEGEND_CONSTANTS(:,18) = strpad('Ka in component main (per_hr)'); LEGEND_CONSTANTS(:,19) = strpad('Kfc in component main (per_hr)'); LEGEND_CONSTANTS(:,28) = strpad('KF in component main (per_hr_kg)'); LEGEND_VOI = strpad('t in component main (hr)'); LEGEND_STATES(:,1) = strpad('A_fat in component main (mg)'); LEGEND_STATES(:,2) = strpad('A_slow in component main (mg)'); LEGEND_STATES(:,3) = strpad('A_liv in component main (mg)'); LEGEND_STATES(:,4) = strpad('A_rapid in component main (mg)'); LEGEND_STATES(:,5) = strpad('A_brain in component main (mg)'); LEGEND_CONSTANTS(:,20) = strpad('A_blood in component main (mg)'); LEGEND_STATES(:,6) = strpad('A_stomach in component main (mg)'); LEGEND_STATES(:,7) = strpad('A_ven in component main (mg)'); LEGEND_STATES(:,8) = strpad('A_art in component main (mg)'); LEGEND_STATES(:,9) = strpad('A_liv_CL in component main (mg)'); LEGEND_ALGEBRAIC(:,1) = strpad('C_art in component main (mg_per_litre)'); LEGEND_ALGEBRAIC(:,2) = strpad('C_brain in component main (mg_per_litre)'); LEGEND_RATES(:,7) = strpad('d/dt A_ven in component main (mg)'); LEGEND_RATES(:,8) = strpad('d/dt A_art in component main (mg)'); LEGEND_RATES(:,1) = strpad('d/dt A_fat in component main (mg)'); LEGEND_RATES(:,2) = strpad('d/dt A_slow in component main (mg)'); LEGEND_RATES(:,5) = strpad('d/dt A_brain in component main (mg)'); LEGEND_RATES(:,4) = strpad('d/dt A_rapid in component main (mg)'); LEGEND_RATES(:,6) = strpad('d/dt A_stomach in component main (mg)'); LEGEND_RATES(:,3) = strpad('d/dt A_liv in component main (mg)'); LEGEND_RATES(:,9) = strpad('d/dt A_liv_CL in component main (mg)'); 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) = 0.1615; CONSTANTS(:,2) = 14; CONSTANTS(:,3) = 0.25; CONSTANTS(:,4) = 0.09; CONSTANTS(:,5) = 0.03; CONSTANTS(:,6) = 0.278; CONSTANTS(:,7) = 0.05; CONSTANTS(:,8) = 0.0116; CONSTANTS(:,9) = 0.04; CONSTANTS(:,10) = 0.63; CONSTANTS(:,11) = 186; CONSTANTS(:,12) = 3.4; CONSTANTS(:,13) = 6.1; CONSTANTS(:,14) = 6.1; CONSTANTS(:,15) = 3; CONSTANTS(:,16) = 60; CONSTANTS(:,17) = 0.8; CONSTANTS(:,18) = 0.1; CONSTANTS(:,19) = 6; STATES(:,1) = 0; STATES(:,2) = 0; STATES(:,3) = 0; STATES(:,4) = 0; STATES(:,5) = 0; CONSTANTS(:,20) = 0; STATES(:,7) = 0; STATES(:,8) = 0; STATES(:,9) = 0; CONSTANTS(:,21) = CONSTANTS(:,2).*power(CONSTANTS(:,1), 0.740000); CONSTANTS(:,22) = 0.0100000.*( 20.0000.*CONSTANTS(:,1)+1.66400); CONSTANTS(:,23) = CONSTANTS(:,10).*CONSTANTS(:,1); CONSTANTS(:,24) = CONSTANTS(:,9).*CONSTANTS(:,1); CONSTANTS(:,25) = CONSTANTS(:,8).*CONSTANTS(:,1); CONSTANTS(:,26) = CONSTANTS(:,7).*CONSTANTS(:,1); CONSTANTS(:,27) = CONSTANTS(:,17).*CONSTANTS(:,16).*CONSTANTS(:,1); CONSTANTS(:,28) = CONSTANTS(:,19)./power(CONSTANTS(:,1), 0.300000); CONSTANTS(:,29) = CONSTANTS(:,3).*CONSTANTS(:,21); CONSTANTS(:,30) = (((1.00000 - CONSTANTS(:,7)) - CONSTANTS(:,9)) - CONSTANTS(:,22)) - CONSTANTS(:,10); CONSTANTS(:,31) = CONSTANTS(:,22).*CONSTANTS(:,1); CONSTANTS(:,32) = 0.750000.*CONSTANTS(:,26); CONSTANTS(:,33) = 0.250000.*CONSTANTS(:,26); CONSTANTS(:,34) = CONSTANTS(:,4).*CONSTANTS(:,21); CONSTANTS(:,35) = CONSTANTS(:,30).*CONSTANTS(:,1); CONSTANTS(:,36) = CONSTANTS(:,5).*CONSTANTS(:,21); CONSTANTS(:,37) = CONSTANTS(:,6).*CONSTANTS(:,21); CONSTANTS(:,38) = (((CONSTANTS(:,21) - CONSTANTS(:,29)) - CONSTANTS(:,34)) - CONSTANTS(:,36)) - CONSTANTS(:,37); STATES(:,6) = CONSTANTS(:,27); 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(:,7) = (( CONSTANTS(:,34).*STATES(:,1))./CONSTANTS(:,31))./CONSTANTS(:,11)+(( CONSTANTS(:,36).*STATES(:,5))./CONSTANTS(:,25))./CONSTANTS(:,15)+(( CONSTANTS(:,37).*STATES(:,2))./CONSTANTS(:,23))./CONSTANTS(:,12)+(( CONSTANTS(:,29).*STATES(:,3))./CONSTANTS(:,24))./CONSTANTS(:,13)+(( CONSTANTS(:,38).*STATES(:,4))./CONSTANTS(:,35))./CONSTANTS(:,14); RATES(:,1) = CONSTANTS(:,34).*(STATES(:,8)./CONSTANTS(:,26) - (STATES(:,1)./CONSTANTS(:,31))./CONSTANTS(:,11)); RATES(:,2) = CONSTANTS(:,37).*(STATES(:,8)./CONSTANTS(:,26) - (STATES(:,2)./CONSTANTS(:,23))./CONSTANTS(:,12)); RATES(:,5) = CONSTANTS(:,36).*(STATES(:,8)./CONSTANTS(:,26) - (STATES(:,5)./CONSTANTS(:,25))./CONSTANTS(:,15)); RATES(:,4) = CONSTANTS(:,38).*(STATES(:,8)./CONSTANTS(:,26) - (STATES(:,4)./CONSTANTS(:,35))./CONSTANTS(:,14)); RATES(:,6) = - CONSTANTS(:,18).*STATES(:,6); RATES(:,3) = ( CONSTANTS(:,29).*(STATES(:,8)./CONSTANTS(:,26) - (STATES(:,3)./CONSTANTS(:,24))./CONSTANTS(:,13)) - (( CONSTANTS(:,28).*CONSTANTS(:,24).*STATES(:,3))./CONSTANTS(:,24))./CONSTANTS(:,13))+ CONSTANTS(:,18).*STATES(:,6); RATES(:,9) = (( CONSTANTS(:,28).*CONSTANTS(:,24).*STATES(:,3))./CONSTANTS(:,24))./CONSTANTS(:,13); RATES(:,8) = RATES(:,7) - ( CONSTANTS(:,21).*STATES(:,8))./CONSTANTS(:,26); 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) = STATES(:,8)./CONSTANTS(:,26); ALGEBRAIC(:,2) = STATES(:,5)./CONSTANTS(:,25); 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