Main Content

Build Fuzzy Systems Using Fuzzy Logic Designer

Since R2022b

This example shows how to interactively create a type-1 Mamdani fuzzy inference system (FIS) using the Fuzzy Logic Designer app.

Before R2022b: Interactively create a type-1 FIS using the previous version of the Fuzzy Logic Designer app as shown in this example (R2022a).

For this example, you create a FIS to solve the tipping problem defined in Fuzzy vs. Nonfuzzy Logic. The tipping behavior is defined using the following three rules.

  1. If the service is poor or the food is rancid, then the tip is cheap.

  2. If the service is good, then the tip is average.

  3. If the service is excellent or the food is delicious, then the tip is generous.

While this example creates a type-1 Mamdani FIS, the general methods used apply to creating type-2 and Sugeno systems as well. For more information on the different types of fuzzy systems, see Mamdani and Sugeno Fuzzy Inference Systems and Type-2 Fuzzy Inference Systems.

You can also interactively create a FIS tree using Fuzzy Logic Designer. For an example, see Build FIS Tree Using Fuzzy Logic Designer.

For more information on building a FIS at the command line, see Build Fuzzy Systems at the Command Line.

For this example, you build a tipper FIS from scratch. Alternatively, you can load the system from the tipper.fis file. To do so, use the following command.

fuzzyLogicDesigner("tipper.fis")

Create FIS Structure

To build the FIS yourself, first open the app. On the Apps tab, under Control System Design and Analysis, click the app icon.

The app opens a Getting Started dialog box. For more information on the options for creating your initial FIS structure, see Get Started Using Fuzzy Logic Designer.

For this example, since you are creating a type-1 Mamdani system with the two inputs and one output, you can use a built in template as a starting point. To do so:

  1. Since you define the rules for your FIS manually, clear the Generate rules automatically check box.

  2. Under Template Fuzzy Inference Systems, select Mamdani Type-1.

Getting Started dialog - In the top-right corner, the Generate rules automatically parameter is cleared. The Mamdani Type-1 template is highlighted.

The app creates the template FIS and loads it in the app.

Initial Fuzzy Logic Designer view, showing the input and output variables in the Fuzzy Inference System Plot document.

In the Property Editor pane, you can specify the name of your FIS along with its inference methods. For this example, use the default inference methods. For more information on the available inference methods, see Fuzzy Inference Process and Foundations of Fuzzy Logic.

In the Name field, enter the name tipper.

Property Editor with Name field highlighted and the name value set to "tipper"

Define Input Variables

For this example, the template FIS already has two input variables. In the System Browser pane, click Inputs. The app displays the properties of the input variables in the Property Editor.

App with Inputs label selected in the System Browser on the left and the corresponding input variable properties listed in the Property Editor on the right

In the Property Editor, define the following properties for each input variable.

  • Name — Input variable name. For this example, name the first input service and the second input food.

  • Range — Input variable range. For this example, both inputs are rated on a scale from 0 through 10. Therefore, specify each variable range as [0 10]

Property Editor input table with three columns including the Name column on the left, the Range column in the middle and the Number of MFs column on the right. The updated settings for the two input variables are specified in the Name and Range columns.

You can add more input variables if they are required for your application. To do so, on the Design tab, in the Add Components gallery, click Input.

App toolstrip with the Input option listed as the only option in the Add Components gallery

Define Output Variables

For this example, the template FIS already has one output variable. In the System Browser pane, click Outputs. The app displays the properties of the output variable in the Property Editor.

In the Property Editor, define the following properties for the output variable.

  • Name — Output variable name. For this example, name the output variable tip.

  • Range — Output variable range. For this example, specify the output variable range as [0 30]

Property Editor input table with updated settings for the output variable specified in the Name and Range columns.

You can add output variables if they are required for your application. To do so, in the Add Components gallery, click Output.

App toolstrip with the Output option listed as the only option in the Add Components gallery

Define Membership Functions

To add or remove membership functions (MFs) for a given variable, select the variable in the System Browser or click the variable in the Fuzzy Inference System document. For example, select the food input variable. In the Property Editor, the app shows the input variable and membership function properties.

Fuzzy Inference System Plot document on the left showing the food input as selected and the Property Editor on the right showing the three default MFs for the food input.

