close all;
clear all;

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

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

% part 3, plot the SVMs using linear kernel, C=inf, 1e10, 100
% data range=[-30,30],[-200,200],[-1e11,1e11],respectively
net = svm(size(X, 2), 'linear', [ ], inf);
net = svmtrain(net, X, Y, [ ], 2);
x1ran = [-10,10]*3; x2ran = [-10,10]*3; % data range
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)
pause;

net = svm(size(X, 2), 'linear', [ ], 1e10);
net = svmtrain(net, X, Y, [ ], 2);
x1ran = [-10,10]*20; x2ran = [-10,10]*20; % data range
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', [ ], 100);
net = svmtrain(net, X, Y, [ ], 2);
x1ran = [-10,10]*1e10; x2ran = [-10,10]*1e10; % data range
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with linear kernel, C=100: decision boundary (black) plus Support Vectors (red)']);
net
d=2/sqrt(net.normalw(1)^2+net.normalw(2)^2)

% part 4, plot the SVM using RBF kernel, kernelpar=16
net = svm(size(X, 2), 'rbf', [16], 100);
net = svmtrain(net, X, Y, [ ], 2);
x1ran = [0,10]; x2ran = [0,10]; % data range
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with RBF kernel, kernelpar=16: decision boundary (black) plus Support Vectors (red)']);

%part 5, plot the SVM using RBF kernel, kernelpar=16
net = svm(size(X, 2), 'rbf', [1], 100);
net = svmtrain(net, X, Y, [ ], 2);
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with RBF kernel, kernelpar=1: decision boundary (black) plus Support Vectors (red)']);
pause;

net = svm(size(X, 2), 'rbf', [10], 100);
net = svmtrain(net, X, Y, [ ], 2);
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with RBF kernel, kernelpar=10: decision boundary (black) plus Support Vectors (red)']);
pause;

net = svm(size(X, 2), 'rbf', [70], 100);
net = svmtrain(net, X, Y, [ ], 2);
x1ran = [0,10]; x2ran = [0,10]; % data range
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with RBF kernel, kernelpar=70: decision boundary (black) plus Support Vectors (red)']);

% part 6, plot the SVM using RBF kernel, changing with C
net = svm(size(X, 2), 'rbf', [70], 500);
net = svmtrain(net, X, Y, [ ], 2);
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with RBF kernel, C=500: decision boundary (black) plus Support Vectors (red)']);
pause;

net = svm(size(X, 2), 'rbf', [70], inf);
net = svmtrain(net, X, Y, [ ], 2);
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with RBF kernel, C=inf: decision boundary (black) plus Support Vectors (red)']);

% part 7. Use the polynomial kernel
net = svm(size(X, 2), 'poly', 2, 100);
net = svmtrain(net, X, Y, [ ], 2);
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with Poly kernel, Degree 2, C=100: decision boundary (black) plus Support Vectors (red)']);
pause

net = svm(size(X, 2), 'poly', 2, 5);
net = svmtrain(net, X, Y, [ ], 2);
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with Poly kernel, Degree 2, C=5: decision boundary (black) plus Support Vectors (red)']);

% part 8, we can increase the kernelpar to 4 in order compensate small C=5
net = svm(size(X, 2), 'poly', 4, 5);
net = svmtrain(net, X, Y, [ ], 2);
figure; plotboundary(net, x1ran, x2ran);
plotdata(X, Y, x1ran, x2ran); plotsv(net, X, Y);
title(['SVM with Poly kernel, Degree 4, C=5: decision boundary (black) plus Support Vectors (red)']);
