Está en la página 1de 26

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Create a Fuzzy Inference System Using Commands in M-file


Step 1: In Matlab main console window, select FileNewM File. The M File
Editor window is pop-up.
Step 2: Type the following code into your M File Editor and save it as M1.mat.
Notice that the same code can be obtained by Select FileOpen
MFile1.mat in M File Editor Window.
a = newfis('fis1.fis'); %create a new FIS file in "work" folder
%add and input variable 'x' into the FIS
a = addvar(a, 'input', 'x', [2 9]);
%remove the 3 MFs created by default when we add an input variable
a = rmmf(a, 'input', 1, 'mf1', 1);
a = rmmf(a, 'input', 1, 'mf2', 1);
a = rmmf(a, 'input', 1, 'mf3', 1);
%add 2 MFs into the variable x
a = addmf(a, 'input', 1, 'A1', 'trimf', [2 5 8]);
a = addmf(a, 'input', 1, 'A2', 'trimf', [3 6 9]);
%add and input variable 'x' into the FIS
a = addvar(a, 'input', 'y', [4 11]);
%remove the 3 MFs created by default when we add an input variable
a = rmmf(a, 'input', 2, 'mf1', 1);
a = rmmf(a, 'input', 2, 'mf2', 1);
a = rmmf(a, 'input', 2, 'mf3', 1);
%add 2 MFs into the variable y
a = addmf(a, 'input', 2, 'B1', 'trimf', [5 8 11]);
a = addmf(a, 'input', 2, 'B2', 'trimf', [4 7 10]);
%add and output variable 'y' into the FIS
a = addvar(a, 'output', 'z', [1 9]);
%remove the 3 MFs created by default when we add an output variable
a = rmmf(a, 'output', 1, 'mf1', 1);
a = rmmf(a, 'output', 1, 'mf2', 1);
a = rmmf(a, 'output', 1, 'mf3', 1);
%add 2 MFs into the variable x
a = addmf(a, 'output', 1, 'C1', 'trimf', [1 4 7]);
a = addmf(a, 'output', 1, 'C2', 'trimf', [3 6 9]);
%1 rule is defined and added into FIS
rule = [1 1 1 1 1];
a = addrule(a, rule);
%The 1st '1' is mean that the 1st MF of 1st input variable will be use in this rule, in this case, A1 will be use
%You can put '2' if you want to use 2nd MF of 1st input variable, use '0' if you do not wise the rule involve with 1st input variable
%The 2nd '1' is mean that the 1st MF of 2nd input variable will be use in this rule, in this case, B1 will be use
%You can put '2' if you want to use 2nd MF of 1st input variable, use '0' if you do not wise the rule involve with 2nd input variable
%The 3rd '1' is mean that the 1st MF of output variable will be use in this rule, in this case, C1 will be use
%You can put '2' if you want to use 2nd MF of output variable
%The 4th '1' is mean that this rule is create and Enable, use '0' to to create the rule but Disable it.
%The 5th '1' is mean that the relationship between MF of 1st input variable and MF 2nd input variable is "AND"
%Use '2' is you want it to be "OR"
%Conclusion, this will add a fule, if x is A1 and y is B1 Then z is C1

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


writefis(a, 'fis1.fis');
disp('A new FIS is created and save as \work\fis1.fis');
disp('Type "Fuzzy fis1.fis" in console window to see the new FIS created');
disp('End of the program mfile1.m');

To View the FIS created


This can be done by Type Fuzzy fis1 (the .fis in behind can be ignore).
The FIS Editor will be shown as in Figure 1 below.

Figure 1

You can change the setting of the FIS here, such as change the Defuzzification to
MOM rather using COA (See Figure 1).
Double Click on the part show the MFs of x in order to see (and change) the detail
information of MFs of input x created in the code as shown in Figure 2.
Same steps can be done to for variables y and z.

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Figure 2

Select EditRule to view and edit the rule added in the code above. The
Rule Editor is shown in Figure 3 below.

Figure 3
3

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Select ViewSurface to see the decision surface base on the rule(s) created.
The Surface Viewer is shown in Figure 4 below.

Figure 4

Select ViewRules to see how to inference system work in order to product


and output based on the inputs given. We can use to mouse to drag and place the
red lines in order to change the inputs and to see the new output as well. See
Figure 5 for detail

Figure 5
4

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

As a conclusion, to create a new FIS via command is slow. The FIS Edit actually
can be use to create or modify FIS in very short time compare to command coding
method.

