You are on page 1of 59

Intelligent Control Systems

EPMn 643
Module 3: Fuzzy Logic Examples &
Tools

Oct 2017 Dr. Khaled El-Metwally, CU 1


Today Objectives
• Fuzzy Examples
• Our First Fuzzy Controller
• Fuzzy OCR Example
• Software Implementation of Fuzzy Logic
• Fuzzy Matlab toolbox introduction
• Using the Simulink toolbox
• Fuzzy Hardware Implementation insights
• Project Announcement (Ideas)
Oct 2017 Dr. Khaled El-Metwally, CU 2
Example 1: Classifying Houses
Example 1: Classifying Houses
Problem. A realtor wants to classify the houses he offers to his clients. One
indicator of comfort of these houses is the number of bedrooms in them. Let
the available types of houses be represented by the following set.
U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The houses in this set are described by u number of bedrooms in a house.
The realtor wants to describe a "comfortable house for a 4-person family,"
using a fuzzy set.

Oct 2017 Dr. Khaled El-Metwally, CU 3


Solution. Classifying Houses
Solution. The fuzzy set "comfortable type of house for
a 4-person family may be described using a fuzzy set in
the following manner.

HouseForFour = { 0.2/1, 0.5/2, 0.8/3, 1/4, 0.7/5, 0.3/6}

Oct 2017 Dr. Khaled El-Metwally, CU 4


Example 2: Representing Age
Example 2: Representing Age
Fuzzy sets can be used to represent fuzzy concepts. Let U be a
reasonable age interval of human beings.
U = {0, 1, 2, 3, ... , 100}

Define the Young and the Old fuzzy sets then induce the Middle
Age fuzzy set using proper fuzzy operators.

Oct 2017 Dr. Khaled El-Metwally, CU 5


Solution: Representing Age
Solution of : Representing Age
Assume that the concept of "young" is represented by a fuzzy set
Young, whose membership function is given by a trapezoidal
fuzzy set 0,0,25, 40.
The concept of "old" can also be represented by a fuzzy set, Old,
whose membership function could be defined in a trapezoidal set
50, 65, 100,100.
Now, Can you define the concept of middle-aged ?
Middle age is to be neither young nor old. We do this by using fuzzy
operators from Fuzzy Logic.

We can find a fuzzy set to represent the concept of middle-aged by


taking the intersection of the complements of our Young and Old
fuzzy sets.

Oct 2017 Dr. Khaled El-Metwally, CU 6


Example 3: Natural Numbers
Example 3: Natural Numbers
Suppose you are asked to define the set of natural numbers
close to 6. There are a number of different ways in which you
could accomplish this using fuzzy sets.

Oct 2017 Dr. Khaled El-Metwally, CU 7


Solution: Natural Numbers
Solution 1. One solution would be to manually create a fuzzy set
describing the numbers near 6. This can be done as follows:

Six = { 0.1/3, 0.3/4, 0.6/5, 1/6. 0.6/7, 0.3/8, 0.1/9}

Oct 2017 Dr. Khaled El-Metwally, CU 8


Different solutions are possible
Solution 2. A second solution would be to use the Fuzzy Trapezoidal function to
create the fuzzy set. For a case such as this, a triangular fuzzy set would
probably be better than a trapezoid, so we set the middle two parameters of the
Fuzzy Trapezoidal function to 6.
Trapizodal = {x, 2 6 6 10}
We can also use triangular or Gaussian membership function to represent the
number 6

Oct 2017 Dr. Khaled El-Metwally, CU 9


Fuzzy Systems Examples

Oct 2017 Dr. Khaled El-Metwally, CU 10


Building a Fuzzy Logic
Controller

Oct 2017 Dr. Khaled El-Metwally, CU 11


Controlling a System
Desired output error
reference
Input Output
Dynamic
+ Controller
- model

Feedback

Oct 2017 Dr. Khaled El-Metwally, CU 12


Fuzzy Logic Controller

Mapping
Process

Fuzzy
Crisp Inputs
FLC
Rule Crisp Outputs
Base