By default, the template adds three membership functions for each variable. For this example, the service input variable and the tip output variable both require three membership functions.

However, the food variable requires two membership functions. To remove one of the default membership functions, in the System Browser, under food, right-click the membership function and select Delete.

System Browser with the food variable expanded to show the MFs and the context menu showing the Delete option for the second MF.

You can add membership functions to a given variable if they are required for your application. To do so, in the Add Components gallery, click MF.

App toolstrip with the MF option listed as the only option in the Add Components gallery

To edit membership function properties, in the System Browser, select the membership function or the corresponding variable. Then, in the Property Editor, specify the following membership function properties.

  • Name — Membership function name

  • Type — Membership function type

  • Parameters — Membership function parameters

To view a plot of the membership functions for a given variable, select the variable in the System Browser and open the Membership Function Editor. The following figure shows the membership functions for the service input variable.

Configure the membership functions for the service input using the properties shown in the figure.

Membership Function Editor on the left showing a plot of three Gaussian MFs: poor on the left side of the plot, good in the middle, and excellent on the right. The Property Editor table on the right lists the MFs and their parameters: poor with parameters [1.5 0], good with parameters [1.5 5], and excellent with parameters [1.5 10].

Similarly, configure the properties for the food input variable.

Membership Function Editor on the left showing a plot of two trapezoidal MFs: rancid on the left side of the plot and delicious on the right. The Property Editor table on the right lists the MFs and their parameters: rancid with parameters [0 0 1 3] and delicious with parameters [7 9 10 10].

Finally, define the membership functions for the tip output.

Membership Function Editor on the left showing a plot of three triangular MFs: cheap on the left side of the plot, average in the middle, and generous on the right. The Property Editor table on the right lists the MFs and their parameters: cheap with parameters [0 5 10], average with parameters [10 15 20], and generous with parameters [20 25 30].

For more information on defining membership functions, see Define Membership Functions Using Fuzzy Logic Designer.

Define Rule Base

Once you define the variables and membership functions for your FIS, you can define the if-then rule base for the system.

For this example, use the following rules.

  1. If the service is poor or the food is rancid, then the tip is cheap.

  2. If the service is good, then the tip is average.

  3. If the service is excellent or the food is delicious, then the tip is generous.

Open the Rule Editor document.

Rule Editor with no rules currently listed in the table. There is a plus icon on the right edge of the Rule Editor near the top of the rule table

To add a rule, click the plus icon. The app adds a rule to the rule table and shows the rule properties in the Property Editor.

Rule Editor on the left showing the single default rule in the rule table as selected. On the right, the Property Editor shows the properties of this rule.

Modify the rule properties for the first rule: If the service is poor or the food is rancid, then the tip is cheap.

  1. Set the Connection parameter to Or.

  2. For the service input, select poor as the linguistic term.

  3. For the food input, select rancid as the linguistic term.

  4. For the tip output, select cheap as the linguistic term.

Property Editor showing the updated parameters for the first rule

Similarly, add the next rule: If the service is good, then the tip is average. For this rule:

  1. For the service input, select good as the linguistic term.

  2. For the tip output, select average as the linguistic term.

  3. Since this rule has only the service input in the antecedent, ignore the food input by setting its linguistic term to none.

Finally, add the third rule: If the service is excellent or the food is delicious, then the tip is generous. For this rule:

  1. Set the Connection parameter to Or.

  2. For the service input, select excellent as the linguistic term.

  3. For the food input, select delicious as the linguistic term.

  4. For the tip output, select generous as the linguistic term.

View the final rule base in the Rule Editor.

Rule Editor showing the final three rules in the rule table.

For more information on specifying rules for your FIS, see Define Fuzzy Rules Using Fuzzy Logic Designer.

Analyze Design

Once you define your rule base, you can analyze your design using the following documents.

  • Rule Inference — Specify input values and view the inference diagram and resulting output value.

  • Control Surface — Plot the output values for all combinations of two input variables.

  • System Validation — Compare the outputs from each FIS design with the corresponding output value from the testing data. (since R2023a)

  • Error Distribution — For a given FIS design and testing data, view the output error for different combinations of inputs. (since R2023a)

For more information on FIS analysis methods, see Analyze Fuzzy System Using Fuzzy Logic Designer.

