how to implement fuzzy logic code without using fuzzy Toolbox
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
1 vote
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:
- if (power is very small and voltage is very smal), then NFI
- if(power is very small and voltage is small,) then OCF
- if(power is small and voltage is medium) then SCF
- if (power is medium and voltage is medium) then BDPars
- if(power is large and voltage is medium) then Pars
- if(power is very large and voltage is medium) then OCF
- if(power is medium and voltage is large) then SCF
- if(power is medium and voltage is very) large OCinv
- if(power is very large and voltage is very large) then OCF
Thanks for your helping at this problem,
1 Comment
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

Accepted Answer
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
fis = addrule(fis, ruleList);
% 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')


29 Comments
merlin toche
on 16 Jan 2023
thank you for your interest in me. really, thank you.
absolutely you are right about my fat MF, there is indeed an error. nevertheless I corrected it is the same MF as Dirt.
I tried to run the code, but it shows the same error message: "Undefined function or variable 'mamfis'."
maybe as you said, the problem comes from the fact that my r2015a version is older. thank you for your help, I will come back to you soon for any requests please
best regards
Sam Chak
on 17 Jan 2023
Most likely the issue is related to the Fuzzy Logic Toolbox. By the way, it is not the end of the world when the toolbox is unavailable. You can still mathematically construct the desired shapes of the Membership Functions and the define your preferred Defuzzification method. For simplicity and yet as good as Mamdani FIS, I suggest you use the Sugeno FIS.
x = linspace(0, 100, 10001);
m = 1/50*[1 1 1]; % gradients
c = [0 50 100]; % centres
y1 = max(0, 1 - m(1)*abs(x - c(1))); % triMF 1
y2 = max(0, 1 - m(2)*abs(x - c(2))); % triMF 2
y3 = max(0, 1 - m(3)*abs(x - c(3))); % triMF 3
plot(x, [y1; y2; y3]), ylim([-0.2 1.2]), grid on
xlabel('Dirt')
ylabel('Degree of membership, \mu')
text( 0+1.0, 1.05, 'SD')
text( 50-2.5, 1.05, 'MD')
text(100-5.0, 1.05, 'LD')

