% Testing analytical discrete rate perfect CSI case
warning('off','MATLAB:divideByZero')
R = 0:2:6;
BER = 1e-3;
Rext = [-inf,R,R(end)];
Eta = -log(5*BER)/1.6*(2.^R-1);
Etaext = [0,Eta,inf];
Slopes = (Rext(2:end)-Rext(1:end-1))./(Etaext(2:end)-Etaext(1:end-1));
gvec = linspace(0,10,1001);
gbar = 10^(20/10);
w = 1;
l = .3;
Fg = zeros(size(gvec));
fg = zeros(size(gvec));
for i = 1 : length(gvec)
  Fg(i) = lmaxcdf3(gvec(i),l,w,gbar,Etaext,Rext);
  fg(i) = lmaxpdf3vec(gvec(i),l,w,gbar,Etaext,Rext);
end;
figure(1)
plot(gvec,Fg,'bo-',gvec,fg,'rx--')
tot = quad(@(x) lmaxpdf3vec(x,l,w,gbar,Etaext,Rext), 0, 10,1e-15)
hold on
% Use monte carlo to test pdfs
N = 1000000;
G = exprnd(gbar,1,N);
maxl = max(repmat(w*R',1,N)-l*Eta'*(1./G));
[H] = histc(maxl,gvec);
dx = gvec(2)-gvec(1);
bar(gvec,H/(N*dx),'histc')
% for i = 1 : N
%   if mod(i,1000)==0
%     i
%   end
%   g = exprnd
% end

