0% found this document useful (0 votes)
60 views10 pages

Appendix B: Matlab Programs For Fuzzy Logic

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views10 pages

Appendix B: Matlab Programs For Fuzzy Logic

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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 = trimf(x,[a b c])
y = trapmf(x,[a b c d ])
y = gaussmf(x,[a c]),
y = 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 sig-
moidal, 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

1 1

0.5 0.5

0 0
0 5 10 0 5 10
(a) (b)

1 1

0.5 0.5

0 0
0 5 10 0 5 10
(c) (d)

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 = sigmf(x,[a c])
y = dsigmf(x,[a1 c1 a2 c2])
y = psigmf(x,[a1 c1 a2 c2]),
y = pimf(x,[a b c d])
y = zmf(x,[a b])
y =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 + e−a(x−c)
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 defined by f 1 (x, a1 , c1 ) − f 2 (x, a2 , c2 ) =
1 1
−a
− −a
. The product sigmoidal function psigmf() is simply the prod-
1+e 1 (x−c 1 ) 1 + e 2 (x−c2 )
uct of two sigmoidal curves defined by
1 1
f 1 (x, a1 , c1 ) ∗ f 2 (x, a2 , c2 ) = ∗ .
1+ e−a1 (x−c1 ) 1+ e−a2 (x−c2 )
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

1 1

0.5 0.5

0 0
0 5 10 0 5 10
(a) (b)

1 1

0.5 0.5

0 0
0 5 10 0 5 10
(c) (d)

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 Zadeh’s 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
and Z-shaped functions are shown in Figure B.1.3. A sample of MATLAB R
code is given in
Example B.1.1 and Example B.1.2 for difference sigmoidal and -shaped functions below.

1 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
0 0
0 5 10 0 5 10
(a) (b)

Figure B.1.3 (a) S-shaped MF = [1 10]; (b) Z-shaped MF = [1 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:
newfis(), addvar(), addmf(), addrule(). MATLAB R
supports only Mamdani- and Sugeno-type
inference systems. To perform a fuzzy inference calculation, the evalfis() 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. newfis() creates a new fuzzy inference system.
newfis() 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 specifies 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 defuzzification, respectively.
For example:
A=newfis ('NewSys', 'mamdani');
A=newfis ('NewSys', 'sugeno');
Once an FIS is created, addvar() defines 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 newfis()
in the MATLAB R
workspace. A string representing the type of the variable is specified by
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 first
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 defined, 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
MATLAB R
workspace. Indices are assigned to membership functions in the order in which
they are added, so the first 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 defined. 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 = newfis ('NewSys');
A = addvar (A,'input','X1',[-10 10]);
A = addvar (A,'input','X2',[-5 15]);
A = addvar(A,'output','Y',[-5 5]);
A = addmf(A,'input',1,'nagative','gaussmf',[1.5 -5]);
A = addmf(A,'input',1,'zero','gaussmf',[1.5 5]);
A = addmf(A,'input',2,'small','gaussmf',[1.5 0]);
A = addmf(A,'input',2,'medium','gaussmf',[1.5 10]);
A = addmf(A,'output',1,'small','gaussmf',[1.5 0]);
A = addmf(A,'output',1,'medium','gaussmf',[1.5 2.5]);

B.1.3 Fuzzy Rule Base


Once the FIS structure is defined with appropriate variables and membership functions, a rule
base has to be defined. 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 first 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 mf1 mf2


mf2 mf2 mf1

that the rule list matrix must take is very specific. 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 first 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 rule’s 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 first two rules (first 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 file; ‘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 Defuzzification
The Fuzzy Logic Toolbox provides a number of defuzzification 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
used for the defuzz() function in MATLAB R
:
df = defuzz (x, mf, type)
Appendix B: MATLAB® Programs for Fuzzy Logic 439

defuzz (x, mf, type) returns a defuzzified value df of a membership function mf positioned at
the associated variable value x, using one of several defuzzification strategies, according to
the specified 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 specified, the Fuzzy Logic Toolbox assumes it to be a user-defined function. x
and mf are passed to this function to generate the defuzzified 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


evalfis() 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 evalfis().
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 defined 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 B1 B2 C1 C2
1 1 1

.5 .5 .5

2 5 8 11 x1 4 9 14 x2 1 5 9 y

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 C1 C2
A2 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


Takagi–Sugeno-type fuzzy system are defined by Gaussian functions:

       
1 x1 − m 1 2 1 x1 − m 2 2
μ A1 (x1 ) = exp − , μ A2 (x1 ) = exp − ,
2 σ1 2 σ2
       
1 x2 − m 3 2 1 x2 − m 4 2
μ B1 (x2 ) = exp − , μ B2 (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 follow-


ing rules for the Takagi–Sugeno fuzzy system:

If x1 is A1 and x2 is B1 Then z 1 = x1 + x2 + 1
If x1 is A2 and x2 is B1 Then z 2 = 2x1 + x2 + 1
If x1 is A1 and x2 is B2 Then z 3 = 2x1 + 3x2
If x1 is A2 and x2 is B2 Then z 4 = 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 C1 W &=1
rule=[1 1 1 1 1;
2 1 2 1 1;
1 2 3 1 1;
2 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