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 =4;
end
% There are a total of 1 entries in each of the rate and state variable arrays.
% There are a total of 17 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 (minute)');
    LEGEND_CONSTANTS(:,1) = strpad('CNA in component antidiuretic_hormone (monovalent_mEq_per_litre)');
    LEGEND_CONSTANTS(:,2) = strpad('PA1 in component antidiuretic_hormone (mmHg)');
    LEGEND_CONSTANTS(:,13) = strpad('ADHNA in component osmotic_control_of_ADH_secretion (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('CNR in component parameter_values (monovalent_mEq_per_litre)');
    LEGEND_CONSTANTS(:,12) = strpad('ADHNA1 in component osmotic_control_of_ADH_secretion (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('ADHPR in component pressure_control_of_ADH_secretion (dimensionless)');
    LEGEND_CONSTANTS(:,4) = strpad('ADHPUL in component parameter_values (mmHg)');
    LEGEND_CONSTANTS(:,5) = strpad('ADHPAM in component parameter_values (per_mmHg2)');
    LEGEND_CONSTANTS(:,14) = strpad('ADHPA in component pressure_control_of_ADH_secretion (mmHg)');
    LEGEND_CONSTANTS(:,17) = strpad('ADH in component total_ADH_secretion (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('ADHINF in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,16) = strpad('ADH1 in component total_ADH_secretion (dimensionless)');
    LEGEND_STATES(:,1) = strpad('ADHC in component ADH_in_blood (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('ADHTC in component parameter_values (minute)');
    LEGEND_ALGEBRAIC(:,3) = strpad('ADHMV in component ADH_effect_on_nonrenal_vascular_resistance (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('ADHVUL in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,9) = strpad('ADHVLL in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('ADHMV1 in component ADH_effect_on_nonrenal_vascular_resistance (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('ADHMK in component ADH_effect_on_kidney (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('ADHKLL in component parameter_values (dimensionless)');
    LEGEND_CONSTANTS(:,11) = strpad('ADHKUL in component parameter_values (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('ADHMK1 in component ADH_effect_on_kidney (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt ADHC in component ADH_in_blood (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 = [];
    CONSTANTS(:,1) = 142.035;
    CONSTANTS(:,2) = 103.525;
    CONSTANTS(:,3) = 139;
    CONSTANTS(:,4) = 85;
    CONSTANTS(:,5) = 0.3;
    CONSTANTS(:,6) = 0;
    STATES(:,1) = 1.0;
    CONSTANTS(:,7) = 15;
    CONSTANTS(:,8) = 2.5;
    CONSTANTS(:,9) = 0.93617;
    CONSTANTS(:,10) = 0.2;
    CONSTANTS(:,11) = 5;
    CONSTANTS(:,12) = (CONSTANTS(:,1) - CONSTANTS(:,3))./(142.000 - CONSTANTS(:,3));
    CONSTANTS(:,13) = piecewise({CONSTANTS(:,12)<0.00000, 0.00000 }, CONSTANTS(:,12));
    CONSTANTS(:,14) = piecewise({CONSTANTS(:,2)>CONSTANTS(:,4), CONSTANTS(:,4) }, CONSTANTS(:,2));
    CONSTANTS(:,15) =  power(CONSTANTS(:,4) - CONSTANTS(:,14), 2.00000).*CONSTANTS(:,5);
    CONSTANTS(:,16) = CONSTANTS(:,13)+CONSTANTS(:,15)+CONSTANTS(:,6);
    CONSTANTS(:,17) = piecewise({CONSTANTS(:,16)<0.00000, 0.00000 }, CONSTANTS(:,16));
    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(:,1) = (CONSTANTS(:,17) - STATES(:,1))./CONSTANTS(:,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(:,8) - (CONSTANTS(:,8) - 1.00000)./( ((CONSTANTS(:,9) - 1.00000)./(CONSTANTS(:,9) - CONSTANTS(:,8))).*(STATES(:,1) - 1.00000)+1.00000);
    ALGEBRAIC(:,2) = CONSTANTS(:,11) - (CONSTANTS(:,11) - 1.00000)./( ((CONSTANTS(:,10) - 1.00000)./(CONSTANTS(:,10) - CONSTANTS(:,11))).*(STATES(:,1) - 1.00000)+1.00000);
    ALGEBRAIC(:,3) = piecewise({ALGEBRAIC(:,1)<CONSTANTS(:,9), CONSTANTS(:,9) }, ALGEBRAIC(:,1));
    ALGEBRAIC(:,4) = piecewise({ALGEBRAIC(:,2)<CONSTANTS(:,10), CONSTANTS(:,10) }, ALGEBRAIC(:,2));
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