# Plotting a bump function

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]

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))) });

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?

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]

plot (\x, {0.5*(1+tanh(5*(\x-0.5)))});
\end{axis}
\end{tikzpicture}
\end{document}


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$}]

plot (\x, {0.5*(1+tanh(\X*(\x-0.5)))});
\end{axis}
\end{tikzpicture}}
\end{document}


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

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]
plot (\x, { 0 });
\addplot[black, samples=100, smooth, domain=-1:1, thick, label={x}]
plot (\x, {exp(1-1/(1-x^2)});

(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