load svm_training;
[n p] = size(X);
C = 100;

% Build and execute model.
%
% We introduce nonnegative variables s(i) for the maximum of 0 and
% 1-y(i)*(w'x(i)-w0).

% cvx_begin
%     variable w(p)
%     variable w0
%     variable s(n)
%     minimize ( C*sum(s)/n + dot(w,w) )
%     s >= 0
%     s >= 1 - y.*(X*w-w0)
% cvx_end

% We use the vector x = [w,w0,s] of unknowns and solve the above QP
% directly in MATLAB.
c = [zeros(p,1); 0; ones(n,1)]*C/n; % linear costs for s
h = [ones(p,1); 0; zeros(n,1)];     % quadratic costs for w
H = 2*sparse(diag(h));
A = [-diag(y)*X, y, -eye(n)]; % constr -diag(y)*X*w + y*w0 - s <= -1
rhs = -ones(n,1);
lb = [-inf*ones(p,1); -inf; zeros(n,1)]; % lower bounds on s

x = quadprog(H,c,A,rhs,[],[],lb);
w = x(1:p);
w0 = x(p+1);
s = x(p+2:end);

% Plot points and hyperplane.
svmplot(X,y);
b = (w0-100*w(1))/w(2);
hold on;
plot([0,100],[w0/norm(w),b],'k');
hold off;
