how can i simulate feedback system with fuzzy controller in m file without simulink?

4 visualizaciones (últimos 30 días)
i have plant g=1/(s^2+s+1) and i have fuzzy inference struct (fuzi) i need to simulate this system in m file like this (if i use pid ) c=pid(1,2); t=feedback(c*g,1); step(t) instead of c i need to use fuzi without using simulink only in m file

Respuesta aceptada

Sam Chak
Sam Chak el 7 de Feb. de 2025
If the fuzzy controller (or the *.fis object) has been designed, it is straightforward to implement it in m-code using the evalfis() and ode45() functions, as advised by @Arkadiy Turevskiy. Here is a simple demo illustrating how a fuzzy controller can be utilized to eliminate overshoot and reduce settling time, thereby achieving a faster response:
%% Fuzzy Controller
fis = sugfis('Name', "Sugeno_FLC");
% Fuzzy Input 1
ud = 1.5; % universe of discourse
fis = addInput(fis, [-ud +ud], 'Name', 'in1');
fis = addMF(fis, 'in1', 'trimf', [-ud -ud ud], 'Name', 'N');
fis = addMF(fis, 'in1', 'trimf', [-ud ud ud], 'Name', 'P');
% Fuzzy Input 2
fis = addInput(fis, [-ud +ud], 'Name', 'in2');
fis = addMF(fis, 'in2', 'trimf', [-ud -ud ud], 'Name', 'N');
fis = addMF(fis, 'in2', 'trimf', [-ud ud ud], 'Name', 'P');
% Fuzzy Output
fis = addOutput(fis, [-4.875 4.875], 'Name', 'out');
fis = addMF(fis, 'out', 'constant', -4.875, 'Name', 'NB');
fis = addMF(fis, 'out', 'constant', -1.125, 'Name', 'NS');
fis = addMF(fis, 'out', 'constant', 1.125, 'Name', 'PS');
fis = addMF(fis, 'out', 'constant', 4.875, 'Name', 'PB');
% Fuzzy Rules
rules = [
"in1==N & in2==N => out=NB"
"in1==N & in2==P => out=PS"
"in1==P & in2==N => out=NS"
"in1==P & in2==P => out=PB"
];
fis = addRule(fis, rules);
% ---------- Implement this part in a script: START ----------
figure(1)
subplot(211)
plotmf(fis, 'input', 1), grid on, xlabel('Error')
title('Error membership functions')
subplot(212)
plotmf(fis, 'input', 2), grid on, xlabel('Change in Error')
title('Change in Error membership functions')
sgtitle('Input Fuzzy Sets')
% Fuzzy surface
figure(2)
opt = gensurfOptions('NumGridPoints', 51);
gensurf(fis, opt);
xlabel('Error'), ylabel('Change in Error'), zlabel('Fuzzy Control Output')
title ('Fuzzy Control Surface'), axis([-1.5 +1.5 -1.5 +1.5 -5.0 +5.0])
%% Plant
Gp = tf(1, [1 1 1])
Gp = 1 ----------- s^2 + s + 1 Continuous-time transfer function.
figure(3)
step(Gp , 10), hold on
%% Call ode45 to solve the system
[t, x] = ode45(@(t, x) ode(t, x, fis), [0 10], [0; 0]);
plot(t, x(:,1), 'color', [0.8500 0.3250 0.0980]), grid on, hold off
legend('Plant response', 'Closed-loop response', 'location', 'east')
%% System dynamics
function dx = ode(t, x, fis)
% Control settings
r = 1; % reference
sf = 2.25; % scaling factor
% Fuzzy Controller
u = - evalfis(fis, x) + sf*r;
% Plant's state-space
A = [ 0 1;
-1 -1];
B = [ 0;
1];
dx = A*x + B*u; % equivalent to G(s) = 1/(s^2 + s + 1)
end
% ---------- Implement this part in a script: END ----------

Más respuestas (1)

Arkadiy Turevskiy
Arkadiy Turevskiy el 16 de Ag. de 2018
Hi,
I am afraid there is no easy way to do it. You would have to use evalfis to evaluate your fizzy logic controller at each computation step and then use something like ode45 to simulate plant step by step.
As you mention, Simulink would make this much easier.

Categorías

Más información sobre Fuzzy Logic in Simulink en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by