Traditionally, trimf and gaussmf are the two most commonly used membership functions in fuzzy systems. However, if the centers of the fuzzy sets are not uniformly distributed over the variable range, it may be more appropriate to use piecewise functions such as trapmf or gauss2mf to flexibly connect adjacent fuzzy sets at the desired crossover points. However, an excessive number of trimf or trapmf functions can result in a fuzzy surface that resembles the Grand Canyon. Therefore, if a smooth surface is desired, gaussmf or gauss2mf are generally recommended.
fis = mamfis;
%% Fuzzy Input #1
c11 = 0.0; % center of MF1 (poor)
c12 = 0.5; % center of MF2 (good)
c13 = 0.7; % center of MF3 (very good)
cross = 0.5; % crossover point where the deg of MF μ(x) equals to 0.5
dist11 = c12 - c11;
sigma11 = 0.5*(dist11)/sqrt(-2*log(cross)); % std dev 1
dist12 = c13 - c12;
sigma12 = 0.5*(dist12)/sqrt(-2*log(cross)); % std dev 2
fis = addInput(fis, [c11 c13], 'Name', 'SecurityStability');
fis = addMF(fis, 'SecurityStability', 'gaussmf', [sigma11 c11], 'Name', 'Poor');
fis = addMF(fis, 'SecurityStability', 'gauss2mf', [sigma11 c12 sigma12 c12], 'Name', 'Good');
fis = addMF(fis, 'SecurityStability', 'gaussmf', [sigma12 c13], 'Name', 'VeryGood');
%% Fuzzy Input #2
c21 = 0.2;
c22 = 0.5;
c23 = 0.7;
c24 = 0.8;
c25 = 0.9;
c26 = 1.0;
dist21 = c22 - c21;
sigma21 = 0.5*(dist21)/sqrt(-2*log(cross));
dist22 = c23 - c22;
sigma22 = 0.5*(dist22)/sqrt(-2*log(cross));
dist23 = c24 - c23;
sigma23 = 0.5*(dist23)/sqrt(-2*log(cross));
fis = addInput(fis, [0.0 c26], 'Name', 'TypeOfAgreement');
fis = addMF(fis, 'TypeOfAgreement', 'gauss2mf', [sigma21 0.0 sigma21 c21], 'Name', 'Weak');
fis = addMF(fis, 'TypeOfAgreement', 'gauss2mf', [sigma21 c22 sigma22 c22], 'Name', 'Good');
fis = addMF(fis, 'TypeOfAgreement', 'gauss2mf', [sigma22 c23 sigma23 c23], 'Name', 'VG');
fis = addMF(fis, 'TypeOfAgreement', 'gaussmf', [sigma23 c24], 'Name', 'Strong');
fis = addMF(fis, 'TypeOfAgreement', 'gaussmf', [sigma23 c25], 'Name', 'VS');
fis = addMF(fis, 'TypeOfAgreement', 'gaussmf', [sigma23 c26], 'Name', 'Exc');
%% Fuzzy Output
in1nMFs = numel(fis.Inputs(1).MembershipFunctions);
in2nMFs = numel(fis.Inputs(2).MembershipFunctions);
outnMFs = in1nMFs + in2nMFs - 1;
fis = addOutput(fis, [1 outnMFs], 'Name', 'Index', 'NumMFs', outnMFs, 'MFType', "gaussmf");
%% Fuzzy Rules
rules = [
"SecurityStability==Poor & TypeOfAgreement==Weak => Index=mf1"
"SecurityStability==Poor & TypeOfAgreement==Good => Index=mf2"
"SecurityStability==Poor & TypeOfAgreement==VG => Index=mf3"
"SecurityStability==Poor & TypeOfAgreement==Strong => Index=mf3"
"SecurityStability==Poor & TypeOfAgreement==VS => Index=mf4"
"SecurityStability==Poor & TypeOfAgreement==Exc => Index=mf4"
"SecurityStability==Good & TypeOfAgreement==Weak => Index=mf3"
"SecurityStability==Good & TypeOfAgreement==Good => Index=mf3"
"SecurityStability==Good & TypeOfAgreement==VG => Index=mf4"
"SecurityStability==Good & TypeOfAgreement==Strong => Index=mf5"
"SecurityStability==Good & TypeOfAgreement==VS => Index=mf6"
"SecurityStability==Good & TypeOfAgreement==Exc => Index=mf6"
"SecurityStability==VeryGood & TypeOfAgreement==Weak => Index=mf4"
"SecurityStability==VeryGood & TypeOfAgreement==Good => Index=mf5"
"SecurityStability==VeryGood & TypeOfAgreement==VG => Index=mf6"
"SecurityStability==VeryGood & TypeOfAgreement==Strong => Index=mf6"
"SecurityStability==VeryGood & TypeOfAgreement==VS => Index=mf7"
"SecurityStability==VeryGood & TypeOfAgreement==Exc => Index=mf8"
];
fis = addRule(fis, rules);
%% Plot figures
figure
tL = tiledlayout(2, 1, 'TileSpacing', 'Compact');
nexttile
numpts1 = (c13 - c11)*1000 + 1;
plotmf(fis, 'input', 1, numpts1), grid on, title('Input 1 fuzzy sets')
nexttile
numpts2 = (c26 - 0.0)*1000 + 1;
plotmf(fis, 'input', 2, numpts2), grid on, title('Input 2 fuzzy sets')
figure
numpts3 = (outnMFs - 1)*100 + 1;
plotmf(fis, 'output', 1, numpts3), grid on, title('Output fuzzy sets')
figure
opt = gensurfOptions('NumGridPoints', 51);
gensurf(fis, opt), title('Fuzzy decision surface')



