Build Fuzzy Systems Using Custom Functions

Build Fuzzy Inference Systems Using Custom Functions in Fuzzy Logic Designer

When you build a fuzzy inference system, as described in Fuzzy Inference Process, you can replace the built-in membership functions, inference functions, or both with custom functions. In this section, you learn how to build a fuzzy inference system using custom functions in the Fuzzy Logic Designer app.

To build a fuzzy inference system using custom functions in the Fuzzy Logic Designer app:

  1. Open Fuzzy Logic Designer. At the MATLAB® command line, type:

    fuzzyLogicDesigner
  2. Specify the number of inputs and outputs of the fuzzy system, as described in The Fuzzy Logic Designer.

  3. Create custom membership functions, and replace the built-in membership functions with them, as described in Specify Custom Membership Functions.

    Membership functions define how each point in the input space is mapped to a membership value between 0 and 1.

  4. Create rules using the Rule Editor, as described in The Rule Editor.

    Rules define the logical relationship between the inputs and the outputs.

  5. Create custom inference functions, and replace the built-in inference functions with them, as described in Specify Custom Inference Functions.

    Inference methods include the AND, OR, implication, aggregation, and defuzzification methods. This action generates the output values for the fuzzy system.

    The next figure shows the tipping problem example where the built-in Implication, Aggregation and Defuzzification functions are replaced with the custom functions, customimp, customagg, and customdefuzz, respectively.

  6. Select View > Surface to view the output of the fuzzy inference system in the Surface Viewer, as described in The Surface Viewer.

Specify Custom Membership Functions

You can create custom membership functions and use them in the fuzzy inference process. The values of these functions must lie between 0 and 1. For more information on the properties of membership functions, see Membership Functions.

To create a custom membership function, and replace the built-in membership function:

  1. Create a MATLAB function, and save it in your current working folder.

    To learn how to create MATLAB functions, see Scripts vs. Functions (MATLAB).

    The following code is an example of a multistep custom membership function, custmf1, that depends on eight parameters between 0 and 10.

    % Function to generate a multi-step custom membership function
    % using 8 parameters for the input argument x
    function out = custmf1(x,params)
    
    for i = 1:length(x)
        if x(i) < params(1)
            y(i) = params(1);
        elseif x(i) < params(2)
            y(i) = params(2);
        elseif x(i) < params(3)
            y(i) = params(3);
        elseif x(i) < params(4)
            y(i) = params(4);
        elseif x(i) < params(5)
            y(i) = params(5);
        elseif x(i) < params(6)
            y(i) = params(6);
        elseif x(i) < params(7)
            y(i) = params(7);
        elseif x(i) < params(8)
            y(i) = params(8);
        else
            y(i) = 0;
        end
    end
    
    out = 0.1*y'; % Scale the output to lie between 0 and 1.
    
    end
  2. Open the Fuzzy Logic Designer app.

    fuzzyLogicDesigner

    The Fuzzy Logic Designer opens with the default FIS name, Untitled, and contains one input, input1, and one output, output1.

  3. In the Fuzzy Logic Designer, select Edit > Membership Functions to open the Membership Function Editor.

    Three triangular-shaped membership functions for input1 are displayed by default.

  4. To replace the default membership function with a custom function in the Membership Function Editor:

    1. Select Edit > Remove All MFs to remove the default membership functions for input1.

    2. Select Edit > Add Custom MF to open the Custom Membership Function dialog box.

  5. To specify a custom function, in the Custom Membership Function dialog box:

    1. In the MF name field, specify a name for the custom membership function.

      Note

      When adding additional custom membership functions, specify a different MF name for each function.

    2. In the M-file function name field, specify the name of the custom membership function file.

    3. In the Parameter list, specify a vector of parameters.

      These values determine the shape and position of the membership function, and the function is evaluated using these parameter values.

      Note

      The length of the parameter vector must be greater than or equal to the number of parameters in the custom membership function.

      Using the custmf1 example in step 1, the Custom Membership Function dialog box looks similar to the following figure.

    4. Click OK to add the custom membership function.

    5. Specify both the Range and Display Range to be [0 10] to match the range of the custom membership function.

    The Membership Function Editor displays the custom membership function plot.

    This action also adds the custom membership function to the Rule Viewer, and makes it available for creating rules for the fuzzy inference process. To view the custom function in the Rule Viewer, select Edit > Rules in either the Fuzzy Logic Designer or the Membership Function Editor.

  6. To add custom membership functions for output1, select it in the Membership Function Editor, and repeat steps 4 and 5.

