function [eta,P_r1] = power_optimization(Pmax,A,Q,P,M,N,K,rho,w,H,sigma2)
%This Function implementes the 3rd step in the suggested algorithim where
%it performs the power control optimization 
% P_r1 is the power is the new optimum power step P is the power at the previous iteration 

P_r1 = P ; % pre-allocation 
% 
% problem = createOptimProblem('fmincon','x0',P,...
%     'objective', @(P_r1) -min(Rate_Calculation(SINR_calculation(Q,P_r1,w,rho,H,sigma2,M,K,N),A,K,M,N)) ... 
%                          ,'lb',zeros(size(P_r1)),'ub',Pmax*ones(size(P_r1)));
% gs = GlobalSearch('Display','off') ; 
% P_r1 = run(gs,problem);
% 
% eta = min(Rate_Calculation(SINR_calculation(Q,P_r1,w,rho,H,sigma2,M,K,N),A,K,M,N));

                     
                     
                     
cvx_begin quiet

    variable eta
    variable P_r1(N,M) % new power
    
    maximize eta 
    
    subject to 
    
    Rate_function_approx_for_power_alloc(A,Q,P,P_r1,M,N,K,rho,w,H,sigma2) >= eta; 
    
    0 <= P_r1 <= Pmax;
    

cvx_end

end

