please explain this code

2 views (last 30 days)
Sunil Kumar Sah
Sunil Kumar Sah on 23 Jul 2022
Edited: the cyclist on 23 Jul 2022
x = 0:0.1:20;
mf1 = trapmf(x,[0 2 8 12]);
mf2 = trapmf(x,[5 7 12 14]);
mf3 = trapmf(x,[12 13 18 19]);
mf = max(0.5*mf2,max(0.9*mf1,0.1*mf3));
figure('Tag','defuzz')
plot(x,mf,'LineWidth',3)
h_gca = gca;
h_gca.YTick = [0 .5 1] ;
ylim([-1 1])
xCentroid = defuzz(x,mf,'centroid');
hCentroid = line([xCentroid xCentroid],[-0.2 1.2],'Color','k');
tCentroid = text(xCentroid,-0.2,' centroid','FontWeight','bold');
xBisector = defuzz(x,mf,'bisector');
hBisector = line([xBisector xBisector],[-0.4 1.2],'Color','k');
tBisector = text(xBisector,-0.4,' bisector','FontWeight','bold');
gray = 0.7*[1 1 1];
hCentroid.Color = gray;
tCentroid.Color = gray;
xMOM = defuzz(x,mf,'mom');
xSOM = defuzz(x,mf,'som');
xLOM = defuzz(x,mf,'lom');
hMOM = line([xMOM xMOM],[-0.7 1.2],'Color','k');
tMOM = text(xMOM,-0.7,' MOM','FontWeight','bold');
hSOM = line([xSOM xSOM],[-0.7 1.2],'Color','k');
tSOM = text(xSOM,-0.7,' SOM','FontWeight','bold');
hLOM = line([xLOM xLOM],[-0.7 1.2],'Color','k');
tLOM = text(xLOM,-0.7,' LOM','FontWeight','bold');
hBisector.Color = gray;
tBisector.Color = gray;
hCentroid.Color = 'red';
tCentroid.Color = 'red';
hMOM.Color = gray;
tMOM.Color = gray;
hSOM.Color = gray;
tSOM.Color = gray;
hLOM.Color = gray;
tLOM.Color = gray;

Answers (1)

the cyclist
the cyclist on 23 Jul 2022
Edited: the cyclist on 23 Jul 2022
I ran your code here. You can see that figure it creates. That's what the code does. It defines some data, does some calculations, draws some line and writes some text.
What every command does is explained in the documentation, for example in trapmf and defuzz. It doesn't make much sense to just copy that information here. You just need to go line-by-line and understand what each command does.
I hope that helps.

Community Treasure Hunt

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

Start Hunting!