# how to implement fuzzy logic code without using fuzzy Toolbox

62 views (last 30 days)
merlin toche on 12 Jan 2023
Commented: Sam Chak on 3 Feb 2023
Hello.
I need to implement a fuzzy logic code in matlab without using toolbox, i have two input power and voltage and i should estimate power in the next time,membershiP function is triangular,defuzz method is center method.
fuzzy inference (power and voltage level) and each input have 6 membershipfunction as follow
input={power : very small,small, medium, large, very large and Voltage:very small,small, medium, large, very large }
output={power : NFl,OCF, SCF, Pars,BDPars, OCinv }
formarulebase:
1. if (power is very small and voltage is very smal), then NFI
2. if(power is very small and voltage is small,) then OCF
3. if(power is small and voltage is medium) then SCF
4. if (power is medium and voltage is medium) then BDPars
5. if(power is large and voltage is medium) then Pars
6. if(power is very large and voltage is medium) then OCF
7. if(power is medium and voltage is large) then SCF
8. if(power is medium and voltage is very) large OCinv
9. if(power is very large and voltage is very large) then OCF
Thanks for your helping at this problem,
merlin toche on 13 Jan 2023
please sir help me solve this problem on my fuzzy logic code.
here is the code and the error message that is displayed. attached code

Sam Chak on 14 Jan 2023
Edited: Sam Chak on 18 Jan 2023
Some functions from the fuzzy logic toolbox in your code do not work on the much older R2015a release.
The toolbox does not a built-in function called "trmf()". Mostly likely you were trying to enter trimf(), which is the triangular MF. The MFs on the Grease look strange. Please check.
Edit: Check if the following code works in R2015a.
% Declare FIS Type (mamfis was introduced in R2018b)
% fis = newfis('fis', 'FISType', 'mamdani'); % for R2017a and newer
fis = newfis('Fuzzy_Car_Wash') % Create a default Mamdani FIS.
% Input 1: Amount of Dirt
fis = addvar(fis, 'input', 'Dirt', [0 100]);
fis = addmf(fis, 'input', 1, 'Small', 'trimf', [ 0 0 50]);
fis = addmf(fis, 'input', 1, 'Moderate', 'trimf', [ 0 50 100]);
fis = addmf(fis, 'input', 1, 'Large', 'trimf', [50 100 100]);
% Input 2: Amount of Grease
fis = addvar(fis, 'input', 'Grease', [0 100]);
fis = addmf(fis, 'input', 2, 'No', 'trimf', [ 0 0 50]);
fis = addmf(fis, 'input', 2, 'Moderate', 'trimf', [ 0 50 100]);
fis = addmf(fis, 'input', 2, 'Large', 'trimf', [50 100 150]);
% Output: Wash Time
fis = addvar(fis, 'output', 'WashTime', [0 60]);
fis = addmf(fis, 'output', 1, 'VS', 'trimf', [ 0 0 10]);
fis = addmf(fis, 'output', 1, 'S', 'trimf', [ 0 10 25]);
fis = addmf(fis, 'output', 1, 'M', 'trimf', [10 25 40]);
fis = addmf(fis, 'output', 1, 'L', 'trimf', [25 40 60]);
fis = addmf(fis, 'output', 1, 'VL', 'trimf', [40 60 60]);
% Plot membership functions
figure(1)
subplot(3,1,1)
plotmf(fis, 'input', 1), grid on, title('Input 1: Amount of Dirt')
subplot(3,1,2)
plotmf(fis, 'input', 2), grid on, title('Input 2: Amount of Grease')
subplot(3,1,3)
plotmf(fis, 'output', 1), grid on, title('Output: Wash Time')
% Fuzzy Rules
% [In1 In2 Out Weight AndOp]
ruleList = [1 1 1 1 1;... % R1: if Dirt = SD & Grease = NG, then WT = VS
1 2 2 1 1;... % R2: if Dirt = SD & Grease = MG, then WT = M
1 3 3 1 1;... % R3: if Dirt = SD & Grease = LG, then WT = L
2 1 2 1 1;... % R4: if Dirt = MD & Grease = NG, then WT = S
2 2 3 1 1;... % R5: if Dirt = MD & Grease = MG, then WT = M
2 3 4 1 1;... % R6: if Dirt = MD & Grease = LG, then WT = L
3 1 3 1 1;... % R7: if Dirt = LD & Grease = NG, then WT = M
3 2 4 1 1;... % R8: if Dirt = LD & Grease = MG, then WT = L
3 3 5 1 1]; % R9: if Dirt = LD & Grease = LG, then WT = VL
% Plot Output Surface of Mamdani FIS
figure(2)
gensurf(fis)
% Save the designed fuzzy inference system in a FIS file (Fuzzy_Car_Wash.fis)
writefis(fis, 'Fuzzy_Car_Wash')
Sam Chak on 3 Feb 2023
@merlin toche, Mostly you entered incorrectly in the Rules.
By the way, the syntax in your image was incorrect. Perhaps you should refer to my attachment in this comment. The correct syntax is
writefis(fis, 'Filename'); % for version older than R2018b
as given here:

### Categories

Find more on Fuzzy Inference System Modeling in Help Center and File Exchange

R2015a

### Community Treasure Hunt

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

Start Hunting!