Fuzzy implementation on 2nd order transfer function

2 次查看(过去 30 天)
I want to implement fuzzy logic on 2nd order transfer function. The input signal is a step signal. I am confusing how will i define membership function for input and output. The input is the error signal and the output is the response. Please help me.

回答(1 个)

Sam Chak
Sam Chak 2025-4-1
The Fuzzy Logic Toolbox still lacks examples demonstrating how to implement fuzzy PID controllers in MATLAB using ODE solvers. Most examples focus on the implementation of fuzzy controllers in Simulink.
Here is a simple example demonstrating how to convert a 2nd-order plant transfer function to a state-space model (matrix differential equation form) and design a Sugeno-type fuzzy controller. In my opinion, beginners should start with Sugeno fuzzy control design, as only true fuzzy experts can fully leverage the capabilities of Mamdani fuzzy controllers.
%% Step 1: Convert plant transfer function to state space
Gp = tf(1, [1 2 0])
Gp = 1 --------- s^2 + 2 s Continuous-time transfer function.
sys = compreal(Gp);
sys = ss(sys.A', sys.C', sys.B', sys.D'); % standard controllable canonical form
%% Step 1.5 (optional): Design a linear Proportional Controller
Gc = pid(1)
Gc = Kp = 1 P-only controller.
Gcl = feedback(Gc*Gp, 1); % closed-loop system
[y, ty] = step(Gcl, 10); % generate step response data
%% Step 2: Design a Fuzzy Controller
fis = sugfis('Name', "Sugeno_FLC");
% Section 1: Fuzzy Input
fis = addInput(fis, [-1.5 +1.5], 'Name', 'Err');
fis = addMF(fis, 'Err', 'linzmf', [-0.5 +0.5], 'Name', 'N'); % can experiment with other MFs
fis = addMF(fis, 'Err', 'linsmf', [-0.5 +0.5], 'Name', 'P'); % DO NOT follow exactly my approach
% Section 2: Fuzzy Output
fis = addOutput(fis, [-1 1], 'Name', 'U');
fis = addMF(fis, 'U', 'constant', -1, 'Name', 'N'); % lower threshold of control action
fis = addMF(fis, 'U', 'constant', +1, 'Name', 'P'); % upper threshold of control action
% Section 3: Fuzzy Rules
rules = [
"Err==N => U=P" % Rule 1: If Error is negative then U is positive.
"Err==P => U=N" % Rule 2: If Error is positive then U is negative.
];
fis = addRule(fis, rules);
%% Step 3: Write the code for second-order system in 4 simple lines
function dx = sosys(t, x, sys, fis)
ref = 1; % line 1: reference
err = x(1) - ref; % line 2: error
u = evalfis(fis, err); % line 3: fuzzy controller
dx = sys.A*x + sys.B*u; % line 4: 2nd-order system
end
%% Step 4: Call ode45 solver
[t, x] = ode45(@(t, x) sosys(t, x, sys, fis), [0 10], [0; 0]);
%% Step 5: Plot the results
figure(1)
subplot(211)
plotmf(fis, 'input', 1), grid on,
xlabel('Error (rad)')
title('Fuzzy sets of Error')
% Fuzzy surface
subplot(212)
opt = gensurfOptions('NumGridPoints', 401);
gensurf(fis, opt);
title ('Fuzzy control curve')
figure(2)
plot(ty, y), hold on
plot(t, x(:,1)), grid on,
xlabel('Time / s'), ylabel('Plant output'), title('Step responses')
legend('Linear P-control', 'Fuzzy controller')

类别

Help CenterFile Exchange 中查找有关 Fuzzy Logic Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by