# Fuzzy PID Controller in MATLAB and Simulink

URL: https://yarpiz.com/fuzzy-pid-ypfz103
Author: Mostapha Kalami Heris
Published: 2015-09-11
Updated: 2026-10-30
Topics: Fuzzy Systems, Control Systems

> Simulate fuzzy PID gain scheduling in MATLAB and Simulink with a two-input rule base, variable gains, and three plant examples.

A PID controller has three gains: the proportional gain $K_p$, the integral gain $K_i$ and the derivative gain $K_d$. Classic tuning rules, such as Ziegler-Nichols, choose fixed values once. One way to tune a PID controller with fuzzy logic is **fuzzy gain scheduling**, where a set of fuzzy rules changes the gains online, at every step, based on the current error and its rate of change. This approach was proposed by Zhen-Yu Zhao, Masayoshi Tomizuka and Satoru Isaka in 1993, in the paper "Fuzzy Gain Scheduling of PID Controllers" ([IEEE Transactions on Systems, Man, and Cybernetics, 23(5), 1392 to 1398](https://doi.org/10.1109/21.260670)).

This post shares a MATLAB and Simulink implementation of this Fuzzy PID Controller. It uses the Fuzzy Logic Toolbox blocks in Simulink. The three examples from the reference paper are implemented as Simulink models. Models for MATLAB 7.9 are provided for backward compatibility, in addition to the models for MATLAB R2015a (version 8.5).

## How fuzzy gain scheduling works

The method starts from two numbers that describe the plant: the ultimate gain $K_u$ and the ultimate period $T_u$. These are the gain at which a proportional controller makes the closed loop oscillate, and the period of that oscillation. From them, the method sets a range for the proportional and derivative gains:

$$
K_{p,\min} = 0.32\,K_u, \quad K_{p,\max} = 0.6\,K_u, \qquad K_{d,\min} = 0.08\,K_u T_u, \quad K_{d,\max} = 0.15\,K_u T_u
$$

A fuzzy system then looks at the error $e(k)$ and its first difference $\Delta e(k) = e(k) - e(k-1)$. It returns three values:

- $K_p'$, a normalized proportional gain between 0 and 1,
- $K_d'$, a normalized derivative gain between 0 and 1,
- $\alpha$, the ratio between the integral and derivative time constants.

The actual gains follow from these outputs:

$$
K_p = K_{p,\min} + (K_{p,\max} - K_{p,\min})\,K_p', \qquad K_d = K_{d,\min} + (K_{d,\max} - K_{d,\min})\,K_d'
$$

$$
K_i = \frac{K_p^2}{\alpha\,K_d}
$$

The rules follow the intuition of an operator. When the error is large, the controller uses a large proportional gain and a small derivative gain to move the output quickly. When the error is small but changing quickly, it lowers the proportional gain and raises the derivative gain to limit the overshoot.

## The fuzzy system in the code

The file `fuzzy_pid.fis` holds the fuzzy system. It is a Mamdani system with two inputs and three outputs:

- Inputs: `e` and `edot`, each on the range $[-1, 1]$, with seven membership functions (NB, NM, NS, ZO, PS, PM, PB).
- Outputs: `kpp` and `kdp` ($K_p'$ and $K_d'$) on $[0, 1]$, each with the terms Small and Big, and `alpha` with the four values 2, 3, 4 and 5.
- 49 rules, one for each pair of input terms, with min for AND, max aggregation and centroid defuzzification.

In each Simulink model, the Fuzzy PID subsystem computes $\Delta e$ with a Unit Delay block, feeds $e$ and $\Delta e$ to the Fuzzy Logic Controller block, and turns its outputs into $K_p$, $K_i$ and $K_d$ with the formulas above. A second loop with a classic Ziegler-Nichols PID controller runs next to it for comparison, with $K_p = 0.6\,K_u$, $K_i = 1.2\,K_u/T_u$ and $K_d = 0.6\,K_u T_u/8$.

## The three examples

Each model applies a unit step to both loops and simulates 20 seconds. The plants and the values of $K_u$ and $T_u$ set in the model are:

| Model | Plant | $K_u$ | $T_u$ |
| --- | --- | ---: | ---: |
| `example1` | $G_1(s) = \dfrac{e^{-0.5s}}{(s+1)^2}$ | 4.6737 | 3.26 |
| `example2` | $G_2(s) = \dfrac{4.228}{(s+0.5)(s^2+1.64s+8.456)}$ | 3.665 | 2.06 |
| `example3` | $G_3(s) = \dfrac{27}{(s+1)(s+3)^3}$ | 5.1278 | 2.72 |

The first plant has a time delay, the second has a pair of lightly damped complex poles, and the third is a fourth order system.

## What is in the download and how to run it

The download has two folders with the same content:

- `01 For MATLAB R2015a`: the models `example1.slx`, `example2.slx` and `example3.slx`.
- `02 Previous Versions`: the same models as `.mdl` files, for older releases.

Each folder also has `fuzzy_pid.fis` and the first page of the reference paper as a PDF.

To run an example, set the folder as the current folder in MATLAB and open one of the models. When the simulation starts, the model's initialization code sets $K_u$, $T_u$ and the gain ranges, and reads `fuzzy_pid.fis` with `readfis`. Run the simulation and open the scopes to compare the fuzzy and the classic responses. The models use the Fuzzy Logic Controller block, so they probably need the Fuzzy Logic Toolbox.

To try your own plant, replace the plant block in both loops and change the values of `Ku` and `Tu` in the model's initialization code.

## Check the result

Compare responses for the same plant, reference, and simulation settings. Inspect overshoot, settling behavior, actuator effort, and disturbance response as appropriate to the task rather than declaring one controller universally better from a step plot. The model uses the error difference, so its interpretation depends on the sample time and scaling. Check the fuzzy input ranges and valid gain calculations, including nonzero derivative gain in the formula for $K_i$. The supplied models were not tested on hardware or a current MATLAB release.

## Applications

Fuzzy gain scheduling is used where one set of fixed PID gains does not work well over the whole operating range, for example in process control (temperature, level and flow loops), motor and servo control, and plants with time delay or changing dynamics.

## Related posts

- [Nonlinear Regression using ANFIS](https://yarpiz.com/nonlinear-regression-using-anfis-ypfz101)
- [Evolutionary ANFIS Training in MATLAB](https://yarpiz.com/evolutionary-anfis-training-ypfz104)

## How to cite

Mostapha Kalami Heris, Fuzzy PID Controller in MATLAB and Simulink (YPFZ103), Yarpiz, 2015. https://yarpiz.com/fuzzy-pid-ypfz103
