3

I would like to plot a bump function in a similar way as its done in Loring W. Tu's Book 'An introduction to Manifolds' (page 129, fig. 13.4), however it never quite works the way I want. Here is my MWE:

\documentclass[border=10pt]{standalone} 

\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{%
every x tick/.style={black, thick},
every y tick/.style={black, thick},
every tick label/.append style = {font=\footnotesize},
every axis label/.append style = {font=\footnotesize},
compat=1.12
  }
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-1.2, xmax=2, ymin=-0.7, ymax=1.2,

 xtick = {-1,0,1}, ytick = { 1},
 scale=0.4, restrict y to domain=-1.5:1.2,
 axis x line=center, axis y line= center,
 samples=40]


 \addplot[black, samples=100, smooth, domain=-1.2:0, thick]
   plot (\x, { 0 });

 \addplot[black, samples=100, smooth, domain=0:1, thick, label={x}]
   plot (\x, { exp( -1/\x)/(exp (-1/\x)+exp(1/(\x-1))) });


 \addplot[black, thick, samples=100, smooth, domain=1:2]
   plot (\x, {1} );

 \end{axis}
 \end{tikzpicture}

  \end{document}

My main problem with this result is, that the "plateau" is already attained before x=1, which really doesn't look like it's right. Changing sample sizes to higher than 100 will immediately yield dimension errors. Any tips?

4

Welcome to TeX.SE! I do not have that book but often people use tanh for that.

\documentclass[border=10pt]{standalone} 

\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{%
every x tick/.style={black, thick},
every y tick/.style={black, thick},
every tick label/.append style = {font=\footnotesize},
every axis label/.append style = {font=\footnotesize},
compat=1.12
  }
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-1.2, xmax=2, ymin=-0.7, ymax=1.2,
 xtick = {-1,0,1}, ytick = { 1},
 scale=0.4, restrict y to domain=-1.5:1.2,
 axis x line=center, axis y line= center,
 samples=40]


 \addplot[black, samples=100, smooth, domain=-1.2:2, thick]
   plot (\x, {0.5*(1+tanh(5*(\x-0.5)))});
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Of course, you can vary the width of the step by playing with the prefactor, which is 5 above.

\documentclass[border=10pt,tikz]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{%
every x tick/.style={black, thick},
every y tick/.style={black, thick},
every tick label/.append style = {font=\footnotesize},
every axis label/.append style = {font=\footnotesize},
compat=1.12
  }
\begin{document}
\foreach \X in {2,2.2,...,6,5.8,5.6,...,2.2}
{\begin{tikzpicture}
\begin{axis}[xmin=-1.2, xmax=2, ymin=-0.7, ymax=1.2,
 xtick = {-1,0,1}, ytick = { 1},
 scale=0.4, restrict y to domain=-1.5:1.2,
 axis x line=center, axis y line= center,
 samples=40,
 title={$f(x)=\left[1+\tanh\bigl(
 \pgfmathprintnumber[precision=1,fixed,zerofill]{\X}(x-1/2)\bigr)\right]/2$}]

 \addplot[black, samples=100, smooth, domain=-1.2:2, thick]
   plot (\x, {0.5*(1+tanh(\X*(\x-0.5)))});
\end{axis}
\end{tikzpicture}}
\end{document}

enter image description here

  • great answer, exactly what I needed thanks a lot! – rhodelta Feb 23 at 16:23
  • 1
    @ArtificialStupidity Well, fixed it, but do you think that is important here? – user121799 Feb 23 at 16:29
  • 1
    Yes. It is important for me. :-) Thank you for fixing! – Money Oriented Programmer Feb 23 at 16:59
2

The plots in proferred answers do not look like what I understand to be a bump function; rather, the plots of the derivatives of the indicated functions would be bump functions. The following directly produces a bump function plot, with support the interval $[-1,1]$:

\documentclass[border=10pt]{standalone} 

\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{%
  every x tick/.style={black, thin},
  every y tick/.style={black, thick},
  every tick label/.append style = {font=\footnotesize},
  every axis label/.append style = {font=\footnotesize},
  compat=1.12
 }
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-1.2, xmax=1.2, ymin=-0.2, ymax=1.2,
  xtick = {-1,0,1}, ytick = { 1},
  scale=0.4, restrict y to domain=-0.2:1.2,
  axis x line=center, axis y line= center,
  samples=40]
\addplot[black, samples=100, smooth, domain=-1.2:-1, thick]
   plot (\x, { 0 });
\addplot[black, samples=100, smooth, domain=-1:1, thick, label={x}]
   plot (\x, {exp(1-1/(1-x^2)});
\addplot[black, thick, samples=100, smooth, domain=1:1.2]
   plot (\x, {0} );
\end{axis}
\end{tikzpicture}

\end{document}

Bump function

(I'm unsure how to avoid the apparent gap in the graph immediately to the right of $x=-1$.)

  • The gap will disappear once you plot \addplot[black, samples=101, smooth, domain=-1.2:1.2, thick, label={x}] plot (\x, {ifthenelse(abs(x)>1,0,exp(1-1/(1-x^2))}); instead of three plots. – user121799 Feb 23 at 17:15
  • @marmot: but why does the middle of the 3-part plot code fail to fill in just to the right of -1, even if I use samples=101 for that portion? Is this just due to a rounding error? – murray Feb 24 at 22:44
  • No, the function is not defined there. LaTeX does not take limits. So you ask it to evaluate exp(1-1/(1-x^2)) at x=-1 and when LaTeX parses this, it just sees an infinity resulting from -1/(1-x^2) while it does not care whether or not the infinity means that the exponential will vanish. As a consequence, it drops this coordinate, and starts the plot at the next point. If you draw everything in one stretch, it will still drop these coordinates, but then connect two "well-behaved" points, so that there won't be a gap. – user121799 Feb 24 at 22:57

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.