function fis = mkFIS()
%mkFIS makes FIS



% FIS control
%  R2025 compatibility:
%  - newfis->mamfis
%  - defuzzMethod='cos' -> AggregationMethod="sum", DefuzzificationMethod="centroid" 
fis = mamfis( ...
    AndMethod="min", ...
    OrMethod="max", ...
    ImplicationMethod="min", ...
    AggregationMethod="sum", ...
    DefuzzificationMethod="centroid" );


% Member functions
mf.Names = ["NV"; "NL"; "NM"; "NS"; "ZR"; "PS"; "PM"; "PL"; "PV"];
mf.Edges = [     ...
    [-1.25 -1    -0.75]; ...  % NV
    [-1    -0.75 -0.5 ]; ...  % NL 
    [-0.75 -0.5  -0.25]; ...  % NM
    [-0.5  -0.25  0   ]; ...  % NS
    [-0.25  0     0.25]; ...  % ZR
    [ 0     0.25  0.5 ]; ...  % PS
    [ 0.25  0.5   0.75]; ...  % PM
    [ 0.5   0.75  1   ]; ...  % PL
    [ 0.75  1     1.25]; ...  % PV
];

% Setup input/output
fis = addInput(fis, [-1 1], Name="e");
fis = addInput(fis, [-1 1], Name="de");
fis = addOutput(fis, [-1 1], Name="du");
for k=1:length(mf.Names)
    fis = addMF(fis, "e",  "trimf", mf.Edges(k, :), Name=mf.Names(k));
    fis = addMF(fis, "de", "trimf", mf.Edges(k, :), Name=mf.Names(k));
    fis = addMF(fis, "du", "trimf", mf.Edges(k, :), Name=mf.Names(k));
end

% Rules 9x9

% rows = e (NV..PV), cols = de (NV..PV)
ruleTable = [
% de: NV   NL   NM   NS   ZR   PS   PM   PL   PV
     "NV","NV","NV","NV","NV","NL","NM","NS","ZR";  % e = NV
     "NV","NV","NV","NV","NL","NM","NS","ZR","PS";  % e = NL
     "NV","NV","NV","NL","NM","NS","ZR","PS","PM";  % e = NM
     "NV","NV","NL","NM","NS","ZR","PS","PM","PL";  % e = NS
     "NV","NL","NM","NS","ZR","PS","PM","PL","PV";  % e = ZR
     "NL","NM","NS","ZR","PS","PM","PL","PV","PV";  % e = PS
     "NM","NS","ZR","PS","PM","PL","PV","PV","PV";  % e = PM
     "NS","ZR","PS","PM","PL","PV","PV","PV","PV";  % e = PL
     "ZR","PS","PM","PL","PV","PV","PV","PV","PV"   % e = PV
];

for i=1:length(mf.Names)
    for j=1:length(mf.Names)
        rule = "IF e IS "+mf.Names(i)+" AND de IS "+mf.Names(j)+" THEN du IS "+ruleTable(i,j);
        fis = addRule(fis, rule);
    end
end


end