function [Gbest,FG] = Particle_Swarm_Optimization (Func_name,N,Lower,Upper,Maxiter)


fobj = TestFunctions (Func_name); 
D=length(Lower);
X=zeros(N,D);     
V=zeros(N,D);    
Pbest=zeros(N,D); 
fitness=zeros(1,N);
FP=zeros(1,N);
c1=2.0;
c2=2.0;
Gbest=zeros(1,D);


for i=1:N
  for j=1:D
    X(i,j)=Lower(j)+(Upper(j)-Lower(j))*rand;
    Pbest(i,j)=X(i,j);
    V(i,j)=Lower(j)+(Upper(j)-Lower(j))*rand;
  end
  fitness(i)=fobj(X(i,:));
  FP(i)=fitness(i);
end

[minval,mindex]=min(FP);
Gbest=Pbest(mindex,:);
FG=minval;

iter=1;

while (iter <= Maxiter)
  
    w=( 0.1+(0.6-0.1)*( ((Maxiter-iter)/Maxiter)) );
    for i=1:N
         for j=1:D
          V(i,j)=w*V(i,j)+c1*rand*(Pbest(i,j)-X(i,j))+c2*rand*(Gbest(j)-X(i,j));
          X(i,j)=X(i,j)+V(i,j);
         end 
    end  

    X = boundary (X, Lower,Upper,N,D); 
    
    for i=1:N
      fitness(i)=fobj(X(i,:));
      if(fitness(i)<FP(i))
        Pbest(i,:)=X(i,:);
        FP(i)=fitness(i);
      end
      if (FP(i)<FG)
        Gbest=Pbest(i,:);
         FG=FP(i);
      end  
    end

    iter=iter+1;    
end  


endfunction