Though you need write a few lines of code instead of using plotmf directly, it is possible to get the work done.
Anyhow, here is how you can check if you have the Fuzzy Logic Toolbox installed.
ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.13.0.2145394 (R2022b) Update 3
MATLAB License Number: 0
Operating System: Linux 5.4.224-0504224-generic #202211101403 SMP Thu Nov 10 21:10:29 UTC 2022 x86_64
Java Version: Java 1.8.0_292-b10 with AdoptOpenJDK OpenJDK 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.13 (R2022b)
Simulink Version 10.6 (R2022b)
5G Toolbox Version 2.5 (R2022b)
Aerospace Blockset Version 5.3 (R2022b)
Aerospace Toolbox Version 4.3 (R2022b)
Antenna Toolbox Version 5.3 (R2022b)
Audio Toolbox Version 3.3 (R2022b)
Automated Driving Toolbox Version 3.6 (R2022b)
Bioinformatics Toolbox Version 4.16.1 (R2022b)
Bluetooth Toolbox Version 1.1 (R2022b)
Communications Toolbox Version 7.8 (R2022b)
Computer Vision Toolbox Version 10.3 (R2022b)
Control System Toolbox Version 10.12 (R2022b)
Curve Fitting Toolbox Version 3.8 (R2022b)
DO Qualification Kit Version 3.14 (R2022b)
DSP System Toolbox Version 9.15 (R2022b)
Database Toolbox Version 10.4 (R2022b)
Datafeed Toolbox Version 6.3 (R2022b)
Deep Learning Toolbox Version 14.5 (R2022b)
Econometrics Toolbox Version 6.1 (R2022b)
Embedded Coder Version 7.9 (R2022b)
Filter Design HDL Coder Version 3.1.12 (R2022b)
Financial Instruments Toolbox Version 3.5 (R2022b)
Financial Toolbox Version 6.4 (R2022b)
Fixed-Point Designer Version 7.5 (R2022b)
Fuzzy Logic Toolbox Version 3.0 (R2022b)
Global Optimization Toolbox Version 4.8 (R2022b)
HDL Coder Version 4.0 (R2022b)
HDL Verifier Version 7.0 (R2022b)
IEC Certification Kit Version 3.20 (R2022b)
Image Acquisition Toolbox Version 6.7 (R2022b)
Image Processing Toolbox Version 11.6 (R2022b)
Industrial Communication Toolbox Version 6.1 (R2022b)
Instrument Control Toolbox Version 4.7 (R2022b)
LTE Toolbox Version 3.8 (R2022b)
MATLAB Compiler Version 8.5 (R2022b)
MATLAB Compiler SDK Version 7.1 (R2022b)
MATLAB Report Generator Version 5.13 (R2022b)
Mapping Toolbox Version 5.4 (R2022b)
Mixed-Signal Blockset Version 2.3 (R2022b)
Model Predictive Control Toolbox Version 8.0 (R2022b)
Navigation Toolbox Version 2.3 (R2022b)
Optimization Toolbox Version 9.4 (R2022b)
Parallel Computing Toolbox Version 7.7 (R2022b)
Partial Differential Equation Toolbox Version 3.9 (R2022b)
Phased Array System Toolbox Version 4.8 (R2022b)
Powertrain Blockset Version 1.12 (R2022b)
Predictive Maintenance Toolbox Version 2.6 (R2022b)
RF Blockset Version 8.4 (R2022b)
RF Toolbox Version 4.4 (R2022b)
Requirements Toolbox Version 2.1 (R2022b)
Risk Management Toolbox Version 2.1 (R2022b)
Robotics System Toolbox Version 4.1 (R2022b)
Robust Control Toolbox Version 6.11.2 (R2022b)
Sensor Fusion and Tracking Toolbox Version 2.4 (R2022b)
SerDes Toolbox Version 2.4 (R2022b)
Signal Processing Toolbox Version 9.1 (R2022b)
SimBiology Version 6.4 (R2022b)
SimEvents Version 5.13 (R2022b)
Simscape Version 5.4 (R2022b)
Simscape Driveline Version 3.6 (R2022b)
Simscape Electrical Version 7.8 (R2022b)
Simscape Fluids Version 3.5 (R2022b)
Simscape Multibody Version 7.6 (R2022b)
Simulink 3D Animation Version 9.5 (R2022b)
Simulink Check Version 6.1 (R2022b)
Simulink Code Inspector Version 4.2 (R2022b)
Simulink Coder Version 9.8 (R2022b)
Simulink Control Design Version 6.2 (R2022b)
Simulink Coverage Version 5.5 (R2022b)
Simulink Design Optimization Version 3.12 (R2022b)
Simulink Design Verifier Version 4.8 (R2022b)
Simulink PLC Coder Version 3.7 (R2022b)
Simulink Real-Time Version 8.1 (R2022b)
Simulink Report Generator Version 5.13 (R2022b)
Simulink Test Version 3.7 (R2022b)
Stateflow Version 10.7 (R2022b)
Statistics and Machine Learning Toolbox Version 12.4 (R2022b)
Symbolic Math Toolbox Version 9.2 (R2022b)
System Identification Toolbox Version 10.0 (R2022b)
Text Analytics Toolbox Version 1.9 (R2022b)
Vehicle Dynamics Blockset Version 1.9 (R2022b)
Vehicle Network Toolbox Version 5.3 (R2022b)
Vision HDL Toolbox Version 2.6 (R2022b)
WLAN Toolbox Version 3.5 (R2022b)
Wavelet Toolbox Version 6.2 (R2022b)
Wireless HDL Toolbox Version 2.5 (R2022b)
If the Fuzzy Logic Toolbox was installed previously, then check the status of the license.
[status, errmsg] = license('checkout', 'fuzzy_toolbox')
status = 1
errmsg =
0×0 empty char array
If the status output is 1 and the errmsg output array is empty, then you are good.
merlin toche
on 17 Jan 2023
hello, thank you for the very quick return, your sensitivity goes straight to my heart!
however, as you mentioned, some functions of my toolbox may not work. yet the status is 1 showing that it is well presented. I tried to declare the function sugfis, but the same message is displayed as with mamfis. I'm confused.
very dear, please I am still an apprentice, can you accompany this piece of code with the comments (I do not understand this code well). please look at my attached code to better understand me. I'm new to ML and matlab.
thank you for your kindness

