openExample('stats/CreateAndVisualizeDiscriminantAnalysisClassifierExample')
%
%  Load the iris data from the file that is opened in 
%   the Matlab example
%
load fisheriris.mat
SL=meas(:,1);
SW=meas(:,2);
%
% do a scatter plot of sepal length and width
%
h1 = gscatter(SL,SW,species,'krb','ov^',[],'off');
h1(1).LineWidth = 2;
h1(2).LineWidth = 2;
h1(3).LineWidth = 2;
legend('Setosa','Versicolor','Virginica','Location','best')
hold on
%
%  save the sepal length and width data and do a linear 
%   discriminant analysis and graph it
%
X = [SL,SW];
MdlLinear = fitcdiscr(X,species);
MdlLinear.ClassNames([2 3])
K = MdlLinear.Coeffs(2,3).Const;  
L = MdlLinear.Coeffs(2,3).Linear;
f = @(x1,x2) K + L(1)*x1 + L(2)*x2;
h2=fimplicit(f,[5.8 7 1.5 4.5]);
h2.Color = 'r';
h2.LineWidth = 2;
h2.DisplayName = 'Boundary between Versicolor & Virginica';
%
% add to the graph the other boundary between species using 
%  a second linear discriminant analysis
%
K = MdlLinear.Coeffs(1,2).Const;
L = MdlLinear.Coeffs(1,2).Linear;
f = @(x1,x2) K + L(1)*x1 + L(2)*x2;
h3 = fimplicit(f,[ 4 6 1.5 4.5]);
h3.Color = 'k';
h3.LineWidth = 2;
h3.DisplayName = 'Boundary between Versicolor & Setosa';
%
xlabel('Sepal Length')
ylabel('Sepal Width')
title('{\bf Linear Classification with Fisher Training Data}')
%
% now do a quadratic discriminant analysis
% and graph it on the redrawn scatter plot
%
hold off
figure
h1 = gscatter(SL,SW,species,'krb','ov^',[],'off');
h1(1).LineWidth = 2;
h1(2).LineWidth = 2;
h1(3).LineWidth = 2;
legend('Setosa','Versicolor','Virginica','Location','best')
hold on
MdlQuadratic = fitcdiscr(X,species,'DiscrimType','quadratic');
MdlQuadratic.ClassNames([2 3])
K = MdlQuadratic.Coeffs(2,3).Const;
L = MdlQuadratic.Coeffs(2,3).Linear; 
Q = MdlQuadratic.Coeffs(2,3).Quadratic;
f = @(x1,x2) K + L(1)*x1 + L(2)*x2 + Q(1,1)*x1.^2 + ...
    (Q(1,2)+Q(2,1))*x1.*x2 + Q(2,2)*x2.^2;
h2 = fimplicit(f,[4.5 7.5 2 4 ] );
h2.Color = 'r';
h2.LineWidth = 2;
h2.DisplayName = 'Boundary between Versicolor & Virginica';
%   
%  now do the second boundary of the quadratic discriminant
%
MdlQuadratic.ClassNames([1 2])
K = MdlQuadratic.Coeffs(1,2).Const;
L = MdlQuadratic.Coeffs(1,2).Linear; 
Q = MdlQuadratic.Coeffs(1,2).Quadratic;
f = @(x1,x2) K + L(1)*x1 + L(2)*x2 + Q(1,1)*x1.^2 + ...
    (Q(1,2)+Q(2,1))*x1.*x2 + Q(2,2)*x2.^2;
h3 = fimplicit(f,[4 6.5 2 4.5]);
h3.Color = 'k';
h3.LineWidth = 2;
h3.DisplayName = 'Boundary between Versicolor & Setosa';
xlabel('Petal Length')
ylabel('Petal Width')
title('{\bf Quadratic Classification with Fisher Training Data}')

