You are on page 1of 10

Appendix B

MATLAB Programs for


Fuzzy Logic
B.1.1

Membership Functions

The Fuzzy Logic Toolbox provides a number of membership functions. The most widely used
MFs are triangular, Gaussian, bell-shaped and trapezoidal. trimf(), trapmf(), gaussmf() and
gbellmf() are built-in functions for triangular, trapezoidal, Gaussian and bell-shaped MFs. The
general forms of use with parameters are described below:
y
y
y
y

=
=
=
=

trimf(x,[a b c])
trapmf(x,[a b c d ])
gaussmf(x,[a c]),
gbellmf(x,[a b c])

Different parametric membership functions are discussed in Section 2.4 of Chapter 2. The
triangular membership function trimf() depends on three parameters a, b and c. The parameters
a, b and c locate the feet of the triangle and the parameter c locates the peak. The trapezoidal
function trapmf() depends on four scalar parameters a, b, c and d. The parameters a and
d locate the feet of the trapezoid and the parameters b and c locate the shoulders. The
symmetric Gaussian membership function gaussmf() depends on two parameters a and c. The
parameter c is the centre of the function and the width is determined by the parameter a of
the Gaussian function. The generalized bell function gbellmf() depends on three parameters
a, b and c. The parameter b is usually positive. The parameter c locates the centre and the
parameter a determines the width of the membership function. Sample plots of these MFs are
shown in Figure B.1.1.
A second set of built-in MFs sigmf(), dsigmf(), psigmf(), pimf(), zmf() and smf() are sigmoidal, difference sigmoidal, product sigmoidal, -shaped, Z-shaped and S-shaped functions,

Computational Intelligence: Synergies of Fuzzy Logic, Neural Networks and Evolutionary Computing, First Edition.
Nazmul Siddique and Hojjat Adeli.
2013 John Wiley & Sons, Ltd. Published 2013 by John Wiley & Sons, Ltd.

434

Computational Intelligence

0.5

0.5

5
(a)

10

0.5

0.5

5
(c)

10

5
(b)

10

5
(d)

10

Figure B.1.1 (a) Triangular MF = [3 6 8]; (b) Gaussian MF = [2 5]; (c) Bell-shaped MF = [2 3 5] and
(d) Trapezoidal MF = [1 5 7 9]

respectively. The parameterized general forms are described below:


y
y
y
y
y
y

= sigmf(x,[a c])
= dsigmf(x,[a1 c1 a2 c2])
= psigmf(x,[a1 c1 a2 c2]),
= pimf(x,[a b c d])
= zmf(x,[a b])
=smf(x,[a b])

The sigmoidal membership function sigmf() depends on the two parameters a and c and is
1
given by f (x, a, c) =
. The parameter c is the centre of the sigmoidal function.
1 + ea(xc)
The sign of the parameter a determines the spread of the sigmoidal membership function, i.e.,
whether it inherently open to the right or to the left. Thus, the parameter a is appropriate for
representing concepts of linguistic hedges such as very large or more or less small.
The difference sigmoidal function dsigmf() depends on four parameters a1, c1 , a2 and c2 and
is the difference between two sigmoidal functions dened by f 1 (x, a1 , c1 ) f 2 (x, a2 , c2 ) =
1
1

. The product sigmoidal function psigmf() is simply the prod(xc


)
a
a
1
1
1+e
1 + e 2 (xc2 )
uct of two sigmoidal curves dened by
f 1 (x, a1 , c1 ) f 2 (x, a2 , c2 ) =

1
1+

ea1 (xc1 )

1
1+

ea2 (xc2 )

The parameters of dsigmf() and psigmf() are listed in the order [a1 c1 a2 c2 ]. The -shaped
membership function pimf() is a spline-based curve. It is named -shape because of its shape.

Appendix B: MATLAB Programs for Fuzzy Logic

435

0.5

0.5

5
(a)

10

0.5

0.5

