# 2. FUZZY SETS. FUZZY SETS OPERATIONS

## Contents

*Each work team will create a directory named representatively, in which to save all the results from the exercises (Matlab
scripts and a Word file with the numerical and graphical results).*

## Objectives

- Visual illustration of the fuzzy set concept
- To familiarize with the type of fuzzy sets implemented in the Fuzzy Logic Toolbox
- To understand some fuzzy sets operation (complement, union, intersection)
- To get accustomed to the fuzzy set editor "mfedit"

## Membership function gallery

To visualize the membership function gallery in Matlab, enter the following statements in the Command Window:

close all; % closes all the open figure windows mfdemo;

## Using fuzzy sets in Matlab

To find details about each of the 11 membership functions type:

"help [membership_function_type]"

For example, for "trapmf":

```
help trapmf
```

TRAPMF Trapezoidal membership function. TRAPMF(X, PARAMS) returns a matrix which is the trapezoidal membership function evaluated at X. PARAMS = [A B C D] is a 4-element vector that determines the break points of this membership function. We require that A <= B and C <= D. If B >= C, this membership function becomes a triangular membership function that could have a height less than unity. (See the example below.) For example: x = (0:0.1:10)'; y1 = trapmf(x, [2 3 7 9]); y2 = trapmf(x, [3 4 6 8]); y3 = trapmf(x, [4 5 5 7]); y4 = trapmf(x, [5 6 4 6]); plot(x, [y1 y2 y3 y4]); set(gcf, 'name', 'trapmf', 'numbertitle', 'off'); See also DSIGMF, EVALMF, GAUSS2MF, GAUSSMF, GBELLMF, MF2MF, PIMF, PSIGMF, SIGMF, SMF, TRIMF, ZMF. Reference page in Help browser doc trapmf

**Run the following sequence:**

close all % closes all the open figure windows clear all % removes all variables in the workspace clc % clears the command window x = (0:0.1:10)'; % The universe of discourse is [0,10]; the points are defined with a step of 0.1 params=[2 3 7 9];% parameters for a trapezoidal membership function y = trapmf(x,params); % compute the membership function values plot(x, y,'linewidth',2); axis([0 10 -0.1 1.1]); xlabel('universe of discourse'); % horizontal axis variable name ylabel ('membership degree'); % vertical axis variable name set(gcf, 'name', 'Trapezoidal membership function', 'numbertitle', 'off'); % figure name

**Determining the membership degree**

Run the next sequence to find the membership degree of the point x1=2.75, to the above defined trapezoidal fuzzy set.

hold on x1=2.75; u1=evalmf(x1,params,'trapmf'); sprintf( 'x1=%1.2f has the memebership degree u1=%1.2f',x1,u1); plot (x1,u1,'r*') % place the point on the graph plot ([x1,x1],[0,u1],... 'linestyle','-','color','r') plot ([0,x1],[u1,u1],... 'linestyle','-','color','r') hold off

**Exercises**

- 1. Plot another two types of membership function at your choice, assuming [-5, 8] as the universe of discourse.
- 2. Write a Matlab script to solve the next exercise:

Consider "Speed" as a linguistic variable whose universe of discourse is [0,140]km/h. Define three linguistic values and plot the corresponding fuzzy sets on the same axis (use "hold on"); use a different color for each fuzzy set. What are the membership degrees to each fuzzy set, for the following values of the speed: 10km/h, 52km/h, 85 km/h and 100km/h?

## Fuzzy sets operations

The union of two fuzzy sets A and B is defined based on the membership functions, using the relation:

**Graphical representation of the union of two fuzzy sets**

Run the next sequence to graphically represent the result of the union of two fuzzy sets:

close all x = (0:0.1:10)'; % % The universe of discourse is [0,10]; the points are defined with a step of 0.1 u1=gaussmf(x,[1,4]); % first membership function, gaussian type u2=trimf(x,[3 6.5 9]); % second membership function, triangular type u_reuniune=max(u1,u2); % compute the membership degrees for the union using the "MAX" operator hold on subplot(2,1,1); % breaks the figure in two windows % the current plot appears in the upper window plot (x,u1,'r');hold on plot (x,u2,'m'); hold off axis([0 10 0 1.05]); legend ('A','B'); title('Fuzzy sets A and B') subplot (2,1,2) % the current plot appears in the lower window plot(x, u_reuniune,'color','b','linewidth',2) axis([0 10 0 1.05]); title('Union') set(gcf, 'name', 'Union of A and B fuzzy sets - "max" operator', 'numbertitle', 'off'); %figure name

**Exercises**

- 1. Write a Matlab script to plot the result of the intersection of fuzzy sets A and B, using the "min" operator. Using the "Data Cursor" in the figure toolbar, mark on the graph the membership degrees for the next input values: x=4,5,6,7.

- 2. Write a Matlab script to plot the result of the fuzzy complement of a fuzzy set A using the relation:

## Membership function editor "mfedit"

Launch the membership function editor by entering "mfedit" at the prompt in the Command Window.

```
close all
mfedit
```

**Exercise**

- Consider "Speed" as a linguistic variable whose universe of discourse is [0,140]km/h. Define five linguistic values and create the corresponding fuzzy sets. Use the 'zmf', 'gbellmf' and 'smf' membership function types.