function [User, BS, decreasement, curr_obj] = Find_Newton_Direction(User,Cells,BS,Sim,Beta,t)

%find the amount of backhaul for compression for each BS
for c = 1:Sim.Num_Cell
    for b = 1:Sim.Num_BS
        BS(c,b).curr_data_sharing_bkhaul = 0;
        
        for row_idx = 1:size(BS(c,b).data_sharing_users,1)
            cell_idx = BS(c,b).data_sharing_users(row_idx,1);
            usr_idx = BS(c,b).data_sharing_users(row_idx,2);
            
            BS(c,b).curr_data_sharing_bkhaul = BS(c,b).curr_data_sharing_bkhaul + User(cell_idx,usr_idx).current_rate;
        end
    end
end


%the decreasemet of the log barrier function,i.e. (derivative of f)^(T) * (delta x)
%for backtrack line search use
decreasement = 0;

%the objective value under current values of R_k and backhaul for
%compression
curr_obj = 0;

%find the derative w.r.t. user rate
for c = 1:Sim.Num_Cell
    for ik = 1:length(Cells(c).Scheduled_User)
        k = Cells(c).Scheduled_User(ik);
        
        rx_quantization_noise = 0;
        for cc = 1:Sim.Num_Cell
            for bb = 1:Sim.Num_BS
                
                rx_quantization_noise = rx_quantization_noise + Beta(cc,bb,c,k) / (2^BS(cc,bb).current_compression_bkhaul - 1);
                
            end
        end
        User(c,k).curr_sinr = User(c,k).signal_u_k / (User(c,k).intf_plus_noise_v_k + rx_quantization_noise);
        
        User(c,k).curr_rate_derative = 1 / (log2(1 + User(c,k).curr_sinr) - User(c,k).current_rate); %1st derative
        User(c,k).curr_rate_derative2 = ( User(c,k).curr_rate_derative )^2; %2nd derative
        
        curr_obj = curr_obj - User(c,k).wgt_wsr * User(c,k).current_rate - log( log2(1 + User(c,k).curr_sinr) - User(c,k).current_rate ) / t;
        
        for row_idx = 1:size(User(c,k).data_sharing_BSs,1)
            cell_idx = User(c,k).data_sharing_BSs(row_idx,1);
            BS_idx = User(c,k).data_sharing_BSs(row_idx,2);
            
            temp = BS(cell_idx,BS_idx).total_bkhaul_budget - BS(cell_idx,BS_idx).current_compression_bkhaul - BS(cell_idx,BS_idx).curr_data_sharing_bkhaul;
            
            User(c,k).curr_rate_derative = User(c,k).curr_rate_derative + 1/temp;
            User(c,k).curr_rate_derative2 = User(c,k).curr_rate_derative2 + 1/temp^2;
            
        end
        
        User(c,k).curr_rate_derative = - User(c,k).wgt_wsr + User(c,k).curr_rate_derative / t;
        User(c,k).curr_rate_derative2 = User(c,k).curr_rate_derative2 / t;
        
        if User(c,k).curr_rate_derative2 <0
            fprintf('$$$$ Hessian is Less Than 0 $$$$\n');
            return;
        end
                
        %%%%%%%%% Newton's direction for user rate  %%%%%%%%%%%%%%%%
        %User(c,k).curr_rate_newton = - User(c,k).curr_rate_derative / User(c,k).curr_rate_derative2;
        User(c,k).curr_rate_newton = - User(c,k).curr_rate_derative;
        decreasement = decreasement + User(c,k).curr_rate_derative * User(c,k).curr_rate_newton;

        %preparation for the derative w.r.t. the backhaul for compression
        for cc = 1:Sim.Num_Cell
            for bb = 1:Sim.Num_BS
                
                User(c,k).log_derative_to_compression_backhaul(cc,bb) = ...
                    (User(c,k).curr_sinr)^2 / (1 + User(c,k).curr_sinr) * Beta(cc,bb,c,k) / User(c,k).signal_u_k * ...
                    (1/(2^BS(cc,bb).current_compression_bkhaul -1) + 1/(2^BS(cc,bb).current_compression_bkhaul -1)^2);
                
                User(c,k).log_derative_to_compression_backhaul2(cc,bb) = ...
                    ((User(c,k).curr_sinr)^2 - (User(c,k).curr_sinr /(1 + User(c,k).curr_sinr))^2) * log(2) * ...
                    ( Beta(cc,bb,c,k) / User(c,k).signal_u_k * ( 1/(2^BS(cc,bb).current_compression_bkhaul -1) + 1/(2^BS(cc,bb).current_compression_bkhaul -1)^2 ) )^2 ...
                    + (User(c,k).curr_sinr)^2 / (1 + User(c,k).curr_sinr) * Beta(cc,bb,c,k) / User(c,k).signal_u_k * log(2) * ...
                    (-2^BS(cc,bb).current_compression_bkhaul/(2^BS(cc,bb).current_compression_bkhaul -1)^2 - 2 * 2^BS(cc,bb).current_compression_bkhaul/(2^BS(cc,bb).current_compression_bkhaul -1)^3);
                
            end
        end        
    end
end


%find the derative w.r.t. the backhaul for compression for each BS
for c = 1:Sim.Num_Cell
    for b = 1:Sim.Num_BS
        
        temp = BS(c,b).total_bkhaul_budget - BS(c,b).current_compression_bkhaul - BS(c,b).curr_data_sharing_bkhaul;        
        
        curr_obj = curr_obj - log(temp) / t;
        
        BS(c,b).curr_bkhaul_derative = -1/temp;
        BS(c,b).curr_bkhaul_derative2 = 1/temp^2;
        
        for cc = 1:Sim.Num_Cell
            for ik = 1:length(Cells(cc).Scheduled_User)
                k = Cells(cc).Scheduled_User(ik);
                
                delta_rate = log2(1 + User(cc,k).curr_sinr) - User(cc,k).current_rate;
                
                BS(c,b).curr_bkhaul_derative = BS(c,b).curr_bkhaul_derative + User(cc,k).log_derative_to_compression_backhaul(c,b) / delta_rate;
                
                BS(c,b).curr_bkhaul_derative2 = BS(c,b).curr_bkhaul_derative2 + (User(cc,k).log_derative_to_compression_backhaul(c,b) / delta_rate)^2 ...
                    - User(cc,k).log_derative_to_compression_backhaul2(c,b) / delta_rate;
                
            end
        end
        
        BS(c,b).curr_bkhaul_derative = - BS(c,b).curr_bkhaul_derative / t;
        BS(c,b).curr_bkhaul_derative2 = BS(c,b).curr_bkhaul_derative2 / t;
        
        if BS(c,b).curr_bkhaul_derative2 <0
            fprintf('$$$$ Hessian is Less Than 0 $$$$\n');
            return;
        end
                
        %%%%%%%%%% Newton's direction for backhaul for compression
        %BS(c,b).curr_bkhaul_newton = - BS(c,b).curr_bkhaul_derative / BS(c,b).curr_bkhaul_derative2;
        BS(c,b).curr_bkhaul_newton = - BS(c,b).curr_bkhaul_derative;
        
        decreasement = decreasement + BS(c,b).curr_bkhaul_derative * BS(c,b).curr_bkhaul_newton;
    end
end