5
(c)

10

5
(b)

10

5
(d)

10

Figure B.1.2 (a) Sigmoidal MF = [2 4]; (b) Difference sigmoidal MF = [5 2 5 7]; (c) Product sigmoidal
MF = [2 3 5 8] and (d) -shaped MF = [1 4 5 10]

The parameters a and d locate the feet of the curve, while b and c locate its shoulders. Plots
of sigmf(), dsigmf(), psigmf() and pimf() MFs on a vector x are shown in Figure B.1.2.
There are also two spline-based functions zmf() and smf(). They are so named because
of their Z-shape and S-shape (also called Zadehs S-function) (Driankov et al., 1993). The
parameters a and b locate the extremes of the sloped portion of the curve. Plots of S-shaped
R
code is given in
and Z-shaped functions are shown in Figure B.1.3. A sample of MATLAB
Example B.1.1 and Example B.1.2 for difference sigmoidal and -shaped functions below.
1

0. 9

0. 9

0. 8

0. 8

0. 7

0. 7

0. 6

0. 6

0. 5

0. 5

0. 4

0. 4

0. 3

0. 3

0. 2

0. 2

0. 1

0. 1

5
(a)

10

5
(b)

Figure B.1.3 (a) S-shaped MF = [1 10]; (b) Z-shaped MF = [1 10]

10

436

Computational Intelligence

Example B.1.1
x=0:0.1:10;
y=dsigmf(x,[5 2 5 7]);
plot(x,y)
xlabel('dsigmf, P=[5 2 5 7]')

Example B.1.2
x=0:0.1:10;
y=pimf(x,[1 4 5 10]);
plot(x,y)
xlabel('pimf, P=[1 4 5 10]')

B.1.2

Fuzzy Inference

To build a fuzzy system entirely from the command line, the following commands are used:
R
supports only Mamdani- and Sugeno-type
news(), addvar(), addmf(), addrule(). MATLAB
inference systems. To perform a fuzzy inference calculation, the evals() command is used.
To view the control surface, the gensurf() command is used.
There are two types of fuzzy inference system that can be implemented in the Fuzzy Logic
Toolbox: Mamdani-type and Sugeno-type. news() creates a new fuzzy inference system.
news() can be used to create a Mamdani-type or Sugeno-type FIS structure and can be used
in the following general format:
A = newfis(FIS_Name, [FIS_Type], [AND_Method], [OR_Method],
[Imp_Method], ...
[Agg_Method], [Defuzz_Method]) creates a FIS structure for a
Mamdani or Sugeno-style system with the name FIS_Name

The other six optional arguments are as follows: FIS_Type, which species the FIS structure
of Mamdani or Sugeno type and by default is Mamdani-type; AND_Method, OR_Method,
Imp_Method, Agg_Method and Defuzz_Method, which specify the methods for AND, OR,
implication, aggregation and defuzzication, respectively.
For example:
A=newfis ('NewSys', 'mamdani');
A=newfis ('NewSys', 'sugeno');

Once an FIS is created, addvar() denes input or output variables for an FIS structure with
respective input and output ranges. The general form of the command is
A = addvar (A, 'Var_Type', 'Var_Name', Var_Bounds)

addvar() has four arguments in this order. A is the name of a FIS structure created by news()
R
workspace. A string representing the type of the variable is specied by
in the MATLAB
Var_Type and can be input or output. Var_Name is a string representing the name of the
variable, e.g., X1, Y2. Var_Bounds is a vector describing the range values for the variables,
e.g., [5 11]. Indices are applied to variables in the order in which they are added, so the rst
input variable added to a system will always be known as input variable number one for that
system. Input and output variables are numbered independently.

Appendix B: MATLAB Programs for Fuzzy Logic

437

For example:
A=newfis ('NewSys');
A=addvar (A,'input','X1',[10 20]);
A=addvar (A,'input','X2',[-7 9]);
A=addvar(A,'output','Y1',[-5 5]);
A=addvar(A,'output','Y2',[-15 10]);

