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 7 entries in each of the rate and state variable arrays.
% There are a total of 28 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_STATES(:,1) = strpad('r in component r (nanomolar)');
    LEGEND_ALGEBRAIC(:,1) = strpad('ract in component r (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('hr in component r (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('Ir in component r (flux)');
    LEGEND_ALGEBRAIC(:,7) = strpad('Ir2 in component r (flux)');
    LEGEND_CONSTANTS(:,1) = strpad('k6 in component r (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,2) = strpad('n1 in component r (dimensionless)');
    LEGEND_CONSTANTS(:,3) = strpad('theta_1 in component r (nanomolar)');
    LEGEND_CONSTANTS(:,4) = strpad('n3 in component r (dimensionless)');
    LEGEND_CONSTANTS(:,5) = strpad('theta_3 in component r (dimensionless)');
    LEGEND_STATES(:,2) = strpad('s in component s (nanomolar)');
    LEGEND_ALGEBRAIC(:,9) = strpad('h_delta1 in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,6) = strpad('j1 in component model_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('Is2 in component s (flux)');
    LEGEND_ALGEBRAIC(:,8) = strpad('Is in component model_parameters (flux)');
    LEGEND_CONSTANTS(:,8) = strpad('k7 in component model_parameters (first_order_rate_constant)');
    LEGEND_STATES(:,3) = strpad('g in component g (dimensionless)');
    LEGEND_STATES(:,4) = strpad('se in component se (nanomolar)');
    LEGEND_CONSTANTS(:,9) = strpad('Ise in component se (flux)');
    LEGEND_STATES(:,5) = strpad('f in component f (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('k1 in component f (second_order_rate_constant)');
    LEGEND_CONSTANTS(:,11) = strpad('k2 in component f (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,12) = strpad('k3 in component f (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,3) = strpad('phi_b_s in component f (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('sb in component f (dimensionless)');
    LEGEND_CONSTANTS(:,14) = strpad('delta_b in component f (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('c in component model_parameters (nanomolar)');
    LEGEND_STATES(:,6) = strpad('h in component h (nanomolar)');
    LEGEND_CONSTANTS(:,16) = strpad('k4 in component h (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,17) = strpad('k5 in component h (first_order_rate_constant)');
    LEGEND_ALGEBRAIC(:,4) = strpad('phi_r_s in component h (dimensionless)');
    LEGEND_CONSTANTS(:,18) = strpad('sr in component h (dimensionless)');
    LEGEND_CONSTANTS(:,19) = strpad('delta_r in component h (dimensionless)');
    LEGEND_CONSTANTS(:,20) = strpad('k8 in component model_parameters (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,21) = strpad('g1 in component g (first_order_rate_constant)');
    LEGEND_CONSTANTS(:,22) = strpad('gmax in component g (dimensionless)');
    LEGEND_CONSTANTS(:,23) = strpad('g2 in component g (per_nanomolar)');
    LEGEND_ALGEBRAIC(:,6) = strpad('hact in component g (dimensionless)');
    LEGEND_CONSTANTS(:,24) = strpad('n2 in component g (dimensionless)');
    LEGEND_CONSTANTS(:,25) = strpad('theta_2 in component g (dimensionless)');
    LEGEND_ALGEBRAIC(:,2) = strpad('h_delta in component model_parameters (dimensionless)');
    LEGEND_STATES(:,7) = strpad('hh in component hh (nanomolar)');
    LEGEND_CONSTANTS(:,26) = strpad('Ih in component hh (flux)');
    LEGEND_CONSTANTS(:,27) = strpad('delta in component model_parameters (per_nanomolar)');
    LEGEND_CONSTANTS(:,28) = strpad('delta1 in component model_parameters (per_nanomolar)');
    LEGEND_RATES(:,1) = strpad('d/dt r in component r (nanomolar)');
    LEGEND_RATES(:,2) = strpad('d/dt s in component s (nanomolar)');
    LEGEND_RATES(:,4) = strpad('d/dt se in component se (nanomolar)');
    LEGEND_RATES(:,5) = strpad('d/dt f in component f (dimensionless)');
    LEGEND_RATES(:,6) = strpad('d/dt h in component h (nanomolar)');
    LEGEND_RATES(:,3) = strpad('d/dt g in component g (dimensionless)');
    LEGEND_RATES(:,7) = strpad('d/dt hh in component hh (nanomolar)');
    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.0;
    CONSTANTS(:,1) = 5.0;
    CONSTANTS(:,2) = 4.0;
    CONSTANTS(:,3) = 1.0;
    CONSTANTS(:,4) = 5.0;
    CONSTANTS(:,5) = 30.0;
    STATES(:,2) = 0.0;
    CONSTANTS(:,6) = 10;
    CONSTANTS(:,7) = 50.0;
    CONSTANTS(:,8) = 5.0;
    STATES(:,3) = 2.0;
    STATES(:,4) = 0.0;
    CONSTANTS(:,9) = 10.0;
    STATES(:,5) = 0.3;
    CONSTANTS(:,10) = 0.1;
    CONSTANTS(:,11) = 0.002;
    CONSTANTS(:,12) = 0.018;
    CONSTANTS(:,13) = 0.029;
    CONSTANTS(:,14) = 0.3;
    CONSTANTS(:,15) = 0.01;
    STATES(:,6) = 0.0;
    CONSTANTS(:,16) = 0.5;
    CONSTANTS(:,17) = 71.0;
    CONSTANTS(:,18) = -0.56;
    CONSTANTS(:,19) = 0.2;
    CONSTANTS(:,20) = 0.07;
    CONSTANTS(:,21) = 1.0;
    CONSTANTS(:,22) = 5.0;
    CONSTANTS(:,23) = 0.008;
    CONSTANTS(:,24) = 2.0;
    CONSTANTS(:,25) = 30.0;
    STATES(:,7) = 0.0;
    CONSTANTS(:,26) = 50.0;
    CONSTANTS(:,27) = 60.0;
    CONSTANTS(:,28) = 15.0;
    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(:,4) = CONSTANTS(:,9) -  CONSTANTS(:,8).*STATES(:,4);
    RATES(:,7) =  CONSTANTS(:,6).*(CONSTANTS(:,26) -  CONSTANTS(:,20).*STATES(:,7));
    ALGEBRAIC(:,3) = 1.00000./(1.00000+exp( - (arbitrary_log( 1.00000.*(STATES(:,2)+STATES(:,4)), 10) - CONSTANTS(:,13))./CONSTANTS(:,14)));
    RATES(:,5) =  - ( CONSTANTS(:,10).*(STATES(:,1)+CONSTANTS(:,15)).*STATES(:,5))+ (CONSTANTS(:,11)+ CONSTANTS(:,12).*ALGEBRAIC(:,3)).*(1.00000 - STATES(:,5));
    ALGEBRAIC(:,4) = 1.00000./(1.00000+exp( - (arbitrary_log( 1.00000.*(STATES(:,2)+STATES(:,4)), 10) - CONSTANTS(:,18))./CONSTANTS(:,19)));
    RATES(:,6) =  CONSTANTS(:,6).*( (CONSTANTS(:,16)+ CONSTANTS(:,17).*(1.00000 - ALGEBRAIC(:,4))).*( (STATES(:,1)+CONSTANTS(:,15)).*STATES(:,5)) -  CONSTANTS(:,20).*STATES(:,6));
    ALGEBRAIC(:,8) = piecewise({VOI>0.00000&VOI<=90.0000, 10.0000 , VOI>90.0000&VOI<=180.000, 0.00000 , VOI>180.000&VOI<=270.000, 10.0000 , VOI>270.000&VOI<=360.000, 0.00000 }, 0.00000);
    RATES(:,2) = ( ALGEBRAIC(:,8).*STATES(:,3) -  CONSTANTS(:,8).*STATES(:,2))+CONSTANTS(:,7);
    ALGEBRAIC(:,2) =  (STATES(:,6)+STATES(:,7)).*CONSTANTS(:,27);
    ALGEBRAIC(:,6) = power(ALGEBRAIC(:,2), CONSTANTS(:,24))./(power(ALGEBRAIC(:,2), CONSTANTS(:,24))+power( CONSTANTS(:,6).*CONSTANTS(:,25), CONSTANTS(:,24)));
    RATES(:,3) =  CONSTANTS(:,21).*ALGEBRAIC(:,6).*((CONSTANTS(:,22) - STATES(:,3))./CONSTANTS(:,22)) -  CONSTANTS(:,23).*ALGEBRAIC(:,8).*STATES(:,3);
    ALGEBRAIC(:,1) = 1.00000 - power(STATES(:,2), CONSTANTS(:,2))./(power(STATES(:,2), CONSTANTS(:,2))+power(CONSTANTS(:,3), CONSTANTS(:,2)));
    ALGEBRAIC(:,9) =  (STATES(:,6)+STATES(:,7)).*CONSTANTS(:,28);
    ALGEBRAIC(:,10) = 1.00000 - power(ALGEBRAIC(:,9), CONSTANTS(:,4))./(power(ALGEBRAIC(:,9), CONSTANTS(:,4))+power( CONSTANTS(:,6).*CONSTANTS(:,5), CONSTANTS(:,4)));
    ALGEBRAIC(:,5) = piecewise({VOI>=0.00000&VOI<=90.0000, 0.00000 , VOI>=91.0000&VOI<=92.0000, 10.0000 , VOI>=93.0000&VOI<=113.000, 0.00000 , VOI>=114.000&VOI<=115.000, 10.0000 , VOI>=116.000&VOI<=136.000, 0.00000 , VOI>=137.000&VOI<=138.000, 10.0000 , VOI>=139.000&VOI<=159.000, 0.00000 , VOI>=160.000&VOI<=161.000, 10.0000 , VOI>=162.000&VOI<=252.000, 0.00000 , VOI>=253.000&VOI<=254.000, 10.0000 , VOI>=255.000&VOI<=275.000, 0.00000 , VOI>=276.000&VOI<=277.000, 10.0000 , VOI>=278.000&VOI<=298.000, 0.00000 , VOI>=299.000&VOI<=300.000, 10.0000 , VOI>=301.000&VOI<=321.000, 0.00000 , VOI>=322.000&VOI<=323.000, 10.0000 }, 0.00000);
    ALGEBRAIC(:,7) = piecewise({VOI>=0.00000&VOI<=5.00000, 0.00000 , VOI>=6.00000&VOI<=7.00000, 1000.00 , VOI>=8.00000&VOI<=12.0000, 0.00000 , VOI>=13.0000&VOI<=14.0000, 1000.00 , VOI>=15.0000&VOI<=21.0000, 0.00000 , VOI>=22.0000&VOI<=23.0000, 1000.00 , VOI>=24.0000&VOI<=204.000, 0.00000 , VOI>=205.000&VOI<=206.000, 1000.00 , VOI>=207.000&VOI<=217.000, 0.00000 , VOI>=218.000&VOI<=219.000, 1000.00 , VOI>=220.000&VOI<=227.000, 0.00000 , VOI>=228.000&VOI<=229.000, 1000.00 , VOI>=230.000&VOI<=310.000, 0.00000 , VOI>=311.000&VOI<=312.000, 1000.00 , VOI>=313.000&VOI<=321.000, 0.00000 , VOI>=322.000&VOI<=323.000, 1000.00 }, 0.00000);
    RATES(:,1) = ( ALGEBRAIC(:,1).*ALGEBRAIC(:,10).*ALGEBRAIC(:,5) -  CONSTANTS(:,1).*STATES(:,1))+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(:,3) = 1.00000./(1.00000+exp( - (arbitrary_log( 1.00000.*(STATES(:,2)+STATES(:,4)), 10) - CONSTANTS(:,13))./CONSTANTS(:,14)));
    ALGEBRAIC(:,4) = 1.00000./(1.00000+exp( - (arbitrary_log( 1.00000.*(STATES(:,2)+STATES(:,4)), 10) - CONSTANTS(:,18))./CONSTANTS(:,19)));
    ALGEBRAIC(:,8) = piecewise({VOI>0.00000&VOI<=90.0000, 10.0000 , VOI>90.0000&VOI<=180.000, 0.00000 , VOI>180.000&VOI<=270.000, 10.0000 , VOI>270.000&VOI<=360.000, 0.00000 }, 0.00000);
    ALGEBRAIC(:,2) =  (STATES(:,6)+STATES(:,7)).*CONSTANTS(:,27);
    ALGEBRAIC(:,6) = power(ALGEBRAIC(:,2), CONSTANTS(:,24))./(power(ALGEBRAIC(:,2), CONSTANTS(:,24))+power( CONSTANTS(:,6).*CONSTANTS(:,25), CONSTANTS(:,24)));
    ALGEBRAIC(:,1) = 1.00000 - power(STATES(:,2), CONSTANTS(:,2))./(power(STATES(:,2), CONSTANTS(:,2))+power(CONSTANTS(:,3), CONSTANTS(:,2)));
    ALGEBRAIC(:,9) =  (STATES(:,6)+STATES(:,7)).*CONSTANTS(:,28);
    ALGEBRAIC(:,10) = 1.00000 - power(ALGEBRAIC(:,9), CONSTANTS(:,4))./(power(ALGEBRAIC(:,9), CONSTANTS(:,4))+power( CONSTANTS(:,6).*CONSTANTS(:,5), CONSTANTS(:,4)));
    ALGEBRAIC(:,5) = piecewise({VOI>=0.00000&VOI<=90.0000, 0.00000 , VOI>=91.0000&VOI<=92.0000, 10.0000 , VOI>=93.0000&VOI<=113.000, 0.00000 , VOI>=114.000&VOI<=115.000, 10.0000 , VOI>=116.000&VOI<=136.000, 0.00000 , VOI>=137.000&VOI<=138.000, 10.0000 , VOI>=139.000&VOI<=159.000, 0.00000 , VOI>=160.000&VOI<=161.000, 10.0000 , VOI>=162.000&VOI<=252.000, 0.00000 , VOI>=253.000&VOI<=254.000, 10.0000 , VOI>=255.000&VOI<=275.000, 0.00000 , VOI>=276.000&VOI<=277.000, 10.0000 , VOI>=278.000&VOI<=298.000, 0.00000 , VOI>=299.000&VOI<=300.000, 10.0000 , VOI>=301.000&VOI<=321.000, 0.00000 , VOI>=322.000&VOI<=323.000, 10.0000 }, 0.00000);
    ALGEBRAIC(:,7) = piecewise({VOI>=0.00000&VOI<=5.00000, 0.00000 , VOI>=6.00000&VOI<=7.00000, 1000.00 , VOI>=8.00000&VOI<=12.0000, 0.00000 , VOI>=13.0000&VOI<=14.0000, 1000.00 , VOI>=15.0000&VOI<=21.0000, 0.00000 , VOI>=22.0000&VOI<=23.0000, 1000.00 , VOI>=24.0000&VOI<=204.000, 0.00000 , VOI>=205.000&VOI<=206.000, 1000.00 , VOI>=207.000&VOI<=217.000, 0.00000 , VOI>=218.000&VOI<=219.000, 1000.00 , VOI>=220.000&VOI<=227.000, 0.00000 , VOI>=228.000&VOI<=229.000, 1000.00 , VOI>=230.000&VOI<=310.000, 0.00000 , VOI>=311.000&VOI<=312.000, 1000.00 , VOI>=313.000&VOI<=321.000, 0.00000 , VOI>=322.000&VOI<=323.000, 1000.00 }, 0.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