function frf = compute_fr_flows(densities, flows, celldata, outflow, ts)
% COMPUTE_FR_FLOWS - compute off-ramp flow values from the vector of flows
% and off-ramp split-ratio data.
%
% Call: frf = compute_fr_flows(densities, flows, celldata, outflow, ts)
%
% Parameters:
% densities - vector of densities;
% flows - vector of flows, must have the same size as 'densities';
% celldata - array of freeway cell data structures, whose length
% must be the same as the size of 'flows' vector;
% outflow - how much can be allowed through the right boundary;
% ts - sampling period.
%
%
% Returns: frf - array of off-ramp flows.
%
% Last modified: 09/14/2006.
%
% Alex Kurzhanskiy <akurzhan@eecs.berkeley.edu>
%
M = size(flows, 1);
N = size(celldata, 2); % number of cells
if M ~= N
error('COMPUTE_FR_FLOWS: number of cells does not match number of flows.');
end
frf = [];
for i = 1:N
if i == N
f = get_rbflow(densities(N, 1), flows(N, 1), celldata(N), outflow, ts);
f = celldata(N).FRbeta * f;
else
if celldata(i).FRbeta < 1
f = (celldata(i).FRbeta / (1 - celldata(i).FRbeta)) * flows(i+1, 1);
else
l = abs(celldata(i).PMend - celldata(i).PMstart); % cell length
v = celldata(i).FDfmax / celldata(i).FDrhocrit; % free-flow speed
n = densities(i, 1) * l; % number of vehicles
r = celldata(i).ORgamma * celldata(i).ORflow * ts; % number of vehicles from on-ramp
f = (v/l) * (n + r);
end
end
frf = [frf; min([celldata(i).FRfmax f])];
end
return;