0% found this document useful (0 votes)
7 views28 pages

Control Temperatura Fuzzy

The document describes the implementation of a fuzzy inference system (FIS) for temperature control in a shower using Simulink, detailing the model's inputs, outputs, and fuzzy rules. It also explains the design of a fuzzy PID controller using a 2-D Lookup Table to enhance performance in nonlinear control applications. Additionally, it covers the simulation of closed-loop responses for different controller types, demonstrating their effectiveness in controlling a plant.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views28 pages

Control Temperatura Fuzzy

The document describes the implementation of a fuzzy inference system (FIS) for temperature control in a shower using Simulink, detailing the model's inputs, outputs, and fuzzy rules. It also explains the design of a fuzzy PID controller using a 2-D Lookup Table to enhance performance in nonlinear control applications. Additionally, it covers the simulation of closed-loop responses for different controller types, demonstrating their effectiveness in controlling a plant.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

2 Tutorial

Temperature Control in a Shower


This model shows how to implement a fuzzy inference system (FIS) in a Simulink® model.

Simulink Model

The model controls the temperature of a shower using a fuzzy inference system
implemented using a Fuzzy Logic Controller block. Open the shower model.
open_system('shower')

For this system, you control the flow rate and temperature of a shower by adjusting hot
and cold water valves.

Since there are two inputs for the fuzzy system, the model concatenates the input signals
using a Mux block. The output of the Mux block is connected to the input of the Fuzzy
Logic Controller block. Similarly, the two output signals are obtained using a Demux block
connected to the controller.

2-104
Temperature Control in a Shower

Fuzzy Inference System

The fuzzy system is defined in a FIS structure, fisMatrix, 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 temperature error, temp, and the flow rate
error, flow. Each input has three membership functions.

figure
plotmf(fisMatrix,'input',1)
figure
plotmf(fisMatrix,'input',2)

2-105
2 Tutorial

The two outputs of the fuzzy system are the rate at which the cold and hot water valves
are opening or closing, cold and hot respectively. Each output has five membership
functions.

figure
plotmf(fisMatrix,'output',1)
figure
plotmf(fisMatrix,'output',2)

2-106
Temperature Control in a Shower

2-107
2 Tutorial

The fuzzy system has nine rules for adjusting the hot and cold water valves based on the
flow and temperature errors. The rules adjust the total flow rate based on the flow error,
and adjust the relative hot and cold flow rates based on the temperature error.

showrule(fisMatrix)

ans =

9x86 char array

'1. If (temp is cold) and (flow is soft) then (cold is openSlow)(hot is openFast) (
'2. If (temp is cold) and (flow is good) then (cold is closeSlow)(hot is openSlow)
'3. If (temp is cold) and (flow is hard) then (cold is closeFast)(hot is closeSlow)

2-108
Temperature Control in a Shower

'4. If (temp is good) and (flow is soft) then (cold is openSlow)(hot is openSlow) (
'5. If (temp is good) and (flow is good) then (cold is steady)(hot is steady) (1)
'6. If (temp is good) and (flow is hard) then (cold is closeSlow)(hot is closeSlow)
'7. If (temp is hot) and (flow is soft) then (cold is openFast)(hot is openSlow) (1
'8. If (temp is hot) and (flow is good) then (cold is openSlow)(hot is closeSlow) (
'9. If (temp is hot) and (flow is hard) then (cold is closeSlow)(hot is closeFast)

Simulation

The model simulates the controller with periodic changes in the setpoints of the water
temperature and flow rate.

set_param('shower/flow scope','Open','on','Ymin','0','Ymax','1')
set_param('shower/temp scope','Open','on','Ymin','15','Ymax','30')
sim('shower',50)

2-109
2 Tutorial

The flow rate tracks the setpoint well. The temperature also tracks its setpoint, though
there are temperature deviations when the controller adjusts to meet a new flow setpoint.

bdclose('shower');

See Also
Blocks
Fuzzy Logic Controller

More About
• “Simulate Fuzzy Inference Systems in Simulink” on page 2-84
• “Water Level Control in a Tank” on page 2-96

2-110
Implement Fuzzy PID Controller in Simulink Using Lookup Table

Implement Fuzzy PID Controller in Simulink Using


Lookup Table
This example shows how to implement a fuzzy inference system for nonlinear PID control
using a 2-D Lookup Table block.

Overview

A fuzzy inference system (FIS) maps given inputs to outputs using fuzzy logic. For
example, a typical mapping of a two-input, one-output fuzzy controller can be depicted in
a 3-D plot. The plot is often referred to as a control surface plot.

