% Function to compute rates of change of demand and supply functions
%
% two state variables in the system
% p(1) = L(t) = L(t-1) + [demand(t) - supply(t)] (dt)	% queue length 
% p(2) = A(t) = A(t-1) + [L(t)] (dt)					% area under L(t) curve

function pprime = fqueue_deterministic_mod(t,p)

global demand capacity time


% Define the rate equations

demand_table 	= interp1(time,demand,t);
capacity_table 	  = interp1(time,capacity,t);

if (demand_table < capacity_table) & (p(1) > 0)
	pprime(1) = demand_table - capacity_table;	% rate of change in state variable
elseif demand_table > capacity_table
	pprime(1) = demand_table - capacity_table;	% rate of change in state variable
else
	pprime(1) = 0.0;				% avoids accumulation of entities if queue length is zero
end

pprime(2) = p(1);					% integrates the delay curve over time

% Enhancements to calculate cumulative demand and cumulative capacity
pprime(3) = demand_table;           % acumulates the demand table

% p(4) = cumulative demand curve
% p(5) = cumulative supply curve

if demand_table <= capacity_table & (p(1) <= 0)
	pprime(4) = demand_table ;	% rate of change in demand
    pprime(5) = demand_table ;	% rate of change in supply equals demand since supply > demand
else  %  & (p(1) > 0)
    pprime(4) = demand_table ;	% rate of change in demand
    pprime(5) = capacity_table ;	% rate of change in supply
end

pprime = pprime';