You are on page 1of 10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

BuildMamdaniSystemsattheCommandLine
TippingProblemfromtheCommandLine
ThetippingproblemisoneofmanyFuzzyLogicToolboxexamplesoffuzzyinferencesystems.TheFISisalways
castasaMATLABstructure.Toloadthissystem,type:
a=readfis('tipper.fis')
Thiscommandreturnsthefollowingresult:
a=
name:'tipper'
type:'mamdani'
andMethod:'min'
orMethod:'max'
defuzzMethod:'centroid'
impMethod:'min'
aggMethod:'max'
input:[1x2struct]
output:[1x1struct]
rule:[1x3struct]
ThelabelsontheleftofthislistingrepresentthevariouscomponentsoftheMATLABstructureassociatedwith
tipper.fis.Toaccessthevariouscomponentsofthisstructure,typethecomponentnameafteraattheMATLAB
prompt.Forexample:
a.type
returnsthefollowingresult:
ans=
mamdani
Thefunction
getfis(a)
returnsalmostthesamestructureinformationthattypinga,alonedoes:
Name=tipper
Type=mamdani
NumInputs=2
InLabels=
service
food
NumOutputs=1
OutLabels=
tip
NumRules=3
AndMethod=min
OrMethod=max
ImpMethod=min
AggMethod=max
DefuzzMethod=centroid
Someofthesefieldsarenotpartofthestructurea.Thus,youcannotgetinformationbytypinga.Inlabels,butyou
cangetitbytyping
http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

1/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

getfis(a,'Inlabels')
Similarly,youcanobtainstructureinformationusinggetfisinthismanner.
getfis(a,'input',1)
getfis(a,'output',1)
getfis(a,'input',1,'mf',1)
Thestructure.fieldsyntaxalsogeneratesthisinformation.FormoreinformationonthesyntaxforMATLAB
structuresandcellarrays,seeCreateaStructureArrayandCreateaCellArrayintheMATLABdocumentation.
Forexample,type
a.input
or
a.input(1).mf(1)
ThefunctiongetfisislooselymodeledontheHandleGraphics functionget.Thefunctionsetfisactsasthe
reciprocaltogetfis.ItallowsyoutochangeanypropertyofaFIS.Forexample,ifyouwantedtochangethenameof
thissystem,youcouldtype
a=setfis(a,'name','gratuity');
However,becauseaisalreadyaMATLABstructure,youcansetthisinformationmoresimplybytyping
a.name='gratuity';
NowtheFISstructureahasbeenchangedtoreflectthenewname.IfyouwantalittlemoreinsightintothisFIS
structure,try
showfis(a)
Thissyntaxreturnsaprintoutlistingalltheinformationabouta.Thisfunctionisintendedmorefordebuggingthan
anythingelse,butitshowsalltheinformationrecordedintheFISstructure
Becausethevariable,a,designatesthefuzzytippingsystem,youcandisplayanyoftheGUIsforthetippingsystem
directlyfromthecommandline.AnyofthefollowingfunctionswilldisplaythetippingsystemwiththeassociatedGUI:

fuzzyLogicDesigner(a)displaystheFuzzyLogicDesigner.

mfedit(a)displaystheMembershipFunctionEditor.

ruleedit(a)displaystheRuleEditor.

ruleview(a)displaystheRuleViewer.

surfview(a)displaystheSurfaceViewer.

If,inaddition,aisaSugenotypeFIS,thenneuroFuzzyDesigner(a)displaystheNeuroFuzzyDesigner.
WhenyouopenanyoftheseGUIs,youcanaccessanyoftheotherGUIsusingthepulldownmenuratherthanthe
commandline.

SystemDisplayFunctions
Therearethreefunctionsdesignedtogiveyouahighlevelviewofyourfuzzyinferencesystemfromthecommand
line:plotfis,plotmf,andgensurf.Thefirstofthesedisplaysthewholesystemasablockdiagrammuchasit
wouldappearontheFuzzyLogicDesigner.
plotfis(a)

http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

2/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

AfterclosinganyopenMATLABfiguresorGUIwindows,thefunctionplotmfplotsallthemembershipfunctions
associatedwithagivenvariableasfollows.
plotmf(a,'input',1)
returnsthefollowingplots

plotmf(a,'output',1)
http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

3/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

TheseplotsappearintheMembershipFunctionEditorGUI,orinanopenMATLABfigure,ifplotmfiscalledwhile
eitheroftheseisopen.
Finally,thefunctiongensurfplotsanyoneortwoinputsversusanyoneoutputofagivensystem.Theresultiseither
atwodimensionalcurve,orathreedimensionalsurface.Whentherearethreeormoreinputs,gensurfmustbe
generatedwithallbuttwoinputsfixed,asisdescribedingensurf.
gensurf(a)

http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

4/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