Exercise: Modify the code in MFile1.m above so that it can take three rules as
below.
R1: If x is A1 and y is B1 then z is C1
R2: If x is A2 and y is B2 then z is C2
R3: If x is A1 or y is B2 then z is C2
Solution:
Modify only one line in to code
rule = [1 1 1 1 1; 2 2 2 1 1; 1 2 2 1 2];
You can use FIS editor to check your answer, notice that the surface viewer will not
be the same as before since rules are added.
Another way to see the result is typing command in console windows.
F = readfis(fis1.fis) %Press enter key
%information of the FIS will be show.
Showrule(F);
%the rule of this FIS will be show.
Create common FIS use for Control Systems Using FIS Editor.
Step 1: Type Fuzzy in the Matlab console window. The FIS edit will be show.
Step 2: Select EditAdd VariableInput to add the second input variable.
Step 3: Highlight on input1, change the name of input1 to E (error), as shown in
picture below. The rest of information is no change. See Figure 6 for detail

P
R
Q

Name of variable is E

Figure 6
5

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Step 4: Repeat step 3 for input2 become DE (change of error) and the output
become U (control signal).
Step 5: Double click on the picture of MFs for variable E, as shown in area P in
Figure 6, in order to shown the MFs of variable E. By default when a variable is
added, 3 MFs will be automatically. See Figure 7.
Step 6: It will be easy for us to delete all the default MFs, this can be done by select
EditRemove All MFs as shown in Figure 7. After this, no more MFs for input
variable E.

Figure 7
Step 7: Now we plan to add 7 MFs to input variable E, this can be done by select
EditAdd MFs. After select this, another dialog box is pop up as shown in
Figure 8. Select Type MFs as gaussmf and number of MFs is 7. See Figure 8 for
detail.

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Figure 8
Step 8: Select OK by click on the OK button. Now 7 MFs have been add into
input variable E as shown in Figure 9

Figure 9

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Step 9: Highlight the mf1 as shown in Figure 9. Change the name of mf1 to NB as
shown in area X
Step 10: Repeat Step 9 for mf2 to mf7, to change their names become NM, NS, ZR,
PS, PM and PB.
Step 11: Change the range of the variable to [-1 1] as shown in area Y.
Step 12: Repeat Step 5 to Step 11 but for input variable DE.
Step 13: Repeat Step 5 to Step 12 but for output variable U.
Step 14: Select EditRule, the rule editor windows is shown as Figure 10.

Figure 10
Figure 10
Step 15: Add the 1st rule into rule editor base on what we have learn in Module 3.
E.g the 1st rule is: If E is NB and DE is NB then U is NB
Step 16: Repeat Step 16 to create 2nd until 49th rules.
Step 17: Now we can select ViewSurfaces to see the decision surface of this
general fuzzy logic control, or select ViewRules to see the inference process
and the output when inputs are given.
Step 18: Select FileExportDisk to save the FIS created into disk. Do type
a file name when you are being as to do so. See Figure 11 for detail.

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Figure 11
Step 19: If you want the FIS to load into memory, select
FileExportWorkspace Do type a workspace name when you are being as to
do so. See Figure 11 for detail.
Step 20: Test your FIS created in console windows of Matlab, type the commands
below.
>> f = readfis('fis2.fis') %Command that you type, read the fis into pointer f
f = %information show by matlab
name: 'fis2'
type: 'mamdani'
andMethod: 'min'
orMethod: 'max'
defuzzMethod: 'centroid'
impMethod: 'min'
aggMethod: 'max'
input: [1x2 struct]
output: [1x1 struct]
rule: [1x49 struct]

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


>> evalfis([0.2 0.9], f) %command that you type, meaning if E = 0.2 and DE = 0.9, what is U?
ans =

%answer calculated by Matlab, U = 0.7530


0.7530

A Fuzzy Logic Controller for Water bath Temperature Control


Problem statement: In this demonstration, we shall design a fuzzy logic controller for the
temperature control of a water bath. The plant to be controlled is described by:
b(T )u (k )
(1 a (T ))Y0
1 exp(0.5 y (k ) )
(1 exp(T ))
Where a (T ) exp(T ) and b(T )
. The parameters of the plant
y (k 1) a (T ) y (k )

o
, 8.7 x10 3 , 40 and Y0 25 C . The plant input u(k) is

are set as 1x10


