Project2-Level Tank With Fuzzy Controllerpdf
Project2-Level Tank With Fuzzy Controllerpdf
Project2-Level Tank With Fuzzy Controllerpdf
For more information on creating fuzzy inference systems, see “Build Mamdani Systems
Using Fuzzy Logic Designer” on page 2-32 and “Build Mamdani Systems at the Command
Line” on page 2-71.
For this example, you control the level of water in a tank using a fuzzy inference system
implemented using a Fuzzy Logic Controller block. Open the sltank model.
open_system('sltank')
2-84
Simulate Fuzzy Inference Systems in Simulink
For this system, you control the water that flows into the tank using a valve. The outflow
rate depends on the diameter of the output pipe, which is constant, and the pressure in
the tank, which varies with water level. Therefore, the system has nonlinear
characteristics.
The two inputs to the fuzzy system are the water level error, level, and the rate of
change of the water level, rate. The output of the fuzzy system is the rate at which the
control valve is opening or closing, valve.
To implement a fuzzy inference system, specify the FIS name parameter of the Fuzzy
Logic Controller block as the name of a FIS structure in the MATLAB® workspace. In this
example, the block uses the FIS structure tank.
For more information on this system, see “Water Level Control in a Tank” on page 2-96.
As a first attempt to control the water level, set the following rules in the FIS. These rules
adjust the valve based on only the water level error.
2-85
2 Tutorial
open_system('sltank/Comparison')
sim('sltank',100)
2-86
Simulate Fuzzy Inference Systems in Simulink
2-87
2 Tutorial
These rules are insufficient for controlling the system, since the water level oscillates
around the setpoint.
To reduce the oscillations, add two more rules to the system. These rules adjust the valve
based on the rate of change of the water level when the water level is near the setpoint.
• If the water level is okay and increasing, then close the valve slowly.
• If the water level is okay and decreasing, then open the valve slowly.
rule4 = "If level is okay and rate is positive then valve is close_slow";
rule5 = "If level is okay and rate is negative then valve is open_slow";
rules = [rule1 rule2 rule3 rule4 rule5];
tank = parsrule(tank,rules);
sim('sltank',100)
2-88
Simulate Fuzzy Inference Systems in Simulink
2-89
2 Tutorial
You can also simulate fuzzy systems using the Fuzzy Logic Controller with Ruleviewer
block. The sltankrule model is the same as the sltank model, except that it uses the
Fuzzy Logic Controller with Ruleviewer block.
open_system('sltankrule')
2-90
Simulate Fuzzy Inference Systems in Simulink
During simulation, this block displays the Rule Viewer from the Fuzzy Logic Designer
app.
sim('sltankrule',100)
2-91
2 Tutorial
2-92
Simulate Fuzzy Inference Systems in Simulink
If you pause the simulation, you can examine the FIS behavior by manually adjusting the
input variable values in the Rule Viewer, and observing the inference process and output.
You can also access the Fuzzy Logic Designer editors from the Rule Viewer. From the
Rule Viewer, you can then adjust the parameters of your fuzzy system using these editors,
and export the updated system to the MATLAB workspace. To simulate the updated FIS,
restart the simulation. For more information on using these editors, see “Build Mamdani
Systems Using Fuzzy Logic Designer” on page 2-32.
2-93
2 Tutorial
Simulation Modes
The Fuzzy Logic Controller block has the following two simulation modes:
To select a simulation mode, set the Simulate using parameter of the block. By default,
the block uses Interpreted execution mode for simulation.
2-94
See Also
The following table shows the block parameters that map to evalfisOptions
properties.
The remaining parameters of the Fuzzy Logic Controller block do not map to arguments
of evalfis.
See Also
Blocks
Fuzzy Logic Controller | Fuzzy Logic Controller with Ruleviewer
More About
• “Temperature Control in a Shower” on page 2-104
• “Water Level Control in a Tank” on page 2-96
2-95
2 Tutorial
Simulink Model
This model controls the level of water in a tank using a fuzzy inference system
implemented using a Fuzzy Logic Controller block. Open the sltank model.
open_system('sltank')
For this system, you control the water that flows into the tank using a valve. The outflow
rate depends on the diameter of the output pipe, which is constant, and the pressure in
the tank, which varies with water level. Therefore, the system has nonlinear
characteristics.
2-96
Water Level Control in a Tank
The fuzzy system is defined in a FIS structure, tank, in the MATLAB® workspace. For
more information on how to specify a FIS in a Fuzzy Logic Controller block, see Fuzzy
Logic Controller.
The two inputs to the fuzzy system are the water level error, level, and the rate of
change of the water level, rate. Each input has three membership functions.
figure
plotmf(tank,'input',1)
figure
plotmf(tank,'input',2)
2-97
2 Tutorial
The output of the fuzzy system is the rate at which the control valve is opening or closing,
valve, which has five membership functions.
plotmf(tank,'output',1)
2-98
Water Level Control in a Tank
Due to the diameter of the outflow pipe, the water tank in this system empties more
slowly than it fills up. To compensate for this imbalance, the close_slow and
open_slow valve membership functions are not symmetrical. A PID controller does not
support such asymmetry.
The fuzzy system has five rules. The first three rules adjust the valve based on only the
water level error.
2-99
2 Tutorial
The other two rules adjust the valve based on the rate of change of the water level when
the water level is near the setpoint.
• If the water level is okay and increasing, then close the valve slowly.
• If the water level is okay and decreasing, then open the valve slowly.
showrule(tank)
ans =
In this model, you can also control the water level using a PID controller. To switch to the
PID controller, set the const block to a value greater than or equal to zero.
Simulation
The model simulates the controller with periodic changes in the setpoint of the water
level. Run the simulation.
sim('sltank',100)
open_system('sltank/Comparison')
2-100
Water Level Control in a Tank
2-101
2 Tutorial
The water level tracks the setpoint well. You can adjust the performance of the controller
by modifying the rules of the tank FIS. For example, if you remove the last two rules,
which are analogous to a derivative control action, the controller performs poorly, with
large oscillations in the water level.
See Also
Blocks
Fuzzy Logic Controller | Fuzzy Logic Controller with Ruleviewer
2-102
See Also
More About
• “Simulate Fuzzy Inference Systems in Simulink” on page 2-84
• “Temperature Control in a Shower” on page 2-104
2-103