We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
zorzitorat Fuzzy vs. Nonfuzzy Logic - MATLAB & Simulink
Fuzzy vs. Nonfuzzy Logic
Basic Tipping Problem Try it in M
ATLAB
To illustrate the value of fuzzy logic, examine both linear and fuzzy
approaches to the following problem:
What is the right amount to tip your waitperson?
First, work through this problem the conventional (nonfuzzy) way, writing MATLAB® commands that spell out linear and
piecewise-linear relations. Then, look at the same system using fuzzy logic.
Basic Tipping Problem. Given a number between 0 and 10 that represents the qualily of service at a restaurant
(where 10 is excellent), what should the tip be?
(This problem is based on tipping as itis typically practiced in the United States. An average tip for a meal in the U.S. is
115%, though the actual amount may vary depending on the quality of the service provided.)
Nonfuzzy Approach
Begin with the simplest possible relationship. Suppose that the tip always equals 15% of the total bill
service = @:.5:10;
tip = 0.15tones(size(service));
plot (service, tip)
xlabel( Service’)
ylabel(‘Tip")
ylin([@.05 @.25])
025
02
a
Fo15
on
0.05
o 1 2 8 4 5 6 7 8 9
Service
This relationship does not take into account the qualliy of the service, so you need to add a new term to the equation.
Because service is rated on a scale of 0 to 10, you might have the tip go linearly from 5% if the service is bad to 25% if
the service is excellent. Now the relation looks like the following plot:
tip = (.20/10)*service+0.05;
plot (service,tip)
xlabel (‘Service’)
festicuser
allDesktop/Geltng?420Started 200: %20F uzzy%20Logic%h20TeolbouFuzzy%20vs. %20Noniuzzy%20L0gic%420-%.20MATLAB.... 182orzitorat Fuzzy vs. Nonfuzzy Logic - MATLAB & Simulink
ylabel('Tip")
ylin([@.05 @.25])
025
02
a
Fo15
on
0.05
o 1 2 38 4 5 6 7 8 9
Service
‘The formula does what you want it to do, and is straight forward. However, you may want the tip to reflect the quality of
the food as well. This extension of the problem is defined as follows.
Extended Tipping Problem. Given two sets of numbers between 0 and 10 (where 10 is excellent) that respectively
represent the quality of the service and the quality of the food at a restaurant, what should the tip be?
‘See how the formula is affected now that you have added another variable, Try the following equation:
food = @:.5:10;
[F,S] = meshgrid(food, service);
tip = (8.20/20). *(S+F)+0.055
surt(S,F, tip)
xlabel (‘Service’)
ylabel('Food" )
zlabel('Tip')
.allDesktop/Geltng?420Started20w:N'%20F uzzy%.20Logic%h20TeolbouFuzzy%20vs. %20Noniuzzy%20L0gic%420-%.20MATLAB.... 2182orzitorat Fuzzy vs. Nonfuzzy Logic - MATLAB & Simulink
4
Food 9 0 Service
In this case, the results look satisfactory, but when you look at them closely, they do not seem quite right. Suppose you
want the service to be a more important factor than the food quality, Specify that service accounts for 80% of the overall
tipping grade and the food makes up the other 20%, Try this equation
servRatio = 0.8;
tip = servRatio*(@,20/10*5#0.05) +...
(A-servRatio)*(0.20/10*F=0.05);
surf(S,F,tip)
xlabel( Service’)
ylabel('Food")
zlabel (‘Tip’)
fe:1IC:1UsersimattDesktop/Geling?20Started%20with%420Fuzzy%20L ogic%20Toolbox!Fuzzy%20vs.%420Nonfuzzy%.20L0gic%20-420MATLAB.... 3182orzitorat Fuzzy vs. Nonfuzzy Logic - MATLAB & Simulink
Food 0 0 Service
The response is still some how too uniformly linear. Suppose you want more of a flat response in the middle, i.e., you
want to give a 15% tip in general, but want to also specify a variation if the service is exceptionally good or bad. This
factor, in tum, means that the previous linear mappings no longer apply. You can stil use the linear calculation with a
piecewise linear construction. Now, return to the one-dimensional problem of just considering the service. You can
create a simple conditional tip assignment using logical indexing
tip = zeros(size(service));
tip(servicec3) = (@,10/3)*service(service<3)+0.05;
tip(servicer=3 & servicec7) = @.15;
tip(services=7 & servicec=10)
(0.10/3)*(service(service:
plot(service,tip)
xlabel (‘Service’)
ylabel (‘Tip’)
ylin([@.05 .25])
& servicec=10)-7)+0.15;
.altDesktop/Geltng?420Startod 200: %20F uzzy%20Logic%k20TeolbouFuzzy%20vs. %20Noniuzzy%20L0gic%420-%.20MATLAB.... 82orzitorat Fuzzy vs. Nonfuzzy Logic - MATLAB & Simulink
025
02 of
ots
ot
0.05
o 41 2 3 4 6 6 7 & 9
Service
‘Suppose you extend this to two dimensions, where you take food into account again,
servRatio = 0.8;
tip = zeros(size(s));
tip(S<3) = ((0.10/3)*S(S<3)+8.05)*servRatio + .
(1-servRatio)*(0.20/10*F(S<3)+0.05);
tip(S>=3 & S<7) = (0.15)*servRatio + .
(1-servRatio) *(0.20/10*F(S>=3 & S<7)+8.05);
tip(S>=7 & S<=18) = ((@.10/3)*(S(S>=7 & S<=18)-7)12.15)*servRatio +
(1-servRatio)*(0.20/10*F (S>=7 & S<=10)+0.05) ;
surt(S,F, tip)
xlabel (‘Service’)
ylabel('Food" )
zlabel('Tip")
.altDesktop/Geltng?420Started 200: %20F uzzy%20Logic%k20TeolbouFuzzy%20vs. %20Noniuzzy%20L0gic%420-%.20MATLAB.... 582orzitorat Fuzzy vs. Nonfuzzy Logic - MATLAB & Simulink
4
Food 0 0 Service
The plot looks good, but the function is surprisingly complicated, It was a little difficult to code this correctly, and itis
definitely not easy to modify this code in the future. Moreover, it is even less apparent how the algorithm works to
someone who did not see the original design process,
Fuzzy Logic Approach
You need to capture the essentials of this problem, leaving aside all the factors that could be arbitrary. If you make a list
of what really matters in this problem, you might end up with the following rule descriptions.
Tipping Problem Rules - Service Factor
+ If service is poor, then tip is cheap
+ If service is good, then tip is average
+ If service is excellent, then tip is generous
‘The order in which the rules are presented here is arbitrary. It does not matter which rules come first. If you want to
include the food's effect on the tip, add the following two rules,
Tipping Problem Rules - Food Factor
+ If food is rancid, then tip is cheap
+ If food is delicious, then tip is generous
You can combine the two different lists of rules into one tight list of three rules lke so.
Tipping Problem Rules - Both Service and Food Factors
+ If service is poor or the food is rancid, then tip is cheap
+ If service is good, then tip is average
+ If service is excellent or food is delicious, then tip is generous
‘These three rules are the core of your solution. Coincidentally, you have just defined the rules for a fuzzy logic system.
When you give mathematical meaning to the linguistic variables (what is an average tip, for example) you have a
complete fuzzy inference system. The methodology of fuzzy logic must also consider:
+ How are the rules all combined?
+ How do I define mathematically what an average tip is?
.allDesktop/Geltng?420Startod200:N'%20F uzzy%20Logic%h20TeolbouFuzzy%20vs. %20Noniuzzy%20L0gic%420-%.20MATLAB.... 82orzitorat Fuzzy vs. Nonfuzzy Logic - MATLAB & Simulink
‘See other sections of the documentation for detailed answers to these questions. The details of the method don't really
change much from problem to problem - the mechanics of fuzzy logic aren't terribly complex. What matters is that you
understand that fuzzy logic is adaptable, simple, and easily applied.
Problem Solution
‘The following plot represents the fuzzy logic system that solves the tipping problem.
gensurf(readfis( ‘tipper"))
food 0 0
‘This plot was generated by the three rules that accounted for both service and food factors. The mechanics of how
fuzzy inference works is explained in the Overview section of Foundations of Fuzzy Logic topic. In the topic, Bulld
Mamdani Systems (GUI), the entire tipping problem is worked through using the Fuzzy Logic Toolbox apps.
Observations Consider some observations about the example so far. You found a piecewise linear relation that solved
the problem. It worked, but it was problematic to derive, and when you wrote it down as code, it was not very easy to
interpret. Conversely, the fuzzy logic system is based on some common sense statements. Also, you were able to add
two more rules to the bottom of the list thal influenced the shape of the overall output without needing to undo what had
already been done. Making the subsequent modification was relatively easy.
Moreover, by using fuzzy logic rules, the maintenance of the structure of the algorithm decouples along fairly clean
lines, The notion of an average tip might change from day to day, city to city, country to country, but the underiying logic
is the same: if the service is good, the tip should be average.
Recalibrating the Method You can recalibrate the method quickly by simply shifting the fuzzy set that defines average
without rewriting the fuzzy logic rules,
You can shif lists of piecewise linear functions, but there is a greater likelihood that recalibration will not be so quick
and simple.
In the following example, the piecewise linear tipping problem is rewritten to make it more generic. It performs the same
function as before, only now the constants can be easily changed.
lowTip = 8.05;
averTip = 0.15;
highTip = 0.25;
‘tipRange = highTip-lowTip;
badService = @;
festicsuser
allDesktop/Geltng?s20Started200:N'%20F uzzy%.20Logic%h20TeolbouFuzzy%20vs. %20Noniuzzy%20L0gic9420-%.20MATLAB.... 7182orzitorat Fuzzy vs. Nonfuzzy Logic - MATLAB & Simulink
okayService = 3;
goodService = 7;
greatService = 10;
serviceRange = greatService-badServices
badFood = 0;
greatFood = 10;
foodRange = greatFood-badFood;
% Tf service is poor or food is rancid, tip is cheap
if servicecokayService
tip = (((averTip-lowTip) /(okayService-badService))
*servicetlowTip)*servRatio + ...
(1-servRatio) *(tipRange/foodRange*food+lowTip) ;
% If service is good, tip is average
elseif servicecgoodservice
tip = averTip*servRatio + (1-servRatio)* .
(tipRange/FoodRange*food+lowTip) ;
% If service is excellent or food is delicious, tip is generous
else
tip = (((highTip-averTip)/
(greatService-goodService))* ...
(service-goodService)+averTip)*servRatio + ...
(1-servRatio) *(tipRange/foodRange*food+lowTip) ;
end
‘As with all code, the more generality that is introduced, the less precise the algorithm becomes. You can improve clarity
by adding more comments, or perhaps rewriting the algorithm in slightly more self-evident ways. But, the piecewise
linear methodology is not the optimal way to resolve this issue.
Ifyou remove everything from the algorithm except for three comments, what remain are exactly the fuzzy logic rules
you previously wrote down,
+ If service is poor or food is rancid, tip is cheap
+ If service is good, tip is average
+ If service is excellent or food is delicious, tip is generous
If, as with a fuzzy system, the comment is identical with the code, think how much more likely your code is to have
‘comments. Fuzzy logic uses language that is clear to you, high level comments, and that also has meaning to the
machine, which is why it is a very successful technique for bridging the gap between people and machines,
By making the equations as simple as possible (linear) you make things simpler for the machine, but more complicated
for you. However, the limitation is really no longer the computer tis your mental model of what the computer is doing.
Computers have the ability to make things hopelessly complex; fuzzy logic reclaims the middleground and lets the
machine work with your preferences rather than the other way around,
Related Topics
+ Bulld Mamdani Systems at the Command Line
+ Build Mamdani Systems Using Fuzzy Logic Designer
.altDesktop/Geltng?420Started200:h'K20F uzzy%20Logic%k20TeolbouFuzzy%20vs. %20Noniuzzy%20L0gic9420-%.20MATLAB.... 818