2-111
2 Tutorial

For control applications, typical FIS inputs are the error (e(k)) and change of error
(e(k)-e(k-1)), E and CE respectively in the control surface plot. The FIS output is the
control action inferred from the fuzzy rules, u in the surface plot. Fuzzy Logic Toolbox™
provides commands and apps for designing a FIS for a desired control surface. You can
then simulate the designed FIS using the Fuzzy Logic Controller block in Simulink®.

You can often approximate nonlinear control surfaces using lookup tables to simplify the
generated code and improve execution speed. For example, you can replace a Fuzzy Logic
Controller block in Simulink with a set of Lookup Table blocks, one table for each output
defined in the FIS. You can compute the data used in the lookup table using the evalfis
command.

For this example, you design a nonlinear fuzzy PID controller for a plant in Simulink. The
plant is a single-input, single-output system in discrete time. The design goal is to achieve
good reference tracking performance.

Ts = 0.1;
Plant = c2d(zpk([],[-1 -3 -5],1),Ts);

You also implement the fuzzy inference system using a 2-D lookup table that approximates
the control surface and achieves the same control performance.

Fuzzy PID Controller Structure

The fuzzy controller in this example is in the feedback loop and computes PID-like actions
using fuzzy inference. Open the Simulink model.

open_system('sllookuptable')

2-112
Implement Fuzzy PID Controller in Simulink Using Lookup Table

The fuzzy PID controller uses a parallel structure as shown in the Fuzzy PID subsystem.
For more information, see [1]. The controller is a combination of fuzzy PI control and
fuzzy PD control.

open_system('sllookuptable/Fuzzy PID')

2-113
2 Tutorial

The fuzzy PID controller uses the change of the output -(y(k)-y(k-1)), instead of
change of error e(k)-e(k-1), as the second input signal to the FIS. Doing so prevents
the step change in reference signal from directly triggering the derivative action. The two
gain blocks, GCE and GCU, in the feed forward path from r to u, ensure that the error
signal e is used in proportional action when the fuzzy PID controller is linear.

Design Conventional PID Controller

The conventional PID controller in this example is a discrete-time PID controller with
Backward Euler numerical integration in both the integral and derivative actions. The
controller gains are Kp, Ki, and Kd.

open_system('sllookuptable/Conventional PID')

Similar to the fuzzy PID controller, the input signal to the derivative action is -y(k),
instead of e(k).

You can tune the PID controller gains manually or using tuning formulas. In this example,
obtain the initial PID design using the pidtune command from Control System Toolbox™.

Define the PID structure, tune the controller, and extract the PID gains.

2-114
Implement Fuzzy PID Controller in Simulink Using Lookup Table

C0 = pid(1,1,1,'Ts',Ts,'IF','B','DF','B');
C = pidtune(Plant,C0)
[Kp,Ki,Kd] = piddata(C);

C =

Ts*z z-1
Kp + Ki * ------ + Kd * ------
z-1 Ts*z

with Kp = 30.6, Ki = 25.2, Kd = 9.02, Ts = 0.1

Sample time: 0.1 seconds


Discrete-time PID controller in parallel form.

Design Equivalent Linear Fuzzy PID Controller

By configuring the FIS and selecting the four scaling factors, you can obtain a linear fuzzy
PID controller that reproduces the control performance of the conventional PID
controller.

First, configure the fuzzy inference system so that it produces a linear control surface
from inputs E and CE to output u. The FIS settings are based on design choices described
in [2]:

• Use a Mamdani style fuzzy inference system.


• Use an algebraic product for the AND operation.
• Normalize the ranges of both inputs to [-10 10].
• Use triangular input membership functions that overlap their neighbor functions at a
membership value of 0.5.
• Use an output range of [-20 20].
• Use singletons as output, determined by the sum of the peak positions of the input
sets.
• Use the center of gravity method (COG) for defuzzification.

Construct the fuzzy inference system.

FIS = newfis('FIS','FISType','mamdani','AndMethod','prod','OrMethod','probor',...
'ImplicationMethod','prod','AggregationMethod','sum');

2-115
2 Tutorial

Define input variable E.

FIS = addvar(FIS,'input','E',[-10 10]);


FIS = addmf(FIS,'input',1,'Negative','trimf',[-20 -10 0]);
FIS = addmf(FIS,'input',1,'Zero','trimf',[-10 0 10]);
FIS = addmf(FIS,'input',1,'Positive','trimf',[0 10 20]);

Define input CE.

FIS = addvar(FIS,'input','CE',[-10 10]);


