You are on page 1of 22

FUZZY EXERCISES

By

Heikki Koivo1

© 2008

1
Heikki Koivo © 2008

1
EXERCISE 1.

Let y = f(x) = -2x - x2.


a) Form a fuzzy system, which approximates function f, when
x ∈ [− 10,10].
Repeat the same by adding random, normally distributed noise
with zero mean and unit variance.
b) Simulate the output of the fuzzy system when the input is
sin(t). Observe what happens to the signal shape at the output.

SOLUTION:

% Generate input-output data and plot it


x=[-10:.5:10]'; y=-2*x-x.*x;
plot(x,y,'.')
grid
xlabel('x');ylabel('output');title('Nonlinear characteristics')

Nonlinear characteris tic s


20

-20

-40
output

-60

-80

-100

-120
-10 -8 -6 -4 -2 0 2 4 6 8 10
x

Fig.E.1. Plot of the parabola.

2
% Store data in appropriate form for genfis1 and anfis and plot it

data=[x y];
trndata=data(1:2:size(x),:);
chkdata=data(2:2:size(x),:);
plot(trndata(:,1),trndata(:,2),'o',chkdata(:,1),chkdata(:,2),'x')
xlabel('x');ylabel('output');title('Measurement data'); grid

Training and chec king data


20

-20

-40
output

-60

-80

-100

-120
-10 -8 -6 -4 -2 0 2 4 6 8 10
x

Fig. E.2. Training data (o) and checking data (x) generated from the
parabolic equation.

Initialize the fuzzy system with command genfis1. Use 5 bellshaped


membership functions.

nu=5; mftype='gbellmf'; fismat=genfis1(trndata, nu, mftype);

The initial membership functions produced by genfis1 are plotted

plotmf(fismat,'input',1)
xlabel('x');ylabel('output');title('Initial membership functions'); grid

3
Initial m em bership func tions

in1m f1 in1m f2 in1m f3 in1m f4 in1m f5


1

0.8

0.6
output

0.4

0.2

-10 -8 -6 -4 -2 0 2 4 6 8 10
x

Fig. E.3. Initial fuzzy system (fismat) for anfis.

Next apply anfis-command to find the best FIS system - max number of
iterations = 100
numep=100;
[parab,trnerr,ss,parabcheck,chkerr]=anfis(trndata,fismat,numep,[],chkdata);

Evaluate the output of FIS system using input x

anfi=evalfis(x,parab);
plot(trndata(:,1),trndata(:,2),'o',chkdata(:,1),chkdata(:,2),'x',x,anfi,'-')
grid
xlabel('x');ylabel('output');title('Goodness of fit')

4
Goodnes s of fit
20

-20

-40
output

-60

-80

-100

-120
-10 -8 -6 -4 -2 0 2 4 6 8 10
x

Fig. E.4. Fitting the trained fuzzy system on training data.

Are there enough points?


Why checking data should be in between rather than following
training data?

Another approach using Anfis GUI.

Once you have opened Fuzzy Toolbox GUI, choose New Sugeno
system

5
Fig. E.5. Generate a new Sugeno-type fuzzy system.

Fig. E.6. Display of new Sugeno-type system.

6
Fig. E.7. Generate anfis display.

Fig. E.8. Anfis Editor display.

7
Fig. E.9. Load training data.

Fig. E.10. Plot of training data. Note that x-axis indicates numbering of data
points rather than absolute values.

8
Next generate the initial FIS matrix

Fig. E. 11. Generate initial FIS matrix using Grid partition.

9
Fig. E.11. Default membership function type (gaussmf) and their
number (4).

10
Fig. E.12. Training when error tolerance is chosen to be 0.001 and
number of epochs is limited to 100.

11
EXERCISE 2. Study how the nonlinearity modeled with the fuzzy
system fismat1 distorts a sinusoidal signal. Assume sin(t) at the
input.

Remark: Clearly higher order harmonics are generated. Such


phenomenon can be observed e.g. in electrical transformers.

SOLUTION:
This exercise should be continued immediately after Exercise 1,
because MATLAB assumes that Fuzzy system matrix parab is
available.
Otherwise you must repeat exercise 1.

Open SIMULINK in MATLAB Command Window by typing


simulink. Open a new File to configure the system.

In the SIMULINK Library open Fuzzy Logic Toolbox. Now you can
choose either Fuzzy Logic Controller block or Fuzzy Logic
Controller with ruleviewer. Membership Functions block is not
useful in normal control design.

The alternative is to type fuzblock on MATLAB command side. This


will also open the fuzzy block library.

12
13
Put the cursor on top of Fuzzy Logic Controller block and press your
mouse down. Keep it down and drag the block to the Untitled File.
The alternative is to use copy – paste commands, i.e., activate Fuzzy Logic
Controller block by moving the cursor on top of it and clicking once. Then
press Control button down with C. This will copy the block. Now activate
the Untitled file. Press Control button and V. This will paste the block on
Untitled file. Try both of the methods mentioned.

14
Similarly open Sources library and pick Sine Wave block and drag it to
your Untitled file. Finally, open Sinks library, choose Scope and drag it to
your Untitled file. Now the components of your system are in place.

Next place the cursor at the output of Sine Wave block and keeping the
mouse pressed down, connect the line to the input of Fuzzy Logic
Controller.

Fig. Form the connecting line between the output of the Sine wave block
and the input of the Fuzzy Logic Controller.

15
Connect also the output of the Fuzzy Logic Controller to the input of
Scope. Scope will be used to see the output.

Open the Fuzzy Logic Control block and type in the FIS matrix that you
determined in Exercise 1: parab.

Now you are ready to simulate. Choose Simulation and Start from the
Menu as shown in Fig.

16
Open Scope to see the result.

Comparison between the actual function –2x-x2 and the fuzzy system
approximation is shown below.

17
REMARK:
Combining the Fuzzy system with SIMULINK is an important feature from
the user's point of view. Once the fuzzy system has been determined, it can
be used in SIMULINK to simulate dynamical systems. This provides the
user a very powerful tool to investigate behavior of complex systems.
Computation by hand is tedious and in practice impossible without a
computer, but it is at this point when Fuzzy Systems become really
interesting and exciting.

Next we'll try combining fuzzy systems with dynamical systems.

18
EXERCISE 3. Simulate the system

x = −0.6 x + f ( x)

with different initial conditions. Here f(x) = -2x - x2 as before. Start with
x(0) = −0.2 and x (0) = 0.5 .

Study both the exact relationship and the fuzzy approximation determined
in the previous example. Are there any differences?

SOLUTION:
Here we study only the fuzzy approximation. Set up the following
SIMULINK diagram as explained above.

The Fuzzy Logic Controller block is defined to be parab as before. This


approximates the function f(x)= -2x - x2.

The nonlinear system is stable in a small region around the origin, so pick
initial values to be x(0) = −0.2 and x (0) = 0.5 . Open the integrators to set
the initial conditions.

19
The simulation produces the following result.

20
It is also interesting to study the phase plane. For that purpose introduce
XY Graph.

The phase plane plot of the system is shown below.

21
PROBLEM 1. Consider a nonlinear process

x (t ) = 0.5 x 2 (t ) − x(t ) + u (t )

a. Set the right hand side = 0 and solve for the operation points as a
function of input.
b. Design a (discrete) PI type of fuzzy controller for the system.
Sampling interval is h=0.05 s.

22

You might also like