x=[1.0 0.0
   0.0 1.0
   0.0 0.0
   0.5 0.5];
labels=[1 1  -1 1];
%phi_x*phi_x'
phi_x = x;
labels_mat=labels'*labels;
H=phi_x*phi_x';
H=labels_mat.*H;

f=labels*0-1;
A=eye(size(phi_x,1))*-1;
b=zeros([1 size(phi_x,1)]);
Aeq=labels;
beq=0;
LB = b*0-20;
UB = b*0+20;
%LB=[]; UB=[];
options.LargeScale='off';
[alphas]=quadprog(H,f,A,b,Aeq,beq,LB,UB,[],options);
w = alphas(1)*labels(1)*phi_x(1,:);
for ii=2:size(phi_x,1),
  w=w+alphas(ii)*labels(ii)*phi_x(ii,:);
end
[dummy, alpha_max] = max(alphas);
%we need to solve for w0
w0 = 1/labels(alpha_max)-w*phi_x(alpha_max,:)';
%We check the result is 1 or -1 for each of the support vector
for ii=1:size(phi_x,1),
  if alphas(ii) > 1.0E-6,
     fprintf(1,'Pattern %d is a support vector with alpha=%12.10f,\n', ii, alphas(ii));
     fprintf(1,'    the output is %12.10f, which should be %12.10f.\n', ...
         w*phi_x(ii,:)'+w0, labels(ii));
   end
end
for ii=1:size(phi_x,1),
  res = labels(ii) * (w*phi_x(ii,:)'+w0);
  if res > 0 ,
    fprintf(1,'Pattern %d is classified correctly.\n', ii);
  else
    fprintf(1,'Pattern %d is classified incorrectly.\n',ii);
  end
end