limited to between 0 and 5 volts. The sampling period, T, is set as 25 seconds. The goal is
to design a fuzzy controller what will control the water temperature to follow a reference
profile as closely as possible. This reference profile is 35 o C for 0 t 40 minutes,
50 o C for 40 t 80 minutes, 65 o C for 80 t 120 minutes and 80 o C for
120 t 180 minutes.
4

The input variables for this controller are chosen as e(k), de(k) where e(k) is the
performance error indicating the error between the desire output and the actually output.
The de(k) is the change of the error. The output of the controller is the voltage that
limited to between 0 to 5 volts.
The source code is shown as below, where we use the FIS (fis2.fis) designed in previous
section in this application. Notice that the same source code can be found in
\work\mfile2.m
% =======================
%
Filename : mfile2
% =======================
clear; %clear the memory of matlab
echo on; %to show result of every command
clc;%to clear the matlab console screen
%
%
%

===========================================================
A Fuzzy Controller for the Water Bath Temperature Control
===========================================================

% ====================================================================
% Problem Statement: To design a fuzzy controller for
% the temperature control of a water bath.
% The equations and the parameters of the plant

10

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


% to be controlled are described in Module 3.
% The goal is to design a fuzzy controller that will
% control the water temperature to follow a
% reference profile as close as possible.
%
% =========================================================
% Notice that the FIS used is desgined using FIS editor.
% =========================================================
%
%
%

===============
Test program
===============

pause % Hit any key to begin this program.


% Read the FIS file .
f = readfis('fis2.fis');
pause % Hit any key to define the plant parameters.
T = 25;
p = 1*10^(-4);
q = 8.7*10^(-3);
r = 40.0;
yo = 25;
y(1) = yo;
a = exp(-p*T);
b=(q/p)*(1-exp(-p*T));
pause % Hit any key to define the reference output.
for k = 1:180
if (k <= 40)
ref(k) = 35;
elseif (k > 40 & k <= 80)
ref(k) = 50;
elseif (k > 80 & k <= 120)
ref(k) = 65;
elseif (k > 120)
ref(k) = 80;
end;
end;
pause % Hit any key to define the gain constanT.
GE = 1/15;
GDE = 1/15;
GU = 80;
pause % Hit any key to test this fuzzy controller.
for k = 1:179
e(k)=(ref(k)-y(k)); %error
ef(k)=e(k)*GE; %scaled of error
if k == 1
de(k) = 0; %change of error
else

11

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


de(k) = e(k)-e(k-1);
end;
def(k) = de(k)*GDE; %scaled of change of error
uf(k) = evalfis([ef(k) def(k)],f); %obtain the control signal using FIS base on the error and change of
error
u(k) = uf(k)*GU; %scale it back
%because in real world the control signal shall be in between 0 to 5 volt
if (u(k)>=5)
u(k)=5;
elseif (u(k)<=0)
u(k)=0;
end;
%put all the information including the new control signal into the plant
y(k+1)=a*y(k)+b/(1+exp(0.5*y(k)-r))*u(k)+(1-a)*yo;
end;
% ...end of testing.
pause % Hit any key to plot the plant input and output, and the reference output.
hold on;
grid;
plot(y(1:180),'.b');
plot(ref(1:180),'-r');
plot(u(1:179),'g');
xlabel('Sampling Time Step KT T=25 seconds');
ylabel('Temperature(degree)');
title('Reference Signal-- Actual Output . Control Signal __');
AES=sum(abs(ref-y))
echo off
disp('End of mfile2.m')

The absolute error obtained for this system is 446.9001. The graphs in Figure 12 shown
the result of FLC is satisfactory.

12

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Figure 12
Water Level Control in a Surge Tank Using Fuzzy Logic Controller
In this section we use a water level problem for a surge tank to show how to the fuzzy
logic controller work even though the reference point is keep changing fast all the time.
The discrete time equation of a surge tank we use is:
h(k 1) h( k ) T [

19.6h(k )
h (k ) 1
2

u (k )
] where T is the sampling rate that we set as
h (k ) 1
2

0.5 second.
However, we have other restrictions on the plant dynamic. In particular, we assume the
plant input, u(k) saturates as +50 and -50. This mean u(k) will be limited in between -50
to +50. Also to ensure that the liquid level never goes negative (which is physically
impossible), we simulate our plant using:

13

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

19.6h(k ) u(k )
h(k 1) max{0, h(k ) T[ 2 2 ]} .
h (k ) 1 h (k ) 1
For this application we try to test the ability of fuzzy logic controller on a fast response
plant, therefore the desire output is also change with time such as:
r (k ) cos(0.07 k ) sin(0.05k ) 2.7