Oct 2017 Dr. Khaled El-Metwally, CU 13


A Closer Look to FLC

Knowledge Base

Data base Rule base

Crisp Fuzzification Fuzzy Inference Fuzzy Defuzzification Crisp

Oct 2017 Dr. Khaled El-Metwally, CU 14


Position control of a DC motor

Desired output error


reference
Input Output
DC motor
+ FLC
- model

Feedback

Oct 2017 Dr. Khaled El-Metwally, CU 15


Selecting I/Os
Controller Input Output Signal
Signals

e
FLC U
De

Oct 2017 Dr. Khaled El-Metwally, CU 16


Creating Membership Functions

ZERO

NB NM NS PS PM PB
1

-Ve
0 +Ve

e
Oct 2017 Dr. Khaled El-Metwally, CU 17
How Fuzzification is done ?

NB NM NS ZZ PS PM PB
0.6
0.4 1

De
Oct 2017 Dr. Khaled El-Metwally, CU 18
Defining FLPSS Memberships
NB NM NS ZZ PS PM PB

e
NB NM NS ZZ PS PM PB

De
NB NM NS ZZ PS PM PB

Oct 2017 Dr. Khaled El-Metwally, CU U 19


Creating The Rules

IF e is NB AND De is NB THEN U is PB

Described by Described by
7 Fuzzy Subsets 7 Fuzzy Subsets

Simply! We have 49 rules


(the whole state space)
Oct 2017 Dr. Khaled El-Metwally, CU 20
How Rules are Fired?

IF Dw is NB AND De is NB THEN U is PB

e = 0.6 PS U=?
e = 1 PS

De = 0.2 NM
F U = 0.2 PB
U = 0 PB
De = 0 NM

Oct 2017 Dr. Khaled El-Metwally, CU 21


Combining All Rules Together
De
NB NM NS ZZ PS PM PB
NB PB PB PM PM PS PS ZZ
NM PB PM PM PS PS ZZ NS
NS PM PM PS PS ZZ NS NS
e ZZ PM PS PS ZZ NS NS NM U
PS PS PS ZZ NS NS NM NM
PM PS ZZ NS NS NM NM NB
PB ZZ NS NS NM NM NB NB

Oct 2017 Dr. Khaled El-Metwally, CU 22


Rules Inference
NB NM NS ZZ PS PM PB

NB NM NS ZZ PS PM PB

e
NB NM NS ZZ PS PM PB

De
Oct 2017 Dr. Khaled El-Metwally, CU 23
Rules Inference
0.6 0.4

De NB NM NS ZZ PS PM PB

0.6 PS 0.4 PM
e NB NM NS ZZ PS
PS PM
PM PB F 0.2PS
F 0.2ZZ

NB NB PB PB PM PM PS PS ZZ 0.2 NM 0.2 NM
0.2
NM NM PB PM PM PS PS
NM PS ZZ
ZZ NS 0.6 PS 0.4 PM
NSNS PM PM PS PS ZZ
ZZ NS
NS NS
NS
0.8 ZZ PM PS PS ZZ NS NS NM F 0.6ZZ
F 0.4NS
ZZ
PS PS PS ZZ NS NS NM NM 0.8 NS 0.8 NS
PS
PM PS ZZ NS NS NM NM NB
PM
PB ZZ NS NS NM NM NB NB
PB U
Oct 2017 Dr. Khaled El-Metwally, CU 24
Defuzzification

NB NM NS ZZ PS PM PB
0.6 PS 0.4 PM
0.2PS 0.2ZZ
F F
0.2 NM 0.2 NM
0.6 PS 0.4 PM
0.6ZZ 0.4NS
F F
0.8 NS 0.8 NS
The Center of Mass
U = -0.03
U

Oct 2017 Dr. Khaled El-Metwally, CU 25


Simplifying The COM

NB NM NS ZZ PS PM PB

For Symmetrical Fuzzy


I/O sets UCi
U
S UCi * mRi
U =
S mRi

