# 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 =9;
end
% There are a total of 1 entries in each of the rate and state variable arrays.
% There are a total of 29 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_ALGEBRAIC(:,9) = strpad('F_CE in component F_CE (newton)');
LEGEND_ALGEBRAIC(:,1) = strpad('f_L_CE in component f_L_CE (newton)');
LEGEND_ALGEBRAIC(:,6) = strpad('g_V_CE in component g_V_CE (dimensionless)');
LEGEND_CONSTANTS(:,1) = strpad('a in component user_defined_constants (dimensionless)');
LEGEND_CONSTANTS(:,2) = strpad('F_min in component f_L_CE (newton)');
LEGEND_CONSTANTS(:,3) = strpad('F_max in component user_defined_constants (newton)');
LEGEND_STATES(:,1) = strpad('L_CE in component L_CE (metre)');
LEGEND_CONSTANTS(:,4) = strpad('L_CE_opt in component user_defined_constants (metre)');
LEGEND_CONSTANTS(:,5) = strpad('W in component f_L_CE (dimensionless)');
LEGEND_CONSTANTS(:,29) = strpad('lambda_a in component lambda_a (second_per_metre)');
LEGEND_CONSTANTS(:,6) = strpad('V_max in component g_V_CE (metre_per_second)');
LEGEND_ALGEBRAIC(:,7) = strpad('V_CE in component V_CE (dimensionless)');
LEGEND_CONSTANTS(:,7) = strpad('A in component g_V_CE (dimensionless)');
LEGEND_CONSTANTS(:,8) = strpad('g_max in component g_V_CE (dimensionless)');
LEGEND_CONSTANTS(:,25) = strpad('d1 in component d1 (dimensionless)');
LEGEND_CONSTANTS(:,27) = strpad('d2 in component d2 (dimensionless)');
LEGEND_CONSTANTS(:,28) = strpad('d3 in component d3 (dimensionless)');
LEGEND_CONSTANTS(:,9) = strpad('gamma in component g_V_CE (dimensionless)');
LEGEND_CONSTANTS(:,10) = strpad('V_max in component d1 (metre_per_second)');
LEGEND_CONSTANTS(:,11) = strpad('A in component d1 (dimensionless)');
LEGEND_CONSTANTS(:,12) = strpad('g_max in component d1 (dimensionless)');
LEGEND_CONSTANTS(:,13) = strpad('S in component d1 (metre_per_second)');
LEGEND_CONSTANTS(:,14) = strpad('S in component d2 (metre_per_second)');
LEGEND_CONSTANTS(:,15) = strpad('A in component d2 (dimensionless)');
LEGEND_CONSTANTS(:,16) = strpad('V_max in component d2 (metre_per_second)');
LEGEND_CONSTANTS(:,17) = strpad('gamma in component d2 (dimensionless)');
LEGEND_CONSTANTS(:,18) = strpad('g_max in component d3 (dimensionless)');
LEGEND_CONSTANTS(:,19) = strpad('gamma in component d3 (dimensionless)');
LEGEND_ALGEBRAIC(:,5) = strpad('F_SEE in component F_SEE (newton)');
LEGEND_CONSTANTS(:,20) = strpad('k_SEE in component F_SEE (newton_per_metre2)');
LEGEND_ALGEBRAIC(:,4) = strpad('L_SEE in component L_SEE (metre)');
LEGEND_CONSTANTS(:,21) = strpad('L_slack in component F_SEE (metre)');
LEGEND_ALGEBRAIC(:,2) = strpad('F_PEE in component F_PEE (newton)');
LEGEND_CONSTANTS(:,26) = strpad('k_PEE in component k_PEE (newton_per_metre2)');
LEGEND_CONSTANTS(:,22) = strpad('L_slack in component F_PEE (metre)');
LEGEND_CONSTANTS(:,23) = strpad('W in component k_PEE (dimensionless)');
LEGEND_CONSTANTS(:,24) = strpad('L_CE_opt in component k_PEE (metre)');
LEGEND_ALGEBRAIC(:,3) = strpad('L_m in component L_m (metre)');
LEGEND_ALGEBRAIC(:,8) = strpad('F_m in component F_m (newton)');
LEGEND_RATES(:,1) = strpad('d/dt L_CE in component L_CE (metre)');
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.8;
CONSTANTS(:,2) = 10;
CONSTANTS(:,3) = 7000;
STATES(:,1) = 0.038;
CONSTANTS(:,4) = 0.093;
CONSTANTS(:,5) = 0.63;
CONSTANTS(:,6) = 0.93;
CONSTANTS(:,7) = 0.25;
CONSTANTS(:,8) = 1.5;
CONSTANTS(:,9) = 5.67;
CONSTANTS(:,10) = 0.93;
CONSTANTS(:,11) = 0.25;
CONSTANTS(:,12) = 1.5;
CONSTANTS(:,13) = 2;
CONSTANTS(:,14) = 2;
CONSTANTS(:,15) = 0.25;
CONSTANTS(:,16) = 0.93;
CONSTANTS(:,17) = 5.67;
CONSTANTS(:,18) = 1.5;
CONSTANTS(:,19) = 5.67;
CONSTANTS(:,20) = 1000000;
CONSTANTS(:,21) = 0.0025;
CONSTANTS(:,22) = 0.0025;
CONSTANTS(:,23) = 0.63;
CONSTANTS(:,24) = 0.01;
CONSTANTS(:,25) = ( CONSTANTS(:,10).*CONSTANTS(:,11).*(CONSTANTS(:,12) - 1.00000))./( CONSTANTS(:,13).*(CONSTANTS(:,11)+1.00000));
CONSTANTS(:,26) = CONSTANTS(:,3)./power( CONSTANTS(:,23).*CONSTANTS(:,24), 2.00000);
CONSTANTS(:,27) = ( CONSTANTS(:,14).*(CONSTANTS(:,15)+1.00000))./( CONSTANTS(:,16).*CONSTANTS(:,15).*power(CONSTANTS(:,17)+1.00000, 2.00000));
CONSTANTS(:,28) = ( (CONSTANTS(:,18) - 1.00000).*power(CONSTANTS(:,19), 2.00000))./power(CONSTANTS(:,19)+1.00000, 2.00000)+1.00000;
CONSTANTS(:,29) =  1.00000.*((1.00000 - exp(  - 3.82000.*CONSTANTS(:,1)))+ CONSTANTS(:,1).*exp( - 3.82000));
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) = ( CONSTANTS(:,3).*( 1.00000.*(1.00000 - STATES(:,1)) - power(CONSTANTS(:,4), 2.00000)))./( power(CONSTANTS(:,5), 2.00000).*power(CONSTANTS(:,4), 2.00000));
ALGEBRAIC(:,3) = piecewise({VOI<=1.00000, 0.0380000 , VOI>1.00000&VOI<2.00000, 0.0380000+ 0.00200000.*(VOI - 1.00000) }, 0.0400000);
ALGEBRAIC(:,4) = ALGEBRAIC(:,3) - STATES(:,1);
ALGEBRAIC(:,5) = piecewise({ALGEBRAIC(:,4)<=CONSTANTS(:,21), 0.00000 },  CONSTANTS(:,20).*power(ALGEBRAIC(:,4) - CONSTANTS(:,21), 2.00000));
ALGEBRAIC(:,2) = piecewise({STATES(:,1)<=CONSTANTS(:,22), 0.00000 },  CONSTANTS(:,26).*power(STATES(:,1) - CONSTANTS(:,22), 2.00000));
[CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
RATES(:,1) =  1.00000.*ALGEBRAIC(:,7);
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) = ( CONSTANTS(:,3).*( 1.00000.*(1.00000 - STATES(:,1)) - power(CONSTANTS(:,4), 2.00000)))./( power(CONSTANTS(:,5), 2.00000).*power(CONSTANTS(:,4), 2.00000));
ALGEBRAIC(:,3) = piecewise({VOI<=1.00000, 0.0380000 , VOI>1.00000&VOI<2.00000, 0.0380000+ 0.00200000.*(VOI - 1.00000) }, 0.0400000);
ALGEBRAIC(:,4) = ALGEBRAIC(:,3) - STATES(:,1);
ALGEBRAIC(:,5) = piecewise({ALGEBRAIC(:,4)<=CONSTANTS(:,21), 0.00000 },  CONSTANTS(:,20).*power(ALGEBRAIC(:,4) - CONSTANTS(:,21), 2.00000));
ALGEBRAIC(:,2) = piecewise({STATES(:,1)<=CONSTANTS(:,22), 0.00000 },  CONSTANTS(:,26).*power(STATES(:,1) - CONSTANTS(:,22), 2.00000));
ALGEBRAIC(:,8) = ALGEBRAIC(:,5);
ALGEBRAIC(:,9) =  ALGEBRAIC(:,1).*ALGEBRAIC(:,6).*CONSTANTS(:,1);
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) - piecewise({ALGEBRAIC(:,7)<=0.00000, ( CONSTANTS(:,29).*CONSTANTS(:,6)+ALGEBRAIC(:,7))./( CONSTANTS(:,29).*CONSTANTS(:,6) - ALGEBRAIC(:,7)./CONSTANTS(:,7)) , 0.00000<ALGEBRAIC(:,7)&ALGEBRAIC(:,7)<= CONSTANTS(:,9).*CONSTANTS(:,25), ( CONSTANTS(:,8).*ALGEBRAIC(:,7)+CONSTANTS(:,25))./(ALGEBRAIC(:,7)+CONSTANTS(:,25)) , ALGEBRAIC(:,7)> CONSTANTS(:,9).*CONSTANTS(:,25), CONSTANTS(:,28)+ CONSTANTS(:,27).*ALGEBRAIC(:,7) }, NaN);
resid(2) = ALGEBRAIC(:,7) -  1.00000.*(( (1.00000./ALGEBRAIC(:,6)).*( ALGEBRAIC(:,5).*(ALGEBRAIC(:,3) - STATES(:,1)) -  ALGEBRAIC(:,2).*STATES(:,1)))./( CONSTANTS(:,1).*ALGEBRAIC(:,1)));
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
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

```
Source
Derived from workspace Mclean, Su, Vandenbogert, 2003 at changeset feaf3ec4af91.
Collaboration
To begin collaborating on this work, please use your git client and issue this command: