function [rate_pow] = Rate_function_approx_for_power_alloc(A,Q,P_r,P_r1,M,N,K,rho,w,H,sigma2)
%UNTITLED10 Summary of this function goes here
%   Detailed explanation goes here

% Note that Pr is the previous power
% Pr1 is the P vector at r+1 instance

% calculation of condition for power allocation 
channel = zeros(N,K,M) ;

my_log2_for_cvx = @(x) log(x)/log(2); 
for n = 1:N
    for k = 1 : K
        for m = 1: M
            channel(n,k,m) =    rho /(H.^2 + norm(Q(n,:,m)-w(k,:),2).^2)  ;
        end
    end
end
% Calculation for the R_ub (upper bound rate approximation) 

for k = 1:K 
    if isnumeric(P_r1)
        rate_pow(k) = 0 ;
    else
        rate_pow(k) = cvx(0) ;
    end
    for n = 1:N
        D_const_1 = (channel(n,k,1)*log(exp(1)))/(P_r(n,1)*channel(n,k,2) +sigma2);
        D_const_2 = (channel(n,k,2)*log(exp(1)))/(P_r(n,2)*channel(n,k,1) +sigma2);
        R_hat_2 = D_const_1*(P_r1(n,1)-P_r(n,1))+log2(P_r(n,1)*channel(n,k,1)+sigma2);
        R_hat_1 = D_const_2*(P_r1(n,2)-P_r(n,2))+log2(P_r(n,2)*channel(n,k,2)+sigma2);
        I = my_log2_for_cvx(P_r1(n,1)*channel(n,k,1) + P_r1(n,2)*channel(n,k,2)+sigma2);
        rate_pow(k) = rate_pow(k) + (1/N)*A(n,k,1)*(I-R_hat_1) + (1/N)*A(n,k,2)*(I-R_hat_2);
    end
end




end

