Page 18 - Algorithm Collections for Digital Signal Processing Applications using MATLAB
P. 18

6                                                           Chapter 1

           1.3      M – program for PSO Algorithm




           psogv .m


           function [value]=psogv(fun,range,ITER)
           %psogv.m
           %Particle swarm algorithm for maximizing the function fun with two variables x
           %and y.
           %Syntax
           %[value]=psogv(fun,range,ITER)
           %example
           %fun='f1'
           %create the function fun.m
           %function [res]=fun(x,y)
           %res=sin(x)+cos(x);
           %range=[-pi pi;-pi pi];
           %ITER is the total number of Iteration
           error=[];
           vel1=[];
           vel2=[];

           %Intialize the swarm position
           swarm=[];
           x(1)=rand*range(1,2)+range(1,1);
           y(1)=rand*range(2,2)+range(2,1);
           x(2)=rand*range(1,2)+range(1,1);
           y(2)=rand*range(2,2)+range(2,1);

           %Intialize weight
           w=1;
           c1=2;
           c2=2;
           %Initialize the velocity
           v1=0;%velocity for x
           v2=0;%velocity for y
           for i=1:1:ITER
           [p,q]=min([f1(fun,x(2),y(1)) f1(fun,x(1),y(2))]);
           if (q==1)
               capture=x(2);
           else
               capture=y(2);
           end
   13   14   15   16   17   18   19   20   21   22   23