You are on page 1of 7

Beginners Tutorial

To demonstrate the fuzzy logic control capabilites of MATLAB we will


simulate the example that was previously worked through. We will use the
same input and output membership functions; A1, A2, B1 and B2. To begin,
we will define a simulink model that will be used to run the simulation. Open
MATLAB and Simulink and create a new file. Add a ramp input, Scope, and
Fuzzy Logic Controller with Ruleviewer to the file as shown. The Fuzzy
Logic Controller with Ruleviewer block can be found in the fuzzy logic
toolbox or by typing the name in the search box of the Library Browser.

Once your simulink file has been created as shown above save the file, for
this example the file name used is tut1. Now that the overall system has been
defined the fuzzy controller needs to be designed. To do this the Fuzzy
Inference System (FIS) editor will be used. To open a new FIS file with the
FIS editor, type the following into MATLAB:
>> fuzzy
The following window will open; it is the Fuzzy Inference System editor.
With the FIS editor the input and output membership functions can be defined,
the rule base is defined, and the fuzzy operators are set.

The default system has one input and one output and uses the Mamdani
inference and aggregation method. This editor also illustrates the three aspects
of a fuzzy contoller; fuzzification, inference, and defuzzification. To begin, let
us define the membership functions. Double click on the yellow input box and
you will be brought to the membership function editor.

By default there are three membership functions, mf1, mf2, and mf3. We
will redefine mf1 and mf2 as A1 and A2 and delete mf3. To delete a
membership function simply click on it, and when it is highlighted in red,
press delete. To manipulate the membership functions simply click on them
and drag them. Click on the line itself to move the function and click on the
boxes at the corners and drag to change the shape. If you know the shape
already, you can also select the function and enter the shape in the Params
box. The name can also be changed using the Name box. The range can be
redefined as well, for the input we want the range to be 0 to 3 so we will put
[0 3] in the Range box. For A1 the paramaters are [0 1 2] giving a triangle that
starts at 0, peaks at 1, and ends at 2. The paramaters for A2 are [1 2 3]. After
these have been entered the membership function editor should look like this:

While still in the membership function editor the output membership


functions can be defined. In the top left, under FIS Variables, click on the blue
output1 box. This will bring you into the output membership function editor.
Again, mf3 needs to be deleted and mf1 and mf2 need to be redefined the
match B1 and B2. The paramaters for B1 are [0 8 12] and B2 are [4 10 15].
The range is [0 15]. Once these have been updated that output membership
functions should look like this:

The membership function editor can now be closed to return to the FIS
editor. The FIS now needs to have rules defined. To do this double click on the
white box in between the input and output membership functions. This will
bring up the Rule Editor, this is where the linguistic inference rules are
entered. These rules are defined using the fuzzy membership functions labels
A1, A2, B1, and B2. To enter a rule select the desired 'if' option and 'then'
option and click 'add rule' at the bottom. We will use two rules for this system:
1. If A1 then B1,as well as 2.If A2 then B2. After these rules are entered the
Rule editor should look like this:

This window can be closed and now the FIS is finished, it only needs to be
exported to the Simulink model. To do this from the FIS editor,
under File theExport and then To Workspace... and enter the name of the
Simulink model in the Workspace Variable field, for this case it is 'tut1'. Now,
in Simulink, double click on the Fuzzy logic controller and set the FIS matrix
to tut1 and change the refresh rate to 0.01. Now the simulation can be run for
3 seconds. While the simulation is running the Rule Viewer window will pop
up:

The Rule Viewer window shows the relevant input and output membership
functions for each rule. Rule 1 involves A1 and B1, so they are shown, and
rule 2 involves A2 and B2. For the given input the modified output
membership function is shown in solid blue. These are them aggregated and
shown in the bottom right membership function. This is the fuzzified result.
For the input of 1.25 both rules are firing and the aggregate membership
function (bottom right) is the same as previously determined. The default
defuzzification process is the centroid method and the red line indicates where
the centroid is and so also what the output is. To view a video of this system
running click on the following link. In this video the input is increased linearly
and the result of each rule can be seen as the input changes as well as the
aggregate output.
Video of System Running (1.2MB)
The files created in this tutorial can all be found in the following zip file.
Simulation Files (<1MB)

You might also like