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 =1;
end
% There are a total of 2 entries in each of the rate and state variable arrays.
% There are a total of 34 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('t in component environment (second)');
    LEGEND_CONSTANTS(:,1) = strpad('x in component environment (nanometer)');
    LEGEND_STATES(:,1) = strpad('n in component Crossbridges_attached (dimensionless)');
    LEGEND_STATES(:,2) = strpad('A_c in component Actin_free (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('f in component f (per_second)');
    LEGEND_CONSTANTS(:,25) = strpad('g in component g (per_second)');
    LEGEND_CONSTANTS(:,2) = strpad('h in component Crossbridges_attached (nanometer)');
    LEGEND_CONSTANTS(:,3) = strpad('f_1 in component f (per_second)');
    LEGEND_CONSTANTS(:,4) = strpad('g_1 in component g (per_second)');
    LEGEND_CONSTANTS(:,5) = strpad('g_2 in component g (per_second)');
    LEGEND_ALGEBRAIC(:,1) = strpad('Ca_f in component Ca_sarcoplasm (molar)');
    LEGEND_CONSTANTS(:,6) = strpad('t_d in component Ca_sarcoplasm (second)');
    LEGEND_CONSTANTS(:,7) = strpad('a_1 in component Ca_sarcoplasm (per_second_squared)');
    LEGEND_CONSTANTS(:,8) = strpad('b_1 in component Ca_sarcoplasm (per_second_squared)');
    LEGEND_CONSTANTS(:,9) = strpad('Ca_0 in component Ca_sarcoplasm (molar)');
    LEGEND_CONSTANTS(:,10) = strpad('c_1 in component Actin_free (per_second)');
    LEGEND_CONSTANTS(:,27) = strpad('c_2 in component Actin_free (per_second)');
    LEGEND_CONSTANTS(:,11) = strpad('c_2_0 in component Actin_free (per_second)');
    LEGEND_CONSTANTS(:,12) = strpad('k_i in component Actin_free (dimensionless)');
    LEGEND_CONSTANTS(:,26) = strpad('s_h in component s_h (muscle_length)');
    LEGEND_CONSTANTS(:,13) = strpad('q in component Actin_free (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('AT_0 in component Actin_free (dimensionless)');
    LEGEND_CONSTANTS(:,33) = strpad('F_SE in component Series_Elastic_Element (force)');
    LEGEND_CONSTANTS(:,15) = strpad('alpha_s in component Series_Elastic_Element (force)');
    LEGEND_CONSTANTS(:,16) = strpad('beta_s in component Series_Elastic_Element (muscle_length)');
    LEGEND_CONSTANTS(:,32) = strpad('x_s in component SE_constants (muscle_length)');
    LEGEND_CONSTANTS(:,17) = strpad('x_so in component Series_Elastic_Element (muscle_length)');
    LEGEND_CONSTANTS(:,31) = strpad('X_M_0 in component X_0 (muscle_length)');
    LEGEND_CONSTANTS(:,18) = strpad('L_max in component Series_Elastic_Element (muscle_length)');
    LEGEND_CONSTANTS(:,29) = strpad('F_PE in component Parallel_Elastic_Element (force)');
    LEGEND_CONSTANTS(:,19) = strpad('alpha_p in component Parallel_Elastic_Element (force)');
    LEGEND_CONSTANTS(:,20) = strpad('beta_p in component Parallel_Elastic_Element (muscle_length)');
    LEGEND_CONSTANTS(:,28) = strpad('x_p in component PE_constants (muscle_length)');
    LEGEND_CONSTANTS(:,21) = strpad('x_po in component Parallel_Elastic_Element (muscle_length)');
    LEGEND_CONSTANTS(:,34) = strpad('F_CE in component Contractile_Element (force)');
    LEGEND_CONSTANTS(:,22) = strpad('F_PL in component s_h (force)');
    LEGEND_CONSTANTS(:,30) = strpad('X_S_0 in component X_0 (muscle_length)');
    LEGEND_CONSTANTS(:,23) = strpad('F_PL in component X_0 (force)');
    LEGEND_RATES(:,1) = strpad('d/dt n in component Crossbridges_attached (dimensionless)');
    LEGEND_RATES(:,2) = strpad('d/dt A_c in component Actin_free (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) = 10;
    STATES(:,1) = 0;
    STATES(:,2) = 1;
    CONSTANTS(:,2) = 12;
    CONSTANTS(:,3) = 70;
    CONSTANTS(:,4) = 40;
    CONSTANTS(:,5) = 240;
    CONSTANTS(:,6) = 0.3;
    CONSTANTS(:,7) = 200;
    CONSTANTS(:,8) = 5;
    CONSTANTS(:,9) = 0.45e-6;
    CONSTANTS(:,10) = 200e12;
    CONSTANTS(:,11) = 20;
    CONSTANTS(:,12) = 30.9;
    CONSTANTS(:,13) = 1.45;
    CONSTANTS(:,14) = 2;
    CONSTANTS(:,15) = 0.1027;
    CONSTANTS(:,16) = 20;
    CONSTANTS(:,17) = 0.0387;
    CONSTANTS(:,18) = 1;
    CONSTANTS(:,19) = 0.00224;
    CONSTANTS(:,20) = 20;
    CONSTANTS(:,21) = 0.221;
    CONSTANTS(:,22) = 3;
    CONSTANTS(:,23) = 3;
    CONSTANTS(:,24) = piecewise({CONSTANTS(:,1)<0.00000, 0.00000 , CONSTANTS(:,1)>=0.00000&CONSTANTS(:,1)<CONSTANTS(:,2), ( CONSTANTS(:,3).*CONSTANTS(:,1))./CONSTANTS(:,2) }, 0.00000);
    CONSTANTS(:,25) = piecewise({CONSTANTS(:,1)<0.00000, CONSTANTS(:,5) , CONSTANTS(:,1)>=0.00000&CONSTANTS(:,1)<CONSTANTS(:,2), ( CONSTANTS(:,4).*CONSTANTS(:,1))./CONSTANTS(:,2) }, ( CONSTANTS(:,4).*CONSTANTS(:,1))./CONSTANTS(:,2));
    CONSTANTS(:,26) = CONSTANTS(:,21) - ( 1.00000.*1.00000.*arbitrary_log(1.00000+CONSTANTS(:,22)./CONSTANTS(:,19), 10))./CONSTANTS(:,20);
    CONSTANTS(:,27) =  CONSTANTS(:,11).*exp( CONSTANTS(:,12).*power(CONSTANTS(:,26)./1.00000, CONSTANTS(:,13)));
    CONSTANTS(:,28) = CONSTANTS(:,21) - CONSTANTS(:,26);
    CONSTANTS(:,29) =  CONSTANTS(:,19).*(exp(( CONSTANTS(:,20).*CONSTANTS(:,28))./( 1.00000.*1.00000)) - 1.00000);
    CONSTANTS(:,30) = ( 1.00000.*1.00000.*arbitrary_log(1.00000+CONSTANTS(:,23)./CONSTANTS(:,15), 10))./CONSTANTS(:,16);
    CONSTANTS(:,31) = ((CONSTANTS(:,30)+CONSTANTS(:,18)) - CONSTANTS(:,26)) - CONSTANTS(:,17);
    CONSTANTS(:,32) = (CONSTANTS(:,17)+CONSTANTS(:,26)+CONSTANTS(:,31)) - CONSTANTS(:,18);
    CONSTANTS(:,33) =  CONSTANTS(:,15).*(exp(( CONSTANTS(:,16).*CONSTANTS(:,32))./( 1.00000.*1.00000)) - 1.00000);
    CONSTANTS(:,34) = CONSTANTS(:,33) - CONSTANTS(:,29);
    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(:,24).*(STATES(:,2) - STATES(:,1)) -  CONSTANTS(:,25).*STATES(:,1);
    ALGEBRAIC(:,1) =  CONSTANTS(:,9).*abs(1.00000 - exp(  - CONSTANTS(:,7).*power(VOI, 2.00000))).*exp(  - CONSTANTS(:,8).*power(VOI - CONSTANTS(:,6), 2.00000));
    RATES(:,2) =  CONSTANTS(:,10).*power(ALGEBRAIC(:,1)./1.00000, 2.00000).*(CONSTANTS(:,14) - STATES(:,2)) -  CONSTANTS(:,27).*STATES(:,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(:,1) =  CONSTANTS(:,9).*abs(1.00000 - exp(  - CONSTANTS(:,7).*power(VOI, 2.00000))).*exp(  - CONSTANTS(:,8).*power(VOI - CONSTANTS(:,6), 2.00000));
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

% Compute a logarithm to any base" +
function x = arbitrary_log(a, base)
    x = log(a) ./ log(base);
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