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 =12;
end
% There are a total of 3 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 (dimensionless)');
    LEGEND_STATES(:,1) = strpad('C in component C (dimensionless)');
    LEGEND_CONSTANTS(:,1) = strpad('gamma in component C (dimensionless)');
    LEGEND_ALGEBRAIC(:,6) = strpad('Vs in component Vs (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('V1 in component V1 (dimensionless)');
    LEGEND_ALGEBRAIC(:,11) = strpad('V2 in component V2 (dimensionless)');
    LEGEND_ALGEBRAIC(:,7) = strpad('Vd in component Vd (dimensionless)');
    LEGEND_STATES(:,2) = strpad('K in component K (dimensionless)');
    LEGEND_STATES(:,3) = strpad('RP in component RP (dimensionless)');
    LEGEND_ALGEBRAIC(:,12) = strpad('V3 in component V3 (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('V4 in component V4 (dimensionless)');
    LEGEND_ALGEBRAIC(:,3) = strpad('E in component E (dimensionless)');
    LEGEND_CONSTANTS(:,2) = strpad('sigma in component E (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('RE in component RE (dimensionless)');
    LEGEND_ALGEBRAIC(:,8) = strpad('KP in component KP (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('KPI in component KPI (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('thetaI in component KPI (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('I in component I (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('lambda in component I (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('thetaE in component RE (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('R in component R (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('VCs in component Vs (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('Vsm in component Vs (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('KsE in component Vs (dimensionless)');
    LEGEND_CONSTANTS(:,9) = strpad('V1m in component V1 (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('K1C in component V1 (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('K1 in component V1 (dimensionless)');
    LEGEND_CONSTANTS(:,12) = strpad('V2m in component V2 (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('K2 in component V2 (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('V3m in component V3 (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('K3 in component V3 (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('V4m in component V4 (dimensionless)');
    LEGEND_CONSTANTS(:,17) = strpad('K4 in component V4 (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('VdEm in component Vd (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('KdC in component Vd (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt C in component C (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt K in component K (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt RP in component RP (dimensionless)');
    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.38;
    CONSTANTS(:,1) = 1.0;
    STATES(:,2) = 0.1;
    STATES(:,3) = 1.0;
    CONSTANTS(:,2) = 10;
    CONSTANTS(:,3) = 1.0;
    CONSTANTS(:,4) = 1.0;
    CONSTANTS(:,5) = 0.01;
    CONSTANTS(:,6) = 1.0;
    CONSTANTS(:,7) = 1.0;
    CONSTANTS(:,8) = 0.1;
    CONSTANTS(:,9) = 50.0;
    CONSTANTS(:,10) = 0.1;
    CONSTANTS(:,11) = 0.0001;
    CONSTANTS(:,12) = 40;
    CONSTANTS(:,13) = 0.0001;
    CONSTANTS(:,14) = 3000;
    CONSTANTS(:,15) = 0.0001;
    CONSTANTS(:,16) = 3.0;
    CONSTANTS(:,17) = 0.0001;
    CONSTANTS(:,18) = 1000.0;
    CONSTANTS(:,19) = 0.005;
    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
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_0(VOI, CONSTANTS, STATES, ALGEBRAIC);
    ALGEBRAIC(:,6) = CONSTANTS(:,6)+ CONSTANTS(:,7).*(ALGEBRAIC(:,3)./(CONSTANTS(:,8)+ALGEBRAIC(:,3)));
    ALGEBRAIC(:,1) =  CONSTANTS(:,9).*(STATES(:,1)./(CONSTANTS(:,10)+STATES(:,1))).*(STATES(:,2)./(CONSTANTS(:,11)+STATES(:,2)));
    [CONSTANTS, STATES, ALGEBRAIC] = rootfind_1(VOI, CONSTANTS, STATES, ALGEBRAIC);
    ALGEBRAIC(:,11) =  CONSTANTS(:,12).*(ALGEBRAIC(:,8)./(CONSTANTS(:,13)+ALGEBRAIC(:,8)));
    ALGEBRAIC(:,7) = STATES(:,1)+ CONSTANTS(:,18).*ALGEBRAIC(:,3).*(STATES(:,1)./(CONSTANTS(:,19)+STATES(:,1)));
    RATES(:,1) = (ALGEBRAIC(:,6)+ CONSTANTS(:,1).*ALGEBRAIC(:,11)) - ( CONSTANTS(:,1).*ALGEBRAIC(:,1)+ALGEBRAIC(:,7));
    RATES(:,2) = ALGEBRAIC(:,11) - ALGEBRAIC(:,1);
    ALGEBRAIC(:,12) =  CONSTANTS(:,14).*ALGEBRAIC(:,8).*(ALGEBRAIC(:,4)./(CONSTANTS(:,15)+ALGEBRAIC(:,4)));
    ALGEBRAIC(:,2) =  CONSTANTS(:,16).*(STATES(:,3)./(CONSTANTS(:,17)+STATES(:,3)));
    RATES(:,3) = ALGEBRAIC(:,12) - ALGEBRAIC(:,2);
   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(:,6) = CONSTANTS(:,6)+ CONSTANTS(:,7).*(ALGEBRAIC(:,3)./(CONSTANTS(:,8)+ALGEBRAIC(:,3)));
    ALGEBRAIC(:,1) =  CONSTANTS(:,9).*(STATES(:,1)./(CONSTANTS(:,10)+STATES(:,1))).*(STATES(:,2)./(CONSTANTS(:,11)+STATES(:,2)));
    ALGEBRAIC(:,11) =  CONSTANTS(:,12).*(ALGEBRAIC(:,8)./(CONSTANTS(:,13)+ALGEBRAIC(:,8)));
    ALGEBRAIC(:,7) = STATES(:,1)+ CONSTANTS(:,18).*ALGEBRAIC(:,3).*(STATES(:,1)./(CONSTANTS(:,19)+STATES(:,1)));
    ALGEBRAIC(:,12) =  CONSTANTS(:,14).*ALGEBRAIC(:,8).*(ALGEBRAIC(:,4)./(CONSTANTS(:,15)+ALGEBRAIC(:,4)));
    ALGEBRAIC(:,2) =  CONSTANTS(:,16).*(STATES(:,3)./(CONSTANTS(:,17)+STATES(:,3)));
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) ~= 3), initialGuess_0 = [0.1,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(:,3) = soln(1);
        ALGEBRAIC(:,4) = soln(2);
        ALGEBRAIC(:,5) = soln(3);
    else
        SET_ALGEBRAIC(:,3) = logical(1);
        SET_ALGEBRAIC(:,4) = logical(1);
        SET_ALGEBRAIC(:,5) = 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(:,3) = soln(1);
            TEMP_ALGEBRAIC(:,4) = soln(2);
            TEMP_ALGEBRAIC(:,5) = soln(3);
            ALGEBRAIC(i,SET_ALGEBRAIC) = TEMP_ALGEBRAIC(SET_ALGEBRAIC);
        end
    end
end

function resid = residualSN_0(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    ALGEBRAIC(:,3) = algebraicCandidate(1);
    ALGEBRAIC(:,4) = algebraicCandidate(2);
    ALGEBRAIC(:,5) = algebraicCandidate(3);
    resid(1) = ALGEBRAIC(:,3) - (1.00000 -  CONSTANTS(:,2).*ALGEBRAIC(:,4));
    resid(2) = ALGEBRAIC(:,4) -  CONSTANTS(:,5).*ALGEBRAIC(:,5).*ALGEBRAIC(:,3);
    resid(3) = ALGEBRAIC(:,5) - (1.00000 - (STATES(:,3)+ALGEBRAIC(:,4)));
end

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

function resid = residualSN_1(algebraicCandidate, ALGEBRAIC, VOI, CONSTANTS, STATES)
    ALGEBRAIC(:,8) = algebraicCandidate(1);
    ALGEBRAIC(:,9) = algebraicCandidate(2);
    ALGEBRAIC(:,10) = algebraicCandidate(3);
    resid(1) = ALGEBRAIC(:,8) - (1.00000 - (ALGEBRAIC(:,9)+STATES(:,2)));
    resid(2) = ALGEBRAIC(:,9) -  CONSTANTS(:,3).*ALGEBRAIC(:,8).*ALGEBRAIC(:,10);
    resid(3) = ALGEBRAIC(:,10) - (1.00000 -  CONSTANTS(:,4).*ALGEBRAIC(:,9));
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