Main Content

Build Fuzzy Systems at the Command Line

You can construct a fuzzy inference system (FIS) at the MATLAB® command line. This method is an alternative to interactively designing your FIS using Fuzzy Logic Designer.

This example shows you how to create a Mamdani fuzzy inference system. While you create a Mamdani FIS, the methods used apply to creating Sugeno systems as well.

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');

This command returns a mamfis object that contains the properties of the fuzzy system. For a Sugeno system, this command returns a sugfis object.

You can access the FIS properties using dot notation. For example, view the inputs of the fuzzy system.

ans = 
  1x2 fisvar array with properties:


           Name        Range     MembershipFunctions
         _________    _______    ___________________

    1    "service"    0    10        {1x3 fismf}    
    2    "food"       0    10        {1x2 fismf}    

To set the properties of your fuzzy system, use dot notation. For example, set the name of the FIS.

fis.Name = "gratuity";

FIS Object

You represent fuzzy inference systems using mamfis and sugfis objects. These objects contain all the fuzzy inference system information, including the variable names, membership function definitions, and fuzzy inference methods. Each FIS is itself a hierarchy of objects. The following objects are used within a fuzzy system:

  • fisvar objects represent both input and output variables.

  • fismf objects represent membership functions within each input and output variable.

  • fisrule objects represent fuzzy rules that map inputs to outputs.

View all the information for a FIS by directly listing its properties.

fis = 
  mamfis with properties:

                       Name: "gratuity"
                  AndMethod: "min"
                   OrMethod: "max"
          ImplicationMethod: "min"
          AggregationMethod: "max"
      DefuzzificationMethod: "centroid"
                     Inputs: [1x2 fisvar]
                    Outputs: [1x1 fisvar]
                      Rules: [1x3 fisrule]
    DisableStructuralChecks: 0

	See 'getTunableSettings' method for parameter optimization.

You can view the properties of the objects within a FIS object using dot notation. For example, view the fisvar object for first input variable.

ans = 
  fisvar with properties:

                   Name: "service"
                  Range: [0 10]
    MembershipFunctions: [1x3 fismf]

Also, view the membership functions for this variable.

ans = 
  1x3 fismf array with properties:


            Name          Type       Parameters
         ___________    _________    __________

    1    "poor"         "gaussmf"    1.5      0
    2    "good"         "gaussmf"    1.5      5
    3    "excellent"    "gaussmf"    1.5     10

System Display Functions

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


Figure contains 4 axes objects. Axes object 1 contains 3 objects of type line. Axes object 2 contains 2 objects of type line. Axes object 3 contains 3 objects of type line. Axes object 4 contains 3 objects of type text.

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


Figure contains an axes object. The axes object contains 6 objects of type line, text.

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


Figure contains an axes object. The axes object contains 6 objects of type line, text.

plotmf does not support viewing the output membership functions for Sugeno systems.

To view the rules of the fuzzy system, type:

ans = 
  1x3 fisrule array with properties:



    1    "service==poor | food==rancid => tip=cheap (1)"           
    2    "service==good => tip=average (1)"                        
    3    "service==excellent | food==delicious => tip=generous (1)"

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


Figure contains an axes object. The axes object contains an object of type surface.

Build Fuzzy Inference System

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

First, create a Mamdani FIS, specifying its name.

fis = mamfis('Name',"tipper");

Add the first input variable for the service quality using addInput.

fis = addInput(fis,[0 10],'Name',"service");

Add membership functions for each of the service quality levels using addMF. In this case, use Gaussian membership functions. For more information on Gaussian membership function properties, see gaussmf.

fis = addMF(fis,"service","gaussmf",[1.5 0],'Name',"poor");
fis = addMF(fis,"service","gaussmf",[1.5 5],'Name',"good");
fis = addMF(fis,"service","gaussmf",[1.5 10],'Name',"excellent");

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

fis = addInput(fis,[0 10],'Name',"food");
fis = addMF(fis,"food","trapmf",[-2 0 1 3],'Name',"rancid");
fis = addMF(fis,"food","trapmf",[7 9 10 12],'Name',"delicious");

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

