Professional Documents
Culture Documents
By
Heikki Koivo1
© 2008
1
Heikki Koivo © 2008
1
EXERCISE 1.
SOLUTION:
-20
-40
output
-60
-80
-100
-120
-10 -8 -6 -4 -2 0 2 4 6 8 10
x
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
-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.
plotmf(fismat,'input',1)
xlabel('x');ylabel('output');title('Initial membership functions'); grid
3
Initial m em bership func tions
0.8
0.6
output
0.4
0.2
-10 -8 -6 -4 -2 0 2 4 6 8 10
x
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);
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
Once you have opened Fuzzy Toolbox GUI, choose New Sugeno
system
5
Fig. E.5. Generate a new Sugeno-type fuzzy system.
6
Fig. E.7. Generate anfis 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
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.
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.
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.
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.
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 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.
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