clear, close all,clc
%% =============================
% Training Data Generation
%=============================
randn('state',0)
xr=1.5*randn(200,1)+4;
xi=1.5*randn(200,1)+4;
for k=1:200
    if ((xr(k)-4)^2+(xi(k)-4)^2 >1) & ((xr(k)-4)^2+(xi(k)-4)^2<4)
        xr(k)=4; 
        xi(k)=4;
    end
end
x=[xr xi]';
y=zeros(1,200);
x1=[];
x2=[];
for k=1:200
    if (xr(k)-4)^2+(xi(k)-4)^2 <=1
        y(k)=1;
        x1=[x1,x(:,k)];
    else y(k)=-1;
        x2=[x2,x(:,k)];
    end
end
theta=[0:0.01:2*pi]';
xxx1=4+cos(theta);
yyy1=4+sin(theta);
xxx2=4+4*cos(theta);
yyy2=4+4*sin(theta);
yyy3=4+2*sin(theta);
xxx3=4+2*cos(theta);
figure(1)
plot(x1(1,:),x1(2,:),'*b',x2(1,:),x2(2,:),'*r',...
    xxx1, yyy1,'k',xxx2,yyy2, 'k',xxx3,yyy3,'k')
title('Training data')
%% ==================================
% SVM training --> Support vectors
%==================================
c=0.5;
epsilon = .000001;
kerneloption = 1;
kernel ='gaussian';
verbose = 0;
[xsup,w,b,pos]= svmclass (x',y',c,epsilon,kernel,...
kerneloption, verbose);
figure(2)
plot(xsup(:,1),xsup(:,2),'og',x1(1,:),x1(2,:),...
    '*b',x2(1,:),x2(2,:),'*r',...
    xxx1, yyy1,'k',xxx2,yyy2, 'k',xxx3,yyy3,'k')
title('Training Data plus Support Vectors')
legend('Support vectors','Class 1','Class 2')
%% ==========================
% Testing Data Generation
%==========================
randn('state',1259)
xxr=1.5*randn(200,1)+4;
xxi=1.5*randn(200,1)+4;
for k=1:200
    if ((xxr(k)-4)^2+(xxi(k)-4)^2 >1) & ((xxr(k)-4)^2+(xxi(k)-4)^2<4)
        xxr(k)=4; 
        xxi(k)=4;
    end
end
xx=[xxr xxi]';
%% =============================
% SVM Classification
%=============================
yhat=svmval(xx',xsup,w,b,kernel,kerneloption);
yyhat=sign(yhat);
xx1=[];
xx2=[];
for k=1:200
    if yyhat(k)==1
        xx1=[xx1,xx(:,k)];
    else
        xx2=[xx2,xx(:,k)];
    end
end
figure(3)
plot(xsup(:,1),xsup(:,2),'og',xx1(1,:),xx1(2,:),'*b',...
    xx2(1,:),xx2(2,:),'*r',...
    xxx1, yyy1,'k',xxx2,yyy2, 'k',xxx3,yyy3,'k')
title('Testing Data plus Support Vectors')
legend('Support vectors','Class 1','Class 2')