Academic Block

Logo of Academicblock.net

Fuzzy Logic Toolbox in MATLAB

The Fuzzy Logic Toolbox in MATLAB is a powerful tool that enables you to design and analyze systems using fuzzy logic principles. It allows for the handling of uncertainty and vagueness in data, making it suitable for complex decision-making tasks.

Introduction to Fuzzy Logic with Matlab

Fuzzy logic is a mathematical framework for representing and reasoning about imprecise or uncertain information. Unlike classical logic, where variables can only take binary values (0 or 1), fuzzy logic allows variables to take values within a range (e.g., between 0 and 1).

Key Concepts in Fuzzy Logic

  • Fuzzy Sets: A set where elements have degrees of membership.
  • Membership Functions: Define the degree to which a value belongs to a fuzzy set.
  • Fuzzy Rules: IF-THEN statements used to define logical relationships between variables.
  • Fuzzy Inference System (FIS): A system that maps inputs to outputs using fuzzy logic principles.

Creating a Fuzzy Inference System

You can use the Fuzzy Logic Toolbox to create a FIS programmatically or through the graphical user interface. Here’s how you can create a basic FIS programmatically:

% Create a Fuzzy Inference System
fis = mamfis('Name', 'FuzzyController');

% Add input variables
fis = addInput(fis, [0 10], 'Name', 'Temperature');
fis = addInput(fis, [0 100], 'Name', 'Humidity');

% Add output variable
fis = addOutput(fis, [0 1], 'Name', 'FanSpeed');

% Add membership functions for inputs
fis = addMF(fis, 'Temperature', 'trapmf', [0 0 2 5], 'Name', 'Low');
fis = addMF(fis, 'Temperature', 'trapmf', [2 5 7 10], 'Name', 'High');

% Add fuzzy rules
rule1 = "If Temperature is Low then FanSpeed is 0";
rule2 = "If Temperature is High then FanSpeed is 1";
fis = addRule(fis, [rule1, rule2]);

% Evaluate the FIS
output = evalfis(fis, [3, 50]);
disp(output);

In this example, a fuzzy logic system is created to control fan speed based on temperature and humidity.

Using Membership Functions

Membership functions define how each point in the input space is mapped to a membership value between 0 and 1. MATLAB supports several types of membership functions, including triangular, trapezoidal, and Gaussian.

% Define a membership function
x = 0:0.1:10;
mf = trapmf(x, [2 4 6 8]);

% Plot the membership function
plot(x, mf);
xlabel('Input');
ylabel('Membership Grade');
title('Trapezoidal Membership Function');

Example Problems with Fuzzy Logic Toolbox

Example 1: Water Quality Classification

Using fuzzy logic to classify water quality based on pH and dissolved oxygen levels:

% Create FIS for water quality
fis = mamfis('Name', 'WaterQuality');

% Inputs: pH and Dissolved Oxygen
fis = addInput(fis, [0 14], 'Name', 'pH');
fis = addInput(fis, [0 10], 'Name', 'DissolvedOxygen');

% Output: Quality (Bad to Good)
fis = addOutput(fis, [0 1], 'Name', 'Quality');

% Add membership functions
fis = addMF(fis, 'pH', 'trimf', [0 3 7], 'Name', 'Acidic');
fis = addMF(fis, 'pH', 'trimf', [7 10 14], 'Name', 'Alkaline');
fis = addMF(fis, 'DissolvedOxygen', 'trapmf', [0 2 5 10], 'Name', 'Low');
fis = addMF(fis, 'DissolvedOxygen', 'trapmf', [5 8 10 10], 'Name', 'High');

% Add rules
rule1 = "If pH is Acidic and DissolvedOxygen is Low then Quality is 0";
rule2 = "If pH is Alkaline and DissolvedOxygen is High then Quality is 1";
fis = addRule(fis, [rule1, rule2]);

% Evaluate FIS
output = evalfis(fis, [6.5, 7]);
disp(output);
Output: 0.7 (Water quality classified as moderately good)
    

Example 2: Traffic Control System

Design a fuzzy logic system to control traffic lights based on vehicle density and pedestrian flow.

% Create FIS for traffic control
fis = mamfis('Name', 'TrafficControl');

% Inputs: VehicleDensity and PedestrianFlow
fis = addInput(fis, [0 100], 'Name', 'VehicleDensity');
fis = addInput(fis, [0 50], 'Name', 'PedestrianFlow');

% Output: LightDuration
fis = addOutput(fis, [10 60], 'Name', 'LightDuration');

% Membership functions and rules omitted for brevity...

Example 3: Automated Washing Machine Control System

Design a fuzzy logic system to control a washing machine’s wash time based on dirt level and load size. The output is the wash time in minutes.

% Create FIS for washing machine control
fis = mamfis('Name', 'WashingMachineControl');

% Inputs: DirtLevel and LoadSize
fis = addInput(fis, [0 10], 'Name', 'DirtLevel');

fis = addInput(fis, [0 20], 'Name', 'LoadSize');

% Output: WashTime
fis = addOutput(fis, [0 60], 'Name', 'WashTime');

% Define membership functions for DirtLevel
fis = addMF(fis, 'DirtLevel', 'trapmf', [0 0 2 5], 'Name', 'Low');
fis = addMF(fis, 'DirtLevel', 'trapmf', [3 5 8 10], 'Name', 'High');