The input and output variables are e(k), de(k) and du(k).Here the u(k) can be obtain base
on the equation: u(k) = u(k-1) + du(k).
The code is given as below. Notice that the same code can be found in mFile3.m.
%read FIS from file
f = readfis('fis2.fis');
%f = readfis('mfis1.fis');
%define the plant parameters
T = 0.5;
h0 = 3.5;
p = 1;
q = 2;
%define the gain or scaling factor for all variables
GE = 0.3;
GDE = 1;
GDU = 10;
%number of interval
index = 600;
%initial the level of water to h0
h(1) = h0;
for k = 1 : (index-1)
ref(k) = (cos(0.07*k)) + (sin(0.05*k)) + 2.7;
e(k) = ref(k)-h(k);
%the error use for FIS must be scaled/fuzzify
ef(k) = e(k)*GE;
if k == 1
de(k) = 0;
else
de(k) = e(k) - e(k-1);
end;
%the change of error use for FIS must be scaled/fuzzify
def(k) = de(k)*GDE;
%to void warning message because of bigger than 1 or smaller than -1

14

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


if ef(k) < -1
ef(k)= -1;
elseif ef(k) >= 1
ef(k) = 1;
else
ef(k) = ef(k);
end;
if def(k) < -1
def(k)= -1;
elseif def(k) >= 1
def(k) = 1;
else
def(k) = def(k);
end;
%evaluate the FIS based on the error and change of error calculated
duf(k) = evalfis([ef(k) def(k)], f);
%the output of FIS shall be scaled/defuzzify
du(k) = duf(k)*GDU;
if(k > 1)
u(k) = u(k-1) + du(k);
else
u(k) = du(k);
end;
%input to the plant shall be saturated
if (u(k) <= -50)
u(k) = -50;
elseif (u(k) >= 50)
u(k) = 50;
else
u(k) = u(k);
end;
%put the output of FIS into the plant for control purpose
a = -sqrt(19.6*h(k));
b = p*h(k)*h(k) + q;
h(k+1) = h(k) + T*((a/b) + (u(k)/b));
if (h(k+1)) < 0
h(k+1) = 0;
end;
end;
ref(index) = h(index);
%plot the plant output, target output and error of the plant
hold on;
grid;
plot(ref(1:index-1), '-r');
plot(h(1:index-1), 'b');
plot(e(1:index-1),'g');

15

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


xlabel('Sampling time step kT where T = 0.5 second');
ylabel('Temperature(degree)');
title('Target Output (red) Actual Output (blue) Error (green)');
AES=sum(abs(ref-h))

The absolute error obtained for this system is 22.9821. The result of this application is
given in Figure 13.

Figure 13
Exercise
For both water level and water temperature control applications, try to fine tune the
scaling/gain parameters of the input/output variables so that the absolute errors are
smaller.

16

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Using Fuzzy Logic Controller in Simulink for Single Link Robot Arm Control
In this demo, the objective is to control the movement of a simple, single-link robot arm,
as shown in the following figure.

Figure 14
d 2 x (t )
dx (t )
10 sin( x(t )) 2
u (t ) . Where
2
dt
dt
x(t) is the angle and u(t) is the control signal (torque). Here we model the single link arm
as a library before use it as a plant to test the fuzzy controller in Simulink. This library is
then saved as myrobotarm.mdl. Type myrobotarm in Matlab console window will
actually call it out. Figure 15 below shown this robot arm has been model as a library.

The equation of motion for the arm is

17

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Figure 15
In Simulink, fuzzy logic controller has been model as a library. If we need to use FIS in
Simulink, 1st, type fuzblock in Matlab console window. The fuzzy models are loaded as
shown in Figure 15.

Figure 15

18

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Once we have both libraries (robot arm and fuzzy controller), start a new Simulink
model, right click robot arm library and select copy, paste it into the new Simulink
model. Repeat the copy and paste but for the fuzzy logic controller. See Figure 16 for
detail. After that, add other model in Simulink such as Sum, Gain and Delay. Notice that
the Simulink model shown in Figure 16 is can be found in sim1.mdl.

Figure 16
However, the Simulink model shown in Figure 16 has not yet ready to run. This is
because the fuzzy logic control must be specific the FIS matrix, where the FIS used to be
design using FIS Editor.
To do this, design a new FIS or open a existing FIS with FIS Editor, select
FileExportWorkSpace. You shall type in a name as you are being as to do so.
E.g., you had just designed an FIS, and you export it to workspace with name fis3.fis.
The next thing to do is come back to your Simulink model, double click the FLC library,
a dialog box is show. See Figure 17 for detail.