FIS = addmf(FIS,'input',2,'Negative','trimf',[-20 -10 0]);
FIS = addmf(FIS,'input',2,'Zero','trimf',[-10 0 10]);
FIS = addmf(FIS,'input',2,'Positive','trimf',[0 10 20]);

Define output variable u. To implement a singleton membership function use trimf,


specifying the same value for all three parameters.

FIS = addvar(FIS,'output','u',[-20 20]);


FIS = addmf(FIS,'output',1,'LargeNegative','trimf',[-20 -20 -20]);
FIS = addmf(FIS,'output',1,'SmallNegative','trimf',[-10 -10 -10]);
FIS = addmf(FIS,'output',1,'Zero','trimf',[0 0 0]);
FIS = addmf(FIS,'output',1,'SmallPositive','trimf',[10 10 10]);
FIS = addmf(FIS,'output',1,'LargePositive','trimf',[20 20 20]);

Define the following fuzzy rules:

1 If E is negative and CE is negative, then u is -20.


2 If E is negative and CE is zero, then u is -10.
3 If E is negative and CE is positive then u is 0.
4 If E is zero and CE is negative, then u is -10.
5 If E is zero and CE is zero, then u is 0.
6 If E is zero and CE is positive, then u is 10.
7 If E is positive and CE is negative, then u is 0.
8 If E is positive and CE is zero, then u is 10.
9 If E is positive and CE is positive, then u is 20.

ruleList = [1 1 1 1 1; % Rule 1
1 2 2 1 1; % Rule 2
1 3 3 1 1; % Rule 3
2 1 2 1 1; % Rule 4
2 2 3 1 1; % Rule 5

2-116
Implement Fuzzy PID Controller in Simulink Using Lookup Table

2 3 4 1 1; % Rule 6
3 1 3 1 1; % Rule 7
3 2 4 1 1; % Rule 8
3 3 5 1 1]; % Rule 9
FIS = addrule(FIS,ruleList);

While you implement your FIS from the command line in this example, you can
alternatively build your FIS using the Fuzzy Logic Designer app.

Plot the linear control surface.

gensurf(FIS)

2-117
2 Tutorial

Determine scaling factors GE, GCE, GCU, and GU from the Kp, Ki, and Kd gains of by the
conventional PID controller. Comparing the expressions of the traditional PID and the
linear fuzzy PID, the variables are related as follows:

• Kp = GCU * GCE + GU * GE
• Ki = GCU * GE
• Kd = GU * GCE

Assume that the maximum reference step is 1, and thus the maximum error e is 1. Since
the input range of E is [-10 10], set GE to 10. You can then solve for GCE, GCU, and GU.

GE = 10;
GCE = GE*(Kp-sqrt(Kp^2-4*Ki*Kd))/2/Ki;
GCU = Ki/GE;
GU = Kd/GCE;

Implement Fuzzy Inference System Using 2-D Lookup Table

The fuzzy controller block has two inputs (E and CE) and one output (u). Therefore, you
can replace the fuzzy system using a 2-D lookup table.

To generate a 2-D lookup table from your FIS, loop through the input universe, and
compute the corresponding output values using evalfis. Since the control surface is
linear, you can use a few sample points for each input variable.

Step = 10;
E = -10:Step:10;
CE = -10:Step:10;
N = length(E);
LookUpTableData = zeros(N);
for i=1:N
for j=1:N
% Compute output u for each combination of sample points.
LookUpTableData(i,j) = evalfis([E(i) CE(j)],FIS);
end
end

View the fuzzy PID controller using 2-D lookup table.

open_system('sllookuptable/Fuzzy PID using Lookup Table')

2-118
Implement Fuzzy PID Controller in Simulink Using Lookup Table

The only difference compared to the Fuzzy PID controller is that the Fuzzy Logic
Controller block is replaced with a 2-D Lookup Table block.

When the control surface is linear, a fuzzy PID controller using the 2-D lookup table
produces the same result as one using the Fuzzy Logic Controller block.

Simulate Closed-Loop Response in Simulink

The Simulink model simulates three different controller subsystems, namely Conventional
PID, Fuzzy PID, and Fuzzy PID using Lookup Table, to control the same plant.

Run the simulation. To compare the closed-loop responses to a step reference change,
open the scope. As expected, all three controllers produce the same result.

sim('sllookuptable')
open_system('sllookuptable/Scope')

2-119
2 Tutorial

Design Fuzzy PID Controller with Nonlinear Control Surface

Once you have a linear fuzzy PID controller, you can obtain a nonlinear control surface by
adjusting your FIS settings, such as its style, membership functions, and rule base.

