Basics Using Matlab Fuzzy Toolbox
Basics Using Matlab Fuzzy Toolbox
By
Heikki N. Koivo
©
2006
1
2.1. Fuzzy sets
Membership functions
µF :U →[0,1]
Fuzzy set
F = {x, µ F ( x) x ∈ U }
The value of the membership function µF(u) describes the
degree of membership of u in the fuzzy set F. It takes values
between 0 and 1.
µ(u) bellshaped triangular trapezoidal singleton
1
2
2. 2. USING MATLAB Fuzzy Toolbox GUI
PROBLEM 2.1. Let the room temperature T be a fuzzy
variable. Characterize it with three different (fuzzy)
temperatures: cold,warm, hot.
SOLUTION:
1. First define the temperature range, e.g. [00,400].
3
Next activate input membership block by moving mouse
on top of it and by clicking once. Activation is shown by
a red boundary on the block. If you click twice, the
MEMBERSHIP FUNCTION EDITOR opens up. This
is needed when defining membership functions.
4
Fig.2.4. Display to edit input membership functions.
5
Fig.2.5. Change the range of input variable to [00,400].
6
Default value is three triangular (trimf) (3) membership
functions as seen on the display. These will divide the
range into three equal parts.
7
2.3. Fuzzy set operations
µA and µB.
Union
µ A∪ B (u ) = max{µ A (u ), µ B (u )}, ∀ u ∈ U
µ A∪B (u)
1
µ B (u )
µ A (u )
8
Intersection
µ A∩ B (u ) = min{µ A (u ), µ B (u )}, ∀ u ∈ U
µ A∩ B
1 µ A (u ) µ B (u )
Complement
µ A (u ) = 1 − µ A (u ), ∀ u ∈ U
µ A (u ) µ A (u )
1
u
µ (u )
b ells hap ed t ria ngul ar tr apez oid al s in gleton
1
µ (u )
b ells hap ed t ria ngul ar tr apez oid al s in gleton
1
9
2.3. Fuzzy logical operations
µ A∧ B (u , v) = min{µ A (u ), µ B (v)}, u ∈ U ∧ v ∈ V
µ A (u ) = 1 − µ A (u ), u ∈ U
10
EXAMPLE
RULEBASE
Rule 1: If x is A1 and y is B1 then z is C1,
Rule 2: If x is A2 and y is B2 then z is C2.
Interpretation:
1. and corresponds to min
2. or corresponds to max
3. then corresponds to min
MORE GENERALLY
1. α i = T ( µ Ai ( x0 ), µ Bi ( y0 ))
2. µC ' ( z ) = T (α i , µCi ( z ))
i
To combine the rules
3. µC ( z ) = T ( µC1 ( z ), µC2 ( z ),..., µC N
*
( z ))
11
FUZZY INFERENCE (AND-OR)
µA ( x) µB ( y) µC ( z)
1 1
1 1 1
µA2( x) µB 2( y) µC 2( z)
1 1 1
0
z
µ C' 2 ()z
0 0 0
x0 x y0 y min z
12
OUTPUT MEMBERSHIP FUNCTIONS
SINGLETONS
µC (z)
µ C' ( z)
1 1
0 0 0
x y z
0
z
µ C' ( z)
2
0 0 0
x0 x y0 y min z
13
FUZZY INFERENCE (PRODUCT)
14
2.3.1. T-norm
Triangular norm
a ≤ c ∧ b ≤ d = T ( a , b ) ≤ T ( c, d ) nondecreasing
1. Minimum min(a, b)
2. Product ba
15
2.3.2. T-conorm
Triangular conorm
Mapping T * : [0,1]× [0,1] → [0,1] is called T-conorm,
Example 2.2:
T-conorm is generally used to connect propositions in
connection of or-operation.
The most common T-conorms:
1. Maximum max(a, b)
16
2.4. Fuzzy reasoning
(GMP)
Fact 1: x is A
Premise 2: If x is A then y is B
Conclusion: y is B
(GMT)
Fact 1: y is B
Premise 2: If x is A then y is B
Conclusion: x is A
17
Example 2.4: Car driving
Distance
A d B
SAFE
DISTANCE
Proceed as follows:
rulebase.
18
SOLUTION:
INPUT: Distance d
In MATLAB type
» fuzzy
19
Give a name to the fuzzy input variable. Call it distance. Click
Close.
Next click the input block twice with mouse to open the
membership function window. First define the range, say
from 0 to 30 m/s.
20
Fig. 2.13. Set the range in GUI.
Next choose from Edit Add MFs. Pick the default values:
3 triangular membership functions. Give a name to each:
Call them high, medium, and short. When you are
finished, click Close.
21
Fig. 2.14. Three triangular membership functions have been
chosen for the input variable distance. The middle one has
been activated and renamed as medium.
Define the name of the output, break, and its range. Use
three membership functions: hard, medium and no. The
following GUI display is obtained.
22
Fig. 2.15. Three triangular membership functions are
chosen for the output variable breaking. The last one has
been activated and renamed as hard.
23
Fig. 2.16. Rule Editor display. On the right, the input side.
On the left, the output side.
24
If distance is low, then brake is hard.
Let us set two other rules, one for medium distance and
the other for long distance. Our simple, rule base is now
complete. Click Close.
25
Fig. 2.18. Complete rule base of three rules. Note the
weighting parameter (1) at the end of each rule, i.e., all rules
have the same weighting.
26
Fig. 2.19. The rule base can be expressed in two other ways
in GUI. Here verbose format is shown.
27
Fig. 2.20. By choosing View the rule base can be viewed
differently.
28
Fig. 2.21. The rule base can be displayed graphically. The
red line on the left indicates the value of the input, 15 m.
Similarly the bar on the right hand side, indicates the output
value, 50%.
On the left-hand side you can see the input, distance side
and on the left the output, brake side. There are three rules
and the corresponding triangular membership functions
displayed. In the right-hand side lower corner is the result
of fuzzy reasoning. At this point it is a fuzzy set.
Applying defuzzification method, in the figure center of
gravity has been chosen, a crisp value is obtained.
29
Fig. 2.22. Result of fuzzy reasoning is brake = 50%.
30
Fig. 2.23. Changing the input value results in different
output values.
31
Fig. 2.24. The fuzzy system viewed as input-output
mapping.
32
EXAMPLE 2.5: Car Driving - continues
What is missing in Example 2.4?
Distance
d
A B
SPEED!
SOLUTION:
Set up a new rule base with two inputs distance and speed,
one output, braking power.
35
Fig. 2.25. Fuzzy system with two inputs, distance and
velocity, and one output, break.
36
Fig. 2.26. Membership functions for the new input, velocity.
37
Fig. 2.27. A rule base for Example 2.5.
38
Fig. 2.28. The rule base is viewed graphically. Different
inputs can be chosen to see what the output will be.
39
Finally, the surface view of the rules is shown.
Note that the right hand-side corner is flat with value zero
over a fairly large area. You can change that by introducing
a new membership function little for breaking power. You
also have to change the rule base. This is done below.
40
2.5. Sugeno-style fuzzy inference
The result of Sugeno reasoning is an exact number.
Consider input-output data given in the table.
x y
- 1.0 1.0
- 0.5 0.25
0 0
0.5 0.25
1.0 1.0
The data is from the function y = x2. The data can be represented using
Mamdani reasoning with e.g. triangular membership functions at the input
and singletons at the output positioned at yi.
41
Fig. 2.31. The number of xi points is five. Choose as many
triangular membership functions for input variable.
For each data point generate a triangular membership function, the maximum
of which occurs exactly at a given data point, say mf3 has maximum at x = 0,
which is one of the data points. This is seen in the above figure.
42
Fig. 2.32. The number of yi points three. Choose as many
triangular membership functions for output variable.
Then make the base of the triangles narrow, so that they resemble singletons.
43
Observe again the position of singletons. They have nonzero value exactly at
the output data point. Only three membership functions are needed at the
output. Why?
44
Fig. 2.33. The complete rule base of the example.
45
Fig. 2.34. The resulting fuzzy system approximating the given
data.
The fit is exact at the given data points. Overall shape is that of parabola, but
to have a better fit, more data points would be needed.
EXERCISE: Keep the range the same. Add four more data points. Repeat
the procedure and compare the results.
46
The same procedure as above can be produced with Sugeno reasoning.
In Sugeno reasoning the consequence, the output side, is deterministic:
If x is X i then y = yi.
In Mamdani reasoning the determinism is produced with singletons.
Then activate the input and name it x. Similarly activate output and name it y.
Next Edit membership functions. Let the input range be [-1 1]. Click Ready so that
your choice is recorded. Determine the input membership functions as
before by Add MF's (5 triangular). Next repeat the same for output. The
47
range is [0 1], which is default value. Then Add MF's. At this point the
following appears.
48
The values of the constant membership functions are determined by
activating them by clicking with the mouse. Now you change the name and
value of the constant.
The final task is to form the rulebase. Choose Edit rules and write them in as
before.
49
Fig. 2.39. The rule base for the example in Sugeno type of fuzzy
system.
The Sugeno FIS system is now complete. Let us view the result. First the
overall rules
50
Fig. 2.40. The complete rule base of the example in the case of
Sugeno type of fuzzy system.
51
Fig. 2.41. The resulting fit of Sugeno type of fuzzy system.
52
More generally the above procedure
would be the following. The input-
output data is given by
x y
x0 y0
x1 y1
M M
xn yn
m
∑ µ X i ( x) yi
y ( x) = i =1m
∑ µ X i ( x)
i =1
REMARK: Weighted average requires that the rule base is complete and
input fuzzy membership functions cover the input space. Otherwise there is a
danger to divide with zero.
53
Sugeno reasoning allows us to use also functions of input x, not only
constants, on the right hand side. The rules would look like
If x is X i then y = fi ( x) .
54
The two parameters for each straight line can be chosen in Params box. The
slope is first and then the constant.
Fig. 2.44.
Fig. 2.45.
55
The overall view of the rules is shown below.
Fig. 2.46.
56
Fig. 2.47.
The result is smoother than before, but does not agree as well at point x =
0.5, y = 0.25. This would require further fine-tuning of parameters.
m
∑ µ X i ( x ) fi ( x )
y ( x) = i =1 m
∑ µ X i ( x)
i =1
57
where m = number of rules.
y1 y3
y2
µ X1 ( x) µ X 3 ( x)
58
The rules can be more general like
where the consequences of the fuzzy rules are functions of the input vector
x = [ x1,L, xn ] . A general linear function of Sugeno type at the output is
n
y j = c0j + ∑ ckj xk ,
k =1
where ckj are real-valued parameters and specific to each rule. Since each rule
has a crisp output, the aggregate result is obtained by weighted average.
n i
∑ wi (x) y (x)
y (x) = i =1 ,
n
∑ wi (x)
i =1
where x = [ x1,L, xn ] and wi depends on the applied T-norm for logical and.
If product is chosen and the number of rule is m, then
m
wi = ∏ µ Ai ( x), i = 1,L, n .
i =1
59
j ⎛⎜ ⎞
n m
∑ w ⎜ ∏ µ j ( xi ) ⎟⎟
Xi
j =1 ⎝ i =1 ⎠
y ( x) = ,
n ⎛m ⎞
∑ ⎜⎜ ∏ µ X j ( xi ) ⎟⎟
j =1⎝ i =1 i ⎠
m
∏ µ X j ( xi )
b j ( x) = i =1 i
n ⎛m ⎞
∑ ⎜⎜ ∏ µ X j ( xi ) ⎟⎟
j =1⎝ i =1 i ⎠
n
y (x) = ∑ wibi (x) .
j =1
Although the above does not look like a fuzzy system, it can be interpreted as
such, because it is formed using membership functions, T-norms and T-
conorms. This form further implies the use of optimization techniques in
determining the parameters in a fuzzy system in the best way.
60
61
Curve fitting
Suppose input-output data
x y
- 1.0 − 1.2
- 0.5 − 0.3
0 0 .2
0.5 0.4
1.0 1.2
is given. Assume a linear model y = ax + b . Find the best a and b to fit the
data.
n
J (a, b) = ∑ ( yi − axi − b )2
i =1
This represents the cumulative error. Minimize J(a,b) with respect to (a,b).
62
Fig. 2.49. Plotting data.
help LSQNONLIN
LSQNONLIN Solves non-linear least squares problems.
LSQNONLIN solves problems of the form:
63
min sum {FUN(X).^2} where X and the values returned by FUN can be
x vectors or matrices.
Examples
FUN can be specified using @:
x = lsqnonlin(@myfun,[2 3 4])
where MYFUN is a MATLAB function such as:
function F = myfun(x)
F = sin(x);
Plotting both the data and the fitted straight line the figure below is obtained.
yy=1.1*x+0.06
plot(x,yy,'or')
hold % Current plot held
plot(x,y)
grid
64
Fig. 2.50. Result of linear curve fitting.
One could use also polynomial fit. Then the command is polyfit. This
command fits a polynomial of given degree to the data:
65
[p,s]=polyfit(x,y,3)
p = 0.6667 -0.1429 0.5333 0.1314
s = R: [4x4 double] df: 1 normr: 0.0956
y1=polyval(p,x);
and plot all the data and the least square straight line and third order
polynomial to the same figure.
xx=-1:0.01:1;
y1=polyval(p,xx);
yy=1.1*xx+0.06;
plot(x,y);hold; plot(xx,y1,'xr',xx,yy,'obl'); grid
This yields
66
Fig. 2.51. Curve fitting on given data (blue), straight line (black) and third
order polynomial fit (red).
67