
function a=GSA_Gfield(M,X,G,Rnorm,Rpower,ElitistCheck,iteration,max_it)


[N,dim]=size(X);
 final_per=2; %In the last iteration, only 2 percent of agents apply force to the others.

%%%%total force calculation
 if ElitistCheck==1
     kbest=final_per+(1-iteration/max_it)*(100-final_per); %kbest in eq. 21.
     kbest=round(N*kbest/100);
 else
     kbest=N; %eq.9.
 end
    [Ms, ds]=sort(M,'descend');
	    if size(ds)== 1
        ds=ones(N,1);
        end
 E = zeros(N,dim);
 for i=1:N
    
     for ii=1:kbest
         j=ds(ii);
         if j~=i
            R=norm(X(i,:)-X(j,:),Rnorm); %Euclidian distanse.
          
             E(i,:)=E(i,:)+rand(1,dim).*(M(j)).*((X(j,:)-X(i,:))./(R^Rpower+eps));%eps存在的意义是防止两物体靠在一起分母为0
              %note that Mp(i)/Mi(i)=1 
         
         end
     end
 end

%%acceleration
a=E.*G; %note that Mp(i)/Mi(i)=1


