function der = compute_derative(User,Cells,BS,Sim,Beta,t,c2,k2,l2,b2)

% %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
% 
% 
% 
% %find the derative w.r.t. user rate
% 
% 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,c2,k2) / (2^BS(cc,bb).current_compression_bkhaul - 1);
%         
%     end
% end
% User(c2,k2).curr_sinr = User(c2,k2).signal_u_k / (User(c2,k2).intf_plus_noise_v_k + rx_quantization_noise);
% 
% User(c2,k2).curr_rate_derative = 1 / (log2(1 + User(c2,k2).curr_sinr) - User(c2,k2).current_rate); %1st derative
% 
% 
% for row_idx = 1:size(User(c2,k2).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;
%     
% end
% 
% der = - User(c2,k2).wgt_wsr + User(c2,k2).curr_rate_derative / t;


%preparation for the derative w.r.t. the backhaul for compression
for c = 1:Sim.Num_Cell
    for ik = 1:length(Cells(c).Scheduled_User)
        k = Cells(c).Scheduled_User(ik);
        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


temp = BS(l2,b2).total_bkhaul_budget - BS(l2,b2).current_compression_bkhaul - BS(l2,b2).curr_data_sharing_bkhaul;

BS(l2,b2).curr_bkhaul_derative = -1/temp;

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(l2,b2).curr_bkhaul_derative = BS(l2,b2).curr_bkhaul_derative + User(cc,k).log_derative_to_compression_backhaul(l2,b2) / delta_rate;
        
        
    end
end

der = - BS(l2,b2).curr_bkhaul_derative / t;
%der = User(c2,k2).log_derative_to_compression_backhaul(l2,b2);

