Fuzzy Logic Toolbox    

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 controller's 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.

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 contains a Simulink block called the Fuzzy Logic Controller block. The Simulink block diagram for this system is sltank. Typing

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.

We need to add another input, the water level's rate of change, to slow down the valve movement when we get close to the right level.

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

.

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 as level) surface looks like this. If you look closely, you can see a slight asymmetry to the plot.

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

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.

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).

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.

It's 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.


  Working with Simulink Building Your Own Fuzzy Simulink Models