# 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 =10;
end
% There are a total of 3 entries in each of the rate and state variable arrays.
% There are a total of 7 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_ALGEBRAIC(:,1) = strpad('u_in_e in component Environment (J_per_C)');
LEGEND_VOI = strpad('t in component Environment (second)');
LEGEND_STATES(:,1) = strpad('v_1_e in component Voice_coil_equations (C_per_s)');
LEGEND_ALGEBRAIC(:,9) = strpad('a_1_e in component Voice_coil_equations (C_per_s2)');
LEGEND_STATES(:,2) = strpad('q_C_m in component Voice_coil_equations (metre)');
LEGEND_STATES(:,3) = strpad('v_2_m in component Voice_coil_equations (m_per_s)');
LEGEND_ALGEBRAIC(:,10) = strpad('a_2_m in component Voice_coil_equations (m_per_s2)');
LEGEND_ALGEBRAIC(:,3) = strpad('u_R_e in component Voice_coil_equations (J_per_C)');
LEGEND_ALGEBRAIC(:,7) = strpad('u_L_e in component Voice_coil_equations (J_per_C)');
LEGEND_ALGEBRAIC(:,5) = strpad('u_1_e in component Voice_coil_equations (J_per_C)');
LEGEND_ALGEBRAIC(:,2) = strpad('u_2_m in component Voice_coil_equations (J_per_m)');
LEGEND_ALGEBRAIC(:,4) = strpad('u_C_m in component Voice_coil_equations (J_per_m)');
LEGEND_ALGEBRAIC(:,6) = strpad('u_R_m in component Voice_coil_equations (J_per_m)');
LEGEND_ALGEBRAIC(:,8) = strpad('u_L_m in component Voice_coil_equations (J_per_m)');
LEGEND_CONSTANTS(:,1) = strpad('E_1 in component Voice_coil_equations (J_per_C2)');
LEGEND_CONSTANTS(:,2) = strpad('E_2 in component Voice_coil_equations (J_per_m2)');
LEGEND_CONSTANTS(:,3) = strpad('R_1_e in component Voice_coil_equations (Js_per_C2)');
LEGEND_CONSTANTS(:,4) = strpad('R_2_m in component Voice_coil_equations (Js_per_m2)');
LEGEND_CONSTANTS(:,5) = strpad('L_1_e in component Voice_coil_equations (Js2_per_C2)');
LEGEND_CONSTANTS(:,6) = strpad('L_2_m in component Voice_coil_equations (Js2_per_m2)');
LEGEND_CONSTANTS(:,7) = strpad('Bl in component Voice_coil_equations (Js_per_C_m)');
LEGEND_RATES(:,1) = strpad('d/dt v_1_e in component Voice_coil_equations (C_per_s)');
LEGEND_RATES(:,2) = strpad('d/dt q_C_m in component Voice_coil_equations (metre)');
LEGEND_RATES(:,3) = strpad('d/dt v_2_m in component Voice_coil_equations (m_per_s)');
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 = [];
STATES(:,1) = 0;
STATES(:,2) = 0;
STATES(:,3) = 0;
CONSTANTS(:,1) = 1;
CONSTANTS(:,2) = 100;
CONSTANTS(:,3) = 5;
CONSTANTS(:,4) = 0.4;
CONSTANTS(:,5) = 0.2;
CONSTANTS(:,6) = 0.01;
CONSTANTS(:,7) = 6;
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(:,2) = STATES(:,3);
ALGEBRAIC(:,1) =  50.0000.* sin( 50.0000.*2.00000.* pi.*VOI);
ALGEBRAIC(:,3) =  CONSTANTS(:,3).*STATES(:,1);
ALGEBRAIC(:,5) =  CONSTANTS(:,7).*STATES(:,3);
[CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
[CONSTANTS, STATES, ALGEBRAIC] = rootfind_1(VOI, CONSTANTS, STATES, ALGEBRAIC);
RATES(:,1) = ALGEBRAIC(:,9);
ALGEBRAIC(:,2) =  CONSTANTS(:,7).*STATES(:,1);
ALGEBRAIC(:,4) =  CONSTANTS(:,2).*STATES(:,2);
ALGEBRAIC(:,6) =  CONSTANTS(:,4).*STATES(:,3);
[CONSTANTS, STATES, ALGEBRAIC] = rootfind_2(VOI, CONSTANTS, STATES, ALGEBRAIC);
[CONSTANTS, STATES, ALGEBRAIC] = rootfind_3(VOI, CONSTANTS, STATES, ALGEBRAIC);
RATES(:,3) = ALGEBRAIC(:,10);
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) =  50.0000.* sin( 50.0000.*2.00000.* pi.*VOI);
ALGEBRAIC(:,3) =  CONSTANTS(:,3).*STATES(:,1);
ALGEBRAIC(:,5) =  CONSTANTS(:,7).*STATES(:,3);
ALGEBRAIC(:,2) =  CONSTANTS(:,7).*STATES(:,1);
ALGEBRAIC(:,4) =  CONSTANTS(:,2).*STATES(:,2);
ALGEBRAIC(:,6) =  CONSTANTS(:,4).*STATES(:,3);
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
CONSTANTS = CONSTANTS_IN;
STATES = STATES_IN;
ALGEBRAIC = ALGEBRAIC_IN;
global initialGuess_0;
if (length(initialGuess_0) ~= 1), initialGuess_0 = 0.1;, end
options = optimset('Display', 'off', 'TolX', 1E-6);
if length(VOI) == 1
residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
ALGEBRAIC(:,7) = fsolve(residualfn, initialGuess_0, options);
initialGuess_0 = ALGEBRAIC(:,7);
else
SET_ALGEBRAIC(:,7) = logical(1);
for i=1:length(VOI)
residualfn = @(algebraicCandidate)residualSN_0(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
TEMP_ALGEBRAIC(:,7) = fsolve(residualfn, initialGuess_0, options);
ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
initialGuess_0 = TEMP_ALGEBRAIC(:,7);
end
end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
ALGEBRAIC(:,7) = algebraicCandidate;
resid = (ALGEBRAIC(:,1)) - (ALGEBRAIC(:,3)+ALGEBRAIC(:,7)+ALGEBRAIC(:,5));
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_1(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
CONSTANTS = CONSTANTS_IN;
STATES = STATES_IN;
ALGEBRAIC = ALGEBRAIC_IN;
global initialGuess_1;
if (length(initialGuess_1) ~= 1), initialGuess_1 = 0.1;, end
options = optimset('Display', 'off', 'TolX', 1E-6);
if length(VOI) == 1
residualfn = @(algebraicCandidate)residualSN_1(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
ALGEBRAIC(:,9) = fsolve(residualfn, initialGuess_1, options);
initialGuess_1 = ALGEBRAIC(:,9);
else
SET_ALGEBRAIC(:,9) = logical(1);
for i=1:length(VOI)
residualfn = @(algebraicCandidate)residualSN_1(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
TEMP_ALGEBRAIC(:,9) = fsolve(residualfn, initialGuess_1, options);
ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
initialGuess_1 = TEMP_ALGEBRAIC(:,9);
end
end
end

function resid = residualSN_1(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
ALGEBRAIC(:,9) = algebraicCandidate;
resid = (ALGEBRAIC(:,7)) - ( CONSTANTS(:,5).*ALGEBRAIC(:,9));
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_2(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
CONSTANTS = CONSTANTS_IN;
STATES = STATES_IN;
ALGEBRAIC = ALGEBRAIC_IN;
global initialGuess_2;
if (length(initialGuess_2) ~= 1), initialGuess_2 = 0.1;, end
options = optimset('Display', 'off', 'TolX', 1E-6);
if length(VOI) == 1
residualfn = @(algebraicCandidate)residualSN_2(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
ALGEBRAIC(:,8) = fsolve(residualfn, initialGuess_2, options);
initialGuess_2 = ALGEBRAIC(:,8);
else
SET_ALGEBRAIC(:,8) = logical(1);
for i=1:length(VOI)
residualfn = @(algebraicCandidate)residualSN_2(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
TEMP_ALGEBRAIC(:,8) = fsolve(residualfn, initialGuess_2, options);
ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
initialGuess_2 = TEMP_ALGEBRAIC(:,8);
end
end
end

function resid = residualSN_2(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
ALGEBRAIC(:,8) = algebraicCandidate;
resid = (ALGEBRAIC(:,2)) - (ALGEBRAIC(:,4)+ALGEBRAIC(:,6)+ALGEBRAIC(:,8));
end

% Functions required for solving differential algebraic equation
function [CONSTANTS, STATES, ALGEBRAIC] = rootfind_3(VOI, CONSTANTS_IN, STATES_IN, ALGEBRAIC_IN)
CONSTANTS = CONSTANTS_IN;
STATES = STATES_IN;
ALGEBRAIC = ALGEBRAIC_IN;
global initialGuess_3;
if (length(initialGuess_3) ~= 1), initialGuess_3 = 0.1;, end
options = optimset('Display', 'off', 'TolX', 1E-6);
if length(VOI) == 1
residualfn = @(algebraicCandidate)residualSN_3(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES);
ALGEBRAIC(:,10) = fsolve(residualfn, initialGuess_3, options);
initialGuess_3 = ALGEBRAIC(:,10);
else
SET_ALGEBRAIC(:,10) = logical(1);
for i=1:length(VOI)
residualfn = @(algebraicCandidate)residualSN_3(algebraicCandidate, ALGEBRAIC(i,:), VOI(i), CONSTANTS, STATES(i,:));
TEMP_ALGEBRAIC(:,10) = fsolve(residualfn, initialGuess_3, options);
ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
initialGuess_3 = TEMP_ALGEBRAIC(:,10);
end
end
end

function resid = residualSN_3(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
ALGEBRAIC(:,10) = algebraicCandidate;
resid = (ALGEBRAIC(:,8)) - ( CONSTANTS(:,6).*ALGEBRAIC(:,10));
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

```
Collaboration
To begin collaborating on this work, please use your git client and issue this command: