%% 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','linear');

%% Make a plot showing the decision boundary
% Begin by making a grid of values
x = linspace(-5,5);
y = linspace(-5,5);
[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;

% Plot everything
gscatter(A(:,1),A(:,2),a)
hold on
plot(x,y,'g--','LineWidth',2,'DisplayName','Boundary')
plot(sv(:,1),sv(:,2),'ko','MarkerSize',10)
legend('class 1','class 2', 'decision boundary','support vectors')
hold off