You can also add a custom membership function to a FIS at the MATLAB command line. For example, to add custmf1 to the first input variable, input1 of the FIS, myFIS, and name it customMF1, type the following:

myFIS = addMF(myFIS,"input1","custmf1",[0 1 2 4 6 8 9 10],'Name',"customMF1");

Specify Custom Inference Functions

You can replace the built-in AND, OR, implication, aggregation, and defuzzification inference methods with custom functions. After you create the custom inference function, save it in your current working folder. To learn how to build fuzzy systems using custom inference functions, see the Build Fuzzy Inference Systems Using Custom Functions in Fuzzy Logic Designer section.

The guidelines for creating and specifying the functions for building fuzzy inference systems are described in the following sections.

Create Custom AND and OR Functions

The custom AND and OR inference functions must operate column-wise on a matrix, in the same way as the MATLAB functions max, min, or prod.

For a row or column vector x, min(x) returns the minimum element.

x = [1 2 3 4];
min(x)
ans =
     1

For a matrix x, min(x) returns a row vector containing the minimum element from each column.

x = [1 2 3 4;5 6 7 8;9 10 11 12];
min(x)
ans =
     1     2     3     4
For N-D arrays, min(x) operates along the first non-singleton dimension.

The function min(x,y) returns an array that is same size as x and y with the minimum elements from x or y. Either of the input arguments can be a scalar. Functions such as max, and prod operate in a similar manner.

In the toolbox, the AND implication methods perform an element by element matrix operation, similar to the MATLAB function min(x,y).

a = [1 2; 3 4];
b = [2 2; 2 2];
min(a,b)
ans = 
    1     2
    2     2

The OR implication methods perform an element by element matrix operation, similar to the MATLAB function max(x,y).

Create Custom Implication Functions

Custom implication functions must operate in the same way as the MATLAB functions max, min, or prod. Your custom implication function must be a T-norm fuzzy intersection operation. For more information, see Additional Fuzzy Operators.

An implication function must support either one or two inputs because the software calls the function in two ways:

  • To calculate the output fuzzy set values using the firing strength of all the rules and the corresponding output membership functions. In this case, the software calls the implication function using two inputs, similar to the following example:

    impvals = customimp(w,outputmf)
    • w — Firing strength of multiple rules, specified as an nr-by-ns matrix. Here, nr is the number of rules and ns is the number of samples of the output membership functions.

      w(:,j) = w(:,1) for all j. w(i,1) is the firing strength of the ith rule.

    • outputmf — Output membership function values, specified as an nr-by-ns matrix. Here, nr is the number of rules and ns is the number of samples of the output membership functions.

      outputmf(i,:) contains the data of the ith output membership function.

  • To calculate the output fuzzy value using the firing strength of a single rule and the corresponding output membership function, for a given sample. In this case, the software calls the implication function using one input, similar to the following example:

    impval = customimp([w outputmf])

    w and outputmf are scalar values representing the firing strength of a rule and the corresponding output membership function value, for a given sample.

The following is an example of a bounded product custom implication function with binary mapping T(a,b)=max{0,a+b1}. [1]

function y = customimp(x1,x2)

if nargin == 1
    % x1 assumed to be non-empty column vector or matrix.    
    minVal = zeros(1,size(x1,2));
    y = ones(1,size(x1,2));    
 
    for i = 1:size(x1,1)
        y = max(minVal,sum([y;x1(i,:)])-1);
    end
else    
    % x1 and x2 assumed to be non-empty matrices.                  
    minVal = zeros(1,size(x1,2));
    y = zeros(size(x1));
  
    for i = 1:size(x1,1)
        y(i,:) = max(minVal,sum([x1(i,:);x2(i,:)])-1);
    end    
end

end

Note

Custom implication functions are not supported for Sugeno-type systems.

Create Custom Aggregation Functions

The custom aggregation functions must operate in the same way as the MATLAB functions max, min, or prod and must be of the form y = customagg(x). Your custom implication function must be a T-conorm (S-norm) fuzzy intersection operation. For more information, see Additional Fuzzy Operators.