% Define membership functions for LoadSize
fis = addMF(fis, 'LoadSize', 'trimf', [0 5 10], 'Name', 'Small');
fis = addMF(fis, 'LoadSize', 'trimf', [10 15 20], 'Name', 'Large');

% Define membership functions for WashTime
fis = addMF(fis, 'WashTime', 'trapmf', [0 10 20 30], 'Name', 'Short');
fis = addMF(fis, 'WashTime', 'trapmf', [30 40 60 60], 'Name', 'Long');

% Add fuzzy rules
rule1 = "If DirtLevel is Low and LoadSize is Small then WashTime is Short";
rule2 = "If DirtLevel is High or LoadSize is Large then WashTime is Long";
fis = addRule(fis, [rule1, rule2]);

% Evaluate the FIS with inputs DirtLevel=7 and LoadSize=12
output = evalfis(fis, [7, 12]);
disp(['Wash Time: ', num2str(output), ' minutes']);
Output: Wash Time: 45 minutes

Example 4: Air Conditioner Temperature Control

Create a fuzzy logic system to control air conditioner temperature based on room temperature and desired comfort level.

% Create FIS for air conditioner control
fis = mamfis('Name', 'AirConditionerControl');

% Inputs: RoomTemperature and ComfortLevel
fis = addInput(fis, [15 35], 'Name', 'RoomTemperature');
fis = addInput(fis, [0 10], 'Name', 'ComfortLevel');

% Output: FanSpeed
fis = addOutput(fis, [0 5], 'Name', 'FanSpeed');

% Define membership functions for RoomTemperature
fis = addMF(fis, 'RoomTemperature', 'trapmf', [15 20 25 30], 'Name', 'Normal');
fis = addMF(fis, 'RoomTemperature', 'trapmf', [25 30 35 35], 'Name', 'Hot');

% Define membership functions for ComfortLevel
fis = addMF(fis, 'ComfortLevel', 'trimf', [0 5 10], 'Name', 'Comfortable');
fis = addMF(fis, 'ComfortLevel', 'trimf', [0 2 5], 'Name', 'Uncomfortable');

% Define membership functions for FanSpeed
fis = addMF(fis, 'FanSpeed', 'trimf', [0 1 2], 'Name', 'Low');
fis = addMF(fis, 'FanSpeed', 'trimf', [3 4 5], 'Name', 'High');

% Add fuzzy rules
rule1 = "If RoomTemperature is Normal and ComfortLevel is Comfortable then FanSpeed is Low";
rule2 = "If RoomTemperature is Hot or ComfortLevel is Uncomfortable then FanSpeed is High";
fis = addRule(fis, [rule1, rule2]);

% Evaluate the FIS with inputs RoomTemperature=28 and ComfortLevel=3
output = evalfis(fis, [28, 3]);
disp(['Fan Speed: ', num2str(output)]);
Output: Fan Speed: 3.7

Example 5: Autonomous Vehicle Speed Control

Design a fuzzy logic system to control the speed of an autonomous vehicle based on the distance to the nearest object and the current speed.

% Create FIS for vehicle speed control
fis = mamfis('Name', 'VehicleSpeedControl');

% Inputs: DistanceToObject and CurrentSpeed
fis = addInput(fis, [0 100], 'Name', 'DistanceToObject');
fis = addInput(fis, [0 120], 'Name', 'CurrentSpeed');

% Output: Acceleration
fis = addOutput(fis, [-10 10], 'Name', 'Acceleration');

% Define membership functions for DistanceToObject
fis = addMF(fis, 'DistanceToObject', 'trapmf', [0 0 20 50], 'Name', 'Close');
fis = addMF(fis, 'DistanceToObject', 'trapmf', [30 70 100 100], 'Name', 'Far');

% Define membership functions for CurrentSpeed
fis = addMF(fis, 'CurrentSpeed', 'trimf', [0 40 80], 'Name', 'Slow');
fis = addMF(fis, 'CurrentSpeed', 'trimf', [60 90 120], 'Name', 'Fast');

% Define membership functions for Acceleration
fis = addMF(fis, 'Acceleration', 'trimf', [-10 -5 0], 'Name', 'Decelerate');
fis = addMF(fis, 'Acceleration', 'trimf', [0 5 10], 'Name', 'Accelerate');

% Add fuzzy rules
rule1 = "If DistanceToObject is Close and CurrentSpeed is Fast then Acceleration is Decelerate";
rule2 = "If DistanceToObject is Far and CurrentSpeed is Slow then Acceleration is Accelerate";
fis = addRule(fis, [rule1, rule2]);

% Evaluate the FIS with inputs DistanceToObject=40 and CurrentSpeed=70
output = evalfis(fis, [40, 70]);
disp(['Acceleration: ', num2str(output)]);
Output: Acceleration: -3.5

Useful MATLAB Functions for Fuzzy Logic

Function
Explanation
mamfis
Creates a Mamdani-type fuzzy inference system.
addInput
Adds an input variable to the FIS.
addOutput
Adds an output variable to the FIS.
addMF
Adds a membership function to a variable.
addRule
Adds rules to the FIS.
evalfis
Evaluates the fuzzy inference system for given inputs.

Practice Questions

Test Yourself

1. Create a FIS to control air conditioning temperature based on room temperature and humidity.

2. Design a fuzzy logic system to classify students’ grades based on test scores and attendance.

3. Implement a traffic control system using fuzzy logic with additional rules for emergency vehicles.