fis = addOutput(fis,[0 30],'Name',"tip");
fis = addMF(fis,"tip","trimf",[0 5 10],'Name',"cheap");
fis = addMF(fis,"tip","trimf",[10 15 20],'Name',"average");
fis = addMF(fis,"tip","trimf",[20 25 30],'Name',"generous");

Specify the following three rules for the FIS as a numeric 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 (from 0 to 1)

  • Column 5 - Fuzzy operator (1 for AND, 2 for OR)

For the membership function indices, indicate a NOT condition using a negative value. For more information on fuzzy rule specification, see addRule.

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 a combination of dot notation and fisvar, fismf, and fisrule objects. This method is not a good practice for most applications. However, you can use this approach when your application requires greater flexibility in constructing and modifying your FIS.

Create the fuzzy inference system.

fis = mamfis('Name','tipper');

Add and configure the first input variable. In this case, create a default fisvar object and specify its properties using dot notation.

fis.Inputs(1) = fisvar;
fis.Inputs(1).Name = "service";
fis.Inputs(1).Range = [0 10];

Define the membership functions for the first input variable. For each MF, create a fismf object, and set the properties using dot notation.

fis.Inputs(1).MembershipFunctions(1) = fismf;
fis.Inputs(1).MembershipFunctions(1).Name = "poor";
fis.Inputs(1).MembershipFunctions(1).Type = "gaussmf";
fis.Inputs(1).MembershipFunctions(1).Parameters = [1.5 0];
fis.Inputs(1).MembershipFunctions(2) = fismf;
fis.Inputs(1).MembershipFunctions(2).Name = "good";
fis.Inputs(1).MembershipFunctions(2).Type = "gaussmf";
fis.Inputs(1).MembershipFunctions(2).Parameters = [1.5 5];
fis.Inputs(1).MembershipFunctions(3) = fismf;
fis.Inputs(1).MembershipFunctions(3).Name = "excellent";
fis.Inputs(1).MembershipFunctions(3).Type = "gaussmf";
fis.Inputs(1).MembershipFunctions(3).Parameters = [1.5 10];

Add and configure the second input variable. For this variable, specify the name and range when you create the fisvar object.

fis.Inputs(2) = fisvar([0 10],'Name',"food");

Specify the membership functions for the second input. For each MF, specify the name, type, and parameters when you create the fismf object.

fis.Inputs(2).MembershipFunctions(1) = fismf("trapmf",[-2 0 1 3],...
fis.Inputs(2).MembershipFunctions(2) = fismf("trapmf",[7 9 10 12],...

Similarly, add and configure the output variable and its membership functions.

fis.Outputs(1) = fisvar([0 30],'Name',"tip");

In this case, specify the output membership functions using a vector of fismf objects.

mf1 = fismf("trimf",[0 5 10],'Name',"cheap");
mf2 = fismf("trimf",[10 15 20],'Name',"average");
mf3 = fismf("trimf",[20 25 30],'Name',"generous");
fis.Outputs(1).MembershipFunctions = [mf1 mf2 mf3];

Create the rules for the fuzzy system. For each rule create a fisrule object. Then, specify the rules using a vector of these objects. When creating a fisrule object using numeric values, you must specify the number of inputs variables.

rule1 = fisrule([1 1 1 1 2],2);
rule2 = fisrule([2 0 2 1 1],2);
rule3 = fisrule([3 2 3 1 2],2);
rules = [rule1 rule2 rule3];

Before adding your rules to your fuzzy system, you must update them using the data in the FIS object. Update the rules using the update function, and add them the fuzzy system.

rules = update(rules,fis);
fis.Rules = rules;

When constructing your fuzzy system, you can also specify custom membership functions and inference functions. For more information, see Build Fuzzy Systems Using Custom Functions.

Evaluate Fuzzy Inference System

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

evalfis(fis,[1 2])
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];
ans = 3×1


See Also

| | | | |

Related Topics