Fuzzy SystemsControl Systems

Fuzzy PID Controller in MATLAB and Simulink

Get the code
Comments

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

Illustration for Fuzzy PID Controller in MATLAB and Simulink
On this page

A PID controller has three gains: the proportional gain KpK_p, the integral gain KiK_i and the derivative gain KdK_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 (opens in a new tab)).

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 KuK_u and the ultimate period TuT_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:

Kp,min⁡=0.32 Ku,Kp,max⁡=0.6 Ku,Kd,min⁡=0.08 KuTu,Kd,max⁡=0.15 KuTuK_{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)e(k) and its first difference Δe(k)=e(k)−e(k−1)\Delta e(k) = e(k) - e(k-1). It returns three values:

  • Kp′K_p', a normalized proportional gain between 0 and 1,
  • Kd′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:

Kp=Kp,min⁡+(Kp,max⁡−Kp,min⁡) Kp′,Kd=Kd,min⁡+(Kd,max⁡−Kd,min⁡) Kd′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'
Ki=Kp2α KdK_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][-1, 1], with seven membership functions (NB, NM, NS, ZO, PS, PM, PB).
  • Outputs: kpp and kdp (Kp′K_p' and Kd′K_d') on [0,1][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 Δe\Delta e with a Unit Delay block, feeds ee and Δe\Delta e to the Fuzzy Logic Controller block, and turns its outputs into KpK_p, KiK_i and KdK_d with the formulas above. A second loop with a classic Ziegler-Nichols PID controller runs next to it for comparison, with Kp=0.6 KuK_p = 0.6\,K_u, Ki=1.2 Ku/TuK_i = 1.2\,K_u/T_u and Kd=0.6 KuTu/8K_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 KuK_u and TuT_u set in the model are:

Model Plant KuK_u TuT_u
example1 G1(s)=e−0.5s(s+1)2G_1(s) = \dfrac{e^{-0.5s}}{(s+1)^2} 4.6737 3.26
example2 G2(s)=4.228(s+0.5)(s2+1.64s+8.456)G_2(s) = \dfrac{4.228}{(s+0.5)(s^2+1.64s+8.456)} 3.665 2.06
example3 G3(s)=27(s+1)(s+3)3G_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 KuK_u, TuT_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 KiK_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.

Downloads

How to cite

If you use this code or tutorial in your work, articles or publications, please cite it.

Plain text
Mostapha Kalami Heris, Fuzzy PID Controller in MATLAB and Simulink (YPFZ103), Yarpiz, 2015. https://yarpiz.com/fuzzy-pid-ypfz103
BibTeX
@misc{yarpiz-ypfz103,
  author = {{Mostapha Kalami Heris}},
  title = {{Fuzzy PID Controller in MATLAB and Simulink}},
  howpublished = {Yarpiz},
  year = {2015},
  note = {Code YPFZ103},
  url = {https://yarpiz.com/fuzzy-pid-ypfz103}
}

Comments

Sign in or create a free account to join the discussion. No password needed.