3

I would like to draw fuzzy membership functions using tikz. The problem has already been solved in Trapezoidal solution for trapezoidal MF, but I need something similar for sigmoidally shaped functions. I don't have a clue how to do that ...

enter image description here

I have this code:

 \documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\begin{scope}[xshift=5.5cm]
\draw[->] (0,0) -- node[below] {} (5,0) node[below] {Age};
\draw[->] (0,0) -- (0,1.5) node[left] {$\mu$};
\node at (-0.2,0) {0};
\node at (-0.2,1) {1};
\draw (0,1) -- (5,1);
\draw[scale=1,domain=0:5,smooth,variable=\x,blue] plot ({\x},{1/(1+exp(-1*(\x-3)))});
\draw[scale=1,domain=0:5,smooth,variable=\x,red] plot ({\x},{1/(1+exp(-3*(\x-1.5)))});

\end{scope}

\end{tikzpicture}

\end{document}

enter image description here

but I don't undestand why the red line in the final part "fall" at a value different from 1, since it doesn't make any sense given the mathematical function used in "plot"

  • It would be helpful if you provided a link to an image of what you desire. As this is not a math site, not everyone here is familiar with fuzzy logic so am image would go a long way to clarify what you desire. Furthermore, it would be good if you made some sort of attempt and provided a MWE that shows what your have tried so far. – Peter Grill Oct 21 '14 at 23:22
  • Sorry, in the link provided is reported the typical aspect of a fuzzy diagram, so i tought it was not necessary to explain it again. Anyway this is what i'm looking for: on the upper image there is a diagram with trapezoidal functions (already solved in the discussion linked), in the lower image an equivalent version with sigmoidal functions. !image. I would like to make an attempt but I don't know how to draw curves in tikz – dario Oct 21 '14 at 23:45
  • I can't understand how to post images in comments, anyway clicking on the link "image" you can see what I mean ... – dario Oct 21 '14 at 23:53
  • Check if your tikz has the following version. Package: tikz 2013/12/13 v3.0.0 (rcs-revision 1.142), this new version has no fall; but older version v2.10 (rcs-revision 1.76) will. – Jesse Oct 22 '14 at 3:43
3

You can define your membership functions yourself and have control over how you implement it. I've defined the triangle, trapezoid, gaussian and generalized Bell, sigmoid and you can add more such as one sided functions etc. Please notice the percent signs at the end of each line for the function. If you omit them your plot will shift to the right with whitespaces everytime you execute the function becuase they will be picked up by the font.

And I would strongly recommend pgfplotsfor this. It is pretty much the choice for such uses.

Here is one example

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\pgfmathdeclarefunction{fuzzytrapezoid}{4}{%
\begingroup%
\pgfmathparse{max(min((x-#1)/(#2-#1),1,(#4-x)/(#4-#3)),0)}%
\pgfmathfloattofixed{\pgfmathresult}%
\pgfmathreturn\pgfmathresult pt\relax%
\endgroup%
}
\pgfmathdeclarefunction{fuzzytriangle}{3}{%
\begingroup%
\pgfmathparse{max(min((x-#1)/(#2-#1),(#3-x)/(#3-#2)),0)}%
\pgfmathfloattofixed{\pgfmathresult}%
\pgfmathreturn\pgfmathresult pt\relax%
\endgroup%
}
\pgfmathdeclarefunction{fuzzygaussian}{2}{%
\begingroup%
\pgfmathparse{exp(-0.5*((x-#1)/#2)^2)}%
\pgfmathfloattofixed{\pgfmathresult}%
\pgfmathreturn\pgfmathresult pt\relax%
\endgroup%
}
\pgfmathdeclarefunction{fuzzygenbell}{3}{%
\begingroup%
\pgfmathparse{1/(1+abs((x-#3)/#1)^(2*#2))}%
\pgfmathfloattofixed{\pgfmathresult}%
\pgfmathreturn\pgfmathresult pt\relax%
\endgroup%
}
\pgfmathdeclarefunction{fuzzysigmoid}{2}{%
\begingroup%
\pgfmathparse{1/(1+exp(-#1*(x-#2))}%
\pgfmathfloattofixed{\pgfmathresult}%
\pgfmathreturn\pgfmathresult pt\relax%
\endgroup%
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymajorgrids,
ytick={0,1},tick style={draw=none},xtick=\empty,domain=0:10,no marks
]
%\addplot+[no marks] {1/(1+exp(-3*(x-1.5))};
\addplot+[] {fuzzytrapezoid(1,1.5,3,4)};
\addplot+[] {fuzzytriangle(2,5,7)};
\addplot+[samples=201] {fuzzygaussian(6.5,0.7)};
\addplot+[] {fuzzygenbell(4,-2,0.6)};
\addplot+[very thick] {fuzzysigmoid(1.5,4)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks, this is of big help. I can notice, however, that the sigmoid function is not "smooth", but it seems composed of segments ... Can it be fixed or Is just an impression ? – dario Oct 22 '14 at 12:55
  • EDIT: \addplot+ accept "smooth" option. Thank you again – dario Oct 22 '14 at 12:59
  • @dario Increase the sample points instead. Smooth is not a nice way to go. See the gaussian options. – percusse Oct 22 '14 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.