addRule throws "Do not use a rule keyword as a variable name."
Afficher commentaires plus anciens
I get the error on line 44 of my code: fis = addRule(fis,all); I've tried changing the name of the variable name (all) to other names, and the fis. I've removed spaces from my variables or renamed them completely.
% Create a Fuzzy Inference System (FIS) with the name "IFR"
fis = mamfis('Name', 'IFR');
% Add a variable "input" to the FIS
fis = addInput(fis, [0 200], 'Name', 'MAP_mm');
% Add membership functions for variable "input"
fis = addMF(fis, "MAP_mm", "trapmf", [0 0 55 75], 'Name',"Low");
fis = addMF(fis, "MAP_mm", "trapmf", [55 75 100 120], 'Name',"Normal");
fis = addMF(fis, "MAP_mm", "trapmf", [100 120 200 200], 'Name',"High");
% HOU trapezoidal
fis = addInput(fis, [0 200], 'Name', 'HOU_ml-hr');
% HOU Low trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [0 0 30 40], 'Name',"Low");
% HOU Normal trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [30 40 100 125], 'Name',"Normal");
% HOU High trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [100 125 200 200], 'Name',"High");
% Add output variable to the FIS
fis = addOutput(fis, [0 2000], 'Name', 'IFR_ml-hr');
% Add membership functions for output variable
fis = addMF(fis, "IFR_ml-hr", "trapmf", [0 0 60 100], 'Name',"Low");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [0 100 200 400], 'Name',"Maintain");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [200 400 600 800], 'Name',"Moderate");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [600 800 1000 1500], 'Name',"High");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [1000 1500 2000 2000], 'Name',"Very_High");
% Create Rules for the FIS
r1 = "If HOU_ml-hr == Low & MAP_mm == Low => IFR_ml-hr == Very_High";
r2 = "If HOU_ml-hr == Normal & MAP_mm == Low => IFR_ml-hr == High";
r3 = "If HOU_ml-hr == High & MAP_mm == Low => IFR_ml-hr == Moderate";
r4 = "If HOU_ml-hr == Low & MAP_mm == Normal => IFR_ml-hr == Moderate";
r5 = "If HOU_ml-hr == Low & MAP_mm == High => IFR_ml-hr == Low";
r6 = "If HOU_ml-hr == Normal & MAP_mm == Normal => IFR_ml-hr == Maintain";
r7 = "If HOU_ml-hr == Normal & MAP_mm == High => IFR_ml-hr == Low";
r8 = "If HOU_ml-hr == High & MAP_mm == Normal =>IFR_ml-hr == Maintain";
r9 = "If HOU_ml-hr == High & MAP_mm == High => IFR_ml-hr == Low";
all = [r1 r2 r3 r4 r5 r6 r7 r8 r9];
% Add rules and enable rule viewing/debugging
fis = addRule(fis,all);
% Evaluate the FIS for inputs [MAP HOU]
sampleInput = [110 120; 60 25; 30 150; 180 90];
out = evalfis(fis, sampleInput);
% Plot membership functions
subplot(3,1,1)
plotmf(fis, 'input', 1)
subplot(3,1,2)
plotmf(fis, 'input', 2)
subplot(3,1,3)
plotmf(fis, 'output',3)
Réponse acceptée
Plus de réponses (2)
Hi @Michael
If you prefer to use the symbolic approach (==, =>) rather than the linguistic approach (If–Then) as demonstrated by @Walter Roberson, do not use the reserved fuzzy rule-related terms such as "If" and "Then". In your case, when you specified "If HOU_ml-hr == Low", MATLAB interprets this as if you had inadvertently used the phrase "If HOU_ml-hr" as a single variable name. I have made the corrections below, as well as slightly "beautified" the plots to prevent overlapping linguistic names.
If you like the explanation and the enhanced plots, please consider voting 👍 for this Answer.
% Create a Fuzzy Inference System (FIS) with the name "IFR"
fis = mamfis('Name', 'IFR');
% Input variable "MAP"
fis = addInput(fis, [0 200], 'Name', 'MAP_mm');
fis = addMF(fis, "MAP_mm", "trapmf", [ 0 0 55 75], 'Name', "Low");
fis = addMF(fis, "MAP_mm", "trapmf", [ 55 75 100 120], 'Name', "Normal");
fis = addMF(fis, "MAP_mm", "trapmf", [ 100 120 200 200], 'Name', "High");
% Input variable "HOU"
fis = addInput(fis, [0 200], 'Name', 'HOU_ml-hr');
fis = addMF(fis, "HOU_ml-hr", "trapmf", [ 0 0 30 40], 'Name', "Low");
fis = addMF(fis, "HOU_ml-hr", "trapmf", [ 30 40 100 125], 'Name', "Normal");
fis = addMF(fis, "HOU_ml-hr", "trapmf", [ 100 125 200 200], 'Name', "High");
% Output variable "IFR"
fis = addOutput(fis, [0 2000], 'Name', 'IFR_ml-hr');
fis = addMF(fis, "IFR_ml-hr", "trapmf", [ 0 0 60 100], 'Name', "Low");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [ 0 100 200 400], 'Name', "Maintain");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [ 200 400 600 800], 'Name', "Moderate");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [ 600 800 1000 1500], 'Name', "High");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [1000 1500 2000 2000], 'Name', "Very_High");
% Create Rules for the FIS
% r1 = "If HOU_ml-hr==Low & MAP_mm==Low => IFR_ml-hr==Very_High";
% r2 = "If HOU_ml-hr==Normal & MAP_mm==Low => IFR_ml-hr==High";
% r3 = "If HOU_ml-hr==High & MAP_mm==Low => IFR_ml-hr==Moderate";
% r4 = "If HOU_ml-hr==Low & MAP_mm==Normal => IFR_ml-hr==Moderate";
% r5 = "If HOU_ml-hr==Low & MAP_mm==High => IFR_ml-hr==Low";
% r6 = "If HOU_ml-hr==Normal & MAP_mm==Normal => IFR_ml-hr==Maintain";
% r7 = "If HOU_ml-hr==Normal & MAP_mm==High => IFR_ml-hr==Low";
% r8 = "If HOU_ml-hr==High & MAP_mm==Normal => IFR_ml-hr==Maintain";
% r9 = "If HOU_ml-hr==High & MAP_mm==High => IFR_ml-hr==Low";
r1 = "HOU_ml-hr==Low & MAP_mm==Low => IFR_ml-hr==Very_High";
r2 = "HOU_ml-hr==Normal & MAP_mm==Low => IFR_ml-hr==High";
r3 = "HOU_ml-hr==High & MAP_mm==Low => IFR_ml-hr==Moderate";
r4 = "HOU_ml-hr==Low & MAP_mm==Normal => IFR_ml-hr==Moderate";
r5 = "HOU_ml-hr==Low & MAP_mm==High => IFR_ml-hr==Low";
r6 = "HOU_ml-hr==Normal & MAP_mm==Normal => IFR_ml-hr==Maintain";
r7 = "HOU_ml-hr==Normal & MAP_mm==High => IFR_ml-hr==Low";
r8 = "HOU_ml-hr==High & MAP_mm==Normal => IFR_ml-hr==Maintain";
r9 = "HOU_ml-hr==High & MAP_mm==High => IFR_ml-hr==Low";
all = [r1 r2 r3 r4 r5 r6 r7 r8 r9];
fis = addRule(fis, all);
% Evaluate the FIS for inputs [MAP HOU]
% sampleInput = [110 120; 60 25; 30 150; 180 90];
% out = evalfis(fis, sampleInput);
% Plot membership functions
figure
subplot(3,1,1)
plotmf(fis, 'input', 1)
ax1 = gca;
delete(findall(ax1, 'type', 'text'))
text( 25, 1.2, 'Low');
text( 77, 1.2, 'Normal');
text(155, 1.2, 'High');
ylim([-0.2, 1.4])
ylabel({'$\mu$'}, 'interpreter', 'latex')
xlabel({'MAP / mm'}, 'interpreter', 'latex')
subplot(3,1,2)
plotmf(fis, 'input', 2)
ax2 = gca;
delete(findall(ax2, 'type', 'text'))
text( 10, 1.2, 'Low');
text( 60, 1.2, 'Normal');
text(155, 1.2, 'High');
ylim([-0.2, 1.4])
ylabel({'$\mu$'}, 'interpreter', 'latex')
xlabel({'HOU / ml-hr'}, 'interpreter', 'latex')
subplot(3,1,3)
% plotmf(fis, 'output', 3) % there is no Output number 3
plotmf(fis, 'output', 1)
ax3 = gca;
delete(findall(ax3, 'type', 'text'))
text( 0, 1.2, 'Low');
text( 40, 0.8, 'Maintain');
text( 365, 1.2, 'Moderate');
text( 830, 0.8, 'High');
text(1600, 1.2, 'Very High');
ylim([-0.2, 1.4])
ylabel({'$\mu$'}, 'interpreter', 'latex')
xlabel({'IFR / ml-hr'}, 'interpreter', 'latex')
sgtitle("Membership functions for MAP, HOU, and IFR", 'fontsize', 12)
figure
opt = gensurfOptions('NumGridPoints', 51);
gensurf(fis, opt);
ax4 = gca;
delete(findall(ax4, 'type', 'text'))
xlabel({'MAP / mm'}, 'interpreter', 'latex')
ylabel({'HOU / ml-hr'}, 'interpreter', 'latex')
zlabel({'IFR / ml-hr'}, 'interpreter', 'latex')
[az, el] = view()
view(az+180, el)
title("Surface of Human Decision for IFR")
Michael
le 6 Fév 2026
Déplacé(e) : Walter Roberson
le 6 Fév 2026
0 votes
Catégories
En savoir plus sur Fuzzy Logic in Simulink dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