x is an nv-by-nr matrix, which is the list of truncated output functions returned by the implication method for each rule. nv is the number of output variables, and nr is the number of rules. The output of the aggregation method is one fuzzy set for each output variable.

The following is an example of a bounded sum custom aggregation function with binary mapping S(a,b)=min{a+b,1}. [1]

function y = customagg(x)

maxVal = ones(1,size(x,2));
y = zeros(1,size(x,2));

for i = 1:size(x,1)
    y = min(maxVal,sum([y;x(i,:)]));
end

end

Note

Custom aggregation functions are not supported for Sugeno-type systems.

Create Custom Defuzzification Functions

The custom defuzzification functions must be of the form y = customdefuzz(x,ymf), where x is the vector of values in the membership function input range, and ymf contains the values of the membership function for each x value.

The following is an example of a custom defuzzification function:

function defuzzfun = customdefuzz(x,ymf)

total_area = sum(ymf);
defuzzfun = sum(ymf.*x)/total_area;

end

Note

Custom defuzzification functions are not supported for Sugeno-type systems.

Steps for Specifying Custom Inference Functions

After you create and save a custom inference function, specify the function in the fuzzy inference system using the following steps:

  1. In the lower-left panel of the Fuzzy Logic Designer, select Custom from the drop-down menu corresponding to the inference method for which you want to specify the custom function.

    Doing so opens a dialog box where you specify the name of the custom inference function.

  2. In the Method name field, specify the name of the custom inference function, and click OK.

    The custom function replaces the built-in function when building the fuzzy inference system.

    Note

    In order to specify a custom inference function, you must first add at least one rule to your FIS.

  3. To specify custom functions for other inference methods, repeat steps 1 and 2.

You can also specify custom inference functions for a FIS (myFIS) at the MATLAB command line. For example, to add a custom:

  • Defuzzification method, type

    myFIS.DefuzzificationMethod = "customdefuzz";

    where customdefuzz is the name of the custom defuzzification function.

  • Implication method, type

    myFIS.ImplicationMethod = "customimp";

    where customimp is the name of the custom implication function.

  • Aggregation method, type

    myFIS.AggregationMethod = "customagg";

    where customagg is the name of the custom aggregation function.

Specify Custom Type-Reduction Functions

For type-2 fuzzy inference systems, you can specify a custom type-reduction function. This function must be of the form y = customtr(x,umf,lmf), where x is the vector of values in the membership function input range. umf and lmf are the respective values of the upper and lower membership function for each x value. The output y is a two-element row vector of centroids [cL,cR].

For more information on type reduction, see Type-2 Fuzzy Inference Systems.

By default, type-2 Sugeno systems support only a weighted average form of type reduction. The following custom type-reduction function implements a weighted sum form of type reduction for a Sugeno system.

function y = customtr(x,umf,ymf)

y = zeros(1,2);

y(1) = sum(x.*umf);
y(2) = sum(x.*lmf);

end

To specify the custom type-reduction function for a FIS (myFIS) at the MATLAB command line, type

myFIS.DTypeReductionMethod = "customtr";

where customtr is the name of the custom defuzzification function.

Use Custom Functions in Code Generation

You can use custom functions in fuzzy inference systems for which you generate code. For more information on code generation for fuzzy systems, see Deploy Fuzzy Inference Systems.

If you use a nondouble data type for your generated code, you must propagate the data type from the input arguments of your custom function to the output argument. For example, the following custom aggregation function maintains the data type of x in y using the ones and zeros with the 'like' argument.

function y = customagg(x)

maxVal = ones(1,size(x,2),'like',x);
y = zeros(1,size(x,2),'like',x);

for i = 1:size(x,1)
    y = min(maxVal,sum([y;x(i,:)]));
end

end

For more information on writing functions that support C/C++ code generation, see MATLAB Programming for Code Generation (MATLAB Coder).

References

[1] Mizumoto, M. "Pictorial Representations of Fuzzy Connectives, Part II: Cases of Compensatory Operators and Self-Dual Operators." Fuzzy Sets and Systems. Vol. 32, Number 1., 1989, pp. 45-79.

See Also

Related Topics