%% Discrete Rates Imperfect CSI
pvec = .1:.01:1;
r = [0,1,1.5,2,3,4,4.5];
eta = [0,10.^([5,8,10.5,14,18,20]/10)];
% figure(1)
% stairs(eta,r);
lambda = 1;
w = 0.5;
ghat = 10;
gvar = 1;
df = zeros(size(pvec));
f = df;
for i = 1:length(pvec)
  p = pvec(i);
  CDFvec = [ncx2cdf(eta/p,2,ghat/gvar),1];
  f(i) = sum(w*r.*(CDFvec(2:end)-CDFvec(1:end-1)))-lambda*p;
  df(i) = sum(w/p^2.*eta(2:end).*ncx2pdf(eta(2:end)/p,2,ghat/gvar).*(r(2:end)-...
    r(1:end-1)))-lambda;
end;
figure(2)
hold on
plot(pvec,f,'r')
figure(3)
hold on
plot(pvec,df,'r')
% perfect CSI
pcand = eta/ghat;
[maxf maxfidx] = max(w*r-lambda*eta/ghat);
popt = pcand(maxfidx)

