PID tuning using a fuzzy logic controller

49 views (last 30 days)
Gabriel
Gabriel on 28 Mar 2025
Answered: Sam Chak on 29 Mar 2025
Hi, I'm trying to use a fuzzy controller to tune a PID. my PID system was working, but I needed further optimisation of the parameters. This is my diagram so far, and it's similar to ones I've seen online, but the values I'm getting are useless. my error term just continues to get bigger, and I'm unsure why, any help would be much appreciated.

Answers (2)

Sam Chak
Sam Chak on 28 Mar 2025
You understand that certain combinations of PID gains stabilize the non-minimum phase plant, while other combinations destabilize it. Based on the limited graphical info you presented, the only logical explanation for why the PID controller functions effectively while the fuzzy logic controller does not is that the PID gains were found by the autotuner, whereas the fuzzy system was manually designed without a solid basis. This discrepancy may lead the fuzzy system to produce outputs with specific combinations of PID gains that destabilize the plant, even though the fuzzy system may have initially generated some stabilizing PID gains during the first 50 seconds.
In fact, your control method is known as Fuzzy Gain Scheduling because the fuzzy system only manipulates the PID gains using the error (e) and the rate of change of the error (ec) as the scheduling variables. It does not alter the structure of the linear PID controller, as shown below:
You defined 7 membership functions (MFs) {NB, NM, NS, Z, PS, PM, PB} for each fuzzy input. Fuzzy logic theory suggests that ideally, there should be 13 MFs for each fuzzy output {Kp, Ki, Kd}, but you have only used 7 (slightly more than 50%). In other words, you are compelled to recycle most of the MFs (as shown in the Fuzzy Associative Matrix), which makes certain fuzzy rules significantly less flexible in describing the required nonlinearity in the control action. With the Fuzzy Gain Scheduling approach, you need to design 147 fuzzy rules (49 for Kp​, 49 for Ki​, and 49 for Kd). This is a demanding and time-consuming task, as it is essential to test and ensure that each rule produces a stabilizing effect. If stability proof is required for all 147 rules, then I can only wish you 'Good luck!'
While I cannot teach you how to design the fuzzy controller, I can offer a "trick" to stabilize the plant or at least achieve performance comparable to that of the linear PID controller. Since you mentioned that your PID controller is working, the values for Kp​, Ki, and Kd​ are the nominal gain values. Without adjusting the MFs and rules, the "trick" is to set the fuzzy output ranges of the PID gains around these stabilizing nominal values. For example, if the nominal value of Kp​ is 5, set the deviations to ±1 from this nominal value. In other words, the fuzzy Kp​ range would be from 4 to 6, or (4.0, 6.0) in math notation of interval. If the response is still unsatisfactory, narrow the range to (4.5, 5.5) or until performance is on par with that of the PID controller.
  1 Comment
Gabriel
Gabriel on 28 Mar 2025
I found the gains for my PID using the ziegler-nichlos method, through this i determied {Kp= 249.2, Ki = 45.45371637 and Kd = 341.55975}. That seems like a lot of rules and i dont have time to build that for my proejct. Do you think the built in Fuzzy-PID block would be less demanding in terms of building the rule sets as it only has 2 inputs and 1 output from the FIS. I thought using fuzzy logic would help my system adapt in time to imprve the control of thr system sompared to other methods such as GA or PSO. If you can offer any further guidance that would be very helpful.
Thanks

Sign in to comment.