For this example, analyze the FIS using the Rule Inference and Control Surface documents. To open either document, on the Design tab, click the corresponding icon in the Simulation gallery.

App toolstrip highlighting the Rule Inference and Control Surface options in the Simulation gallery.

Rule Inference

In the Rule Inference document, you specify values for your input variables, inspect the inference process, and view the resulting output value.

For example, consider the case where the service was quite poor (1/10) but the food was very good (9/10). Specify these input values using the Input values parameter. Alternatively, you can drag the input bars in the inference diagram.

Rule inference document for input values [1 9]. For each rule, there is one row with three plots in order left-to-right: service input, food input, and tip output. Each plot shows the MF for that rule. There are vertical lines indicating the input value for each variable that intersect the MFs in the plots. The fuzzy intersection values are combined using the OR inference operator (maximum) and used to clip the output MFs. The aggregate output MF is shown in a plot on the bottom right of the document.

The low service rating causes the first rule to fire with a high firing strength and the high food rating causes the third rule to fire with a high firing strength. When the resulting output membership functions are aggregated and defuzzified, the final tip value is 15.2%.

For more information on fuzzy inference, see Fuzzy Inference Process.

Control Surface

The Control Surface document shows the FIS output value for all possible combinations of two input variables. For this example, the plot shows the tip value for all service and food rating combinations. Generally, the tip is low (5%) when the food and service inputs are both near 0 and high (25%) when the food and service inputs are both near 10.

Control Surface document showing a nonlinear surface plot of tip values.

Store and Modify Designs

You can store multiple FIS designs in the app, which allows you to explore possible design options. All stored designs must have the same number of inputs and the same number of outputs.

A common design option to explore is converting a Mamdani system into a Sugeno system. In general, Sugeno systems are more computationally efficient, which is an important consideration when deploying a FIS.

To convert the FIS to a Sugeno system, on the Design tab, select Mamdani to Sugeno.

App toolstrip with the Mamdani to Sugeno option highlighted in the Convert FIS toolstrip section.

In the Design Browser pane, the app creates a Sugeno version of the FIS called tipper_1.

Design Browser table showing two rows, each with one design. The table has three columns, listed left-to-right: Active, Design, Type. The first row has a check mark in the Active column, its name "tipper" in the Design column, and Mamdani Type-1 in the Type column. The second row has an empty Active column, its name "tipper_1" in the Design column, and Sugeno Type-1 in the Type column.

To make this design active, select the design in the table and click Set Active Design.

You can also manually store a copy of your current FIS in the Design Browser. To do so, on the Design tab, select Store Current Design.

App toolstrip with the Store Current Design option highlighted in the Designs toolstrip section.

You can then analyze the alternative design in the same manner as the first design.

The plot for the Sugeno system in the Control Surface document looks similar to the plot for the Mamdani system.

Control Surface document showing a nonlinear surface plot of tip values.

Using the Rule Inference document, you can see that, for the same service and food ratings, the Sugeno system generates a 16.1% tip, which is similar to the 15.2% tip for the Mamdani system.

Rule inference document for input values [1 9]. The input MF plots match those of the previous Mamdani rule inference plot. The difference is that the output MFs are now singleton MFs rather than fuzzy sets.

Export FIS

Once you design your FIS using Fuzzy Logic Designer, you can export the active and stored designs to the MATLAB® workspace for further simulations and deployment.

To export a FIS design to the MATLAB workspace, in Fuzzy Logic Designer, select Export > Export Fuzzy Inference System to Workspace.

App toolstrip showing the Export Fuzzy Inference System to Workspace selection in the Export drop-down menu on the far right side of the toolstrip.

The Export Fuzzy Inference System to Workspace dialog box lists the active FIS design along with any stored designs.

Export Fuzzy Inference Systems to Workspace dialog box listing two FIS designs to export. The table contains three columns: Export on the left with check boxes for selecting designs, Fuzzy Inference System in the middle with the design names, and Export As on the right with default variable names that match the design names.

In the Export column, select one or more systems that you want to export.

In the Export As column, specify names for the workspace variables.

Click Export.

The app saves the FIS to the MATLAB workspace.

For more information on exporting and saving your FIS, see Export FIS and Simulation Data from Fuzzy Logic Designer.

See Also

Related Topics