Sam Chak
on 17 Jan 2023
I dug out some old documentations and revised the MATLAB code in my Answer above.
If the code works, please consider accepting ✔ and voting 👍 the revised Answer. Thanks a bunch! 🙏
merlin toche
on 17 Jan 2023
Hi ! @Sam Chak
I really thank you for the efforts made to help me. your speed in the execution and the pedagogy in the explanation.
however good code, but unfortunately for me my R2015a version did not run this good code. here is the message displayed. I don't know if you have another alternative for me.
thank you again my dear, despite my quality of learner you manage to listen. really thank you and see you soon

Sam Chak
on 18 Jan 2023
🤦♂️ Didn't know it went through a few changes over the last few years. I used to create the FIS using the GUI by typing the following commands at the MATLAB prompt:
fuzzy
or
fuzzyLogicDesigner
From the newfis() documentation, the name-value pair arguments are introduced in R2017a. That's why you got the error message. The simplest solution is to create a default Mamdani fuzzy inference system, by replacing this line
fis = newfis('fis', 'FISType', 'mamdani')
with this one
fis = newfis('Fuzzy_Car_Wash')
I have revised my Answer above. Let me know if this works so that we can have a proper closure.
merlin toche
on 18 Jan 2023
Hi!
I'm very happy sir! your ease of explanation and your humility, thank you again. the revision you made has partially worked. partly because, an error message appeared at the level of the gensurfoption function. moreover, can't we replace "mamdani" by "sugeno"?
thank you again for your prompt reaction and your dynamism
see you later my dear

Sam Chak
on 18 Jan 2023
Just discovered that gensurfOptions() was introduced in R2017a. I have removed that in the updated Answer.
To create the Sugeno FIS, this is the general syntax for versions before R2017a.
fis = newfis('My FIS', fisType, andMethod, orMethod, impMethod, aggMethod, defuzzMethod);
This are the technical limitations of using the fuzzy toolbox in R2015a. Use the GUI Fuzzy Designer is more user-friendly if you still have problems with R2015a. If you have the opportunity, upgrade to R2022b through the university Campus-Wide License (if available).
merlin toche
on 18 Jan 2023
thank you, thank you very much for your kindness
you teach me a lot about MATLAB and fuzzy logic, I'm going to compile this into a short course
Thank you again and see you soon
could I come back to you for everything in case I try fuzzyLogicDesigner please?
Sam Chak
on 18 Jan 2023
@merlin toche, Yes, you can post a new question if you encounter a problem in fuzzyLogicDesigner. Don't forget to click-accept the Answer as a closure, once you verify that the revised code works for R2015a. It will also benefit to other users who are still using the older (pre-R2017a) versions.
merlin toche
on 18 Jan 2023
thank you @Sam Chak
merlin toche
on 19 Jan 2023
Hi @Sam Chak
please, there is no alternative to be able to display figure(2) apart from the gensurfOption function? since it doesn't work with R2015a.
Thank you and see you soon
Sam Chak
on 19 Jan 2023
Didn't I remove that already in the updated answer? Can you post the error message?
merlin toche
on 19 Jan 2023
Sam Chak
on 20 Jan 2023
As mentioned previously, because you version is R2015a, then use this code:
% Plot Output Surface of Mamdani FIS
figure(2)
gensurf(fis)
Sam Chak
on 20 Jan 2023
As mentioned previously, because you version is R2015a, then use this code:
% Plot Output Surface of Mamdani FIS
figure(2)
gensurf(fis)
merlin toche
on 20 Jan 2023
it work this code
i tried to use evalfis to get a result but it is no work
however, writefis is also unable to save designed fuzzy inference system. is it important to save it? if yes how do we proceed!
thank my dear
Sam Chak
on 20 Jan 2023
@merlin toche, If you already have the code, then it is not so important to save it. If you want to save it, try importing (loading) the fis (as shown in Workspace) into the fuzzy logic app. Then, export it (save to) to the desired folder. This should be simplest way.
Sam Chak
on 22 Jan 2023
@merlin toche. Any update?
merlin toche
on 23 Jan 2023
Hi @Sam Chak
I come back to you momentarily. I'll get back to you, thanks for all your help.
Sam Chak
on 1 Feb 2023
Did you accidentally accepted your own "Answer" (which in fact, is a comment)? Perhaps you did not realize you should click to accept the satisfactory answer to the question you asked.
You can unaccept your own "Answer" and move the contents to the "Comment" section below my "Answer".
merlin toche
on 1 Feb 2023
I hope my message finds you in good health.
thank you first of all for everything you do to support me in my work.
please, I would like to know how to interpret the results in fuzzy logic
for example here is attached my code on fault detection (NF OCF SCF PS PSBD)
I want to be able to explain the two figures obtained please
Thank you and see you soon
Sam Chak
on 2 Feb 2023
Since the code works and if you find that the Answer (updated multiple times) and after-answer supports are satisfactory, please consider accepting ✔ Answer. Thanks a million! 💸🙏
------------------E-X-T-R-A------------------
Figure 1 shows what you designed for the membership functions, though I find it strange that the degree of LARGE membership of 30 V is 0. As if you are saying the 30 V is not Large at all. The same for OCF at FaultDetect = 50. Not OCF at all.