Sam Chak
Sam Chak on 29 Mar 2025
I can provide you with an example involving a 1st-order plant, where a simple fuzzy P+I controller is designed. In this case, only the proportional gain is described by the fuzzy system, making it a SISO (Single Input, Single Output) system. The integral control action remains linear. I used the Sugeno fuzzy system for ease of design, defining 3 input MFs and 2 output singletons. The design steps require knowledge of the autotuned PI controller and the step response of the closed-loop system.
As a beginner, I recommend that you follow my approach in designing a fuzzy controller that outperforms the linear PID controller. For beginners, the fewer the number of MFs and rules, the easier the fuzzy control design becomes.
%% Step 1: obtain the Plant, Gp
Gp = tf(1, [1 1])
Gp = 1 ----- s + 1 Continuous-time transfer function.
%% Step 2: let the Autotuner find the PI gains
Gc = pidtune(Gp, 'PI')
Gc = 1 Kp + Ki * --- s with Kp = 1.04, Ki = 3.13 Continuous-time PI controller in parallel form.
Kp = Gc.Kp; % proportional gain
Ki = Gc.Ki; % integral gain
%% Step 3: find the max amplitude of step response of closed-loop system
Gcl = minreal(feedback(Gc*Gp, 1));
tspan = linspace(0, 10, 101);
[y1, t1] = step(Gp, tspan); % generate step response of Gp
[y2, t2] = step(Gcl, tspan); % generate step response of Gcl
figure
step(Gcl) % to check the maximum amplitude of the response under linear PI controller
%% Step 4: design Fuzzy P Controller
fis = sugfis;
% step 4a: describe the Fuzzy Input
fis = addInput(fis, [-1.5 +1.5], 'Name', 'Err'); % step response is under 1.2, so ±1.5 is more than enough
fis = addMF(fis, 'Err', 'linzmf', [-0.75, 0.00], 'Name', 'N'); % symmetrical design (designer's choice)
fis = addMF(fis, 'Err', 'trimf', [-0.75 0 0.75], 'Name', 'Z'); % symmetrical design (designer's choice)
fis = addMF(fis, 'Err', 'linsmf', [ 0.00, 0.75], 'Name', 'P'); % symmetrical design (designer's choice)
% step 4b: describe the Fuzzy Output
lv = Kp-0.2; % low value of Kp (designer's choice)
hv = Kp+2.2; % high value of Kp (designer's choice)
fis = addOutput(fis, [1 3], 'Name', 'U'); % setting the range in SugFIS has no effect!
fis = addMF(fis, 'U', 'constant', lv, 'Name', 'L'); % 'constant' is singleton MF
fis = addMF(fis, 'U', 'constant', hv, 'Name', 'H');
% step 4c: describe the Fuzzy Rules
rules = [
"Err==N => U=L" % follow this rule (NOT designer's choice!)
"Err==Z => U=H" % follow this rule (NOT designer's choice!)
"Err==P => U=L" % follow this rule (NOT designer's choice!)
];
fis = addRule(fis, rules);
figure
subplot(211)
plotmf(fis, 'input', 1), grid on,
xlabel('Error')
title('Membership functions of Error input')
subplot(212)
opt = gensurfOptions('NumGridPoints', 301); % generate 301 points in the interval [-1.5, +1.5]
gensurf(fis, opt)
xlabel('Error'), ylabel('Kp')
title ('Fuzzy control gain, Kp')
%% Step 5a: write the code for Control system (Plant and Controller)
function dx = plant(t, x, fis, Ki)
% definitions
r = 1; % reference
e = r - x(1); % error
% fuzzy controller
fKp = evalfis(fis, e); % fuzzy Kp only
u = fKp*e + Ki*x(2); % PI controller
% differential equations
dx(1) = - 1*x(1) + 1*u; % plant state space = tf(1, [1 1])
dx(2) = e; % x2 is the integral of error
dx = [dx(1)
dx(2)];
end
%% Step 5b: call ode45 to solve differential equations in the Control System
x0 = [0; 0]; % initial condition
[t, x] = ode45(@(t, x) plant(t, x, fis, Ki), tspan, x0);
figure
hold on
plot(t, x(:,1)), grid on % plot step response of plant under fuzzy P+I controller
plot(t2, y2), % plot step response of plant under PI controller
plot(t1, y1), % plot step response of original plant
hold off
title('Step responses'), xlabel('Time / s'), ylabel('Amplitude')
legend('Fuzzy P+I Ctrl', 'PI Controller', 'Original Plant', 'location', 'east')

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!