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 =7; end % There are a total of 1 entries in each of the rate and state variable arrays. % There are a total of 19 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(:,6) = strpad('v_cc in component contraction (mm_per_second)'); LEGEND_ALGEBRAIC(:,7) = strpad('f_v in component contraction (dimensionless)'); LEGEND_ALGEBRAIC(:,2) = strpad('f_L in component contraction (newton)'); LEGEND_ALGEBRAIC(:,5) = strpad('f_sec in component contraction (newton)'); LEGEND_ALGEBRAIC(:,1) = strpad('f_pec in component contraction (newton)'); LEGEND_ALGEBRAIC(:,4) = strpad('delta_L_sec in component contraction (mm)'); LEGEND_CONSTANTS(:,16) = strpad('delta_L_sec1 in component contraction (mm)'); LEGEND_STATES(:,1) = strpad('delta_L_pec in component contraction (mm)'); LEGEND_CONSTANTS(:,17) = strpad('k in component contraction (newton_per_mm)'); LEGEND_ALGEBRAIC(:,3) = strpad('L_mtc in component contraction (mm)'); LEGEND_CONSTANTS(:,18) = strpad('A in component contraction (dimensionless)'); LEGEND_CONSTANTS(:,19) = strpad('L_mslack in component contraction (mm)'); LEGEND_RATES(:,1) = strpad('d/dt delta_L_pec 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) = -23; CONSTANTS(:,2) = -14; CONSTANTS(:,3) = 2; CONSTANTS(:,4) = 19; CONSTANTS(:,5) = 0.49; CONSTANTS(:,6) = -141; CONSTANTS(:,7) = 5.8; CONSTANTS(:,8) = 0.012; CONSTANTS(:,9) = 0.317; CONSTANTS(:,10) = 4.1; CONSTANTS(:,11) = 4.1; CONSTANTS(:,12) = 3.3; CONSTANTS(:,13) = 0.3; CONSTANTS(:,14) = 18.1; CONSTANTS(:,15) = 0.006; CONSTANTS(:,16) = 4.1; STATES(:,1) = 0.2; CONSTANTS(:,17) = 3.5; CONSTANTS(:,18) = 1; CONSTANTS(:,19) = 0.3; 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(:,2) = 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(:,3) = piecewise({VOI<=1.00000, 0.290000 , VOI>1.00000&VOI<5.00000, 0.220000 }, 0.190000); ALGEBRAIC(:,4) = (ALGEBRAIC(:,3) - STATES(:,1)) - CONSTANTS(:,19); ALGEBRAIC(:,5) = piecewise({ALGEBRAIC(:,4)>0.00000&ALGEBRAIC(:,4)0.00000, CONSTANTS(:,8).*(exp( CONSTANTS(:,9).*STATES(:,1)) - 1.00000) , STATES(:,1)<=0.00000, 0.00000 }, NaN); [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC); RATES(:,1) = ALGEBRAIC(:,6); 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(:,2) = 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(:,3) = piecewise({VOI<=1.00000, 0.290000 , VOI>1.00000&VOI<5.00000, 0.220000 }, 0.190000); ALGEBRAIC(:,4) = (ALGEBRAIC(:,3) - STATES(:,1)) - CONSTANTS(:,19); ALGEBRAIC(:,5) = piecewise({ALGEBRAIC(:,4)>0.00000&ALGEBRAIC(:,4)0.00000, CONSTANTS(:,8).*(exp( CONSTANTS(:,9).*STATES(:,1)) - 1.00000) , STATES(:,1)<=0.00000, 0.00000 }, NaN); 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) ~= 2), initialGuess_0 = [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; ALGEBRAIC(:,6) = soln(1); ALGEBRAIC(:,7) = soln(2); else SET_ALGEBRAIC(:,6) = logical(1); SET_ALGEBRAIC(:,7) = 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_ALGEBRAIC(:,6) = soln(1); TEMP_ALGEBRAIC(:,7) = soln(2); ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC); end end end function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES) ALGEBRAIC(:,6) = algebraicCandidate(1); ALGEBRAIC(:,7) = algebraicCandidate(2); resid(1) = ALGEBRAIC(:,6) - (1.00000./ALGEBRAIC(:,7)).*((ALGEBRAIC(:,5) - ALGEBRAIC(:,1))./( CONSTANTS(:,18).*ALGEBRAIC(:,2).*CONSTANTS(:,14))); resid(2) = ALGEBRAIC(:,7) - (CONSTANTS(:,6) - ALGEBRAIC(:,6))./(CONSTANTS(:,6)+ ALGEBRAIC(:,6).*CONSTANTS(:,7)); end % Compute result of a piecewise function function x = piecewise(cases, default) set = [0]; for i = 1:2:length(cases) if (length(cases{i+1}) == 1) x(cases{i} & ~set,:) = cases{i+1}; else x(cases{i} & ~set,:) = cases{i+1}(cases{i} & ~set); end set = set | cases{i}; if(set), break, end end if (length(default) == 1) x(~set,:) = default; else x(~set,:) = default(~set); end 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