You are on page 1of 31

LAB 2 MATLAB TOOLBOX

CE888 FUZZY LOGIC HYBRID SYSTEMS


Introduction to Basic Fuzzy Inference System
in MATLAB

Head of Module : Professor Hani Hagras


Lab Assistants : Aysenur Bilgin, Nur Naim
(abilgin@essex.ac.uk)(mnnurs@essex.ac.uk)

Introduction to MATLAB

MATLAB (short for MATrix LABoratory) is a language for technical computing,


developed by the The Mathworks, Inc.

It makes it possible to solve technical computing problems faster than with


traditional programming languages, such as C, C++, and Fortran.

It provides a single platform for computation, visualization, programming and


software development.

Specific applications are collected in packages referred to as toolbox.

Examples of toolboxes: Signal processing, Neural Networks, control theory,


Fuzzy Logic, simulation, optimization, and several other fields of applied
science and engineering.

What is Fuzzy Logic?

Fuzzy Logic (FL): is a logical system, which is an extension of multivalued logic,


where membership is a matter of degree.

Fuzzy logic is a convenient way to map an input space to an output space.


Mapping input to output is the starting point for everything.

Some ways to make the black box work: fuzzy systems, linear systems, expert
systems, neural networks, differential equations, etc.

Why Fuzzy Logic?

However, it turns out that fuzzy is often the very best way. Why?
In almost every case you can build the same product
without fuzzy logic, but fuzzy is faster and cheaper.
(Lotfi Zadeh)

FL is conceptually easy to understand.


FL is flexible.
FL is tolerant of imprecise data.
FL can be built on top of the experience of experts.
FL is based on natural language, hence easy to use.

Basic Concepts

Basic concepts underlying FL:

Fuzzy set: is a class of objects with a continuum of grades of membership.


Membership function (MF): is a convex that defines how each point in the input
space is mapped to a membership value (or degree of membership) between
0 and 1.
Linguistic variable: in fuzzy logic applications, the non-numeric linguistic variables are
often used to facilitate the expression of rules and facts, unlike variables in
mathematics usually take numerical values,
Logical operator:

Fuzzy rule: is a fuzzy if-then rule that is used to formulate the conditional statements.

o
o

IF x is A THEN y is B

Antecedent

Consequent

where A and B are linguistic values of the linguistic variables x and y, respectively.

IF-THEN Rules

Example of IF-THEN rules:


IF service is good THEN tip is average

Antecedent is an interpretation: good is represented as a number between 0


and 1.

Conversely, consequent is an assignment: average is represented as a fuzzy


set that assigns the entire fuzzy set B to the output variable y.

In MATLAB terms, this usage is the distinction between a relational test using
"==" and a variable assignment using the "=" symbol.
IF service == good THEN tip = average

In general, the input to an IF-THEN rule is the current value for the input variable
(in this case, service) and the output is an entire fuzzy set (in this case,
average). This set will later be defuzzified, assigning one value to the output.

Interpretation of IF-THEN Rules

Interpreting IF-THEN rules is a three-part process:

1. Fuzzify inputs: Resolve all fuzzy statements in the antecedent to a degree of


membership between 0 and 1.
2. Apply fuzzy operator to multiple part antecedents: If there are multiple parts to
the antecedent, apply fuzzy logic operators and resolve the antecedents to a
single number between 0 and 1. This is the firing strength of the rule.
3. Apply implication method: Use the firing strengths for the entire rule base to
shape the output fuzzy set, that is to truncate the output fuzzy set according to
the implication method.
If the antecedent is true to some degree of membership, then
the consequent is also true to that same degree.

The consequent specifies a fuzzy set be assigned to the output. The implication
function then modifies that fuzzy set to the degree specified by the
antecedent. The most common ways are truncation using the min function
(where the output fuzzy set is truncated) or scaling using the prod function
(where the output fuzzy set is squashed).

Fuzzy Inference Systems Overall Structure

Below is the overall structure of a fuzzy inference system.

Fuzzy Inference Systems

Fuzzy inference is the process of formulating the mapping from a given input to
an output using fuzzy logic.

Fuzzy inference systems have been successfully applied in fields such as


automatic control, data classification, decision analysis, expert systems, and
computer vision.

Mamdani's fuzzy inference method is the most commonly seen fuzzy


methodology.

Mamdani-type inference expects the output membership functions to be fuzzy


sets. After the aggregation process, there is a fuzzy set for each output
variable that needs defuzzification.

Fuzzy inference process comprises of five parts:


1.
2.
3.
4.
5.

fuzzification of the input variables,