Oct 2017 Dr. Khaled El-Metwally, CU 26


Rule Creation
for direct and reverse action systems

Oct 2017 Dr. Khaled El-Metwally, CU 27


Software Building Blocks

Oct 2017 Dr. Khaled El-Metwally, CU 28


Another Example
Character Recognition Using Fuzzy
Logic

Oct 2017 Dr. Khaled El-Metwally, CU 29


Character representation
(7x5 dot matrix)

Oct 2014 Dr. Khaled El-Metwally, CU 30


Hindi numerals

Oct 2014 Dr. Khaled El-Metwally, CU 31


Arabic numerals

Oct 2014 Dr. Khaled El-Metwally, CU 32


Sample of dot matrix generation

zero= 2=
[ 0 1 1 1 0; [ 0 1 1 1 0;
1 0 0 0 1; 1 0 0 0 1;
1 0 0 0 1; 0 0 0 1 0;
1 0 0 0 1; 0 0 1 0 0;
1 0 0 0 1; 0 1 0 0 0;
1 0 0 0 1; 1 0 0 0 0;
0 1 1 1 0 ]; 1 1 1 1 1 ];
1=
[ 0 1 1 0 0;
0 0 1 0 0;
0 0 1 0 0;
0 0 1 0 0;
0 0 1 0 0;
0 0 1 0 0;
0 1 1 1 0 ];

Oct 2014 Dr. Khaled El-Metwally, CU 33


1 Problem Solution

Image map
Rsum1

Y
Rounding
FS Stage

Rsum7
Corresponding
FCR numeral output

Oct 2014 Dr. Khaled El-Metwally, CU 34


Selection of Fuzzy Membership
function

m Slope =-b/2a
1

0.5

0
c
x
2a

Oct 2014 Dr. Khaled El-Metwally, CU 35


FCR I/Os Membership functions
1.2
zero one two three four five
Membership 1
grade
0.8

0.6

0.4

0.2

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

a) Membership functions for each row sum


1.2
Membership Zero One Two Three Four Five Six Seven Eight Nine
1
grade
0.8

0.6

0.4

0.2

0
0 1 2 3 4 5 6 7 8 9

b) Membership functions for the output

Optimized inputs and output membership functions


Oct 2014 Dr. Khaled El-Metwally, CU 36
Building fuzzy rules

If Rsum1 3 and Rsum2  2 and Rsum3  2 and Rsum4  2


and Rsum5  2 and Rsum6  2 and Rsum7  3

Then Y  Zero

Oct 2014 Dr. Khaled El-Metwally, CU 37


FCR rules for Arabic numerals in
tabular form
Antecedent Conseq-
uent
Rsum1 Rsum2 Rsum3 Rsum4 Rsum5 Rsum6 Rsum7 Y
Rule 1 3 2 2 2 2 2 3 Zero
Rule 2 2 1 1 1 1 1 3 One
Rule 3 4 2 1 1 1 1 1 Two
Rule 4 3 2 1 3 1 2 3 Three
Rule 5 1 1 2 2 1 1 1 Four
Rule 6 1 1 1 4 1 2 3 Five
Rule 7 3 2 1 4 2 2 3 Six
Rule 8 1 1 1 1 1 1 1 Seven
Rule 9 3 2 2 1 2 2 3 Eight
Rule 10 3 2 2 4 1 2 3 Nine

Oct 2014 Dr. Khaled El-Metwally, CU 38


Testing system with corrupted
characters

Oct 2014 Dr. Khaled El-Metwally, CU 39


Effect of different memberships on
noise rejection
10
0 a=0.5
9 a=0.3
0 a=0.2
8 a=0.1
Percentage error

0
7
0
6
0
5
0
4
0
3
0
2
0
1
0
0
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Noise standard deviation

Reliability test for different membership functions


Oct 2014 Dr. Khaled El-Metwally, CU 40
Software Implementation of fuzzy
modules

Oct 2014 Dr. Khaled El-Metwally, CU 45


Labs 1-5

Build the your own Fuzzy Controller


