%% Training a SVM model for two-class classification
% These solution hints will only help you get started. Extending to other kernel
% functions and implementing grid search is left for you to do.
%
% Note: The function SVMTRAIN is deprecated, so we use FITCSVM instead.
% FITCSVM does not seem to have a built-in plotting function, so we must
% draw the decision boundaries manually.


%% Load data and train model
%load mynormaldistdataset.mat % A, a
load mybananadataset.mat    % B, b

SVMModel = fitcsvm(A, a, 'BoxConstraint', 5, 'KernelFunction','rbf')

%% Make a plot showing the decision boundary
% Begin by making a grid of values
x = linspace(-12,8);
y = linspace(-10,6);
[XX,YY] = meshgrid(x,y);

% Define the decision boundary function | LINEAR
%f = @(x) -(x*SVMModel.Beta(1) + SVMModel.Bias)/SVMModel.Beta(2);
%y = f(x);

% Get the support vectors
sv = SVMModel.SupportVectors;

% Get the predictions on a grid to show the areas of the classes
[prediction,score] = predict(SVMModel,[XX(:),YY(:)]);
scoreGrid = reshape(prediction,size(XX,1),size(YY,2));

% Plot everything
figure
% this is a surf plot, it is 3D 
surf(XX,YY,scoreGrid,'FaceAlpha',0.5) 
view(0,-90)
hold on

gscatter(A(:,1),A(:,2),a)

plot(sv(:,1),sv(:,2),'go','MarkerSize',10)
legend('This is a 3D plot','class 1','class 2','support vectors')
hold off