BuildingaSystemfromScratch
YoucanbuildafuzzyinferencesystemusingFuzzyLogicToolboxcommandsasanalternativetotheGUItools.For
example,tobuildthetippingsystementirelyfromthecommandline,youusethecommandsnewfis,addvar,addmf,
andaddrule.
Probablythemostdifficultpartofthisprocessislearningtheshorthandthatthefuzzyinferencesystemsusefor
buildingrules.Usethecommandlinefunction,addruletodothis.
Eachvariable,input,oroutput,hasanindexnumber,andeachmembershipfunctionhasanindexnumber.Therules
arebuiltfromstatementssuchasthefollowing:
Ifinput1isMF1orinput2isMF3,thenoutput1isMF2(weight=0.5)
Thisruleisturnedintoastructureaccordingtothefollowinglogic.Ifthereareminputstoasystemandnoutputs,
thenthefirstmvectorentriesoftherulestructurecorrespondtoinputs1throughm.

Theentryincolumn1istheindexnumberforthemembershipfunctionassociatedwithinput1.

Theentryincolumn2istheindexnumberforthemembershipfunctionassociatedwithinput2,andsoon.

Thenextncolumnsworkthesamewayfortheoutputs.

Columnm+n+1istheweightassociatedwiththatrule(typically1)andcolumnm+n+2specifiesthe
connectiveused(whereAND=1andOR=2).

Thestructureassociatedwiththeprecedingruleis
[1320.52]
Thissamplecodeshowsonewayyoucanbuildtheentiretippingsystemfromthecommandline,usingtheMATLAB
structuresyntax.

http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

5/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

a=newfis('tipper');
a.input(1).name='service';
a.input(1).range=[010];
a.input(1).mf(1).name='poor';
a.input(1).mf(1).type='gaussmf';
a.input(1).mf(1).params=[1.50];
a.input(1).mf(2).name='good';
a.input(1).mf(2).type='gaussmf';
a.input(1).mf(2).params=[1.55];
a.input(1).mf(3).name='excellent';
a.input(1).mf(3).type='gaussmf';
a.input(1).mf(3).params=[1.510];
a.input(2).name='food';
a.input(2).range=[010];
a.input(2).mf(1).name='rancid';
a.input(2).mf(1).type='trapmf';
a.input(2).mf(1).params=[2013];
a.input(2).mf(2).name='delicious';
a.input(2).mf(2).type='trapmf';
a.input(2).mf(2).params=[791012];
a.output(1).name='tip';
a.output(1).range=[030];
a.output(1).mf(1).name='cheap'
a.output(1).mf(1).type='trimf';
a.output(1).mf(1).params=[0510];
a.output(1).mf(2).name='average';
a.output(1).mf(2).type='trimf';
a.output(1).mf(2).params=[101520];
a.output(1).mf(3).name='generous';
a.output(1).mf(3).type='trimf';
a.output(1).mf(3).params=[202530];
a.rule(1).antecedent=[11];
a.rule(1).consequent=[1];
a.rule(1).weight=1;
a.rule(1).connection=2;
a.rule(2).antecedent=[20];
a.rule(2).consequent=[2];
a.rule(2).weight=1;
a.rule(2).connection=1;
a.rule(3).antecedent=[32];
a.rule(3).consequent=[3];
a.rule(3).weight=1;
a.rule(3).connection=2
TipYoucanalsobuildtheFISstructureusingMATLABworkspacevariables.Forexample,tospecifytherangeofthe
input,type:
r1=[010]
a.input(1).range=r1;

Alternatively,youcanbuildtheentiretippingsystemfromthecommandlineusingFuzzyLogicToolboxcommands.
Thesecommandsareinthemktipper.mfile.

http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

6/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

a=newfis('tipper');
a=addvar(a,'input','service',[010]);
a=addmf(a,'input',1,'poor','gaussmf',[1.50]);
a=addmf(a,'input',1,'good','gaussmf',[1.55]);
a=addmf(a,'input',1,'excellent','gaussmf',[1.510]);
a=addvar(a,'input','food',[010]);
a=addmf(a,'input',2,'rancid','trapmf',[2013]);
a=addmf(a,'input',2,'delicious','trapmf',[791012]);
a=addvar(a,'output','tip',[030]);
a=addmf(a,'output',1,'cheap','trimf',[0510]);
a=addmf(a,'output',1,'average','trimf',[101520]);
a=addmf(a,'output',1,'generous','trimf',[202530]);
ruleList=[11112;
20211;
32312];
a=addrule(a,ruleList);
SpecifyingCustomMembershipandInferenceFunctions
YoucancreatecustommembershipandinferencefunctionsasdescribedinSpecifyingCustomMembership
Functions,andSpecifyingCustomInferenceFunctions,andspecifythemforbuildingfuzzyinferencesystemsatthe
commandline.
Toincludeacustommembershipfunction,specifythenameofthecustommembershipfunction,asshowninthe
followingexample:
a=addmf(a,'input',1,'customMF1','custmf1',[012468910]);
Toincludeacustominferencefunction,specifythenameofthecustominferencefunction,asshowninthefollowing
example:
a.defuzzMethod='customdefuzz';

