MATLAB Examples

# Build Mamdani Systems at the Command Line

You can construct a Mamdani fuzzy inference system (FIS) at the MATLAB® command line. This method is an alternative to interactively designing your FIS using docid:fuzzy.bu9027w.

## Tipping Problem at the Command Line

To demonstrate the command-line functionality for creating and viewing fuzzy inference systems, this example uses the tipper FIS.

```fis = readfis('tipper.fis') ```
```fis = struct with fields: name: 'tipper' type: 'mamdani' andMethod: 'min' orMethod: 'max' defuzzMethod: 'centroid' impMethod: 'min' aggMethod: 'max' input: [1x2 struct] output: [1x1 struct] rule: [1x3 struct] ```

This command returns a MATLAB structure that contains the properties of the fuzzy system.

The getfis command displays a list of fuzzy system properties, some of which are not fields in the FIS structure.

```getfis(fis) ```
``` 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 ```

To access these properties, use the getfis command, specifying the property name.

```getfis(fis,'Inlabels') ```
```ans = 2x7 char array 'service' 'food ' ```

Similarly, you can also obtain the FIS structure information using getfis.

```getfis(fis,'output',1) ```
```ans = struct with fields: Name: 'tip' NumMFs: 3 mf1: 'cheap' mf2: 'average' mf3: 'generous' range: [0 30] ```

You can also access FIS properties using dot notation. For example, to view the type of the fuzzy system, type:

```fis.type ```
```ans = 'mamdani' ```

To set the properties of your fuzzy system, use the setfis command. For example, to change the name of the FIS, type:

```fis = setfis(fis,'name','gratuity') ```
```fis = struct with fields: name: 'gratuity' type: 'mamdani' andMethod: 'min' orMethod: 'max' defuzzMethod: 'centroid' impMethod: 'min' aggMethod: 'max' input: [1x2 struct] output: [1x1 struct] rule: [1x3 struct] ```

Alternatively, you can use dot notation. For example:

```fis.name = 'gratuity'; ```

## FIS Structure

The FIS structure contains all the fuzzy inference system information, including the variable names, membership function definitions, and fuzzy inference methods. This structure is itself a hierarchy of structures, as shown in the following:

To view all the information about a FIS, use the showfis command.

```showfis(fis) ```
```1. Name gratuity 2. Type mamdani 3. Inputs/Outputs [2 1] 4. NumInputMFs [3 2] 5. NumOutputMFs 3 6. NumRules 3 7. AndMethod min 8. OrMethod max 9. ImpMethod min 10. AggMethod max 11. DefuzzMethod centroid 12. InLabels service 13. food 14. OutLabels tip 15. InRange [0 10] 16. [0 10] 17. OutRange [0 30] 18. InMFLabels poor 19. good 20. excellent 21. rancid 22. delicious 23. OutMFLabels cheap 24. average 25. generous 26. InMFTypes gaussmf 27. gaussmf 28. gaussmf 29. trapmf 30. trapmf 31. OutMFTypes trimf 32. trimf 33. trimf 34. InMFParams [1.5 0 0 0] 35. [1.5 5 0 0] 36. [1.5 10 0 0] 37. [0 0 1 3] 38. [7 9 10 10] 39. OutMFParams [0 5 10 0] 40. [10 15 20 0] 41. [20 25 30 0] 42. Rule Antecedent [1 1] 43. [2 0] 44. [3 2] 45. Rule Consequent 1 46. 2 47. 3 48. Rule Weight 1 49. 1 50. 1 51. Rule Connection 2 52. 1 53. 2 ```

## System Display Functions

To get a high-level view of your fuzzy system from the command line, use the plotfis, plotmf, and gensurf commands. plotfis displays the whole system as a block diagram, as shown in the Fuzzy Logic Designer.

```plotfis(fis) ```

The plotmf command plots all the membership functions associated with a given variable. For example, to view the membership functions for the first input variable, type:

```plotmf(fis,'input',1) ```

Similarly, to view the membership functions for the first output, type:

```plotmf(fis,'output',1) ```

The gensurf command plots the output of the FIS for any one or two input variables.

```gensurf(fis) ```

## Build FIS from Scratch

As an alternative to using the Fuzzy Logic Designer app, you can construct a FIS entirely from the command Fuzzy Logic Toolbox™ commands.

First, create a FIS, specifying its name.

```fis = newfis('tipper'); ```

Add the first input variable for the service quality using docid:fuzzy.FP141139.

```fis = addvar(fis,'input','service',[0 10]); ```

