%% Train SVM Classifiers Using a Gaussian Kernel

%% 
% Plot the points, and plot circles of radii 1 and 2 for comparison.
load ('datasvm.mat')

figure;
plot(data1(:,1),data1(:,2),'r.')
hold on
plot(data2(:,1),data2(:,2),'b.')
axis equal
ezpolar(@(x)1);ezpolar(@(x)2);
hold off
%% 
% Put the data in one matrix, and make a vector of classifications.

data3 = [data1;data2];
theclass = [class1;class2];
% theclass = ones(200,1);
% theclass(1:100) = -1;
%% 
% Train an SVM classifier with |KernelFunction| set to |'rbf'| and |BoxConstraint| 
% set to |Inf|. Plot the decision boundary and flag the support vectors.

%Train the SVM Classifier
cl = fitcsvm(data3,theclass,'KernelFunction','rbf','BoxConstraint',Inf,'ClassNames',[-1,1]);

% Predict scores over the grid
d = 0.02;
[x1Grid,x2Grid] = meshgrid(min(data3(:,1)):d:max(data3(:,1)),min(data3(:,2)):d:max(data3(:,2)));
xGrid = [x1Grid(:),x2Grid(:)];
[~,scores] = predict(cl,xGrid);

% Plot the data and the decision boundary
figure;
h(1:2) = gscatter(data3(:,1),data3(:,2),theclass,'rb','.');
hold on
ezpolar(@(x)1);
h(3) = plot(data3(cl.IsSupportVector,1),data3(cl.IsSupportVector,2),'ko');
contour(x1Grid,x2Grid,reshape(scores(:,2),size(x1Grid)),[0 0],'k');
legend(h,{'-1','+1','Support Vectors'});
axis equal
hold off
%% 
% |fitcsvm| generates a classifier that is close to a circle of radius 1. The 
% difference is due to the random training data.
%% 
% Training with the default parameters makes a more nearly circular classification 
% boundary, but one that misclassifies some training data. Also, the default value 
% of |BoxConstraint| is |1|, and, therefore, there are more support vectors.

cl2 = fitcsvm(data3,theclass,'KernelFunction','rbf');
[~,scores2] = predict(cl2,xGrid);

figure;
h(1:2) = gscatter(data3(:,1),data3(:,2),theclass,'rb','.');
hold on
ezpolar(@(x)1);
h(3) = plot(data3(cl2.IsSupportVector,1),data3(cl2.IsSupportVector,2),'ko');
contour(x1Grid,x2Grid,reshape(scores2(:,2),size(x1Grid)),[0 0],'k');
legend(h,{'-1','+1','Support Vectors'});
axis equal
hold off