Build Mamdani Systems (Code) - MATLAB & Simulink - MathWorks India | Matlab | C (Programming Language)

# Build Mamdani Systems (Code

)
On this page… Tipping Problem from the Command Line System Display Functions Building a System from Scratch FIS Evaluation The FIS Structure

Tipping Problem from the Command Line
The tipping problem is one of many Fuzzy Logic Toolbox™ examples of fuzzy inference systems. The FIS is alw ays cast as a MATLAB® structure. To load this system, type: a=r e a d f i s ( ' t i p p e r . f i s ' ) This command returns the follow ing result: a= n a m e :' t i p p e r ' t y p e :' m a m d a n i ' a n d M e t h o d :' m i n ' o r M e t h o d :' m a x ' d e f u z z M e t h o d :' c e n t r o i d ' i m p M e t h o d :' m i n ' a g g M e t h o d :' m a x ' i n p u t :[ 1 x 2s t r u c t ] o u t p u t :[ 1 x 1s t r u c t ] r u l e :[ 1 x 3s t r u c t ] The labels on the left of this listing represent the various components of the MATLAB structure associated w ith t i p p e r . f i s . To access the various components of this structure, type the component name after aat the MATLAB prompt. For example: a . t y p e returns the follow ing result: a n s= m a m d a n i The function g e t f i s ( a ) returns almost the same structure information that typing a , alone does: N a m e =t i p p e r T y p e =m a m d a n i N u m I n p u t s=2 I n L a b e l s = s e r v i c e f o o d N u m O u t p u t s=1 O u t L a b e l s=