For this example, design a steep control surface using a Sugeno-type FIS. Each input set
has two terms (Positive and Negative), and the number of rules is reduced to four.

Construct the FIS.

FIS = newfis('FIS','FISType','sugeno');

Define input E.

FIS = addvar(FIS,'input','E',[-10 10]);


FIS = addmf(FIS,'input',1,'Negative','gaussmf',[7 -10]);
FIS = addmf(FIS,'input',1,'Positive','gaussmf',[7 10]);

Define input CE.

2-120
Implement Fuzzy PID Controller in Simulink Using Lookup Table

FIS = addvar(FIS,'input','CE',[-10 10]);


FIS = addmf(FIS,'input',2,'Negative','gaussmf',[7 -10]);
FIS = addmf(FIS,'input',2,'Positive','gaussmf',[7 10]);

Define output u.

FIS = addvar(FIS,'output','u',[-20 20]);


FIS = addmf(FIS,'output',1,'Min','constant',-20);
FIS = addmf(FIS,'output',1,'Zero','constant',0);
FIS = addmf(FIS,'output',1,'Max','constant',20);

Define the following rules:

1 If E is negative and CE is negative, then u is -20.


2 If E is negative and CE is positive, then u is 0.
3 If E is positive and CE is negative, then u is 0.
4 If E is positive and CE is positive, then u is 20.

ruleList = [1 1 1 1 1;... % Rule 1


1 2 2 1 1;... % Rule 2
2 1 2 1 1;... % Rule 3
2 2 3 1 1]; % Rule 4
FIS = addrule(FIS,ruleList);

View the 3-D nonlinear control surface. This surface has a higher gain near the center of
the E and CE plane than the linear surface has, which helps reduce the error more quickly
when the error is small. When the error is large, the controller becomes less aggressive
to avoid possible saturation.

gensurf(FIS)

2-121
2 Tutorial

Before starting the simulation, update the lookup table with the new control surface data.
Since the surface is nonlinear, to obtain a sufficient approximation, add more sample
points.

Step = 1;
E = -10:Step:10;
CE = -10:Step:10;
N = length(E);
LookUpTableData = zeros(N);
for i=1:N
for j=1:N
% Compute output u for each combination of sample points.
LookUpTableData(i,j) = evalfis([E(i) CE(j)],FIS);

2-122
Implement Fuzzy PID Controller in Simulink Using Lookup Table

end
end

Run the simulation.

sim('sllookuptable')

Compared with the traditional linear PID controller (the response curve with large
overshoot), the nonlinear fuzzy PID controller reduces the overshoot by 50%. The two
response curves from the nonlinear fuzzy controllers almost overlap, which indicates that
the 2-D lookup table approximates the fuzzy system well.

bdclose('sllookuptable')

Conclusion

You can approximate a nonlinear fuzzy PID controller using a lookup table. By replacing a
Fuzzy Logic Controller block with Lookup Table blocks in Simulink, you can deploy a
fuzzy controller with simplified generated code and improved execution speed.

2-123
2 Tutorial

References

[1] Xu, J. X., Hang, C. C., Liu, C. "Parallel structure and tuning of a fuzzy PID controller."
Automatica, Vol. 36, pp. 673-684. 2000.

[2] Jantzen, J. Tuning of Fuzzy PID Controllers, Technical Report, Dept. of Automation,
Technical University of Denmark. 1999.

See Also
Blocks
2-D Lookup Table | Fuzzy Logic Controller

More About
• “Simulate Fuzzy Inference Systems in Simulink” on page 2-84

2-124
What Is Sugeno-Type Fuzzy Inference?

What Is Sugeno-Type Fuzzy Inference?


This topic discusses the Sugeno, or Takagi-Sugeno-Kang, method of fuzzy inference.
Introduced in 1985 [1], this method is similar to the Mamdani method in many respects.
The first two parts of the fuzzy inference process, fuzzifying the inputs and applying the
fuzzy operator, are the same. The main difference between Mamdani and Sugeno is that
the Sugeno output membership functions are either linear or constant.

A typical rule in a Sugeno fuzzy model has the form:


If Input 1 is x and Input 2 is y, then Output is z = ax + by + c

For a zero-order Sugeno model, the output level z is a constant (a = b = 0).

Each rule weights its output level, zi, by the firing strength of the rule, wi. For example,
for an AND rule with Input 1 = x and Input 2 = y, the firing strength is

wi = AndMethod ( F1 ( x), F2 ( y))

where F1,2(.) are the membership functions for Inputs 1 and 2.

The final output of the system is the weighted average of all rule outputs, computed as

N
 wi zi
