function NewOpinions = CB_UpdateOpinion(OldOpinions, AgentParameters, Current_Adj)
% copy the Opinions as they were before
NewOpinions = OldOpinions;


% 'AgentParameters' is a matrix with as many rows as agents and two
% columns: col 1 corresponds to the radical trait, and col 2 to the
% stubborn trait

num_agents = size(AgentParameters,1);

lambda = 0.4;
xi_value = 2;
mu_value = 5;
   
    % Determine the size of each group
    for id_agent = 1:num_agents

        beta = AgentParameters(id_agent,1); % Radical trait
        gamma = AgentParameters(id_agent,2); % Stubborn trait
        alpha = 1-beta - gamma; % Conformist trait

        if  alpha<-1e-10
            disp(['Problem because alpha = ' num2str(alpha) ])
            keyboard
        end

        alpha = max([alpha 0]);

        Amm_set = 0;
        Am_set = 0;
        N_set = 0;
        Dm_set = 0;
        Dmm_set = 0;

        % go through all the row 
        for id_col = 1:num_agents
            if abs(Current_Adj(id_agent, id_col))>0
                % If there is a connection
                Diferencia = OldOpinions(id_agent) - (sign(Current_Adj(id_agent, id_col))*OldOpinions(id_col));

                if Diferencia >= (6/5)
                    % Neighbours that agree much less than agent i
                    Dmm_set = Dmm_set + 1;

                elseif (Diferencia >= (2/5)) && (Diferencia < (6/5))
                    % Neighbours that agree less than agent i
                    Dm_set = Dm_set + 1;

                elseif (Diferencia > (-2/5) ) && (Diferencia < (2/5))
                    % Neighbours that think the same
                    N_set = N_set + 1;

                elseif (Diferencia > (-6/5)) && (Diferencia <= (-2/5))
                    % Neighbours that agree more than agent i
                    Am_set = Am_set + 1;

                elseif Diferencia <= (-6/5)
                    % Neighbours that agree much more than agent i
                    Amm_set = Amm_set + 1;

                else
                    keyboard
                end    
            end
        end

        % Now compute the weights for each category

        % Now compute the opinion change:

        f_CON_1 = Amm_set - Dmm_set;
        f_CON_2 = Am_set - Dm_set;
        f_RAD = N_set*OldOpinions(id_agent);

        temp_sum = (alpha*xi_value*f_CON_1) + (alpha*f_CON_2) + (beta*mu_value*f_RAD);

        delta_x = (  lambda*temp_sum   )/(Amm_set + Am_set + N_set + Dm_set + Dmm_set);

        % now update the opinion 
        new_opinion = OldOpinions(id_agent) + delta_x;
            
        % Implement the sigmoid function
        if abs(new_opinion)>1
            NewOpinions(id_agent) = sign(new_opinion);
        else
            NewOpinions(id_agent) = new_opinion;
        end

        
    end
end