Figure 2 is the generated surface that described the relationship between these 3 variables (Current, Voltage, and FaultDetect) visually. If the surface is undesirable, then you have to modify shapes of MFs, and sometimes the Rules.

merlin toche
on 2 Feb 2023
Hi @Sam Chak ,thank you very much for your pedagogy and your availability to help me. as I am still learning, apparently the difficulty is to define the membership functions. this is where really my dear, I am overwhelmed. hears expert, your pedagogy fascinates me, please can you explain to me more how to choose the membership functions and how to interpret the results? please, it is important for me thank you very much
for example here is a result that I got from the toolbox, could I have your help once again?
Thank you and see you soon!
figure1

figure2

Sam Chak
on 3 Feb 2023
To be honest, I'm no expert on the PV system (which I guess from your file). But your revised FIS shows that the ranges are changed to [0, 100] for both current and voltage, without any technical reason explained to me. Moreover, I'm unable to interpret the revised Fuzzy Rules as seen in the GUI. Thus, I guess you mistakenly set the Rules.
Anyhow, the MFs and Surface looks more properly shaped in this update (newer than R2015a, change the code as needed). The surface is now symmetrical and stylishly-curved. With the shapes of MFs and the rules are now fixed, how good it affects the system performance depends the range you set. This should simplify you design work.


I'm more familiar with the model-based design. If the system model is available, I will analyze it and the findings will help me to decide on the shape of the MFs and where I should place them.
merlin toche
on 3 Feb 2023
received thank you @Sam Chak
you are right, my ranges have changed, these are the data that I took concerning the various defects in a document to test my program. I actually haven't done my measurements yet. but as soon as I'm done with modeling my model, I'll let you know. by the way, my use fis is sugeno and not mamdani please.
in addition, @Sam Chak for me, given your knowledge and your mastery in this field, really bravo I say a thousand times thank you. frankly live forever, thanks to you I am starting to learn about MATLAB and machine learning.
thank you for all you do for the community.
apparently using the toolbox we don't have the same result, I tried and I see that it's not the same surface as what you have above
figure obtained with fuzzyToolbox

Sam Chak
on 3 Feb 2023
Don't mention it. If you have some issues regarding the designed fuzzy PV system, I suggest you to post a new Question and attach the FIS file for investigation. Sometimes, keeping a long list of comments may derail from its original question.
I guess you saved the fis using the writefis() command and then reopened it (Import From File) using the FuzzyLogicDesigner app. But I'm not exactly sure what happened to yours. Probably there is a bug in R2015a fuzzy toolbox version. Mine looks the same as previously generated using gensurf(fis).

merlin toche
on 3 Feb 2023
ok understood! just a question, i didn't save the file i rather entered my data using fuzzyLogicDesigner.
When I tried to save this is the error message I got.

how can I do? do we have an alternative?
as I told you, as soon as my PV system is ready I will let you know by posting as a new question.
thanks again
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:
More Answers (0)
Categories
Find more on Power and Energy Systems in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
