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 =24;
end
% There are a total of 5 entries in each of the rate and state variable arrays.
% There are a total of 35 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 (millisecond)');
    LEGEND_STATES(:,1) = strpad('V in component membrane (millivolt)');
    LEGEND_CONSTANTS(:,1) = strpad('Cm in component membrane (femtoF)');
    LEGEND_ALGEBRAIC(:,2) = strpad('i_K in component K_current (picoA)');
    LEGEND_ALGEBRAIC(:,13) = strpad('i_K_Ca in component K_Ca_current (picoA)');
    LEGEND_ALGEBRAIC(:,6) = strpad('i_K_ATP in component K_ATP_current (picoA)');
    LEGEND_ALGEBRAIC(:,17) = strpad('i_CRAC in component CRAC_current (picoA)');
    LEGEND_ALGEBRAIC(:,12) = strpad('i_Ca in component Ca_current_total (picoA)');
    LEGEND_ALGEBRAIC(:,19) = strpad('i_leak in component leak_current (picoA)');
    LEGEND_CONSTANTS(:,2) = strpad('V_K in component K_current (millivolt)');
    LEGEND_CONSTANTS(:,3) = strpad('g_K in component K_current (picoS)');
    LEGEND_STATES(:,2) = strpad('n in component K_channel_n_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,1) = strpad('n_infinity in component K_channel_n_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,5) = strpad('tau_n in component K_channel_n_gate (millisecond)');
    LEGEND_CONSTANTS(:,4) = strpad('Vn in component K_channel_n_gate (millivolt)');
    LEGEND_CONSTANTS(:,5) = strpad('Sn in component K_channel_n_gate (millivolt)');
    LEGEND_CONSTANTS(:,6) = strpad('lambda_n in component K_channel_n_gate (dimensionless)');
    LEGEND_CONSTANTS(:,7) = strpad('g_K_ATP in component K_ATP_current (picoS)');
    LEGEND_ALGEBRAIC(:,9) = strpad('i_Ca_f in component fast_Ca_current (picoA)');
    LEGEND_CONSTANTS(:,8) = strpad('V_Ca in component fast_Ca_current (millivolt)');
    LEGEND_CONSTANTS(:,9) = strpad('g_Ca_f in component fast_Ca_current (picoS)');
    LEGEND_ALGEBRAIC(:,8) = strpad('m_f_infinity in component fast_Ca_channel_m_gate (dimensionless)');
    LEGEND_CONSTANTS(:,10) = strpad('Vm_f in component fast_Ca_channel_m_gate (millivolt)');
    LEGEND_CONSTANTS(:,11) = strpad('Sm_f in component fast_Ca_channel_m_gate (millivolt)');
    LEGEND_ALGEBRAIC(:,11) = strpad('i_Ca_s in component slow_Ca_current (picoA)');
    LEGEND_CONSTANTS(:,12) = strpad('g_Ca_s in component slow_Ca_current (picoS)');
    LEGEND_ALGEBRAIC(:,10) = strpad('m_s_infinity in component slow_Ca_channel_m_gate (dimensionless)');
    LEGEND_STATES(:,3) = strpad('jm in component slow_Ca_channel_j_gate (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('Vm_s in component slow_Ca_channel_m_gate (millivolt)');
    LEGEND_CONSTANTS(:,14) = strpad('Sm_s in component slow_Ca_channel_m_gate (millivolt)');
    LEGEND_ALGEBRAIC(:,3) = strpad('j in component slow_Ca_channel_j_gate (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('jm_infinity in component slow_Ca_channel_j_gate (dimensionless)');
    LEGEND_CONSTANTS(:,15) = strpad('Vj in component slow_Ca_channel_j_gate (millivolt)');
    LEGEND_ALGEBRAIC(:,7) = strpad('tau_j in component slow_Ca_channel_j_gate (millisecond)');
    LEGEND_CONSTANTS(:,16) = strpad('Sj in component slow_Ca_channel_j_gate (millivolt)');
    LEGEND_CONSTANTS(:,17) = strpad('g_K_Ca in component K_Ca_current (picoS)');
    LEGEND_STATES(:,4) = strpad('Ca_i in component Ca_equations (micromolar)');
    LEGEND_CONSTANTS(:,18) = strpad('kdkca in component K_Ca_current (micromolar)');
    LEGEND_CONSTANTS(:,19) = strpad('g_CRAC in component CRAC_current (picoS)');
    LEGEND_CONSTANTS(:,20) = strpad('V_CRAC in component CRAC_current (millivolt)');
    LEGEND_STATES(:,5) = strpad('Ca_er in component Ca_equations (micromolar)');
    LEGEND_ALGEBRAIC(:,15) = strpad('r_infinity in component CRAC_r_gate (dimensionless)');
    LEGEND_CONSTANTS(:,21) = strpad('Ca_er_bar in component CRAC_r_gate (micromolar)');
    LEGEND_CONSTANTS(:,22) = strpad('g_leak in component leak_current (picoS)');
    LEGEND_ALGEBRAIC(:,14) = strpad('J_er_p in component ER_parameters (micromolar_per_millisecond)');
    LEGEND_CONSTANTS(:,23) = strpad('IP3 in component ER_parameters (micromolar)');
    LEGEND_CONSTANTS(:,24) = strpad('kerp in component ER_parameters (micromolar)');
    LEGEND_CONSTANTS(:,25) = strpad('verp in component ER_parameters (micromolar_per_millisecond)');
    LEGEND_CONSTANTS(:,26) = strpad('dact in component ER_parameters (micromolar)');
    LEGEND_CONSTANTS(:,27) = strpad('dinh in component ER_parameters (micromolar)');
    LEGEND_CONSTANTS(:,28) = strpad('dip3 in component ER_parameters (micromolar)');
    LEGEND_ALGEBRAIC(:,16) = strpad('a_infinity in component ER_parameters (dimensionless)');
    LEGEND_CONSTANTS(:,35) = strpad('b_infinity in component ER_parameters (dimensionless)');
    LEGEND_ALGEBRAIC(:,18) = strpad('h_infinity in component ER_parameters (dimensionless)');
    LEGEND_ALGEBRAIC(:,20) = strpad('O in component ER_parameters (per_millisecond)');
    LEGEND_ALGEBRAIC(:,23) = strpad('J_er_tot in component Ca_equations (micromolar_per_millisecond)');
    LEGEND_ALGEBRAIC(:,22) = strpad('J_er_IP3 in component Ca_equations (micromolar_per_millisecond)');
    LEGEND_ALGEBRAIC(:,21) = strpad('J_er_leak in component Ca_equations (micromolar_per_millisecond)');
    LEGEND_ALGEBRAIC(:,24) = strpad('J_mem_tot in component Ca_membrane_flux (micromolar_per_millisecond)');
    LEGEND_CONSTANTS(:,29) = strpad('perl in component Ca_equations (per_millisecond)');
    LEGEND_CONSTANTS(:,30) = strpad('lambda_er in component Ca_equations (dimensionless)');
    LEGEND_CONSTANTS(:,31) = strpad('sigma_er in component Ca_equations (dimensionless)');
    LEGEND_CONSTANTS(:,32) = strpad('k_Ca in component Ca_membrane_flux (per_millisecond)');
    LEGEND_CONSTANTS(:,33) = strpad('gamma in component Ca_membrane_flux (micromolar_per_millisecond_picoA)');
    LEGEND_CONSTANTS(:,34) = strpad('f in component Ca_membrane_flux (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt V in component membrane (millivolt)');
    LEGEND_RATES(:,2) = strpad('d/dt n in component K_channel_n_gate (dimensionless)');
    LEGEND_RATES(:,3) = strpad('d/dt jm in component slow_Ca_channel_j_gate (dimensionless)');
    LEGEND_RATES(:,5) = strpad('d/dt Ca_er in component Ca_equations (micromolar)');
    LEGEND_RATES(:,4) = strpad('d/dt Ca_i in component Ca_equations (micromolar)');
    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) = -61;
    CONSTANTS(:,1) = 6158;
    CONSTANTS(:,2) = -70;
    CONSTANTS(:,3) = 3900;
    STATES(:,2) = 0.0005;
    CONSTANTS(:,4) = -15;
    CONSTANTS(:,5) = 6;
    CONSTANTS(:,6) = 1.85;
    CONSTANTS(:,7) = 150;
    CONSTANTS(:,8) = 100;
    CONSTANTS(:,9) = 810;
    CONSTANTS(:,10) = -20;
    CONSTANTS(:,11) = 7.5;
    CONSTANTS(:,12) = 510;
    STATES(:,3) = 0.12;
    CONSTANTS(:,13) = -16;
    CONSTANTS(:,14) = 10;
    CONSTANTS(:,15) = -53;
    CONSTANTS(:,16) = 2;
    CONSTANTS(:,17) = 1200;
    STATES(:,4) = 0.11;
    CONSTANTS(:,18) = 0.55;
    CONSTANTS(:,19) = 75;
    CONSTANTS(:,20) = 0;
    STATES(:,5) = 9;
    CONSTANTS(:,21) = 3;
    CONSTANTS(:,22) = 0;
    CONSTANTS(:,23) = 0;
    CONSTANTS(:,24) = 0.1;
    CONSTANTS(:,25) = 0.24;
    CONSTANTS(:,26) = 0.1;
    CONSTANTS(:,27) = 0.4;
    CONSTANTS(:,28) = 0.2;
    CONSTANTS(:,29) = 0.02;
    CONSTANTS(:,30) = 250;
    CONSTANTS(:,31) = 5;
    CONSTANTS(:,32) = 0.07;
    CONSTANTS(:,33) = 0.000003607;
    CONSTANTS(:,34) = 0.01;
    CONSTANTS(:,35) = CONSTANTS(:,23)./(CONSTANTS(:,23)+CONSTANTS(:,28));
    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
    ALGEBRAIC(:,1) = 1.00000./(1.00000+exp((CONSTANTS(:,4) - STATES(:,1))./CONSTANTS(:,5)));
    ALGEBRAIC(:,5) = 9.09000./(1.00000+exp((STATES(:,1) - CONSTANTS(:,4))./CONSTANTS(:,5)));
    RATES(:,2) = ( CONSTANTS(:,6).*(ALGEBRAIC(:,1) - STATES(:,2)))./ALGEBRAIC(:,5);
    ALGEBRAIC(:,4) = 1.00000 - 1.00000./(1.00000+exp((STATES(:,1) - CONSTANTS(:,15))./CONSTANTS(:,16)));
    ALGEBRAIC(:,7) = 50000.0./(exp((STATES(:,1) - CONSTANTS(:,15))./4.00000)+exp((CONSTANTS(:,15) - STATES(:,1))./4.00000))+1500.00;
    RATES(:,3) = (ALGEBRAIC(:,4) - STATES(:,3))./ALGEBRAIC(:,7);
    ALGEBRAIC(:,2) =  CONSTANTS(:,3).*STATES(:,2).*(STATES(:,1) - CONSTANTS(:,2));
    ALGEBRAIC(:,13) =  (( CONSTANTS(:,17).*power(STATES(:,4), 5.00000))./(power(STATES(:,4), 5.00000)+power(CONSTANTS(:,18), 5.00000))).*(STATES(:,1) - CONSTANTS(:,2));
    ALGEBRAIC(:,6) =  CONSTANTS(:,7).*(STATES(:,1) - CONSTANTS(:,2));
    ALGEBRAIC(:,15) = 1.00000./(1.00000+exp( 1.00000.*(STATES(:,5) - CONSTANTS(:,21))));
    ALGEBRAIC(:,17) =  CONSTANTS(:,19).*ALGEBRAIC(:,15).*(STATES(:,1) - CONSTANTS(:,20));
    ALGEBRAIC(:,8) = 1.00000./(1.00000+exp((CONSTANTS(:,10) - STATES(:,1))./CONSTANTS(:,11)));
    ALGEBRAIC(:,9) =  CONSTANTS(:,9).*ALGEBRAIC(:,8).*(STATES(:,1) - CONSTANTS(:,8));
    ALGEBRAIC(:,10) = 1.00000./(1.00000+exp((CONSTANTS(:,13) - STATES(:,1))./CONSTANTS(:,14)));
    ALGEBRAIC(:,11) =  CONSTANTS(:,12).*ALGEBRAIC(:,10).*(1.00000 - STATES(:,3)).*(STATES(:,1) - CONSTANTS(:,8));
    ALGEBRAIC(:,12) = ALGEBRAIC(:,9)+ALGEBRAIC(:,11);
    ALGEBRAIC(:,19) =  CONSTANTS(:,22).*(STATES(:,1) - CONSTANTS(:,20));
    RATES(:,1) =  - (ALGEBRAIC(:,12)+ALGEBRAIC(:,2)+ALGEBRAIC(:,6)+ALGEBRAIC(:,13)+ALGEBRAIC(:,17)+ALGEBRAIC(:,19))./CONSTANTS(:,1);
    ALGEBRAIC(:,14) = ( CONSTANTS(:,25).*power(STATES(:,4), 2.00000))./(power(STATES(:,4), 2.00000)+power(CONSTANTS(:,24), 2.00000));
    ALGEBRAIC(:,16) = 1.00000./(1.00000+CONSTANTS(:,26)./STATES(:,4));
    ALGEBRAIC(:,18) = 1.00000./(1.00000+STATES(:,4)./CONSTANTS(:,27));
    ALGEBRAIC(:,20) =  power(ALGEBRAIC(:,16), 3.00000).*power(CONSTANTS(:,35), 3.00000).*power(ALGEBRAIC(:,18), 3.00000).*1.00000;
    ALGEBRAIC(:,22) =  ALGEBRAIC(:,20).*(STATES(:,5) - STATES(:,4));
    ALGEBRAIC(:,21) =  CONSTANTS(:,29).*(STATES(:,5) - STATES(:,4));
    ALGEBRAIC(:,23) = (ALGEBRAIC(:,21)+ALGEBRAIC(:,22)) - ALGEBRAIC(:,14);
    RATES(:,5) =  - ALGEBRAIC(:,23)./( CONSTANTS(:,30).*CONSTANTS(:,31));
    ALGEBRAIC(:,24) =   - CONSTANTS(:,34).*( CONSTANTS(:,33).*ALGEBRAIC(:,12)+ CONSTANTS(:,32).*STATES(:,4));
    RATES(:,4) = ALGEBRAIC(:,23)./CONSTANTS(:,30)+ALGEBRAIC(:,24);
   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) = 1.00000./(1.00000+exp((CONSTANTS(:,4) - STATES(:,1))./CONSTANTS(:,5)));
    ALGEBRAIC(:,5) = 9.09000./(1.00000+exp((STATES(:,1) - CONSTANTS(:,4))./CONSTANTS(:,5)));
    ALGEBRAIC(:,4) = 1.00000 - 1.00000./(1.00000+exp((STATES(:,1) - CONSTANTS(:,15))./CONSTANTS(:,16)));
    ALGEBRAIC(:,7) = 50000.0./(exp((STATES(:,1) - CONSTANTS(:,15))./4.00000)+exp((CONSTANTS(:,15) - STATES(:,1))./4.00000))+1500.00;
    ALGEBRAIC(:,2) =  CONSTANTS(:,3).*STATES(:,2).*(STATES(:,1) - CONSTANTS(:,2));
    ALGEBRAIC(:,13) =  (( CONSTANTS(:,17).*power(STATES(:,4), 5.00000))./(power(STATES(:,4), 5.00000)+power(CONSTANTS(:,18), 5.00000))).*(STATES(:,1) - CONSTANTS(:,2));
    ALGEBRAIC(:,6) =  CONSTANTS(:,7).*(STATES(:,1) - CONSTANTS(:,2));
    ALGEBRAIC(:,15) = 1.00000./(1.00000+exp( 1.00000.*(STATES(:,5) - CONSTANTS(:,21))));
    ALGEBRAIC(:,17) =  CONSTANTS(:,19).*ALGEBRAIC(:,15).*(STATES(:,1) - CONSTANTS(:,20));
    ALGEBRAIC(:,8) = 1.00000./(1.00000+exp((CONSTANTS(:,10) - STATES(:,1))./CONSTANTS(:,11)));
    ALGEBRAIC(:,9) =  CONSTANTS(:,9).*ALGEBRAIC(:,8).*(STATES(:,1) - CONSTANTS(:,8));
    ALGEBRAIC(:,10) = 1.00000./(1.00000+exp((CONSTANTS(:,13) - STATES(:,1))./CONSTANTS(:,14)));
    ALGEBRAIC(:,11) =  CONSTANTS(:,12).*ALGEBRAIC(:,10).*(1.00000 - STATES(:,3)).*(STATES(:,1) - CONSTANTS(:,8));
    ALGEBRAIC(:,12) = ALGEBRAIC(:,9)+ALGEBRAIC(:,11);
    ALGEBRAIC(:,19) =  CONSTANTS(:,22).*(STATES(:,1) - CONSTANTS(:,20));
    ALGEBRAIC(:,14) = ( CONSTANTS(:,25).*power(STATES(:,4), 2.00000))./(power(STATES(:,4), 2.00000)+power(CONSTANTS(:,24), 2.00000));
    ALGEBRAIC(:,16) = 1.00000./(1.00000+CONSTANTS(:,26)./STATES(:,4));
    ALGEBRAIC(:,18) = 1.00000./(1.00000+STATES(:,4)./CONSTANTS(:,27));
    ALGEBRAIC(:,20) =  power(ALGEBRAIC(:,16), 3.00000).*power(CONSTANTS(:,35), 3.00000).*power(ALGEBRAIC(:,18), 3.00000).*1.00000;
    ALGEBRAIC(:,22) =  ALGEBRAIC(:,20).*(STATES(:,5) - STATES(:,4));
    ALGEBRAIC(:,21) =  CONSTANTS(:,29).*(STATES(:,5) - STATES(:,4));
    ALGEBRAIC(:,23) = (ALGEBRAIC(:,21)+ALGEBRAIC(:,22)) - ALGEBRAIC(:,14);
    ALGEBRAIC(:,24) =   - CONSTANTS(:,34).*( CONSTANTS(:,33).*ALGEBRAIC(:,12)+ CONSTANTS(:,32).*STATES(:,4));
    ALGEBRAIC(:,3) = 1.00000 - STATES(:,3);
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