FISEvaluation
Toevaluatetheoutputofafuzzysystemforagiveninput,usethefunctionevalfis.Forexample,thefollowingscript
evaluatestipperattheinput,[12].
a=readfis('tipper');
evalfis([12],a)
ans=
5.5586
Thisfunctioncanalsobeusedformultiplecollectionsofinputs,becausedifferentinputvectorsarerepresentedin
differentpartsoftheinputstructure.
evalfis([35;27],a)
ans=
12.2184
7.7885

TheFISStructure
TheFISstructureistheMATLABobjectthatcontainsallthefuzzyinferencesysteminformation.Thisstructureis
storedinsideeachGUItool.Accessfunctionssuchasgetfisandsetfismakeiteasytoexaminethisstructure.
AlltheinformationforagivenfuzzyinferencesystemiscontainedintheFISstructure,includingvariablenames,
membershipfunctiondefinitions,andsoon.Thisstructurecanitselfbethoughtofasahierarchyofstructures,as
showninthefollowingdiagram.

http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

7/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

YoucangeneratealistingofinformationontheFISusingtheshowfiscommand,asshowninthefollowingcode
sample:
showfis(a)

http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

8/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

1.Nametipper
2.Typemamdani
3.Inputs/Outputs[21]
4.NumInputMFs[32]
5.NumOutputMFs3
6.NumRules3
7.AndMethodmin
8.OrMethodmax
9.ImpMethodmin
10.AggMethodmax
11.DefuzzMethodcentroid
12.InLabelsservice
13.food
14.OutLabelstip
15.InRange[010]
16.[010]
17.OutRange[030]
18.InMFLabelspoor
19.good
20.excellent
21.rancid
22.delicious
23.OutMFLabelscheap
24.average
25.generous
26.InMFTypesgaussmf
27.gaussmf
28.gaussmf
29.trapmf
30.trapmf
31.OutMFTypestrimf
32.trimf
33.trimf
34.InMFParams[1.5000]
35.[1.5500]
36.[1.51000]
37.[0013]
38.[791010]
39.OutMFParams[05100]
40.[1015200]
41.[2025300]
42.RuleAntecedent[11]
43.[20]
44.[32]
42.RuleConsequent1
43.2
44.3
42.RuleWeight1
43.1
44.1
42.RuleConnection2
43.1
44.2
ThelistofcommandlinefunctionsassociatedwithFISconstructionincludesgetfis,setfis,showfis,addvar,
addmf,addrule,rmvar,andrmmf.
SavingFISFiles
http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

9/10

1/19/2017

BuildMamdaniSystemsattheCommandLineMATLAB&SimulinkMathWorksIndia

Aspecializedtextfileformatisusedforsavingfuzzyinferencesystems.Thefunctionsreadfisandwritefisare
usedforreadingandwritingthesefiles.
Ifyouprefer,youcanmodifytheFISbyeditingits.fistextfileratherthanusinganyoftheGUIs.Youshouldbe
aware,however,thatchangingoneentrymayobligeyoutochangeanother.Forexample,ifyoudeleteamembership
functionusingthismethod,youalsoneedtomakecertainthatanyrulesrequiringthismembershipfunctionarealso
deleted.
Therulesappearinindexedformatina.fistextfile.Thefollowingsampleshowsthefiletipper.fis.
[System]
Name='tipper'
Type='mamdani'
NumInputs=2
NumOutputs=1
NumRules=3
AndMethod='min'
OrMethod='max'
ImpMethod='min'
AggMethod='max'
DefuzzMethod='centroid'
[Input1]
Name='service'
Range=[010]
NumMFs=3
MF1='poor':'gaussmf',[1.50]
MF2='good':'gaussmf',[1.55]
MF3='excellent':'gaussmf',[1.510]
[Input2]
Name='food'
Range=[010]
NumMFs=2
MF1='rancid':'trapmf',[0013]
MF2='delicious':'trapmf',[791010]
[Output1]
Name='tip'
Range=[030]
NumMFs=3
MF1='cheap':'trimf',[0510]
MF2='average':'trimf',[101520]
MF3='generous':'trimf',[202530]
[Rules]
11,1(1):2
20,2(1):1
32,3(1):2

MoreAbout

WhatIsMamdaniTypeFuzzyInference?

BuildMamdaniSystemsUsingFuzzyLogicDesigner

http://in.mathworks.com/help/fuzzy/workingfromthecommandline.html

10/10

You might also like