%psokurtosis.m
%Particle Swarm Optimization to minimize 
%the absolute value of the kurtosis 
%Initializing 100 particles 
load x 
x=x-repmat(mean(x')',1,length(x)); 
figure 
subplot(1,2,1)
plot(x(1,:),x(2,:),'r*')
VAL1=[];
VAL2=[];
for i=1:1:2
W=rand(2,2); 
W(:,1)=W(:,1)/sqrt(sum(W(:,1).^2));
W(:,2)=W(:,2)-(W(:,2)'*W(:,1))*W(:,1);
W(:,2)=W(:,2)/sqrt(sum(W(:,2).^2));
W_init{i}=W; 
VAL1=[VAL1 objfun(W,x)];
W=rand(2,2); 
W(:,1)=W(:,1)/sqrt(sum(W(:,1).^2));
W(:,2)=W(:,2)-(W(:,2)'*W(:,1))*W(:,1);
W(:,2)=W(:,2)/sqrt(sum(W(:,2).^2));
W_next{i}=W;
VAL2=[VAL2 objfun(W,x)];
end
BESTVAL=[];
BESTSOL=[];
for iter=1:1:10
    [P,Q]=min(VAL2);
    [P1,Q1]=min(VAL1);
    BESTVAL=[BESTVAL VAL1(Q1)];
    BESTSOL{iter}=W_init{Q1};
    %Global and the Actual movement 
    VAL3=[]; 
    for i=1:1:2 
        W=W_init{i}+0.9*(W_next{i}-W_init{i})+0.9*(W_next{Q}-W_init{i});
        W(:,1)=W(:,1)/sqrt(sum(W(:,1).^2)); 
        W(:,2)=W(:,2)-(W(:,2)'*W(:,1))*W(:,1);
        W(:,2)=W(:,2)/sqrt(sum(W(:,2).^2));
        W_move{i}=W; VAL3=[VAL3 objfun(W,x)]; 
    end
for i=1:1:2 
    if(VAL3(i)<VAL2(i)) 
        W_next{i}=W_move{i}; 
    end
    W_init{i}=W_move{i};
end
VAL1=[];
VAL2=[];
VAL3=[];
for i=1:1:2 
    W=W_init{i};
    W(:,1)=W(:,1)/sqrt(sum(W(:,1).^2));
    W(:,2)=W(:,2)-(W(:,2)'*W(:,1))*W(:,1); 
    W(:,2)=W(:,2)/sqrt(sum(W(:,2).^2));
    W_init{i}=W;
    VAL1=[VAL1 objfun(W,x)];
    W=W_next{i};
    W(:,1)=W(:,1)/sqrt(sum(W(:,1).^2));
    W(:,2)=W(:,2)-(W(:,2)'*W(:,1))*W(:,1);
    W(:,2)=W(:,2)/sqrt(sum(W(:,2).^2));
    W_next{i}=W; 
    VAL2=[VAL2 objfun(W,x)];
end
end
[U,V]=min(BESTVAL);
pdata=BESTSOL{V}*x; 
subplot(1,2,2) 
plot(pdata(1,:),pdata(2,:),'r*') 
figure
plot(BESTVAL)