application of the fuzzy operator (AND or OR) in the antecedent,
implication from the antecedent to the consequent,
aggregation of the consequents across the rules, and
defuzzification.

Fuzzy Inference Systems Step 1:Fuzzification

The first step is to take the inputs and determine the degree to which they
belong to each of the appropriate fuzzy sets via membership functions.
To what extent is the food delicious?

The output is a fuzzy degree of membership in the qualifying linguistic set


(always the interval between 0 and 1).

Fuzzy Inference Systems Step 2:Apply Fuzzy Operator

If the antecedent of a given rule has more than one part, the fuzzy operator is
applied to obtain one number that represents the result of the antecedent for
that rule.

The fuzzy OR operator simply selects the maximum of the two values, 0.7, and
the fuzzy operation for this rule is complete.

Fuzzy Inference Systems Step 3:Apply Implication Method

After the firing strength has been assigned to each rule, the consequent is
reshaped according to the firing strength.

The output of the implication is a fuzzy set.

Implication is implemented for each rule.

Fuzzy Inference Systems Step 4:Aggregate All Outputs

Aggregation is the process by which the fuzzy sets that represent the outputs of
each rule are combined into a single fuzzy set.

As the aggregation method is commutative, the order in which the rules are
executed is unimportant.

Fuzzy Inference Systems Step 5:Defuzzification

The input for the defuzzification process is a fuzzy set (the aggregate output
fuzzy set) and the output is a single number.

Perhaps the most popular defuzzification method is the centroid calculation,


which returns the center of area under the curve.

Starting MATLAB

Open MATLAB by double-clicking on the MATLAB shortcut icon on your


Windows desktop.

One of the main parts of MATLAB system is the MATLAB desktop as illustrated
below:

MATLAB Fuzzy Logic Toolbox

There are two ways to open the Fuzzy Logic Toolbox:


o Type fuzzy in the command window
o Click Start Toolboxes FIS Editor GUI

MATLAB Fuzzy Logic Toolbox - Overview

MATLAB Fuzzy Logic Toolbox FIS Editor

The FIS Editor displays information about a fuzzy inference system.

MATLAB Fuzzy Logic Toolbox FIS Editor

The FIS Editor displays information about a fuzzy inference system.

MATLAB Fuzzy Logic Toolbox FIS Editor: Tipping Example

Construct a two-input, one output system. The two inputs are Food and
Service. The one output is Tip.

Add another input:

Rename your variables:

Save your example:

MATLAB Fuzzy Logic Toolbox Membership Function Editor

The Membership Function Editor lets you display and edit all of the
membership functions associated with all of the input and output variables for
the entire fuzzy inference system.

MATLAB Fuzzy Logic Toolbox Membership Function Editor

The Membership Function Editor lets you display and edit all of the
membership functions associated with all of the input and output variables for
the entire fuzzy inference system.

MATLAB Fuzzy Logic Toolbox MF Editor: Tipping Example

Create MFs of Rancid and Delicious for the input variable Food.

Double-click the input variable Food to open MF Editor.

Change the range to [0 9]:

Click Edit Remove All MFs to delete the default MFs.

Click Edit Add MFs to create new MFs:

Rename the MFs and enter parameters:

MATLAB Fuzzy Logic Toolbox MF Editor: Tipping Example

Similarly, create MFs of Poor, Good and Excellent for Service.

MATLAB Fuzzy Logic Toolbox MF Editor: Tipping Example

Similarly, create MFs of Cheap, Average and Generous for the output Tip.

MATLAB Fuzzy Logic Toolbox Rule Editor

MATLAB Fuzzy Logic Toolbox Rule Editor: Tipping Example

Create rules by selecting an item in each input and output variable box,
selecting one Connection item, and clicking Add Rule.

MATLAB Fuzzy Logic Toolbox Rule Viewer: Tipping Example

The Rule Viewer shows one calculation at a time and in great detail.

Try different inputs and see the change in the output.

MATLAB Fuzzy Logic Toolbox Surface View: Tipping Example

The Surface Viewer shows a three-dimensional curve that represents the


mapping from food and service quality to tip amount.

MATLAB Fuzzy Logic Toolbox Exercise

By changing the rules, the type of t-norm operation and the type of
implication, evaluate the various systems and study the behaviour.

References

Tutorial for CE889 Artificial Neural Networks Lab 2

MATLAB Help Documentation

Zadeh, L.A. (1965). "Fuzzy sets", Information and Control 8 (3): 338353.

Zadeh, L. A. et al. 1996 Fuzzy Sets, Fuzzy Logic, Fuzzy Systems, World Scientific
Press, ISBN 981-02-2421-4

You might also like