Once the input/output variables are dened, addmf() adds membership functions to the FIS
structure. The general format of the command is as follows:
A = addmf (A, 'Var_Type', Var_Index, 'mfName', 'mfType', mfParams)

A membership function can only be added to a variable in an existing FIS structure in the
R
workspace. Indices are assigned to membership functions in the order in which
MATLAB
they are added, so the rst membership function added to a variable will always be known as
membership function number one for that variable. A membership function cannot be added to
input variable number two of a system if only one input has been dened. The function requires
six input arguments in this order. A is the FIS structure name in the workspace. Var_Type is
a string input or output representing the type of variable to be added to the membership
function. Var_Index is the index of the variable. String mfName represents the name of the
membership function described in Section 2.14.1 of Chapter 2. String mfType is the type
of the new membership function, such as gaussmf, trimf, etc. mfParam is the vector of
parameters that specify the membership function, e.g., a triangular membership function has
three parameters [3, 1, 5], a Gaussian membership function has two parameters [0.5, 5].
For details of different parameters of membership functions, see Sections 2.4 and 2.15.1 in
Chapter 2.
For example:
A
A
A
A
A
A
A
A
A
A

=
=
=
=
=
=
=
=
=
=

newfis ('NewSys');
addvar (A,'input','X1',[-10 10]);
addvar (A,'input','X2',[-5 15]);
addvar(A,'output','Y',[-5 5]);
addmf(A,'input',1,'nagative','gaussmf',[1.5 -5]);
addmf(A,'input',1,'zero','gaussmf',[1.5 5]);
addmf(A,'input',2,'small','gaussmf',[1.5 0]);
addmf(A,'input',2,'medium','gaussmf',[1.5 10]);
addmf(A,'output',1,'small','gaussmf',[1.5 0]);
addmf(A,'output',1,'medium','gaussmf',[1.5 2.5]);

B.1.3

Fuzzy Rule Base

Once the FIS structure is dened with appropriate variables and membership functions, a rule
base has to be dened. addrule() adds a list of rules to an FIS structure. The following general
form is used:
A = addrule(A, ruleList)

addrule() has two arguments. The rst argument is the name of the FIS structure. The second
argument is a matrix of one or more rows, each of which represents a given rule. The format

438

Computational Intelligence

Table B.1.1

Rule base of a simple fuzzy system


x2

x1

mf1

mf2

mf1
mf2

mf1
mf2

mf2
mf1

that the rule list matrix must take is very specic. If there are m inputs to a system and n
outputs, there must be exactly m + n + 2 columns to the rule list. The entries in the rst m
columns refer to the inputs of the system. Each column contains a number that refers to the
index of the membership function for that variable. The entries in the next n columns refer
to the outputs of the system. Each column contains a number that refers to the index of the
membership function for that variable. The m + n + 1 column contains the weight that is to
be applied to the rule. The weight must be a number between 0 and 1, and is generally left as
1. The m + n + 2 column contains a 1 if the fuzzy operator for the rules antecedent is AND.
It contains a 2 if the fuzzy operator is OR. For example, if the FIS structure A has two inputs
x1 , x2 and one output y with each input/output having two membership functions mf1 and mf2 ,
the rule base will look like Table B.1.1.
The rst two rules (rst row of rule table) can be described as:
If x1 is mf1 and x2 is mf1 Then y is mf1 (rule weight 1)
If x1 is mf1 and x2 is mf2 Then y is mf2 (rule weight 1)

The above rule list can be written as a rule matrix below:


ruleList = [1 1 1 1 1
1 2 2 1 1];

The rule matrix is then added to the FIS system using the addrule() function:
A = addrule(A, ruleList)

The fuzzy inference diagram for an FIS is stored in a le; A.FIS can be viewed by invoking
the rule viewer function ruleview(). This is used to view the entire implication process from
beginning to end. You can move around the line indices that correspond to the inputs and then
watch the system readjust and compute the new output.
For example:
ruleview('A')