Figure 17
As show in Figure 17, you must type in a valid FIS such as fis3.fis which is you just
export to workspace via FIS Editor.

19

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Finally we can start run the Simulink model by select SimulinkStart, a XY plots
shown the output of the fuzzy controlled robot arm compare to random reference points.
The result is shown in Figure 18 below.

Figure 18
Advantage Technique: Adaptive Neuro-Fuzzy-Inference-Systems (ANFIS)
This module is end with a short discussion on ANFIS. ANFIS was 1st proposed by Roger
Jang as a hybrid system between Fuzzy Inference Systems but with neural network
architecture. The learning is required in order to fine-tune the parameters of the
membership functions as well as the parameters of defuzzification. For detail of the
ANFIS, please see [1].
One of the used of ANFIS in control system is to learn the inverse of the plant, so that I
can be used as a controller after training phase. Figure 19 has shown the learning phase of
ANFIS. As an example, we take the water bath temperature system as the target plant to
be controlled. Notice that the learning of ANFIS is based on error back propagation as
well.

y (k 1)

u (k )

ANFIS

y (k ) z 1

z 1
y (k )
Plant, f

u (k )
Figure 19
20

y(k 1) f (u (k ), y (k ))

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

After the learning phase is finished, the trained ANFIS is then to be used as a controller
for the plant. The configuration of block to do this is shown in Figure 20.

z 1
y (k )
ANFIS

u (k )

y d (k 1)

Plant, f

y (k 1)

Figure 20

The Matlab source code for training of ANFIS is list out as below. Notice that same
source code can be found mFile4.m.
% =======================
%
Filename : mFile4.m
% =======================
clear;
echo on;
clc;
% ==================================================================
% ANFIS Controller for the Water Bath Temperature Control
% ==================================================================
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%

===================================================================
Problem Statement: To design an ANFIS controller for the
temperature control of a bath described in Module 3.
We shall implement the ANFIS controller using
the direct inverse control strategy shown in Fig 19 and 20.
We first obtain the training data by imposing random input voltages to
the water bath system and recording the corresponding temperatures.
The ANFIS is then trained to identify the inverse model
of the water bath system using the gathered training data.
To start the ANFIS training, we need an FIS matrix that specifies
the structure and initial parameters of the FIS for learning.
The user can use the command ``genfis1" to generate an FIS matrix
from the training data using the grid-type partition
according to the given number and types of membership functions.
In this demonstration, we use five
``gaussmf''-type membership functions.
===================================================================

21

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


%
%
%

=====================================
This is the Training program of ANFIS
=====================================

pause % Hit any key to begin this program.


pause % Hit any key to define the plant parameters.
Ts = 25;
p = 1*10^(-4);
q = 8.7*10^(-3);
r = 40.0;
yo = 25;
y(1) = yo;
a = exp(-p*Ts);
b = (q/p)*(1-exp(-p*Ts));
% Hit any key to define the training data.
pause % Here, we use 100 training data. You can try different number of patterns.
for k = 1:120
u(k) = rand(1,1)*5;
y(k+1) = a*y(k)+b/(1+exp(0.5*y(k)-r))*u(k)+(1-a)*yo;
end;
trndata = [y(2:101);y(1:100);u(1:100)]'; %The ' in behind of this code is very important!
pause % Hit any key to train the ANFIS structure.
% please wait ...
nummfs = 5;
mftype = 'gaussmf';
infismat = genfis1(trndata,nummfs,mftype);
[outfismat, error,stepsize] = anfis(trndata, infismat,5);
pause % Hit any key to save the parameters to the disk.
save myanfis.mat outfismat
%To run the "mFile5.m" to test the trained ANFIS.
echo off
disp('End of Mfile4.m')

Another source code listed below is for recall phase for ANFIS when it is used as
controller to control the temperature of water bath. Same code can be found in mFile5.m
% =======================
%
Filename : mFile5.m
% =======================
clear;
echo on;
clc;
%

==================================================================

22

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


%
%

ANFIS Controller for the Water Bath Temperature Control


==================================================================

%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%

