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 =6; end % There are a total of 1 entries in each of the rate and state variable arrays. % There are a total of 18 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_CONSTANTS(:,1) = strpad('L_1 in component contraction (mm)'); LEGEND_CONSTANTS(:,2) = strpad('L_2 in component contraction (mm)'); LEGEND_CONSTANTS(:,3) = strpad('L_3 in component contraction (mm)'); LEGEND_CONSTANTS(:,4) = strpad('L_4 in component contraction (mm)'); LEGEND_CONSTANTS(:,5) = strpad('f_c in component contraction (newton)'); LEGEND_CONSTANTS(:,6) = strpad('v_max in component contraction (mm_per_second)'); LEGEND_CONSTANTS(:,7) = strpad('curv in component contraction (dimensionless)'); LEGEND_CONSTANTS(:,8) = strpad('k_1 in component contraction (newton)'); LEGEND_CONSTANTS(:,9) = strpad('k_2 in component contraction (per_mm)'); LEGEND_CONSTANTS(:,10) = strpad('F_1 in component contraction (newton)'); LEGEND_CONSTANTS(:,11) = strpad('d_LSEC1 in component contraction (mm)'); LEGEND_CONSTANTS(:,12) = strpad('k_sh in component contraction (dimensionless)'); LEGEND_CONSTANTS(:,13) = strpad('L_m in component contraction (mm)'); LEGEND_CONSTANTS(:,14) = strpad('F_im in component contraction (newton)'); LEGEND_CONSTANTS(:,15) = strpad('tau in component contraction (second)'); LEGEND_ALGEBRAIC(:,5) = strpad('v_cc in component contraction (mm_per_second)'); LEGEND_ALGEBRAIC(:,6) = strpad('f_v in component contraction (dimensionless)'); LEGEND_ALGEBRAIC(:,1) = strpad('f_L in component contraction (newton)'); LEGEND_ALGEBRAIC(:,4) = strpad('f_sec in component contraction (newton)'); LEGEND_ALGEBRAIC(:,3) = strpad('delta_L_sec in component contraction (mm)'); LEGEND_CONSTANTS(:,16) = strpad('delta_L_sec1 in component contraction (mm)'); LEGEND_CONSTANTS(:,17) = strpad('k in component contraction (newton_per_mm)'); LEGEND_STATES(:,1) = strpad('L_cc in component contraction (mm)'); LEGEND_ALGEBRAIC(:,2) = strpad('L_mtc in component contraction (mm)'); LEGEND_CONSTANTS(:,18) = strpad('A in component contraction (dimensionless)'); LEGEND_RATES(:,1) = strpad('d/dt L_cc in component contraction (mm)'); 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) = -24; CONSTANTS(:,2) = -14; CONSTANTS(:,3) = 4; CONSTANTS(:,4) = 14; CONSTANTS(:,5) = 0.51; CONSTANTS(:,6) = -152; CONSTANTS(:,7) = 9.5; CONSTANTS(:,8) = 0.0064; CONSTANTS(:,9) = 0.414; CONSTANTS(:,10) = 8.9; CONSTANTS(:,11) = 2.9; CONSTANTS(:,12) = 2.5; CONSTANTS(:,13) = 2.6; CONSTANTS(:,14) = 20.6; CONSTANTS(:,15) = 0.065; CONSTANTS(:,16) = 4.1; CONSTANTS(:,17) = 8.2; STATES(:,1) = 0.2; CONSTANTS(:,18) = 1; 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) = piecewise({STATES(:,1)>=CONSTANTS(:,1)&STATES(:,1)<=CONSTANTS(:,2), (CONSTANTS(:,5)./(CONSTANTS(:,2) - CONSTANTS(:,1))).*(STATES(:,1) - CONSTANTS(:,1)) , STATES(:,1)>CONSTANTS(:,2)&STATES(:,1)<=0.00000, ((1.00000 - CONSTANTS(:,5))./ - CONSTANTS(:,2)).*(STATES(:,1) - CONSTANTS(:,2)) , STATES(:,1)>0.00000&STATES(:,1)<=CONSTANTS(:,3), 1.00000 , STATES(:,1)>CONSTANTS(:,3)&STATES(:,1)<=CONSTANTS(:,4), ( - 1.00000./(CONSTANTS(:,4) - CONSTANTS(:,3))).*(STATES(:,1) - CONSTANTS(:,3)) }, NaN); ALGEBRAIC(:,2) = piecewise({VOI<=1.00000, 0.290000 , VOI>1.00000&VOI<5.00000, 0.220000 }, 0.190000); ALGEBRAIC(:,3) = (ALGEBRAIC(:,2) - STATES(:,1)) - CONSTANTS(:,13); ALGEBRAIC(:,4) = piecewise({ALGEBRAIC(:,3)>0.00000&ALGEBRAIC(:,3)=CONSTANTS(:,1)&STATES(:,1)<=CONSTANTS(:,2), (CONSTANTS(:,5)./(CONSTANTS(:,2) - CONSTANTS(:,1))).*(STATES(:,1) - CONSTANTS(:,1)) , STATES(:,1)>CONSTANTS(:,2)&STATES(:,1)<=0.00000, ((1.00000 - CONSTANTS(:,5))./ - CONSTANTS(:,2)).*(STATES(:,1) - CONSTANTS(:,2)) , STATES(:,1)>0.00000&STATES(:,1)<=CONSTANTS(:,3), 1.00000 , STATES(:,1)>CONSTANTS(:,3)&STATES(:,1)<=CONSTANTS(:,4), ( - 1.00000./(CONSTANTS(:,4) - CONSTANTS(:,3))).*(STATES(:,1) - CONSTANTS(:,3)) }, NaN); ALGEBRAIC(:,2) = piecewise({VOI<=1.00000, 0.290000 , VOI>1.00000&VOI<5.00000, 0.220000 }, 0.190000); ALGEBRAIC(:,3) = (ALGEBRAIC(:,2) - STATES(:,1)) - CONSTANTS(:,13); ALGEBRAIC(:,4) = piecewise({ALGEBRAIC(:,3)>0.00000&ALGEBRAIC(:,3) req_length strout = strin(1:req_length); else strout = [strin, blanks(req_length - insize)]; end end