B.1.4

Defuzzication

The Fuzzy Logic Toolbox provides a number of defuzzication methods. The most widely used
methods are centroid of area, bisector of area, mean value of maximum, smallest (absolute)
value of maximum and largest (absolute) value of maximum. The following general form is
R
:
used for the defuzz() function in MATLAB
df = defuzz (x, mf, type)

Appendix B: MATLAB Programs for Fuzzy Logic

439

defuzz (x, mf, type) returns a defuzzied value df of a membership function mf positioned at
the associated variable value x, using one of several defuzzication strategies, according to
the specied type. The variable type can be one of the following:
centroid for centroid of area
bisector for bisector of area
mom for mean value of maximum
som for smallest (absolute) value of maximum
lom for largest (absolute) value of maximum.
If type is not specied, the Fuzzy Logic Toolbox assumes it to be a user-dened function. x
and mf are passed to this function to generate the defuzzied output value.
Example B.1.3
x = -10:0.1:10;
%Trapezoidal MF is defined for defuzzification
mf = trapmf(x,[-10 -8 -4 7]);
%Defuzzification of trapezoidal MF is carried out using different
methods supported by
%MATLAB toolbox
dfc = defuzz(x,mf,'centroid');
dfb = defuzz(x,mf,'bisector ');
dfm = defuzz(x,mf,'mom');
dfs = defuzz(x,mf,'som');
dfl = defuzz(x,mf,'lom');

B.1.5 Simulation of FIS


evals() simulates the FIS for the input data and returns the output data. The general form is
Y = evalfis (X, A)

X is the input data matrix of M N dimension. Each row of the matrix is a particular input
vector. Y is the return output data, an M L matrix, each row being a particular output vector.
For example, if an FIS structure A has two inputs x1 and x2 with x1 = 4 and x2 = 9, then the
FIS A can be simulated using the function evals().
For example:
Y = evalfis ([4 9], A)

Example B.1.4
The membership functions for x1 , x2 and y of a Mamdani-type fuzzy
system are dened within the universes of discourse [2 11], [4 14] and [1 9], respectively and
are shown in Figure B.1.4. For each of the variables, the MFs are taken to be {A1, A2}, {B1,
B2} and {C1, C2}.
The rule base of the Mamdani-type fuzzy inference system is shown in Table B.1.2.

440

Computational Intelligence

A1

A2

.5

.5

11 x1

B1

B2

C2

C1

.5

x2

14

Figure B.1.4 MFs for x1 , x2 and y

Table B.1.2

Rule base for Mamdani-type FLC


X2

X1

B1

B2

A1
A2

C1
C2

C2
C1

Simulate the Mamdani-type fuzzy system for initial conditions x1 (0) = 4 and x2 (0) = 8:
%Mamdani-type Fuzzy Inferencing
clear all;
close all;
sys=newfis('ExampleB.1.4');
%Inputs ----------------------------------------------%Define input variable x1 to FIS within interval [2 11]
sys=addvar(sys,'input', 'x1', [2 11]);
%Define input variable x2 to FIS within interval [4 14]
sys=addvar(sys,'input', 'x2', [4 14]);
%Define Triangular MFs A1 and A2 for input x1
sys=addmf(sys,'input',1,'A1','trimf',[2 5 8]);
sys=addmf(sys,'input',1,'A2','trimf',[5 8 11]);
%Define Trinagular MFs B1 and B2 for input x2
sys=addmf(sys,'input',2,'B1','trimf',[4 4 9]);
sys=addmf(sys,'input',2,'B2','trimf',[4 9 14]);
%outputs----------------------------------------------%Define output variable y to FIS within interval [1 9]
sys=addvar(sys,'output', 'y', [1 9]);
%Define MFs C1 and C2 for output y
sys=addmf(sys,'output',1,'C1','zmf',[1 9]);
sys=addmf(sys,'output',1,'C2','smf',[1 9]);