===================================================================
Problem Statement: To design an ANFIS controller for the
temperature control of a bath described in Module 3.
We shall implement the ANFIS controller using
the direct inverse control strategy shown in Fig 19 and 20.
We first obtain the training data by imposing random input voltages to
the water bath system and recording the corresponding temperatures.
The ANFIS is then trained to identify the inverse model
of the water bath system using the gathered training data.
To start the ANFIS training, we need an FIS matrix that specifies
the structure and initial parameters of the FIS for learning.
The user can use the command ``genfis1" to generate an FIS matrix
from the training data using the grid-type partition
according to the given number and types of membership functions.
In this demonstration, we use five
``gbellmf''-type membership functions.
===================================================================

%
%
%

=====================================
This is the Testing program of ANFIS
=====================================

pause % Hit any key to begin this program.


pause % Hit any key to load the myanfis.mat
load myanfis.mat
pause % Hit any key to define the plant parameters.
Ts = 25;
p = 1*10^(-4);
q = 8.7*10^(-3);
r = 40.0;
yo = 25;
y(1) = yo;
a = exp(-p*Ts);
b = (q/p)*(1-exp(-p*Ts));
pause % Hit any key to define the reference output.
for k = 1:180
if (k <= 40)
ref(k) = 35;
elseif (k > 40 & k <= 80)
ref(k) = 50;
elseif (k > 80 & k <= 120)
ref(k) = 65;
elseif (k > 120)
ref(k)=80;
end;
end;
pause % Hit any key to test 180 time-steps.

23

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink


for k=1:179
u(k) = evalfis([ref(k+1) y(k)],outfismat);
if (u(k) >= 5)
u(k)=5;
elseif (u(k) <= 0)
u(k)=0;
else
u(k)=u(k);
end;
y(k+1)=a*y(k)+b/(1+exp(0.5*y(k)-r))*u(k)+(1-a)*yo;
end;
%

...end of testing.

pause % Hit any key to plot the plant input and output, and the reference output.
hold on;
grid;
plot(y(1:180),'.b');
plot(ref(1:180),'-r');
plot(u(1:179),'g');
xlabel('Sampling Time Step KT T = 25 seconds');
ylabel('Temperature(degree)');
title('Reference Signal-- Actual Output . Control Signal __');
AE=sum(abs(ref-y))
echo off
disp('End of mfile5.m')

The total absolute error of this ANFIS controller for this plant is 369.8952, which is
smaller than fuzzy logic controller that we discussed above. Also it produces a stable
control signal u(k). See Figure 21 for detail.

24

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

Figure 21

Major References
Book
1.
Jang R, Sun C. T, Mizutani E. Neuro-fuzzy and Softcomputing. Prentice Hall,
Upper Saddle River, NJ, 1997.
2.
Lin C.T, Lee G, Neural-fuzzy Systems. Prentice Hall, Upper Saddle River, NJ,
1995.
3.
Passino K.M, Yurkovich S. Fuzzy Control. Addison-Wesley, Carlifonia 1998.
4.
Tsoukalas L.H, Uhrig R.E. Fuzzy and Neural Approaches in Engineering. John
Willey & Son, Inc, USA, 1997.
25

By Yap Keem Siah

UNITEN Matlab Workshop: Fuzzy Logic Control 30 & 31 Oct 2002

Module 3: Matlab Fuzzy Logic Toolbox & Simulink

5.
6.

Singh K.K, Agnihotri. System Design Through Matlab Control Toolbox and
Simulink. Springer, London, 2001.
Gardner, J.H. Simulations of Machines Using Matlab and Simulink. Thomson
Learning, Pacific Grove, USA, 2001.

Journal Papers
7.
Jang R. ANFIS: Adaptive Network Based Fuzzy Inference Systems. IEEE
Transactions on Systems, Man, and Cybernetics, 23(03):665-685, May 1993.
8.
Lin C.T, Lee C.S. Neural Network Base Fuzzy Logic Control and Decision
System. IEEE Transaction on Computer, 40(12) 1320-1336, Dec.1991.
9.
Mamdani E.H. Application of Fuzzy Logic to Approximate Reasoning Using
Linguistic Systems. IEEE Transaction on Computer, C-26:1182-1183, 1977.
10.
Zadeh L.A. Fuzzy Sets. Information & Control. 8:338-353, 1965.
Internet
11.
http://www-isis.ecs.soton.ac.uk/research/ninfo/fuzzy.html
12.
http://www.cs.nthu.edu.tw/~jang/soft.htm
13.
http://www.mathworks.com/fuzzytbx.html

26

By Yap Keem Siah