i=1
Final Output =
N
 wi
i =1

where N is the number of rules.

A Sugeno rule operates as shown in the following diagram.

2-125
2 Tutorial

2. Apply 3. Apply
1. Fuzzify inputs fuzzy implication
operation method (prod).
(OR = max)

1. poor rancid

z 1 (cheap) z1
If service is poor or food is rancid then tip = cheap

2. rule 2 has
no dependency
good on input 2
z 2 (average) z2
If service is good then tip = average

excellent

3. delicious
z 3 (generous) z3
If service is excellent or food is delicious then tip = generous

service = 3 food = 8

input 1 input 2 output

tip = 16.3%

Note Sugeno systems always use product implication and sum aggregation.

The preceding figure shows the fuzzy tipping model developed in “Fuzzy Inference
Process” on page 2-22 adapted for use as a Sugeno system. Fortunately, it is frequently
the case that singleton output functions are sufficient for the needs of a given problem. As
an example, the system tippersg.fis is the Sugeno-type representation of the now-
familiar tipping model. If you load the system and plot its output surface, you see that it is
almost the same as the Mamdani system you have previously seen.

fis = readfis('tippersg');
gensurf(fis)

2-126
What Is Sugeno-Type Fuzzy Inference?

The easiest way to visualize first-order Sugeno systems is to think of each rule as defining
the location of a moving singleton. That is, the singleton output spikes can move around
in a linear fashion in the output space, depending on what the input is. This also tends to
make the system notation compact and efficient. Higher-order Sugeno fuzzy models are
possible, but they introduce significant complexity with little obvious merit. Sugeno fuzzy
models whose output membership functions are greater than first order are not
supported by Fuzzy Logic Toolbox software.

Because of the linear dependence of each rule on the input variables, the Sugeno method
is ideal for acting as an interpolating supervisor of multiple linear controllers that are to
be applied, respectively, to different operating conditions of a dynamic nonlinear system.
For example, the performance of an aircraft may change dramatically with altitude and
Mach number. Linear controllers, though easy to compute and suited to any given flight

2-127
2 Tutorial

condition, must be updated regularly and smoothly to keep up with the changing state of
the flight vehicle. A Sugeno fuzzy inference system is suited to the task of smoothly
interpolating the linear gains that would be applied across the input space; it is a natural
and efficient gain scheduler. Similarly, a Sugeno system is suited for modeling nonlinear
systems by interpolating between multiple linear models.

To see a specific example of a system with linear output membership functions, consider
the one-input, one-output system stored in sugeno1.fis. Load the system and view the
properties of its output variable.
fismat = readfis('sugeno1');
getfis(fismat,'output',1)

ans =

struct with fields:

Name: 'output'
NumMFs: 2
mf1: 'line1'
mf2: 'line2'
range: [0 1]

The output variable has two membership functions. View the properties of the first
membership function.
getfis(fismat,'output',1,'mf',1)

ans =

struct with fields:

Name: 'line1'
Type: 'linear'
params: [-1 -1]

View the properties of the second membership function.


getfis(fismat,'output',1,'mf',2)

ans =

2-128
What Is Sugeno-Type Fuzzy Inference?

struct with fields:

Name: 'line2'
Type: 'linear'
params: [1 -1]

Further, these membership functions are linear functions of the input variable. The
membership function line1 is defined by the equation:

output = ( -1) ¥ input + ( -1)

and the membership function line2 is:

output = (1) ¥ input + (-1)

The input membership functions and rules define which of these output functions are
expressed and when:

showrule(fismat)

ans =

2x48 char array

'1. If (input is low) then (output is line1) (1) '


'2. If (input is high) then (output is line2) (1)'

The function plotmf shows us that the membership function low generally refers to
input values less than zero, while high refers to values greater than zero. The function
gensurf shows how the overall fuzzy system output switches smoothly from the line
called line1 to the line called line2.

subplot(2,1,1)
plotmf(fismat,'input',1)
subplot(2,1,2)
gensurf(fismat)

2-129
2 Tutorial

As this example shows, Sugeno-type system gives you the freedom to incorporate linear
systems into your fuzzy systems. By extension, you could build a fuzzy system that
switches between several optimal linear controllers as a highly nonlinear system moves
around in its operating space.

References
[1] Sugeno, M., Industrial applications of fuzzy control, Elsevier Science Pub. Co., 1985.

See Also
gensurf | readfis

2-130
See Also

More About
• “Comparison of Sugeno and Mamdani Systems” on page 2-132
• “What Is Mamdani-Type Fuzzy Inference?” on page 2-31

2-131

You might also like