When you build a fuzzy inference system, as described in Fuzzy Inference Process, you can replace the built-in membership functions or inference functions, or both with custom functions. In this section, you learn how to build a fuzzy inference system using custom functions in the GUI. To learn how to build the system using custom functions at the command line, see Specifying Custom Membership and Inference Functions in Build Mamdani Systems (Code).

To build a fuzzy inference system using custom functions in the designer:

Open the Fuzzy Logic Designer by typing the following at the MATLAB

^{®}prompt:fuzzyLogicDesigner

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

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

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

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

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

Create custom inference functions, and replace the built-in inference functions with them, as described in Specifying 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.Select

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

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. Save the custom membership functions in your current working folder. To learn how to build fuzzy systems using custom membership functions, see How to Build Fuzzy Inference Systems Using Custom Functions in the Designer.

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

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

To learn how to create MATLAB functions, see Scripts vs. Functions in the MATLAB documentation.

The following code is an example of a multi-step 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'; % scaling the output to lie between 0 and 1

**Note:**Custom membership functions can include a maximum of 16 parameters for the input argument.Open the Fuzzy Logic Designer by typing the following at the MATLAB prompt if you have not done so already:

fuzzyLogicDesigner

The Fuzzy Logic Designer opens with the default FIS name,

`Untitled`

, and contains one input,**input1**, and one output,**output1**.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.To replace the default membership function with a custom function in the Membership Function Editor:

Select

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

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

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

Specify a name for the custom membership function in the

**MF name**field.**Note:**When adding additional custom membership functions, specify a different**MF name**for each function.Specify the name of the custom membership function file in the

**M-file function name**field.Specify a vector of parameters in the

**Parameter list**field.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.Click

**OK**to add the custom membership function.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.To add custom membership functions for

**output1**, select it in the Membership Function Editor, and repeat steps 4 and 5.

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 How to Build Fuzzy Inference Systems Using Custom Functions in the Designer section.

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

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 matrix `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

`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)`

.

Custom implication functions must operate in the same way as
the MATLAB functions `max`

, `min`

,
or `prod`

.

The following is an example of a custom implication function:

function y = customimp(x1,x2) if nargin==1 y = prod(x1).^2; else y = (x1.*x2).^2; end

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*i*^{th}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*i*^{th}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 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)`

.

*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 custom aggregation function:

```
function aggfun = customagg(x)
aggfun = (sum(x)/2).^0.5;
```

The custom defuzzification functions must be of the form ```
y
= customdefuzz(xmf,ymf)
```

, where *(xmf,ymf)* is
a finite set of membership function values. *xmf* is
the vector of values in the membership function input range. *ymf* is
the value of the membership function at *xmf*.

The following is an example of a custom defuzzification function:

```
function defuzzfun = customdefuzz(xmf,ymf)
total_area = sum(ymf);
defuzzfun = sum(ymf.*xmf)/total_area;
```

After you create and save a custom inference function, use the following steps to specify the function in the fuzzy inference process:

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.This action opens a dialog box where you specify the name of the custom inference function.

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.To specify custom functions for other inference methods, repeat steps 1 and 2.

Was this topic helpful?