Add membership functions for each of the service quality levels using docid:fuzzy.FP99407. In this case, use Gaussian membership functions. For more information on Gaussian membership function properties, see docid:fuzzy.FP141360.

```fis = addmf(fis,'input',1,'poor','gaussmf',[1.5 0]); fis = addmf(fis,'input',1,'good','gaussmf',[1.5 5]); fis = addmf(fis,'input',1,'excellent','gaussmf',[1.5 10]); ```

Add the second input variable for the food quality and add two trapezoidal membership functions. For information on trapezoidal membership functions, see docid:fuzzy.FP703.

```fis = addvar(fis,'input','food',[0 10]); fis = addmf(fis,'input',2,'rancid','trapmf',[-2 0 1 3]); fis = addmf(fis,'input',2,'delicious','trapmf',[7 9 10 12]); ```

Add the output variable for the tip, and add three triangular membership functions. For more information on the triangular membership function, see docid:fuzzy.FP715.

```fis = addvar(fis,'output','tip',[0 30]); fis = addmf(fis,'output',1,'cheap','trimf',[0 5 10]); fis = addmf(fis,'output',1,'average','trimf',[10 15 20]); fis = addmf(fis,'output',1,'generous','trimf',[20 25 30]); ```

Specify the following three rules for the FIS as an array:

1. If (service is poor) or (food is rancid), then (tip is cheap).
2. If (service is good), then (tip is average).
3. If (service is excellent) or (food is delicious), then (tip is generous).

Each row of the array contains one rule in the following format.

• Column 1 - Index of membership function for first input
• Column 2 - Index of membership function for second input
• Column 3 - Index of membership function for output
• Column 4 - Rule weight
• Column 5 - Fuzzy operator (1 for AND, 2 for OR)

```ruleList = [1 1 1 1 2; 2 0 2 1 1; 3 2 3 1 2]; ```

Add the rules to the FIS.

```fis = addrule(fis,ruleList); ```

Alternatively, you can create the fuzzy inference system using dot notation.

```fis = newfis('tipper'); fis.input(1).name = 'service'; fis.input(1).range = [0 10]; fis.input(1).mf(1).name = 'poor'; fis.input(1).mf(1).type = 'gaussmf'; fis.input(1).mf(1).params = [1.5 0]; fis.input(1).mf(2).name = 'good'; fis.input(1).mf(2).type = 'gaussmf'; fis.input(1).mf(2).params = [1.5 5]; fis.input(1).mf(3).name = 'excellent'; fis.input(1).mf(3).type = 'gaussmf'; fis.input(1).mf(3).params = [1.5 10]; fis.input(2).name = 'food'; fis.input(2).range = [0 10]; fis.input(2).mf(1).name = 'rancid'; fis.input(2).mf(1).type = 'trapmf'; fis.input(2).mf(1).params = [-2 0 1 3]; fis.input(2).mf(2).name = 'delicious'; fis.input(2).mf(2).type = 'trapmf'; fis.input(2).mf(2).params = [7 9 10 12]; fis.output(1).name = 'tip'; fis.output(1).range = [0 30]; fis.output(1).mf(1).name = 'cheap'; fis.output(1).mf(1).type = 'trimf'; fis.output(1).mf(1).params = [0 5 10]; fis.output(1).mf(2).name = 'average'; fis.output(1).mf(2).type = 'trimf'; fis.output(1).mf(2).params = [10 15 20]; fis.output(1).mf(3).name = 'generous'; fis.output(1).mf(3).type = 'trimf'; fis.output(1).mf(3).params = [20 25 30]; fis.rule(1).antecedent = [1 1]; fis.rule(1).consequent = 1; fis.rule(1).weight = 1; fis.rule(1).connection = 2; fis.rule(2).antecedent = [2 0]; fis.rule(2).consequent = 2; fis.rule(2).weight = 1; fis.rule(2).connection = 1; fis.rule(3).antecedent = [3 2]; fis.rule(3).consequent = 3; fis.rule(3).weight = 1; fis.rule(3).connection = 2; ```

When constructing your fuzzy system, you can also specify custom membership functions and inference functions. For more information, see docid:fuzzy.brkii3j and docid:fuzzy.brkii3k.

## FIS Evaluation

To evaluate the output of a fuzzy system for a given input combination, use the docid:fuzzy.FP312 command. For example, evaluate a using input variable values of 1 and 2.

```evalfis([1 2],fis) ```
```ans = 5.5586 ```

You can also evaluate multiple input combinations using an array where each row represents one input combination.

```inputs = [3 5; 2 7; 3 1]; evalfis(inputs,fis) ```
```ans = 12.2184 7.7885 8.9547 ```