%% Defuzzification Methods
% This example describes the built-in methods for defuzzifying the output fuzzy set of a type-1 Mamdani fuzzy inference system.
%
% Consider the following output fuzzy set, which is an aggregation of three scaled trapezoidal membership functions.

x = 0:0.1:20;

mf1 = trapmf(x,[0 2 8 12]);
mf2 = trapmf(x,[5 7 12 14]);
mf3 = trapmf(x,[12 13 18 19]);
mf = max(0.5*mf2,max(0.9*mf1,0.1*mf3));

figure('Tag','defuzz')
plot(x,mf,'LineWidth',3)
h_gca = gca;
h_gca.YTick = [0 .5 1] ;
ylim([-1 1])

% Fuzzy Logic Toolbox™ software supports five built-in methods for computing a single crisp output value for such a fuzzy set.
% Centroid
% Bisector
% Middle of maximum
% Smallest of maximum
% Largest of maximum
% You can also define your own custom defuzzification method. For more information, see Build Fuzzy Systems Using Custom Functions.
%
%% Centroid
% Centroid defuzzification returns the center of gravity of the fuzzy set along the x-axis. If you think of the area as a plate with uniform thickness and density, the centroid is the point along the x-axis about which the fuzzy set would balance. The centroid is computed using the following formula, where \mu \left(x_i \right) is the membership value for point x_i in the universe of discourse.
% \textrm{xCentroid}=\frac{\sum_i \mu \left(x_i \right)x_i }{\sum_i \mu \left(x_i \right)}
% Compute the centroid of the fuzzy set.

xCentroid = defuzz(x,mf,'centroid');

% Indicate the centroid defuzzification result on the original plot.

hCentroid = line([xCentroid xCentroid],[-0.2 1.2],'Color','k'); 
tCentroid = text(xCentroid,-0.2,' centroid','FontWeight','bold');

%
%% Bisector
% The bisector method finds the vertical line that divides the fuzzy set into two sub-regions of equal area. It is sometimes, but not always, coincident with the centroid line.

xBisector =  defuzz(x,mf,'bisector');

% Indicate the bisector result on the original plot, and gray out the centroid result.

hBisector = line([xBisector xBisector],[-0.4 1.2],'Color','k'); 
tBisector = text(xBisector,-0.4,' bisector','FontWeight','bold');
gray = 0.7*[1 1 1];
hCentroid.Color = gray;
tCentroid.Color = gray;

%
%% Middle, Smallest, and Largest of Maximum
% MOM, SOM, and LOM stand for middle, smallest, and largest of maximum, respectively. In this example, since the aggregate fuzzy set has a plateau at its maximum value, the MOM, SOM, and LOM defuzzification results have distinct values. If the aggregate fuzzy set has a unique maximum, then MOM, SOM, and LOM all produce the same value.

xMOM = defuzz(x,mf,'mom');
xSOM = defuzz(x,mf,'som');
xLOM = defuzz(x,mf,'lom');

% Indicate the MOM, SOM, and LOM results on the originial plot, and gray out the bisector result.

hMOM = line([xMOM xMOM],[-0.7 1.2],'Color','k'); 
tMOM = text(xMOM,-0.7,' MOM','FontWeight','bold');
hSOM = line([xSOM xSOM],[-0.7 1.2],'Color','k'); 
tSOM = text(xSOM,-0.7,' SOM','FontWeight','bold');
hLOM = line([xLOM xLOM],[-0.7 1.2],'Color','k'); 
tLOM = text(xLOM,-0.7,' LOM','FontWeight','bold');
hBisector.Color = gray;
tBisector.Color = gray;

%
%% Choosing Defuzzification Method
% In general, using the default centroid method is good enough for most applications. Once you have created your initial fuzzy inference system, you can try other defuzzification methods to see if any improve your inference results.
% Highlight the centroid result, and gray out the MOM, SOM, and LOM results.

hCentroid.Color = 'red';
tCentroid.Color = 'red';
hMOM.Color = gray;
tMOM.Color = gray;
hSOM.Color = gray;
tSOM.Color = gray;
hLOM.Color = gray;
tLOM.Color = gray;

% Copyright 1994-2019 The MathWorks, Inc.