Sie sind auf Seite 1von 8

Fuzzy Logic

Toolbox
For Use with MATLAB

Computation
Visualization
Programming

Users Guide
Version 2

How to Contact The MathWorks:


www.mathworks.com
comp.soft-sys.matlab

Web
Newsgroup

info@mathworks.com

Technical support
Product enhancement suggestions
Bug reports
Documentation error reports
Order status, license renewals, passcodes
Sales, pricing, and general information

508-647-7000

Phone

508-647-7001

Fax

The MathWorks, Inc.


3 Apple Hill Drive
Natick, MA 01760-2098

Mail

support@mathworks.com
suggest@mathworks.com
bugs@mathworks.com
doc@mathworks.com
service@mathworks.com

For contact information about worldwide offices, see the MathWorks Web site.

Fuzzy Logic Toolbox Users Guide


COPYRIGHT 1995 - 2002 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by
or for the federal government of the United States. By accepting delivery of the Program, the government
hereby agrees that this software qualifies as "commercial" computer software within the meaning of FAR
Part 12.212, DFARS Part 227.7202-1, DFARS Part 227.7202-3, DFARS Part 252.227-7013, and DFARS Part
252.227-7014. The terms and conditions of The MathWorks, Inc. Software License Agreement shall pertain
to the governments use and disclosure of the Program and Documentation, and shall supersede any
conflicting contractual terms or conditions. If this license fails to meet the governments minimum needs or
is inconsistent in any respect with federal procurement law, the government agrees to return the Program
and Documentation, unused, to MathWorks.
MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered trademarks, and
TargetBox is a trademark of The MathWorks, Inc.
Other product or brand names are trademarks or registered trademarks of their respective holders.

Printing History: January 1995


First printing
April 1997
Second printing
January 1998
Third printing
September 2000 Fourth printing
June 2001
Online only
July 2002
Online only

Revised for Version 2.1 (Release 12)


Revised for Version 2.1.1
Revised for Version 2.1.2 (Release 13)

Working with Simulink

Working with Simulink


The Fuzzy Logic Toolbox is designed to work seamlessly with Simulink, the
simulation software available from The MathWorks. Once youve created your
fuzzy system using the GUI tools or some other method, youre ready to embed
your system directly into a simulation.

An Example: Water Level Control


Picture a tank with a pipe flowing in and a pipe flowing out. You can change
the valve controlling the water that flows in, but the outflow rate depends on
the diameter of the outflow pipe (which is constant) and the pressure in the
tank (which varies with the water level). The system has some very nonlinear
characteristics.

A controller for the water level in the tank needs to know the current water
level and it needs to be able to set the valve. Our controllers input will be the
water level error (desired water level minus actual water level) and its output
will be the rate at which the valve is opening or closing. A first pass at writing
a fuzzy controller for this system might be the following.
1. If (level is okay) then (valve is no_change) (1)
2. If (level is low) then (valve is open_fast) (1)
3. If (level is high) then (valve is close_fast) (1)
One of the great advantages of the Fuzzy Logic Toolbox is the ability to take
fuzzy systems directly into Simulink and test them out in a simulation
environment. A Simulink block diagram for this system is shown below. It

2-63

Tutorial

contains a Simulink block called the Fuzzy Logic Controller block. The
Simulink block diagram for this system is sltank. Typing
sltank

at the command line, causes the system to appear.At the same time, the file
tank.fis is loaded into the FIS structure tank.

Some experimentation shows that three rules are not sufficient, since the
water level tends to oscillate around the desired level. This is seen from the
following plot.
2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0
0

2-64

10

20

30

40
50
60
Time (second)

70

80

90

100

Working with Simulink

We need to add another input, the water levels rate of change, to slow down
the valve movement when we get close to the right level.
4. If (level is good) and (rate is negative), then (valve is close_slow) (1)
5. If (level is good) and (rate is positive), then (valve is open_slow) (1)
The demo, sltank is built with these five rules. With all five rules in
operations,you can examine the step response by simulating this system. This
is done by clicking Start from the pull-down menu under Simulate, and
clicking the Comparison block. The result looks like this.
2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0
0

10

20

30

40
50
60
Time (second)

70

80

90

100

One interesting feature of the water tank system is that the tank empties much
more slowly than it fills up because of the specific value of the outflow diameter
pipe. We can deal with this by setting the close_slow valve membership
function to be slightly different from the open_slow setting. A PID controller
does not have this capability. The valve command versus the water level
change rate (depicted as water) and the relative water level change (depicted

2-65

Tutorial

as level) surface looks like this. If you look closely, you can see a slight
asymmetry to the plot.

0.8
0.6
0.4
valve

0.2
0
0.2
0.4
0.6

0.8

0.5

0.1

0.05
0

0.5
0.05

water

0.1

level

Because the MATLAB technical computing environment supports so many


tools (like the Control System Toolbox, the Neural Network Toolbox, the
Nonlinear Control Design Blockset, and so on), you can, for example, easily
make a comparison of a fuzzy controller versus a linear controller or a neural
network controller.
For a demonstration of how the Rule Viewer can be used to interact with a
Fuzzy Logic Controller block in a Simulink model, type
sltankrule

This demo contains a block called the Fuzzy Controller With Rule Viewer block.
In this demo, the Rule Viewer opens when you start the Simulink simulation.
This Rule Viewer provides an animation of how the rules are fired during the
water tank simulation. The windows that open when you simulate the
sltankrule demo are depicted as follows.

2-66

Working with Simulink

The Rule Viewer that opens during the simulation can be used to access the
Membership Function Editor, the Rule Editor, or any of the other GUIs, (see
The Membership Function Editor, or The Rule Editor, for more
information).

2-67

Tutorial

For example, you may want to open the Rule Editor to change one of your rules.
To do so, select the Edit rules menu item under the View menu of the open
Rule Viewer. Now you can view or edit the rules for this Simulink model.

Its best if you stop the simulation prior to selecting any of these editors to
change your FIS. Remember to save any changes you make to your FIS to the
workspace before you restart the simulation.

Building Your Own Fuzzy Simulink Models


To build your own Simulink systems that use fuzzy logic, simply copy the Fuzzy
Logic Controller block out of sltank (or any of the other Simulink demo
systems available with the toolbox) and place it in your own block diagram. You
can also find the Fuzzy Logic Controller block in the Fuzzy Logic Toolbox
library, which you can open either by selecting Fuzzy Logic Toolbox in the
Simulink Library Browser, or by typing
fuzblock

at the MATLAB prompt.

2-68

Das könnte Ihnen auch gefallen