Appendix B: MATLAB Programs for Fuzzy Logic

441

%Rules------------------------------------------------%Define rules and add to FIS


rule=[1 1 1 1 1;
1 2 2 1 1;
1 1 2 1 1;
2 2 1 1 1];
sys=addrule(sys,rule);
%Plot-------------------------------------------------figure(1);
plotfis(sys);
figure(2)
ruleview(sys)
%Perform fuzzy inference for x1=4 and x2=8
y=evalfis([4 8], sys)
%------end of program----------------------------------

Example B.1.5
The membership functions A1 , A2 , B1 , B2 for the inputs x1 and x2 of a
TakagiSugeno-type fuzzy system are dened by Gaussian functions:





 
 
1 x1 m 1 2
1 x1 m 2 2
, A2 (x1 ) = exp
,
A1 (x1 ) = exp
2
1
2
2




 
 
1 x2 m 3 2
1 x2 m 4 2
, B2 (x2 ) = exp
,
B1 (x2 ) = exp
2
3
2
4
Assume m 1 = 2, m 2 = 3, m 3 = 3, m 4 = 4 and 1 = 2 = 3 = 4 = 2. Consider the following rules for the TakagiSugeno fuzzy system:
If x1 is A1 and x2 is B1 Then z 1
If x1 is A2 and x2 is B1 Then z 2
If x1 is A1 and x2 is B2 Then z 3
If x1 is A2 and x2 is B2 Then z 4

= x1 + x2 + 1
= 2x1 + x2 + 1
= 2x1 + 3x2
= 2x1 + 5

Compute the value of the output z for x1 = 1 and x2 = 4:


%Sugeno-type Fuzzy System
%Chapter 2 Example B.1.5
clear all;close all;
sys=newfis('Example2.14.4','sugeno');
%Inputs ----------------------------------------%Define input variable X1 within interval [1 4]
sys=addvar(sys,'input', 'X1', [1 4]);
%Define MFs A1 and A2 for input X1
sys=addmf(sys,'input',1,'A1','gaussmf',[1 2]);
sys=addmf(sys,'input',1,'A2','gaussmf',[1 3]);

442

Computational Intelligence

%Define input variable X2 within interval [2 5]


sys=addvar(sys,'input', 'X2', [2 5]);
%Define MFs B1 and B2 for input X2
sys=addmf(sys,'input',2,'B1','gaussmf',[1 3]);
sys=addmf(sys,'input',2,'B2','gaussmf',[1 4]);
%outputs----------------------------------------%Define output variable Z within interval [-9 9]
sys=addvar(sys, 'output', 'Z', [-9 9]);
sys=addmf(sys,'output',1,'Z1','linear',[1 1 1]);
sys=addmf(sys,'output',1,'Z2','linear',[2 1 1]);
sys=addmf(sys,'output',1,'Z3','linear',[2 3 0]);
sys=addmf(sys,'output',1,'Z4','linear',[2 0 5]);
%Rules------------------------------------------%Define rules and add to FIS
%1If (x1 is A1) and (x2 is B1) then (z is z1) (1)
%2If (x1 is A2) and (x2 is B1) then (z is z2) (1)
%3If (x1 is A1) and (x2 is B2) then (z is z3) (1)
%4If (x1 is A2) and (x2 is B2) then (z is z4) (1)
% A1 B1
rule=[1
2
1
2

C1 W &=1
1 1 1 1;
1 2 1 1;
2 3 1 1;
2 4 1 1];

sys=addrule(sys,rule);
figure(1);
plotfis(sys); %Figure is not shown here
figure(2)
ruleview(sys) %Figure is not shown here
%Perform fuzzy inference for x1=1.5 and x2=4
y=evalfis([1.5 4], sys)
%------end of program----------------------------

You might also like