function[T,lambda] = con_water_filling(r, P_max,tol)
P = -1;
lambda_lo = 0;
lambda_hi = 10000000;
T = max(lambda_lo- 1./r, 0);
itr = 0;
while abs(P_max-P) > tol || P>P_max
    itr = itr+1;
    if itr >=1000
        fprintf('debug, SNR = %f\n', r);
        break
    end
    lambda = (lambda_lo+lambda_hi)/2;
    T = max(lambda- 1./r, 0);
    P = sum(T);
    if P>=P_max
        lambda_hi = lambda;
    else
        lambda_lo = lambda;
    end
    
end

