Fuzzy Logic Toolbox For Use with MATLAB® Computation Visualization Programming User’s Guide Version 2 How to Contact The MathWorks: % 508-647-7000 Phone PHONE 508-647-7001 Fax )FAX TheMathWorks,Inc. Mail MAIL 24PrimeParkWay Natick,MA01760-1500 http://www.mathworks.com Web ftp.mathworks.com AnonymousFTPserver INTERNET comp.soft-sys.matlab Newsgroup @ [email protected] Technicalsupport [email protected] Productenhancementsuggestions [email protected] Bugreports [email protected] Documentationerrorreports [email protected] Subscribinguserregistration [email protected] Orderstatus,licenserenewals,passcodes [email protected] Sales,pricing,andgeneralinformation FuzzyLogicToolboxUser’sGuide (cid:211) COPYRIGHT1995-1999byTheMathWorks,Inc. Thesoftwaredescribedinthisdocumentisfurnishedunderalicenseagreement. Thesoftwaremaybeused orcopiedonlyunderthetermsofthelicenseagreement.Nopartofthismanualmaybephotocopiedorrepro- ducedinanyformwithoutpriorwrittenconsentfromTheMathWorks,Inc. U.S.GOVERNMENT: IfLicenseeisacquiringtheProgramsonbehalfofanyunitoragencyoftheU.S. Government,thefollowingshallapply: (a)ForunitsoftheDepartmentofDefense: theGovernmentshall haveonlytherightsspecifiedinthelicenseunderwhichthecommercialcomputersoftwareorcommercial softwaredocumentationwasobtained,assetforthinsubparagraph(a)oftheRightsinCommercial ComputerSoftwareorCommercialSoftwareDocumentationClauseatDFARS227.7202-3,thereforethe rightssetforthhereinshallapply;and(b)Foranyotherunitoragency: NOTICE:Notwithstandingany otherleaseorlicenseagreementthatmaypertainto,oraccompanythedeliveryof,thecomputersoftware andaccompanyingdocumentation,therightsoftheGovernmentregardingitsuse,reproduction,anddisclo- sureareassetforthinClause52.227-19(c)(2)oftheFAR. MATLAB,Simulink,Stateflow,HandleGraphics,andReal-TimeWorkshopareregisteredtrademarks and TargetLanguageCompileraretrademarksofTheMathWorks,Inc. Otherproductorbrandnamesaretrademarksorregisteredtrademarksoftheirrespectiveholders. PrintingHistory: January1995 Firstprinting April1997 Secondprinting January1998 ThirdprintingRevisedforMATLAB5.2 January1999 MinorrevisionsforRelease11(Onlineonly) Forward Thepastfewyearshavewitnessedarapidgrowthinthenumberandvariety ofapplicationsoffuzzylogic.Theapplicationsrangefromconsumerproducts suchascameras,camcorders,washingmachines,andmicrowaveovensto industrialprocesscontrol,medicalinstrumentation,decision-supportsystems, andportfolioselection. Tounderstandthereasonsforthegrowinguseoffuzzylogicitisnecessary, first,toclarifywhatismeantbyfuzzylogic. Fuzzylogichastwodifferentmeanings.Inanarrowsense,fuzzylogicisa logicalsystem,whichisanextensionofmultivaluedlogic.Butinawider sense—whichisinpredominantusetoday—fuzzylogic(FL)isalmost synonymouswiththetheoryoffuzzysets,atheorywhichrelatestoclassesof objectswithunsharpboundariesinwhichmembershipisamatterofdegree. Inthisperspective,fuzzylogicinitsnarrowsenseisabranchofFL.Whatis importanttorecognizeisthat,eveninitsnarrowsense,theagendaoffuzzy logicisverydifferentbothinspiritandsubstancefromtheagendasof traditionalmultivaluedlogicalsystems. IntheFuzzyLogicToolbox,fuzzylogicshouldbeinterpretedasFL,thatis, fuzzylogicinitswidesense.ThebasicideasunderlyingFLareexplainedvery clearlyandinsightfullyintheIntroduction.Whatmightbeaddedisthatthe basicconceptunderlyingFListhatofalinguisticvariable,thatis,avariable whosevaluesarewordsratherthannumbers.Ineffect,muchofFLmaybe viewedasamethodologyforcomputingwithwordsratherthannumbers. Althoughwordsareinherentlylessprecisethannumbers,theiruseiscloserto humanintuition.Furthermore,computingwithwordsexploitsthetolerance forimprecisionandtherebylowersthecostofsolution. AnotherbasicconceptinFL,whichplaysacentralroleinmostofits applications,isthatofafuzzyif-thenruleor,simply,fuzzyrule.Although rule-basedsystemshavealonghistoryofuseinAI,whatismissinginsuch systemsisamachineryfordealingwithfuzzyconsequentsand/orfuzzy antecedents.Infuzzylogic,thismachineryisprovidedbywhatiscalledthe calculusoffuzzyrules.Thecalculusoffuzzyrulesservesasabasisforwhat mightbecalledtheFuzzyDependencyandCommandLanguage(FDCL). AlthoughFDCLisnotusedexplicitlyinFuzzyLogicToolbox,itiseffectively oneofitsprincipalconstituents.Inthisconnection,whatisimportantto Forward recognizeisthatinmostoftheapplicationsoffuzzylogic,afuzzylogicsolution isinrealityatranslationofahumansolutionintoFDCL. WhatmakestheFuzzyLogicToolboxsopowerfulisthefactthatmostof humanreasoningandconceptformationislinkedtotheuseoffuzzyrules.By providingasystematicframeworkforcomputingwithfuzzyrules,theFuzzy LogicToolboxgreatlyamplifiesthepowerofhumanreasoning.Further amplificationresultsfromtheuseofMATLABandgraphicaluserinterfaces– areasinwhichTheMathWorkshasunparalleledexpertise. Atrendwhichisgrowinginvisibilityrelatestotheuseoffuzzylogicin combinationwithneurocomputingandgeneticalgorithms.Moregenerally, fuzzylogic,neurocomputing,andgeneticalgorithmsmaybeviewedasthe principalconstituentsofwhatmightbecalledsoftcomputing.Unlikethe traditional,hardcomputing,softcomputingisaimedatanaccommodation withthepervasiveimprecisionoftherealworld.Theguidingprincipleofsoft computingis:Exploitthetoleranceforimprecision,uncertainty,andpartial truthtoachievetractability,robustness,andlowsolutioncost.Incoming years,softcomputingislikelytoplayanincreasinglyimportantroleinthe conceptionanddesignofsystemswhoseMIQ(MachineIQ)ismuchhigherthan thatofsystemsdesignedbyconventionalmethods. Amongvariouscombinationsofmethodologiesinsoftcomputing,theonewhich hashighestvisibilityatthisjunctureisthatoffuzzylogicandneurocomputing, leadingtoso-calledneuro-fuzzysystems.Withinfuzzylogic,suchsystemsplay aparticularlyimportantroleintheinductionofrulesfromobservations.An effectivemethoddevelopedbyDr.RogerJangforthispurposeiscalledANFIS (AdaptiveNeuro-FuzzyInferenceSystem).Thismethodisanimportant componentoftheFuzzyLogicToolbox. TheFuzzyLogicToolboxishighlyimpressiveinallrespects.Itmakesfuzzy logicaneffectivetoolfortheconceptionanddesignofintelligentsystems.The FuzzyLogicToolboxiseasytomasterandconvenienttouse.Andlast,butnot leastimportant,itprovidesareader-friendlyandup-to-dateintroductiontothe methodologyoffuzzylogicanditswide-rangingapplications. LotfiA.Zadeh Berkeley,CA January10,1995 Contents Before You Begin WhatIstheFuzzyLogicToolbox? ......................... 6 HowtoUseThisGuide .................................. 7 Installation............................................ 7 TypographicalConventions .............................. 8 .................................................... 10 Introduction 1 WhatIsFuzzyLogic? .................................. 1-2 WhyUseFuzzyLogic?................................. 1-5 WhenNottoUseFuzzyLogic ........................... 1-6 WhatCantheFuzzyLogicToolboxDo? ................... 1-6 AnIntroductoryExample:Fuzzyvs.Non-Fuzzy ......... 1-8 TheNon-FuzzyApproach .............................. 1-9 TheFuzzyApproach ................................. 1-13 SomeObservations .................................. 1-14 Tutorial 2 TheBigPicture ........................................ 18 FoundationsofFuzzyLogic ............................. 20 FuzzySets ........................................... 20 MembershipFunctions ................................. 24 LogicalOperations ..................................... 28 i If-ThenRules ......................................... 32 FuzzyInferenceSystems ............................... 36 DinnerforTwo,Reprise ................................. 37 TheFuzzyInferenceDiagram ............................ 42 Customization ........................................ 43 BuildingSystemswiththeFuzzyLogicToolbox .......... 45 DinnerforTwo,fromtheTop ............................ 45 GettingStarted ....................................... 48 TheFISEditor ........................................ 49 TheMembershipFunctionEditor ......................... 52 TheRuleEditor ....................................... 56 TheRuleViewer ....................................... 59 TheSurfaceViewer .................................... 61 ImportingandExportingfromtheGUITools ............... 62 CustomizingYourFuzzySystem ......................... 63 WorkingfromtheCommandLine ....................... 65 SystemDisplayFunctions ............................... 67 BuildingaSystemfromScratch .......................... 70 FISEvaluation ........................................ 73 TheFISStructure ..................................... 73 WorkingwithSimulink ................................. 78 AnExample:WaterLevelControl ........................ 78 BuildingYourOwnFuzzySimulinkModels ................ 83 Sugeno-TypeFuzzyInference ........................... 86 AnExample:TwoLines ................................. 89 Conclusion ........................................... 90 anfisandtheANFISEditorGUI ......................... 92 AModelingScenario ................................... 92 ModelLearningandInferenceThroughANFIS ............. 93 FamiliarityBreedsValidation:KnowYourData ............. 94 SomeConstraintsofanfis ............................... 95 TheANFISEditorGUI ................................. 95 ANFISEditorGUIExample1: ii Contents CheckingDataHelpsModelValidation................... 98 ANFISEditorGUIExample2: CheckingDataDoesn’tValidateModel .................. 106 anfisfromtheCommandLine ........................... 109 MoreonanfisandtheANFISEditorGUI ................. 114 FuzzyClustering ...................................... 120 FuzzyC-MeansClustering ............................. 120 SubtractiveClustering ................................. 123 Stand-AloneC-CodeFuzzyInferenceEngine ............ 130 Glossary .............................................. 132 References ........................................... 134 Reference 3 GUITools ........................................... 3-2 MembershipFunctions ................................ 3-2 FISDataStructureManagement ........................ 3-3 AdvancedTechniques ................................. 3-4 SimulinkBlocks ...................................... 3-4 Demos .............................................. 3-5 iii iv Contents Before You Begin WhatIstheFuzzyLogicToolbox? . . . . . . . . . . . . . 2 HowtoUseThisGuide . . . . . . . . . . . . . . . . . 3 Installation . . . . . . . . . . . . . . . . . . . . . . 3 TypographicalConventions . . . . . . . . . . . . . . . 4 Before You Begin ThissectiondescribeshowtousetheFuzzyLogicToolbox.Itexplainshowto usethisguideandpointsyoutoadditionalbooksfortoolboxinstallation information. What Is the Fuzzy Logic Toolbox? TheFuzzyLogicToolboxisacollectionoffunctionsbuiltontheMATLAB® numericcomputingenvironment.Itprovidestoolsforyoutocreateandedit fuzzyinferencesystemswithintheframeworkofMATLAB,orifyoupreferyou canintegrateyourfuzzysystemsintosimulationswithSimulink®,oryoucan evenbuildstand-aloneCprogramsthatcallonfuzzysystemsyoubuildwith MATLAB.Thistoolboxreliesheavilyongraphicaluserinterface(GUI)toolsto helpyouaccomplishyourwork,althoughyoucanworkentirelyfromthe commandlineifyouprefer. Thetoolboxprovidesthreecategoriesoftools: •Commandlinefunctions •Graphical,interactivetools •Simulinkblocksandexamples Thefirstcategoryoftoolsismadeupoffunctionsthatyoucancallfromthe commandlineorfromyourownapplications.Manyofthesefunctionsare MATLABM-files,seriesofMATLABstatementsthatimplementspecialized fuzzylogicalgorithms.YoucanviewtheMATLABcodeforthesefunctions usingthestatement type function_name Youcanchangethewayanytoolboxfunctionworksbycopyingandrenaming theM-file,thenmodifyingyourcopy.Youcanalsoextendthetoolboxbyadding yourownM-files. Secondly,thetoolboxprovidesanumberofinteractivetoolsthatletyouaccess manyofthefunctionsthroughaGUI.Together,theGUI-basedtoolsprovide anenvironmentforfuzzyinferencesystemdesign,analysis,and implementation. ThethirdcategoryoftoolsisasetofblocksforusewiththeSimulink simulationsoftware.Thesearespecificallydesignedforhighspeedfuzzylogic inferenceintheSimulinkenvironment. 6