( Membership function definition,
Fuzzification, inference, defuzzification)
Use Steps given in Lab 1-5 as guidelines

Oct 2017 Dr. Khaled El-Metwally, CU 46


Fuzzy Toolbox in Matlab

Oct 2017 Dr. Khaled El-Metwally, CU 47


Fuzzy GUI

Oct 2017 Dr. Khaled El-Metwally, CU 48


Definition of memberships

Oct 2017 Dr. Khaled El-Metwally, CU 49


Oct 2017 Dr. Khaled El-Metwally, CU 50
2-Input/1-output System

Oct 2017 Dr. Khaled El-Metwally, CU 51


Empty rules

Oct 2017 Dr. Khaled El-Metwally, CU 52


9 Rules FIS

Oct 2017 Dr. Khaled El-Metwally, CU 53


In Simulink

Oct 2017 Dr. Khaled El-Metwally, CU 54


Inside the FIS file
[Output1]
[Input1]
[System] Name='u'
Name='error'
Name='Nine_rules' Range=[-1 1]
Range=[-1 1]
Type='mamdani' NumMFs=3
NumMFs=3
Version=2.0 MF1='N':'trimf',[-1.8 -1 -0.2]
MF1='N':'trimf',[-1.8 -1 -0.2]
NumInputs=2 MF2='Z':'trimf',[-0.8 0 0.8]
MF2='Z':'trimf',[-0.8 0 0.8]
NumOutputs=1 MF3='P':'trimf',[0.2 1 1.8]
MF3='P':'trimf',[0.2 1 1.8]
NumRules=9
AndMethod='min' [Rules]
[Input2]
OrMethod='max' 1 1, 1 (1) : 1
Name='de'
ImpMethod='min' 1 2, 1 (1) : 1
Range=[-1 1]
AggMethod='max' 1 3, 2 (1) : 1
NumMFs=3
DefuzzMethod='centroid' 2 1, 1 (1) : 1
MF1='N':'trimf',[-1.8 -1 -0.2]
2 2, 2 (1) : 1
MF2='Z':'trimf',[-0.8 0 0.8]
2 3, 3 (1) : 1
MF3='P':'trimf',[0.2 1 1.8]
3 1, 2 (1) : 1
3 2, 3 (1) : 1
3 3, 3 (1) : 1
Oct 2017 Dr. Khaled El-Metwally, CU 55
PI- Like configuration
Unit Delay
1
z

1.5 4
1.5
s2 +2s+4
Step Ke
Ku Transfer Fcn y
Fuzzy Logic
du/dt 2 Controller

Derivative Kde

Oct 2017 Dr. Khaled El-Metwally, CU 56


Maintaining a good steady state

3 4
1
s2 +2s+4
Step Ke
Ku Transfer Fcn y
Fuzzy Logic
du/dt 2 Controller

Derivative Kde

Oct 2017 Dr. Khaled El-Metwally, CU 57


Hardware Implementation

Oct 2017 Dr. Khaled El-Metwally, CU 58


Simplification for Hardware
Implementation

Oct 2017 Dr. Khaled El-Metwally, CU 59


Hardware Implementation

Oct 2017 Dr. Khaled El-Metwally, CU 60


Hardware Implementation

Oct 2017 Dr. Khaled El-Metwally, CU 61


Hardware Implementation

Oct 2017 Dr. Khaled El-Metwally, CU 62


Course Project

•Hardware
1. Implementation of the fuzzy logic controller algorithm on an Atmel
micro controller or on proper micro controller simulator. You can use
the already available A/D and D/A on the Atmel chip / Arduino
board. The controller performance can be tested on any selected
physical model. A 2nd order R-C model simulation is an accepted
test model.
•Software
1. Tune PID to suitably control the position of a DC motor. Use a
suitable Fuzzy Logic Controller structure to replace the PID
controller. Use the pre-tuned model to generate the training data if
needed.
2. Use derivative based techniques to train a FLC to control the
position of a DC motor.

Oct 2017 Dr. Khaled El-Metwally, CU 63

You might also like