%svm_softmargin2.m
function [POS2]=svm_softmargin2(th)
load DATA
t=[ones(1,50) ones(1,50)*(-1)];
v=100;
figure 
for i=1:1:50
    plot(DATAtrain(1,i),DATAtrain(2,i),'r*')
    hold on 
end
for i=51:1:100
    plot(DATAtrain(1,i),DATAtrain(2,i),'k*')
    hold on
end
%Formulation of Gram-matrix 
for i=1:1:100 
    for j=1:1:100 
        k(i,j)=innerproduct(DATAtrain(:,i),DATAtrain (:,j))*t(i)*t(j);
    end
end
M=[k;t];
W=pinv(M)*ones(101,1) 
%Selection of langrangean constant greater %than zero (Support vectors) 
[p,q]=find((W>0)&(W<max(W)*th)); 
%b is obtained as follows. 
temp=0; 
for i=1:1:length(p) 
    s1=t(q(i)); 
    s=0; 
    for j=1:1:length(p)
        s=s+k(q(i),q(j))*t(q(j));
    end
    temp=temp+s1-s;
end
temp=temp/length(q);
s=0;
for i=1:1:length(q) 
    s=s+DATAtrain(:,q(i))*W(q(i))*t(q(i));
end
w=s;
b=0; 
for i=1:1:length(p)
    b=b+t(q(i))-w(1)*DATAtrain(1,q(i))+w(2)*DATAtrain (2,q(i))
end
b=b/length(q);
if(isempty(p)==0) 
    x=-2:0.1:2;
    y=(ones(1,length(x))*(-b)-w(1)*x)/w(2);
    hold on
    plot(x,y,'-') 
    %Check for the training set 
    RES=[];
    for i=1:1:100
        s=w(1)*DATAtrain(1,i)+w(2)*DATAtrain(2,i)+b; 
        RES=[RES sign(s)];
    end
    %Checking for the testing test
    RES=[]; 
    for i=1:1:100
        s=w(1)*DATAtest(1,i)+w(2)*DATAtest(2,i)+b;
        RES=[RES sign(s)];
    end
    L1=length(find(RES(1:1:50)==1));
    L2=length(find(RES(51:1:100)==- 1));
    POS2=(L1+L2);
end
if(isempty(p)==1) 
    POS2=0;
end
