close all
clear all

% part 3, plot the data set
X = [2,7; 3,6; 2,5; 3,5; 3,3; 2,2; 5,1; 6,2; 8,1; 6,4; 4,8; 5,8; 9,5; 9,9; 9,4; 8,9; 8,8; 6,9; 7,4; 4,4];
Y = [ +1;  +1;  +1;  +1;  +1;  +1;  +1;  +1;  +1;  +1;  -1;  -1;  -1;  -1;  -1;  -1;  -1;  -1;  -1;  -1];
x1ran = [0,10]; x2ran = [0,10]; % data range
f1 = figure; plotdata(X, Y, x1ran, x2ran);
title('Data from class +1 (squares) and class -1 (crosses)');

% part 4, plot the SVM using linear kernel, C=100
net = svm(size(X, 2), 'linear', [ ], 100);
net = svmtrain(net, X, Y, [ ], 2);
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with linear kernel: decision boundary (black) plus Support' ...
       ' Vectors (red)']);

% part 5, investigate the change of number of support vectors and the
% margin length when varying C
net = svm(size(X, 2), 'linear', [ ], 1e10);
net = svmtrain(net, X, Y, [ ], 2);
f3 = figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with linear kernel, C=1e10: decision boundary (black) plus Support Vectors (red)']);
net
d=2/sqrt(net.normalw(1)^2+net.normalw(2)^2)
pause;

net = svm(size(X, 2), 'linear', [ ], 1e100);
net = svmtrain(net, X, Y, [ ], 2);
f3 = figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with linear kernel, C=1e100: decision boundary (black) plus Support Vectors (red)']);
net
d=2/sqrt(net.normalw(1)^2+net.normalw(2)^2)
pause;

net = svm(size(X, 2), 'linear', [ ], inf);
net = svmtrain(net, X, Y, [ ], 2);
f3 = figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with linear kernel, C=inf: decision boundary (black) plus Support Vectors (red)']);
net
d=2/sqrt(net.normalw(1)^2+net.normalw(2)^2)
