# Location: BG_GPCR_M2_reduced @ 2574f843444a / BG_GPCR_B1AR_reduced.m

Author:
Shelley Fong <s.fong@auckland.ac.nz>
Date:
2021-12-03 10:35:31+13:00
Desc:
Removing more
Permanent Source URI:
https://models.cellml.org/workspace/7dd/rawfile/2574f843444accce6d81a92c1dbe95c229491b90/BG_GPCR_B1AR_reduced.m

```tic;
[VOI, STATES, ALGEBRAIC, CONSTANTS] = mainFunction();
toc;

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 =66;
end
% There are a total of 23 entries in each of the rate and state variable arrays.
% There are a total of 72 entries in the constant variable array.
%

function [VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel()
[LEGEND_STATES, LEGEND_ALGEBRAIC, legend_VOI, LEGEND_CONSTANTS] = createLegends();
% Create ALGEBRAIC of correct size
global algebraicVariableCount;  algebraicVariableCount = getAlgebraicVariableCount();

% Set timespan to solve over
tspan = [0, 1e-2];

% Set numerical accuracy options for ODE solver
options = odeset('RelTol', 1e-06, 'AbsTol', 1e-06, 'MaxStep', 1e-6);

% extra options for stimuli [ L_B1 ] and other settings
st = struct;
st.stimHolding = 1e-9;
st.stimMag = 0e1;
st.stimSt = 5e-3;
st.tR = 1e-4; % seconds for rAMP start
st.stimDur = 1e-3;
st.m = st.stimMag/st.tR;
st.qR_init = 0.04579000;
st.qGs_init = 1.45;
p = [st.stimHolding, st.stimMag, st.stimSt, st.tR, st.stimDur, st.m,st.qR_init, st.qGs_init];

stlabels = {'stimHolding in component environment (fmol)',...
'stimMag in component environment (fmol)',...
'stimSt in component environment (second)',...
'tR in component environment (second)',...
'stimDur in component environment (second)',...
'm in component environment (fmol_per_sec)'...
'q_RB1_inactive_init in component environment (fmol)',...
'q_Gs_init in component environment (fmol)'};

[i_stp, ~,~] = find_indices(stlabels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));

% Initialise constants and state variables
[INIT_STATES, CONSTANTS] = initConsts(p,i_stp);
% 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);

labels = {'q_LB1 in component environment (fmol)', ...
'q_RB1_inactive in component environment (fmol)'...
'q_RB1 in component environment (fmol)'...
'q_L_RB1_inactive in component environment (fmol)', ...
'q_L_RB1 in component environment (fmol)', ...
'q_L_RB1_Gs in component environment (fmol)', ...
'q_RB1_Gs in component environment (fmol)', ...
'q_RB1_aby in component environment (fmol)',...
'q_L_RB1_aby in component environment (fmol)',...
'q_RB1_abyT in component environment (fmol)',...
'q_L_RB1_abyT in component environment (fmol)',...
'q_Gsa_GTP in component environment (fmol)'};
[~, i_st, i_alg] = find_indices(labels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
if i_st
plot_selected(i_st,VOI,STATES,legend_VOI,LEGEND_STATES,'STATES',ceil(sqrt(length(i_st))))
end
plot_selected(i_alg,VOI,ALGEBRAIC,legend_VOI,LEGEND_ALGEBRAIC,'ALGEBRAIC',ceil(sqrt(length(i_alg))))

% velocities
vlabels = {'v_C_B1 in component GPCR_B1AR (fmol_per_sec)',...
'v_R_B1 in component GPCR_B1AR (fmol_per_sec)',...
'v_L_B1 in component GPCR_B1AR (fmol_per_sec)',...
'v_Doff1_Gs in component GPCR_B1AR (fmol_per_sec)',...
'v_Ton1_Gs in component GPCR_B1AR (fmol_per_sec)',...
'v_Act1_Gs in component GPCR_B1AR (fmol_per_sec)',...
'v_Doff2_Gs in component GPCR_B1AR (fmol_per_sec)',...
'v_Ton2_Gs in component GPCR_B1AR (fmol_per_sec)',...
'v_Act2_Gs in component GPCR_B1AR (fmol_per_sec)',...
'v_Hyd_Gs in component GPCR_B1AR (fmol_per_sec)',...
'v_Reassoc_Gs in component GPCR_B1AR (fmol_per_sec)',...
'v_PiPhos in component GPCR_B1AR (fmol_per_sec)',...
'v_InternRB1 in component GPCR_B1AR (fmol_per_sec)',...
'v_InternL_RB1 in component GPCR_B1AR (fmol_per_sec)'};
[~, ~, i_alg] = find_indices(vlabels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
plot_selected(i_alg,VOI,ALGEBRAIC,legend_VOI,LEGEND_ALGEBRAIC,'REACTION FLUXES',ceil(sqrt(length(i_alg))))

% debug nonincreasing effect of L stim
if true
labels = {'mu_L_RB1 in component GPCR_B1AR (J_per_mol)',...
'mu_Gs in component GPCR_B1AR (J_per_mol)',...
'mu_L_RB1_Gs in component GPCR_B1AR (J_per_mol)',...
'v_R_B1 in component GPCR_B1AR (fmol_per_sec)'};
[~, i_st, i_alg] = find_indices(labels, cellstr(LEGEND_CONSTANTS), cellstr(LEGEND_STATES), cellstr(LEGEND_ALGEBRAIC));
%         plot_selected(i_st,VOI,STATES,legend_VOI,LEGEND_STATES,'STATES',ceil(sqrt(length(i_st))))
plot_selected(i_alg,VOI,ALGEBRAIC,legend_VOI,LEGEND_ALGEBRAIC,'v_R',ceil(sqrt(length(i_alg))))
end
end

function [i_con, i_st, i_alg] = find_indices(labels, LEGEND_CONSTANTS, LEGEND_STATES, LEGEND_ALGEBRAIC)
% return the indices for the selected labels
all_legends = [LEGEND_CONSTANTS; LEGEND_STATES; LEGEND_ALGEBRAIC];

i_con = [];
for i = 1:length(labels)
i_con = [i_con; find(strcmp(labels{i},LEGEND_CONSTANTS))];
end
i_st = [];
for i = 1:length(labels)
i_st = [i_st; find(strcmp(labels{i},LEGEND_STATES))];
end
i_alg = [];
for i = 1:length(labels)
i_alg = [i_alg; find(strcmp(labels{i},LEGEND_ALGEBRAIC))];
end

if length(i_con) + length(i_st) + length(i_alg) < length(labels)
error('missing index');
end
end

function [] = plot_selected(ids,x,y,legend_x,legend_y,titlestr,ns)
istart = 30;
figure();
%     plot stimuli
for i = 1:length(ids)
subplot(ns,ns,i)
plot(x(istart:end), y(istart:end,ids(i)));
xlabel('time (s)');
str = split(legend_y(ids(i),:), ' ');
str = legend_y(ids(i),:);
l = legend(str);
set(l,'interpreter','none');
end
suptitle(titlestr)
end

function [] = plot_2per(i_alg0,id0,ids,x,y0,y,legend_y,legend_y_con,titlestr,ns)
istart = 30;
figure();
%     plot 2 per subplot
if i_alg0
idoffset = length(i_alg0);
for i = 1:idoffset
subplot(ns,ns,i)
plot(x(istart:end), y(istart:end,i_alg0(i)),'x-'); hold on;
plot(x(istart:end), y(istart:end,ids(i)));
xlabel('time (s)');
diff = mean(abs(y(istart:end,i_alg0(i)) - y(istart:end,ids(i))));
str = split(legend_y(i_alg0(i),:),' in');
title([str(1), strcat('avg abs error = ', num2str(diff))]);
%         legend('init','sumspecies');
str1 = split(legend_y(i_alg0(i),:),' ');
str2 = split(legend_y(ids(i),:),' ');
legend(str1(1),str2(1));
end
else
idoffset = 0;
end
for i = (idoffset+1):length(ids)
subplot(ns,ns,i)
plot(x(istart:end), ones(length(x)-istart+1,1)*y0(id0(i-idoffset)),'x-'); hold on;
plot(x(istart:end), y(istart:end,ids(i)));
diff = abs(mean(y(istart:end,ids(i))) - y0(id0(i-idoffset)));
xlabel('time (s)');
str = split(legend_y(ids(i),:),' in');
title([str(1), strcat('avg abs error = ', num2str(diff))]);
legend(legend_y_con(id0(i-idoffset),:),legend_y(ids(i),:));
end
suptitle(strcat(titlestr,' eps = ',num2str(eps)))
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 (second)');
LEGEND_CONSTANTS(:,1) = strpad('q_RB1_inactive_init in component environment (fmol)');
LEGEND_CONSTANTS(:,2) = strpad('q_L_RB1_inactive_init in component environment (fmol)');
LEGEND_ALGEBRAIC(:,1) = strpad('q_LB1_init in component environment (fmol)');
LEGEND_CONSTANTS(:,3) = strpad('q_RB1_init in component environment (fmol)');
LEGEND_CONSTANTS(:,4) = strpad('q_Gs_init in component environment (fmol)');
LEGEND_CONSTANTS(:,5) = strpad('q_RB1_Gs_init in component environment (fmol)');
LEGEND_CONSTANTS(:,6) = strpad('q_L_RB1_init in component environment (fmol)');
LEGEND_CONSTANTS(:,7) = strpad('q_L_RB1_Gs_init in component environment (fmol)');
LEGEND_CONSTANTS(:,8) = strpad('q_Gsa_GTP_init in component environment (fmol)');
LEGEND_CONSTANTS(:,9) = strpad('q_Gsbetagamma_init in component environment (fmol)');
LEGEND_CONSTANTS(:,10) = strpad('q_Gsa_GDP_init in component environment (fmol)');
LEGEND_CONSTANTS(:,11) = strpad('q_Pi_init in component environment (fmol)');
LEGEND_CONSTANTS(:,12) = strpad('q_RB1_aby_init in component environment (fmol)');
LEGEND_CONSTANTS(:,13) = strpad('q_RB1_abyT_init in component environment (fmol)');
LEGEND_CONSTANTS(:,14) = strpad('q_L_RB1_aby_init in component environment (fmol)');
LEGEND_CONSTANTS(:,15) = strpad('q_L_RB1_abyT_init in component environment (fmol)');
LEGEND_CONSTANTS(:,16) = strpad('q_GTP_init in component environment (fmol)');
LEGEND_CONSTANTS(:,17) = strpad('q_GDP_init in component environment (fmol)');
LEGEND_CONSTANTS(:,18) = strpad('q_RB1_tag_init in component environment (fmol)');
LEGEND_CONSTANTS(:,19) = strpad('q_L_RB1_tag_init in component environment (fmol)');
LEGEND_CONSTANTS(:,20) = strpad('q_RB1_GRKArr_init in component environment (fmol)');
LEGEND_CONSTANTS(:,21) = strpad('q_L_RB1_GRKArr_init in component environment (fmol)');
LEGEND_CONSTANTS(:,22) = strpad('q_GRKArr_init in component environment (fmol)');
LEGEND_CONSTANTS(:,23) = strpad('stimSt in component environment (second)');
LEGEND_CONSTANTS(:,24) = strpad('stimDur in component environment (second)');
LEGEND_CONSTANTS(:,25) = strpad('tR in component environment (second)');
LEGEND_CONSTANTS(:,26) = strpad('stimMag in component environment (fmol)');
LEGEND_CONSTANTS(:,27) = strpad('stimHolding in component environment (fmol)');
LEGEND_CONSTANTS(:,71) = strpad('m in component environment (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,2) = strpad('q_RB1_inactive in component environment (fmol)');
LEGEND_ALGEBRAIC(:,3) = strpad('q_L_RB1_inactive in component environment (fmol)');
LEGEND_ALGEBRAIC(:,4) = strpad('q_LB1 in component environment (fmol)');
LEGEND_ALGEBRAIC(:,5) = strpad('q_RB1 in component environment (fmol)');
LEGEND_ALGEBRAIC(:,6) = strpad('q_Gs in component environment (fmol)');
LEGEND_ALGEBRAIC(:,7) = strpad('q_RB1_Gs in component environment (fmol)');
LEGEND_ALGEBRAIC(:,8) = strpad('q_L_RB1 in component environment (fmol)');
LEGEND_ALGEBRAIC(:,9) = strpad('q_L_RB1_Gs in component environment (fmol)');
LEGEND_ALGEBRAIC(:,10) = strpad('q_Gsa_GTP in component environment (fmol)');
LEGEND_ALGEBRAIC(:,11) = strpad('q_Gsbetagamma in component environment (fmol)');
LEGEND_ALGEBRAIC(:,12) = strpad('q_Gsa_GDP in component environment (fmol)');
LEGEND_ALGEBRAIC(:,13) = strpad('q_Pi in component environment (fmol)');
LEGEND_ALGEBRAIC(:,14) = strpad('q_RB1_aby in component environment (fmol)');
LEGEND_ALGEBRAIC(:,15) = strpad('q_RB1_abyT in component environment (fmol)');
LEGEND_ALGEBRAIC(:,16) = strpad('q_L_RB1_aby in component environment (fmol)');
LEGEND_ALGEBRAIC(:,17) = strpad('q_L_RB1_abyT in component environment (fmol)');
LEGEND_ALGEBRAIC(:,19) = strpad('q_GTP in component environment (fmol)');
LEGEND_ALGEBRAIC(:,20) = strpad('q_GDP in component environment (fmol)');
LEGEND_ALGEBRAIC(:,21) = strpad('q_RB1_tag in component environment (fmol)');
LEGEND_ALGEBRAIC(:,22) = strpad('q_L_RB1_tag in component environment (fmol)');
LEGEND_ALGEBRAIC(:,23) = strpad('q_RB1_GRKArr in component environment (fmol)');
LEGEND_ALGEBRAIC(:,24) = strpad('q_L_RB1_GRKArr in component environment (fmol)');
LEGEND_ALGEBRAIC(:,27) = strpad('q_GRKArr in component environment (fmol)');
LEGEND_STATES(:,1) = strpad('q_RB1_inactive in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,2) = strpad('q_L_RB1_inactive in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,3) = strpad('q_LB1 in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,4) = strpad('q_RB1 in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,5) = strpad('q_Gs in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,6) = strpad('q_RB1_Gs in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,7) = strpad('q_L_RB1 in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,8) = strpad('q_L_RB1_Gs in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,9) = strpad('q_Gsa_GTP in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,10) = strpad('q_Gsbetagamma in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,11) = strpad('q_Gsa_GDP in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,12) = strpad('q_Pi in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,13) = strpad('q_RB1_aby in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,14) = strpad('q_RB1_abyT in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,15) = strpad('q_L_RB1_aby in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,16) = strpad('q_L_RB1_abyT in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,17) = strpad('q_GTP in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,18) = strpad('q_GDP in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,19) = strpad('q_RB1_tag in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,20) = strpad('q_L_RB1_tag in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,21) = strpad('q_RB1_GRKArr in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,22) = strpad('q_L_RB1_GRKArr in component GPCR_B1AR (fmol)');
LEGEND_STATES(:,23) = strpad('q_GRKArr in component GPCR_B1AR (fmol)');
LEGEND_ALGEBRAIC(:,25) = strpad('conserv_R_T in component environment (fmol)');
LEGEND_ALGEBRAIC(:,26) = strpad('conserv_L_T in component environment (fmol)');
LEGEND_ALGEBRAIC(:,18) = strpad('conserv_G_T in component environment (fmol)');
LEGEND_CONSTANTS(:,72) = strpad('total_R_init in component environment (fmol)');
LEGEND_CONSTANTS(:,28) = strpad('kappa_Rswitch_B1 in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,29) = strpad('kappa_LRswitch_B1 in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,30) = strpad('kappa_C_B1 in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,31) = strpad('kappa_R_B1 in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,32) = strpad('kappa_L_B1 in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,33) = strpad('kappa_Doff1_Gs in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,34) = strpad('kappa_Ton1_Gs in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,35) = strpad('kappa_Act1_Gs in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,36) = strpad('kappa_Doff2_Gs in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,37) = strpad('kappa_Ton2_Gs in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,38) = strpad('kappa_Act2_Gs in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,39) = strpad('kappa_Hyd_Gs in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,40) = strpad('kappa_Reassoc_Gs in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,41) = strpad('kappa_PiPhos in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,42) = strpad('kappa_InternRB1 in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,43) = strpad('kappa_InternL_RB1 in component GPCR_B1AR_parameters (fmol_per_sec)');
LEGEND_CONSTANTS(:,44) = strpad('K_RB1_inactive in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,45) = strpad('K_L_RB1_inactive in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,46) = strpad('K_LB1 in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,47) = strpad('K_RB1 in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,48) = strpad('K_Gs in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,49) = strpad('K_RB1_Gs in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,50) = strpad('K_L_RB1 in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,51) = strpad('K_L_RB1_Gs in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,52) = strpad('K_Gsa_GTP in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,53) = strpad('K_Gsbetagamma in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,54) = strpad('K_Gsa_GDP in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,55) = strpad('K_Pi in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,56) = strpad('K_RB1_aby in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,57) = strpad('K_RB1_abyT in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,58) = strpad('K_L_RB1_aby in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,59) = strpad('K_L_RB1_abyT in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,60) = strpad('K_GTP in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,61) = strpad('K_GDP in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,62) = strpad('K_RB1_tag in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,63) = strpad('K_L_RB1_tag in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,64) = strpad('K_RB1_GRKArr in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,65) = strpad('K_L_RB1_GRKArr in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,66) = strpad('K_GRKArr in component GPCR_B1AR_parameters (per_fmol)');
LEGEND_CONSTANTS(:,67) = strpad('R in component constants (J_per_K_per_mol)');
LEGEND_CONSTANTS(:,68) = strpad('T in component constants (kelvin)');
LEGEND_ALGEBRAIC(:,28) = strpad('mu_RB1_inactive in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,29) = strpad('mu_L_RB1_inactive in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,30) = strpad('mu_LB1 in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,31) = strpad('mu_RB1 in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,32) = strpad('mu_Gs in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,33) = strpad('mu_RB1_Gs in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,34) = strpad('mu_L_RB1 in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,35) = strpad('mu_L_RB1_Gs in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,36) = strpad('mu_Gsa_GTP in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,37) = strpad('mu_Gsbetagamma in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,38) = strpad('mu_Gsa_GDP in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,39) = strpad('mu_Pi in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,40) = strpad('mu_RB1_aby in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,41) = strpad('mu_RB1_abyT in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,42) = strpad('mu_L_RB1_aby in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,43) = strpad('mu_L_RB1_abyT in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,44) = strpad('mu_GTP in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,45) = strpad('mu_GDP in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,46) = strpad('mu_RB1_tag in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,47) = strpad('mu_L_RB1_tag in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,48) = strpad('mu_RB1_GRKArr in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,49) = strpad('mu_L_RB1_GRKArr in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,50) = strpad('mu_GRKArr in component GPCR_B1AR (J_per_mol)');
LEGEND_ALGEBRAIC(:,51) = strpad('v_Rswitch_B1 in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,52) = strpad('v_LRswitch_B1 in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,53) = strpad('v_C_B1 in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,55) = strpad('v_R_B1 in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,54) = strpad('v_L_B1 in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,56) = strpad('v_Doff1_Gs in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,57) = strpad('v_Ton1_Gs in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,58) = strpad('v_Act1_Gs in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,59) = strpad('v_Doff2_Gs in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,60) = strpad('v_Ton2_Gs in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,61) = strpad('v_Act2_Gs in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,62) = strpad('v_Hyd_Gs in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,64) = strpad('v_Reassoc_Gs in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,65) = strpad('v_PiPhos in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,63) = strpad('v_InternRB1 in component GPCR_B1AR (fmol_per_sec)');
LEGEND_ALGEBRAIC(:,66) = strpad('v_InternL_RB1 in component GPCR_B1AR (fmol_per_sec)');
LEGEND_CONSTANTS(:,69) = strpad('v_Rsynthesis in component GPCR_B1AR (fmol_per_sec)');
LEGEND_CONSTANTS(:,70) = strpad('F in component constants (C_per_mol)');
LEGEND_RATES(:,1) = strpad('d/dt q_RB1_inactive in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,2) = strpad('d/dt q_L_RB1_inactive in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,3) = strpad('d/dt q_LB1 in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,4) = strpad('d/dt q_RB1 in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,5) = strpad('d/dt q_Gs in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,6) = strpad('d/dt q_RB1_Gs in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,7) = strpad('d/dt q_L_RB1 in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,8) = strpad('d/dt q_L_RB1_Gs in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,9) = strpad('d/dt q_Gsa_GTP in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,10) = strpad('d/dt q_Gsbetagamma in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,11) = strpad('d/dt q_Gsa_GDP in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,12) = strpad('d/dt q_Pi in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,13) = strpad('d/dt q_RB1_aby in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,14) = strpad('d/dt q_RB1_abyT in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,15) = strpad('d/dt q_L_RB1_aby in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,16) = strpad('d/dt q_L_RB1_abyT in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,17) = strpad('d/dt q_GTP in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,18) = strpad('d/dt q_GDP in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,19) = strpad('d/dt q_RB1_tag in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,20) = strpad('d/dt q_L_RB1_tag in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,21) = strpad('d/dt q_RB1_GRKArr in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,22) = strpad('d/dt q_L_RB1_GRKArr in component GPCR_B1AR (fmol)');
LEGEND_RATES(:,23) = strpad('d/dt q_GRKArr in component GPCR_B1AR (fmol)');
LEGEND_STATES  = LEGEND_STATES';
LEGEND_ALGEBRAIC = LEGEND_ALGEBRAIC';
LEGEND_RATES = LEGEND_RATES';
LEGEND_CONSTANTS = LEGEND_CONSTANTS';
end

function [STATES, CONSTANTS] = initConsts(p,i_stp)
VOI = 0; CONSTANTS = []; STATES = []; ALGEBRAIC = [];
CONSTANTS(:,1) = 0.0004579000;
CONSTANTS(:,2) = 1e-18;
CONSTANTS(:,3) = 1e-18;
CONSTANTS(:,4) = 0.1455400000;
CONSTANTS(:,5) = 1e-18;
CONSTANTS(:,6) = 1e-18;
CONSTANTS(:,7) = 1e-18;
CONSTANTS(:,8) = 0.02;
CONSTANTS(:,9) = 0.02;
CONSTANTS(:,10) = 0.02;
CONSTANTS(:,11) = 570;
CONSTANTS(:,12) = 1e-18;
CONSTANTS(:,13) = 1e-18;
CONSTANTS(:,14) = 1e-18;
CONSTANTS(:,15) = 1e-18;
CONSTANTS(:,16) = 2.2;
CONSTANTS(:,17) = 1.1;
CONSTANTS(:,18) = 1e-18;
CONSTANTS(:,19) = 1e-18;
CONSTANTS(:,20) = 1e-18;
CONSTANTS(:,21) = 1e-18;
CONSTANTS(:,22) = 1;
CONSTANTS(:,23) = 1e-5;
CONSTANTS(:,24) = 5e-5;
CONSTANTS(:,25) = 5e-5;
CONSTANTS(:,26) = 1e-1;
CONSTANTS(:,27) = 1e-8;
STATES(:,1) = 1e-16;
STATES(:,2) = 1e-16;
STATES(:,3) = 1e-16;
STATES(:,4) = 1e-16;
STATES(:,5) = 1e-16;
STATES(:,6) = 1e-16;
STATES(:,7) = 1e-16;
STATES(:,8) = 1e-16;
STATES(:,9) = 1e-16;
STATES(:,10) = 1e-16;
STATES(:,11) = 1e-16;
STATES(:,12) = 1e-16;
STATES(:,13) = 1e-16;
STATES(:,14) = 1e-16;
STATES(:,15) = 1e-16;
STATES(:,16) = 1e-16;
STATES(:,17) = 1e-16;
STATES(:,18) = 1e-16;
STATES(:,19) = 1e-16;
STATES(:,20) = 1e-16;
STATES(:,21) = 1e-16;
STATES(:,22) = 1e-16;
STATES(:,23) = 1e-16;
CONSTANTS(:,28) = 2.78295e-08;
CONSTANTS(:,29) = 2.29349e-13;
CONSTANTS(:,30) = 0.00124719;
CONSTANTS(:,31) = 0.0102784;
CONSTANTS(:,32) = 6.53644e-14;
CONSTANTS(:,33) = 3.77936e-05;
CONSTANTS(:,34) = 1.89861;
CONSTANTS(:,35) = 303777;
CONSTANTS(:,36) = 0.16578;
CONSTANTS(:,37) = 8328.15;
CONSTANTS(:,38) = 1.3325e+09;
CONSTANTS(:,39) = 5.22927e-08;
CONSTANTS(:,40) = 4.48153;
CONSTANTS(:,41) = 2.39883e-05;
CONSTANTS(:,42) = 232.462;
CONSTANTS(:,43) = 1.01968e+06;
CONSTANTS(:,44) = 1.04457e+10;
CONSTANTS(:,45) = 1.26749e+15;
CONSTANTS(:,46) = 12376.7;
CONSTANTS(:,47) = 1.04457e+08;
CONSTANTS(:,48) = 6.48657e-05;
CONSTANTS(:,49) = 7.69172e+06;
CONSTANTS(:,50) = 1.26749e+07;
CONSTANTS(:,51) = 1753.52;
CONSTANTS(:,52) = 444724;
CONSTANTS(:,53) = 8.17005e+06;
CONSTANTS(:,54) = 2.79265e-08;
CONSTANTS(:,55) = 5.78662e+09;
CONSTANTS(:,56) = 36.7288;
CONSTANTS(:,57) = 1.53111e-06;
CONSTANTS(:,58) = 0.00837323;
CONSTANTS(:,59) = 3.49054e-10;
CONSTANTS(:,60) = 0.121183;
CONSTANTS(:,61) = 6.08777e-05;
CONSTANTS(:,62) = 2.22564e-25;
CONSTANTS(:,63) = 5.07389e-29;
CONSTANTS(:,64) = 1.25052e-08;
CONSTANTS(:,65) = 2.85086e-12;
CONSTANTS(:,66) = 1.63334e+23;
CONSTANTS(:,67) = 8.31;
CONSTANTS(:,68) = 310;
CONSTANTS(:,69) = 1e-2;
CONSTANTS(:,70) = 96485;
CONSTANTS(:,71) = CONSTANTS(:,26)./CONSTANTS(:,25);
CONSTANTS(:,72) = CONSTANTS(:,1)+CONSTANTS(:,2)+CONSTANTS(:,3)+CONSTANTS(:,5)+CONSTANTS(:,6)+CONSTANTS(:,7)+CONSTANTS(:,12)+CONSTANTS(:,13)+CONSTANTS(:,14)+CONSTANTS(:,15)+CONSTANTS(:,18)+CONSTANTS(:,19)+CONSTANTS(:,20)+CONSTANTS(:,21);
% modify values at indives i_stp with values stored in struct p
for i = 1:length(i_stp)
CONSTANTS(:,i_stp(i)) = p(i);
end

% HACK
if false
disp('HACKILY INCREASING KAPPA BY 1E6');
for i=26:34
CONSTANTS(:,i) = CONSTANTS(:,i)*1e6;
end
end
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(:,2) = STATES(:,1)+CONSTANTS(:,1);
ALGEBRAIC(:,28) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,44).*ALGEBRAIC(:,2));
ALGEBRAIC(:,5) = STATES(:,4)+CONSTANTS(:,3);
ALGEBRAIC(:,31) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,47).*ALGEBRAIC(:,5));
ALGEBRAIC(:,51) =  CONSTANTS(:,28).*(exp(ALGEBRAIC(:,28)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,31)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,3) = STATES(:,2)+CONSTANTS(:,2);
ALGEBRAIC(:,29) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,45).*ALGEBRAIC(:,3));
ALGEBRAIC(:,1) = piecewise({VOI<CONSTANTS(:,23)&VOI>CONSTANTS(:,23) - CONSTANTS(:,25), CONSTANTS(:,27)+ CONSTANTS(:,71).*((VOI - CONSTANTS(:,23))+CONSTANTS(:,25)) , VOI>=CONSTANTS(:,23)&VOI<CONSTANTS(:,23)+CONSTANTS(:,24), CONSTANTS(:,26)+CONSTANTS(:,27) , VOI<CONSTANTS(:,23)+CONSTANTS(:,25)+CONSTANTS(:,24)&VOI>=CONSTANTS(:,23)+CONSTANTS(:,24), CONSTANTS(:,27)+  - CONSTANTS(:,71).*(((VOI - CONSTANTS(:,23)) - CONSTANTS(:,25)) - CONSTANTS(:,24)) }, CONSTANTS(:,27));
ALGEBRAIC(:,4) = STATES(:,3)+ALGEBRAIC(:,1);
ALGEBRAIC(:,30) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,46).*ALGEBRAIC(:,4));
ALGEBRAIC(:,54) =  CONSTANTS(:,32).*(exp((ALGEBRAIC(:,28)+ALGEBRAIC(:,30))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,29)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,1) =  - ALGEBRAIC(:,51) - ALGEBRAIC(:,54);
ALGEBRAIC(:,8) = STATES(:,7)+CONSTANTS(:,6);
ALGEBRAIC(:,34) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,50).*ALGEBRAIC(:,8));
ALGEBRAIC(:,52) =  CONSTANTS(:,29).*(exp(ALGEBRAIC(:,29)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,34)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,2) = ALGEBRAIC(:,54) - ALGEBRAIC(:,52);
RATES(:,3) =  - ALGEBRAIC(:,54);
ALGEBRAIC(:,6) = STATES(:,5)+CONSTANTS(:,4);
ALGEBRAIC(:,32) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,48).*ALGEBRAIC(:,6));
ALGEBRAIC(:,7) = STATES(:,6)+CONSTANTS(:,5);
ALGEBRAIC(:,33) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,49).*ALGEBRAIC(:,7));
ALGEBRAIC(:,53) =  CONSTANTS(:,30).*(exp((ALGEBRAIC(:,31)+ALGEBRAIC(:,32))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,33)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,4) = ALGEBRAIC(:,51) - ALGEBRAIC(:,53);
ALGEBRAIC(:,9) = STATES(:,8)+CONSTANTS(:,7);
ALGEBRAIC(:,35) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,51).*ALGEBRAIC(:,9));
ALGEBRAIC(:,55) =  CONSTANTS(:,31).*(exp((ALGEBRAIC(:,34)+ALGEBRAIC(:,32))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,35)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,7) =  - ALGEBRAIC(:,55)+ALGEBRAIC(:,52);
RATES(:,8) = ALGEBRAIC(:,55);
ALGEBRAIC(:,14) = STATES(:,13)+CONSTANTS(:,12);
ALGEBRAIC(:,40) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,56).*ALGEBRAIC(:,14));
ALGEBRAIC(:,20) = STATES(:,18)+CONSTANTS(:,17);
ALGEBRAIC(:,45) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,61).*ALGEBRAIC(:,20));
ALGEBRAIC(:,56) =  CONSTANTS(:,33).*(exp(ALGEBRAIC(:,33)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,40)+ALGEBRAIC(:,45))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,6) = ALGEBRAIC(:,53) - ALGEBRAIC(:,56);
ALGEBRAIC(:,15) = STATES(:,14)+CONSTANTS(:,13);
ALGEBRAIC(:,41) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,57).*ALGEBRAIC(:,15));
ALGEBRAIC(:,19) = STATES(:,17)+CONSTANTS(:,16);
ALGEBRAIC(:,44) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,60).*ALGEBRAIC(:,19));
ALGEBRAIC(:,57) =  CONSTANTS(:,34).*(exp((ALGEBRAIC(:,40)+ALGEBRAIC(:,44))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,41)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,13) = ALGEBRAIC(:,56) - ALGEBRAIC(:,57);
ALGEBRAIC(:,10) = STATES(:,9)+CONSTANTS(:,8);
ALGEBRAIC(:,36) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,52).*ALGEBRAIC(:,10));
ALGEBRAIC(:,11) = STATES(:,10)+CONSTANTS(:,9);
ALGEBRAIC(:,37) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,53).*ALGEBRAIC(:,11));
ALGEBRAIC(:,21) = STATES(:,19)+CONSTANTS(:,18);
ALGEBRAIC(:,46) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,62).*ALGEBRAIC(:,21));
ALGEBRAIC(:,58) =  CONSTANTS(:,35).*(exp(ALGEBRAIC(:,41)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,36)+ALGEBRAIC(:,37)+ALGEBRAIC(:,46))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,14) = ALGEBRAIC(:,57) - ALGEBRAIC(:,58);
ALGEBRAIC(:,16) = STATES(:,15)+CONSTANTS(:,14);
ALGEBRAIC(:,42) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,58).*ALGEBRAIC(:,16));
ALGEBRAIC(:,59) =  CONSTANTS(:,36).*(exp(ALGEBRAIC(:,35)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,42)+ALGEBRAIC(:,45))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,17) = STATES(:,16)+CONSTANTS(:,15);
ALGEBRAIC(:,43) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,59).*ALGEBRAIC(:,17));
ALGEBRAIC(:,60) =  CONSTANTS(:,37).*(exp((ALGEBRAIC(:,42)+ALGEBRAIC(:,44))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,43)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,15) = ALGEBRAIC(:,59) - ALGEBRAIC(:,60);
ALGEBRAIC(:,22) = STATES(:,20)+CONSTANTS(:,19);
ALGEBRAIC(:,47) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,63).*ALGEBRAIC(:,22));
ALGEBRAIC(:,61) =  CONSTANTS(:,38).*(exp(ALGEBRAIC(:,43)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,36)+ALGEBRAIC(:,37)+ALGEBRAIC(:,47))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,16) = ALGEBRAIC(:,60) - ALGEBRAIC(:,61);
ALGEBRAIC(:,12) = STATES(:,11)+CONSTANTS(:,10);
ALGEBRAIC(:,38) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,54).*ALGEBRAIC(:,12));
ALGEBRAIC(:,13) = STATES(:,12)+CONSTANTS(:,11);
ALGEBRAIC(:,39) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,55).*ALGEBRAIC(:,13));
ALGEBRAIC(:,62) =  CONSTANTS(:,39).*(exp(ALGEBRAIC(:,36)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,38)+ALGEBRAIC(:,39))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,9) = (ALGEBRAIC(:,58)+ALGEBRAIC(:,61)) - ALGEBRAIC(:,62);
ALGEBRAIC(:,23) = STATES(:,21)+CONSTANTS(:,20);
ALGEBRAIC(:,48) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,64).*ALGEBRAIC(:,23));
ALGEBRAIC(:,27) = STATES(:,23)+CONSTANTS(:,22);
ALGEBRAIC(:,50) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,66).*ALGEBRAIC(:,27));
ALGEBRAIC(:,63) =  1.00000.*CONSTANTS(:,42).*(exp((ALGEBRAIC(:,46)+ALGEBRAIC(:,50))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,48)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,19) = ALGEBRAIC(:,58) - ALGEBRAIC(:,63);
RATES(:,21) = ALGEBRAIC(:,63);
ALGEBRAIC(:,64) =  CONSTANTS(:,40).*(exp((ALGEBRAIC(:,38)+ALGEBRAIC(:,37))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,32)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,5) = ( - ALGEBRAIC(:,53) - ALGEBRAIC(:,55))+ALGEBRAIC(:,64);
RATES(:,10) = (ALGEBRAIC(:,58)+ALGEBRAIC(:,61)) - ALGEBRAIC(:,64);
RATES(:,11) = ALGEBRAIC(:,62) - ALGEBRAIC(:,64);
ALGEBRAIC(:,65) =  CONSTANTS(:,41).*(exp((ALGEBRAIC(:,45)+ALGEBRAIC(:,39))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,44)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,12) = ALGEBRAIC(:,62) - ALGEBRAIC(:,65);
RATES(:,17) = ( - ALGEBRAIC(:,57) - ALGEBRAIC(:,60))+ALGEBRAIC(:,65);
RATES(:,18) = (ALGEBRAIC(:,56)+ALGEBRAIC(:,59)) - ALGEBRAIC(:,65);
ALGEBRAIC(:,24) = STATES(:,22)+CONSTANTS(:,21);
ALGEBRAIC(:,49) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,65).*ALGEBRAIC(:,24));
ALGEBRAIC(:,66) =  1.00000.*CONSTANTS(:,43).*(exp((ALGEBRAIC(:,47)+ALGEBRAIC(:,50))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,49)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
RATES(:,20) = ALGEBRAIC(:,61) - ALGEBRAIC(:,66);
RATES(:,22) = ALGEBRAIC(:,66);
RATES(:,23) =  - ALGEBRAIC(:,63) - ALGEBRAIC(:,66);
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(:,2) = STATES(:,1)+CONSTANTS(:,1);
ALGEBRAIC(:,28) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,44).*ALGEBRAIC(:,2));
ALGEBRAIC(:,5) = STATES(:,4)+CONSTANTS(:,3);
ALGEBRAIC(:,31) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,47).*ALGEBRAIC(:,5));
ALGEBRAIC(:,51) =  CONSTANTS(:,28).*(exp(ALGEBRAIC(:,28)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,31)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,3) = STATES(:,2)+CONSTANTS(:,2);
ALGEBRAIC(:,29) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,45).*ALGEBRAIC(:,3));
ALGEBRAIC(:,1) = piecewise({VOI<CONSTANTS(:,23)&VOI>CONSTANTS(:,23) - CONSTANTS(:,25), CONSTANTS(:,27)+ CONSTANTS(:,71).*((VOI - CONSTANTS(:,23))+CONSTANTS(:,25)) , VOI>=CONSTANTS(:,23)&VOI<CONSTANTS(:,23)+CONSTANTS(:,24), CONSTANTS(:,26)+CONSTANTS(:,27) , VOI<CONSTANTS(:,23)+CONSTANTS(:,25)+CONSTANTS(:,24)&VOI>=CONSTANTS(:,23)+CONSTANTS(:,24), CONSTANTS(:,27)+  - CONSTANTS(:,71).*(((VOI - CONSTANTS(:,23)) - CONSTANTS(:,25)) - CONSTANTS(:,24)) }, CONSTANTS(:,27));
ALGEBRAIC(:,4) = STATES(:,3)+ALGEBRAIC(:,1);
ALGEBRAIC(:,30) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,46).*ALGEBRAIC(:,4));
ALGEBRAIC(:,54) =  CONSTANTS(:,32).*(exp((ALGEBRAIC(:,28)+ALGEBRAIC(:,30))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,29)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,8) = STATES(:,7)+CONSTANTS(:,6);
ALGEBRAIC(:,34) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,50).*ALGEBRAIC(:,8));
ALGEBRAIC(:,52) =  CONSTANTS(:,29).*(exp(ALGEBRAIC(:,29)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,34)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,6) = STATES(:,5)+CONSTANTS(:,4);
ALGEBRAIC(:,32) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,48).*ALGEBRAIC(:,6));
ALGEBRAIC(:,7) = STATES(:,6)+CONSTANTS(:,5);
ALGEBRAIC(:,33) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,49).*ALGEBRAIC(:,7));
ALGEBRAIC(:,53) =  CONSTANTS(:,30).*(exp((ALGEBRAIC(:,31)+ALGEBRAIC(:,32))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,33)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,9) = STATES(:,8)+CONSTANTS(:,7);
ALGEBRAIC(:,35) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,51).*ALGEBRAIC(:,9));
ALGEBRAIC(:,55) =  CONSTANTS(:,31).*(exp((ALGEBRAIC(:,34)+ALGEBRAIC(:,32))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,35)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,14) = STATES(:,13)+CONSTANTS(:,12);
ALGEBRAIC(:,40) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,56).*ALGEBRAIC(:,14));
ALGEBRAIC(:,20) = STATES(:,18)+CONSTANTS(:,17);
ALGEBRAIC(:,45) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,61).*ALGEBRAIC(:,20));
ALGEBRAIC(:,56) =  CONSTANTS(:,33).*(exp(ALGEBRAIC(:,33)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,40)+ALGEBRAIC(:,45))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,15) = STATES(:,14)+CONSTANTS(:,13);
ALGEBRAIC(:,41) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,57).*ALGEBRAIC(:,15));
ALGEBRAIC(:,19) = STATES(:,17)+CONSTANTS(:,16);
ALGEBRAIC(:,44) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,60).*ALGEBRAIC(:,19));
ALGEBRAIC(:,57) =  CONSTANTS(:,34).*(exp((ALGEBRAIC(:,40)+ALGEBRAIC(:,44))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,41)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,10) = STATES(:,9)+CONSTANTS(:,8);
ALGEBRAIC(:,36) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,52).*ALGEBRAIC(:,10));
ALGEBRAIC(:,11) = STATES(:,10)+CONSTANTS(:,9);
ALGEBRAIC(:,37) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,53).*ALGEBRAIC(:,11));
ALGEBRAIC(:,21) = STATES(:,19)+CONSTANTS(:,18);
ALGEBRAIC(:,46) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,62).*ALGEBRAIC(:,21));
ALGEBRAIC(:,58) =  CONSTANTS(:,35).*(exp(ALGEBRAIC(:,41)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,36)+ALGEBRAIC(:,37)+ALGEBRAIC(:,46))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,16) = STATES(:,15)+CONSTANTS(:,14);
ALGEBRAIC(:,42) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,58).*ALGEBRAIC(:,16));
ALGEBRAIC(:,59) =  CONSTANTS(:,36).*(exp(ALGEBRAIC(:,35)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,42)+ALGEBRAIC(:,45))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,17) = STATES(:,16)+CONSTANTS(:,15);
ALGEBRAIC(:,43) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,59).*ALGEBRAIC(:,17));
ALGEBRAIC(:,60) =  CONSTANTS(:,37).*(exp((ALGEBRAIC(:,42)+ALGEBRAIC(:,44))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,43)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,22) = STATES(:,20)+CONSTANTS(:,19);
ALGEBRAIC(:,47) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,63).*ALGEBRAIC(:,22));
ALGEBRAIC(:,61) =  CONSTANTS(:,38).*(exp(ALGEBRAIC(:,43)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,36)+ALGEBRAIC(:,37)+ALGEBRAIC(:,47))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,12) = STATES(:,11)+CONSTANTS(:,10);
ALGEBRAIC(:,38) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,54).*ALGEBRAIC(:,12));
ALGEBRAIC(:,13) = STATES(:,12)+CONSTANTS(:,11);
ALGEBRAIC(:,39) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,55).*ALGEBRAIC(:,13));
ALGEBRAIC(:,62) =  CONSTANTS(:,39).*(exp(ALGEBRAIC(:,36)./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp((ALGEBRAIC(:,38)+ALGEBRAIC(:,39))./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,23) = STATES(:,21)+CONSTANTS(:,20);
ALGEBRAIC(:,48) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,64).*ALGEBRAIC(:,23));
ALGEBRAIC(:,27) = STATES(:,23)+CONSTANTS(:,22);
ALGEBRAIC(:,50) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,66).*ALGEBRAIC(:,27));
ALGEBRAIC(:,63) =  1.00000.*CONSTANTS(:,42).*(exp((ALGEBRAIC(:,46)+ALGEBRAIC(:,50))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,48)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,64) =  CONSTANTS(:,40).*(exp((ALGEBRAIC(:,38)+ALGEBRAIC(:,37))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,32)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,65) =  CONSTANTS(:,41).*(exp((ALGEBRAIC(:,45)+ALGEBRAIC(:,39))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,44)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,24) = STATES(:,22)+CONSTANTS(:,21);
ALGEBRAIC(:,49) =  CONSTANTS(:,67).*CONSTANTS(:,68).*log( CONSTANTS(:,65).*ALGEBRAIC(:,24));
ALGEBRAIC(:,66) =  1.00000.*CONSTANTS(:,43).*(exp((ALGEBRAIC(:,47)+ALGEBRAIC(:,50))./( CONSTANTS(:,67).*CONSTANTS(:,68))) - exp(ALGEBRAIC(:,49)./( CONSTANTS(:,67).*CONSTANTS(:,68))));
ALGEBRAIC(:,18) = ALGEBRAIC(:,6)+ALGEBRAIC(:,7)+ALGEBRAIC(:,9)+ALGEBRAIC(:,10)+ALGEBRAIC(:,12)+ALGEBRAIC(:,14)+ALGEBRAIC(:,15)+ALGEBRAIC(:,16)+ALGEBRAIC(:,17);
ALGEBRAIC(:,25) = ALGEBRAIC(:,2)+ALGEBRAIC(:,3)+ALGEBRAIC(:,5)+ALGEBRAIC(:,7)+ALGEBRAIC(:,8)+ALGEBRAIC(:,9)+ALGEBRAIC(:,14)+ALGEBRAIC(:,15)+ALGEBRAIC(:,16)+ALGEBRAIC(:,17)+ALGEBRAIC(:,21)+ALGEBRAIC(:,22)+ALGEBRAIC(:,23)+ALGEBRAIC(:,24);
ALGEBRAIC(:,26) = ALGEBRAIC(:,3)+ALGEBRAIC(:,4)+ALGEBRAIC(:,8)+ALGEBRAIC(:,9)+ALGEBRAIC(:,16)+ALGEBRAIC(:,17)+ALGEBRAIC(:,22)+ALGEBRAIC(:,24);
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