s u r f v i e w ( a )displays the Surface View er. you could type a=s e t f i s ( a . r u l e e d i t ( a )displays the Rule Editor. but you can get it by typing g e t f i s ( a . m f ( 1 ) The function g e t f i sis loosely modeled on the Handle Graphics ® function g e t . try s h o w f i s ( a ) This syntax returns a printout listing all the information about a .designates the fuzzy tipping system. Any of the follow ing functions w ill display the tipping system w ith the associated GUI: f u z z y ( a )displays the FIS Editor. 1 . 1 ) g e t f i s ( a . ' n a m e ' . you cannot get information by typing a . For more information on the syntax for MATLAB structures and cell arrays. you can set this information more simply by typing a . r u l e v i e w ( a )displays the Rule View er. Thus. you can display any of the GUIs for the tipping system directly from the command line. ' m f ' . 1 ) The s t r u c t u r e . but it show s all the information recorded in the FIS structure Because the variable. For example. This function is intended more for debugging than anything else. . How ever. if you w anted to change the name of this system. For example. It allow s you to change any property of a FIS. because ais already a MATLAB structure. Now the FIS structure ahas been changed to reflect the new name. ' g r a t u i t y ' ) . g e t f i s ( a . ' I n l a b e l s ' ) Similarly. you can obtain structure information using g e t f i sin this manner. a . f i e l dsyntax also generates this information. i n p u t or a .t i p N u m R u l e s=3 A n d M e t h o d=m i n O r M e t h o d=m a x I m p M e t h o d=m i n A g g M e t h o d=m a x D e f u z z M e t h o d=c e n t r o i d Some of these fields are not part of the structure a . n a m e=' g r a t u i t y ' . I n l a b e l s . If you w ant a little more insight into this FIS structure. ' o u t p u t ' . type a . ' i n p u t ' . i n p u t ( 1 ) . ' i n p u t ' . m f e d i t ( a )displays the Membership Function Editor. The function s e t f i sacts as the reciprocal to g e t f i s . see Create a Structure Array and Create a Cell Array in the MATLAB documentation. 1 ) g e t f i s ( a .

in addition. p l o t m f ( a . you can access any of the other GUIs using the pull-dow n menu rather than the command line. and g e n s u r f . then a n f i s e d i t ( a )displays the ANFIS Editor GUI.If. p l o t f i s ( a ) After closing any open MATLAB figures or GUI w indow s. ais a Sugeno-type FIS. 1 ) returns the follow ing plots . System Display Functions There are three functions designed to give you a high-level view of your fuzzy inference system from the command line: p l o t f i s .p l o t m f . When you open any of these GUIs. ' i n p u t ' . The first of these displays the w hole system as a block diagram much as it w ould appear on the FIS Editor. the function p l o t m f plots all the membership functions associated w ith a given variable as follow s.

g e n s u r fmust be generated w ith all but tw o inputs fixed. When there are three or more inputs. or in an open MATLAB figure. as is described in g e n s u r f . or a three-dimensional surface.p l o t m f ( a . the function g e n s u r fplots any one or tw o inputs versus any one output of a given system. if p l o t m fis called w hile either of these is open. ' o u t p u t ' . Finally. g e n s u r f ( a ) . 1 ) These plots appear in the Membership Function Editor GUI. The result is either a tw o-dimensional curve.

input. Column m + n + 1 is the w eight associated w ith that rule (typically 1) and column m + n + 2 specifies the connective used (w here AND = 1 and OR = 2). The structure associated w ith the preceding rule is 1320 . then the first m vector entries of the rule structure correspond to inputs 1 through m. n a m e = ' g o o d ' . a . i n p u t ( 1 ) . p a r a m s = [ 1 . i n p u t ( 1 ) . using the MATLAB structure syntax.Building a System from Scratch You can build a fuzzy inference system using Fuzzy Logic Toolbox commands as an alternative to the GUI tools. Probably the most difficult part of this process is learning the shorthand that the fuzzy inference systems use for building rules. a = n e w f i s ( ' t i p p e r ' ) . has an index number. t y p e = ' g a u s s m f ' . The next n columns w ork the same w ay for the outputs. m f ( 2 ) . a . For example. The entry in column 1 is the index number for the membership function associated w ith input 1. you use the commands n e w f i s . and each membership function has an index number. a .5) This rule is turned into a structure according to the follow ing logic. a . p a r a m s = [ 1 . i n p u t ( 1 ) . a . 50 ] . i n p u t ( 1 ) . m f ( 1 ) . n a m e = ' e x c e l l e n t ' . i n p u t ( 1 ) . The entry in column 2 is the index number for the membership function associated w ith input 2. i n p u t ( 1 ) . a d d r u l eto do this. m f ( 3 ) . a . or output. then output1 is MF2 (weight = 0. m f ( 2 ) .a d d v a r . If there are m inputs to a system and n outputs. and a d d r u l e . Each variable. t y p e = ' g a u s s m f ' . i n p u t ( 1 ) . r a n g e = [ 01 0 ] .a d d m f . to build the tipping system entirely from the command line. m f ( 2 ) . i n p u t ( 1 ) . m f ( 1 ) . a . n a m e = ' s e r v i c e ' . Use the command line function. n a m e = ' p o o r ' . 55 ] . m f ( 1 ) . a . 52 This sample code show s one w ay you can build the entire tipping system from the command line. . i n p u t ( 1 ) . The rules are built from statements such as the follow ing: If input1 is MF1 or input2 is MF3. a . and so on.

w e i g h t = 1 . a . ' t r a p m f ' . m f ( 2 ) . i n p u t ( 2 ) . o u t p u t ( 1 ) . ' t r i m f ' . i n p u t ( 2 ) . 2 . o u t p u t ( 1 ) . ' o u t p u t ' . a . m f ( 3 ) . a . [ 2 02 53 0 ] ) . t y p e = ' t r a p m f ' . c o n s e q u e n t = [ 2 ] . i n p u t ( 2 ) . m f ( 1 ) . a = a d d m f ( a . o u t p u t ( 1 ) . a n t e c e d e n t = [ 20 ] . i n p u t ( 2 ) . p a r a m s = [ 2 02 53 0 ] . ' o u t p u t ' . c o n s e q u e n t = [ 1 ] . t y p e = ' g a u s s m f ' . [ 051 0 ] ) . a . a . [ 1 01 52 0 ] ) . a n t e c e d e n t = [ 32 ] . o u t p u t ( 1 ) . [ 01 0 ] ) . r a n g e = [ 03 0 ] . a . n a m e = ' d e l i c i o u s ' . [ 791 01 2 ] ) . a = a d d m f ( a . a . i n p u t ( 2 ) . ' g e n e r o u s ' . 1 . r u l e L i s t = [. a . 1 . a . m f ( 3 ) . . n a m e = ' a v e r a g e ' . c o n n e c t i o n = 1 . m f ( 1 ) . 51 0 ] ) . ' g o o d ' . o u t p u t ( 1 ) . mfile. ' t r i m f ' . ' g a u s s m f ' . a . [ 1 . a = a d d v a r ( a . a . t y p e = ' t r i m f ' . i n p u t ( 1 ) . p a r a m s = [ 1 01 52 0 ] . a . m f ( 3 ) . 55 ] ) . a . i n p u t ( 2 ) . 1 . m f ( 3 ) . 1 . For example. r a n g e = [ 01 0 ] . m f ( 3 ) . ' i n p u t ' . ' i n p u t ' . a . a . . ' i n p u t ' . ' i n p u t ' . m f ( 1 ) . a n t e c e d e n t = [ 11 ] . a . r u l e ( 1 ) . you can build the entire tipping system from the command line using Fuzzy Logic Toolbox commands. ' o u t p u t ' . t y p e = ' t r i m f ' . w e i g h t = 1 . i n p u t ( 2 ) . [ 1 . r u l e ( 2 ) . a . p a r a m s = [ 051 0 ] . ' t r i m f ' . 51 0 ] . These commands are in the m k t i p p e r . i n p u t ( 1 ) . a = a d d m f ( a . [ 01 0 ] ) . a . r a n g e=r 1 . t y p e = ' t r i m f ' . r u l e ( 3 ) .a . to specify the range of the input. 1 . m f ( 2 ) . n a m e = ' f o o d ' . ' f o o d ' . w e i g h t = 1 . a . m f ( 1 ) . ' i n p u t ' . ' i n p u t ' . m f ( 1 ) . a . ' g a u s s m f ' . o u t p u t ( 1 ) . ' e x c e l l e n t ' . a . i n p u t ( 2 ) . ' s e r v i c e ' . . 50 ] ) . c o n s e q u e n t = [ 3 ] . a . a . a . ' p o o r ' . a . ' d e l i c i o u s ' . 1 . n a m e = ' g e n e r o u s ' . a . 2 . c o n n e c t i o n = 2 . r u l e ( 1 ) . m f ( 1 ) . type: r 1 =[ 01 0 ] a . a . a = a d d v a r ( a . n a m e = ' r a n c i d ' . r u l e ( 3 ) . p a r a m s = [ 1 . o u t p u t ( 1 ) . a = n e w f i s ( ' t i p p e r ' ) . t y p e = ' t r a p m f ' . c o n n e c t i o n = 2 Tip You can also build the FIS structure using MATLAB w orkspace variables. ' i n p u t ' . m f ( 2 ) . [ 1 . r u l e ( 1 ) . [ 2013 ] ) . r u l e ( 2 ) . a = a d d m f ( a . r u l e ( 2 ) . a . a = a d d m f ( a . o u t p u t ( 1 ) . r u l e ( 3 ) . o u t p u t ( 1 ) . r u l e ( 3 ) . a = a d d m f ( a . Alternatively. m f ( 2 ) . i n p u t ( 1 ) . m f ( 2 ) . a . ' t r a p m f ' . ' o u t p u t ' . p a r a m s = [ 2013 ] . a = a d d m f ( a . ' t i p ' . a = a d d m f ( a . a . [ 03 0 ] ) . ' a v e r a g e ' . ' g a u s s m f ' . r u l e ( 1 ) . ' c h e a p ' . n a m e = ' t i p ' . r u l e ( 2 ) . a . m f ( 2 ) . n a m e = ' c h e a p ' a . ' r a n c i d ' . a = a d d v a r ( a . o u t p u t ( 1 ) . o u t p u t ( 1 ) . p a r a m s = [ 791 01 2 ] .

5 5 8 6 This function can also be used for multiple collections of inputs. ' i n p u t ' . including variable names. a=r e a d f i s ( ' t i p p e r ' ) . specify the name of the custom membership function. [ 01246891 0 ] ) . r u l e L i s t ) . .a ) a n s= 1 2 .11112 20211 32312] . FIS Evaluation To evaluate the output of a fuzzy system for a given input. as show n in the follow ing example: a = a d d m f ( a . This structure is stored inside each GUI tool. All the information for a given fuzzy inference system is contained in the FIS structure. 1 . e v a l f i s ( [ 12 ] . For example. ' c u s t m f 1 ' . Access functions such as g e t f i sand s e t f i smake it easy to examine this structure. d e f u z z M e t h o d = ' c u s t o m d e f u z z ' . because different input vectors are represented in different parts of the input structure. To include a custom inference function. and Specifying Custom Inference Functions. This structure can itself be thought of as a hierarchy of structures. and specify them for building fuzzy inference systems at the command line. 7 8 8 5 The FIS Structure The FIS structure is the MATLAB object that contains all the fuzzy inference system information. as show n in the follow ing diagram.27 ] . To include a custom membership function. use the function e v a l f i s . membership function definitions. the follow ing script evaluates t i p p e rat the input. as show n in the follow ing example: a . 2 1 8 4 7 . ' c u s t o m M F 1 ' .a ) a n s= 5 . e v a l f i s ( [ 35 . and so on. a = a d d r u l e ( a . [1 2]. Specifying Custom Mem bership and Inference Functions You can create custom membership and inference functions as described in Specifying Custom Membership Functions. specify the name of the custom inference function.

3 3 . t i p p e r m a m d a n i [21] [32] 3 3 m i n m a x m i n m a x c e n t r o i d s e r v i c e f o o d t i p [01 0] [01 0] [03 0] p o o r g o o d e x c e l l e n t r a n c i d d e l i c i o u s c h e a p a v e r a g e g e n e r o u s g a u s s m f g a u s s m f g a u s s m f t r a p m f t r a p m f t r i m f t r i m f t r i m f [1 .O u t R a n g e 1 8 .O u t L a b e l s 1 5 . as show n in the follow ing code sample: s h o w f i s ( a ) 1 .I n M F T y p e s 2 7 .You can generate a listing of information on the FIS using the s h o w f i scommand. 2 1 . 3 6 . 3 4 . I n p u t s / O u t p u t s 4 . 1 7 . 2 2 . A n d M e t h o d 8 . N a m e 2 .I n M F L a b e l s 1 9 . 2 0 . 2 8 .I n R a n g e 1 6 . 51 000] .A g g M e t h o d 1 1 . 2 6 . N u m I n p u t M F s 5 .D e f u z z M e t h o d 1 2 .I n M F P a r a m s 3 5 . 2 3 .I n L a b e l s 1 3 .O u t M F L a b e l s 2 4 . I m p M e t h o d 1 0 . 5000] [1 . O r M e t h o d 9 . T y p e 3 . 5500] [1 . 2 9 . N u m R u l e s 7 . 3 1 .O u t M F T y p e s 3 2 . N u m O u t p u t M F s 6 . 3 0 . 1 4 . 2 5 .

The rules appear in indexed format in a . 4 4 . that changing one entry may oblige you to change another.O u t M F P a r a m s 4 0 . You should be aw are. f i s .R u l eC o n s e q u e n t 1 4 2 . [ S y s t e m ] N a m e = ' t i p p e r ' T y p e = ' m a m d a n i ' N u m I n p u t s = 2 N u m O u t p u t s = 1 N u m R u l e s = 3 A n d M e t h o d = ' m i n ' O r M e t h o d = ' m a x ' I m p M e t h o d = ' m i n ' A g g M e t h o d = ' m a x ' D e f u z z M e t h o d = ' c e n t r o i d ' [ I n p u t 1 ] N a m e = ' s e r v i c e ' R a n g e = [ 01 0 ] N u m M F s = 3 M F 1 = ' p o o r ' : ' g a u s s m f ' . you can modify the FIS by editing its .R u l eW e i g h t 4 3 . you also need to make certain that any rules requiring this membership function are also deleted. 55 ] M F 3 = ' e x c e l l e n t ' : ' g a u s s m f ' .R u l eC o n n e c t i o n 2 The list of command-line functions associated w ith FIS construction includes g e t f i s . [0013] [791 01 0] [051 00] [1 01 52 00] [2 02 53 00] [20] [32] 2 3 1 1 1 1 2 4 2 . Saving FIS Files A specialized text file format is used for saving fuzzy inference systems. 50 ] M F 2 = ' g o o d ' : ' g a u s s m f ' . [ 791 01 0 ] . [ 1 . and r m m f . 3 9 . 4 4 .a d d v a r . [ 1 . 51 0 ] [ I n p u t 2 ] N a m e = ' f o o d ' R a n g e = [ 01 0 ] N u m M F s = 2 M F 1 = ' r a n c i d ' : ' t r a p m f ' . how ever.s e t f i s . 4 3 . f i stext file rather than using any of the GUIs.a d d r u l e . [ 1 .s h o w f i s . The functions r e a d f i sand w r i t e f i sare used for reading and w riting these files. 3 8 . if you delete a membership function using this method. The follow ing sample show s the file t i p p e r . 4 3 . [ 0013 ] M F 2 = ' d e l i c i o u s ' : ' t r a p m f ' . If you prefer. 4 1 . For example.r m v a r .R u l eA n t e c e d e n t [11] 4 2 .a d d m f . 4 3 . 4 4 . 4 2 . 4 4 .3 7 . f i stext file.

[ 051 0 ] M F 2 = ' a v e r a g e ' : ' t r i m f ' .1( 1 ):2 20 .3( 1 ):2 . [ 2 02 53 0 ] [ R u l e s ] 11 .2( 1 ):1 32 .[ O u t p u t 1 ] N a m e = ' t i p ' R a n g e = [ 03 0 ] N u m M F s = 3 M F 1 = ' c h e a p ' : ' t r i m f ' . [ 1 01 52 0 ] M F 3 = ' g e n e r o u s ' : ' t r i m f ' .