# Tikz plot precision

Hi I would like to plot the following function $3-x\cdot\frac{e^x+1}{e^x-1}$ using tikz.

Here is my code

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}

\begin{document}
\centering
\begin{tikzpicture}
\draw[->] (-3,0) -- (3,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\draw[dotted] (-2.5,-2) -- (-2.5,2);
\draw (-2.5,0) node[below]{-5};
\draw[dotted] (2.5,-2) -- (2.5,2);
\draw (2.5,0) node[below]{5};
\draw[gray] (2.6,0.5) node[right]{$y=1$};
\draw[red] (2.6,-1.6) node[right]{$y=1-\tfrac{1}{6}x^2$};
\draw[blue] (2.6,-1) node[right]{$y=f(x)$};
\draw[scale=0.5,domain=-5:5,smooth,variable=\x,gray] plot ({\x},{1});
\draw[scale=0.5,domain=-5:5,smooth,variable=\x,red] plot ({\x},{1-\x*\x/6});
\draw[scale=0.5,domain=-10:10,samples=150,smooth,variable=\x,blue,] plot ({\x},{3-\x*(exp(\x)+1)/(exp(\x)-1)});
\end{tikzpicture}
\end{document}


And here is the output

Obviously this is not the correct graph and I believe the problem is due to the precision. How can I improve the presion of the plot? It seems that simply adjust samples does not resolve the problem.

Thanks!

• Welcome to TSE. Your function is defined at x=0?
– user108724
Commented Apr 2, 2020 at 19:42
• When one repairs your code by adding the missing semicolons, it gives (as expected) dimension too large errors. You may want to use pgfplots.
– user194703
Commented Apr 2, 2020 at 19:51
• Why you need the rest of graph outside the dots?
– user108724
Commented Apr 2, 2020 at 20:03
• No apparently it's not defined at x=0, but that does not seem to be a problem here.... I'm making this graph to show the behavior of this function near zero but I would also like to show its asymptotic behavior. Commented Apr 2, 2020 at 20:15
• Thanks a lot for the comments! I used pgfplots and it worked. So this cannot be done using simply the plot function? Commented Apr 2, 2020 at 20:16

I am not quite sure I understand why the points at plus or minus 2.5 are labeled plus or minus 5, but the following at least has no dimension too large errors. Just switch on fpu for that plot.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fpu}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\draw[->] (-3,0) -- (3,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\draw[dotted] (-2.5,-2) -- (-2.5,2);
\draw (-2.5,0) node[below]{-5};
\draw[dotted] (2.5,-2) -- (2.5,2);
\draw (2.5,0) node[below]{5};
\draw[gray] (2.6,0.5) node[right]{$y=1$};
\draw[red] (2.6,-1.6) node[right]{$y=1-\tfrac16x^2$};
\draw[blue] (2.6,-1) node[right]{$y=f(x)$};
\draw[scale=0.5,domain=-5:5,smooth,variable=\x,gray]
plot ({\x},{1});
\draw[scale=0.5,domain=-5:5,smooth,variable=\x,red]
plot ({\x},{1-\x*\x/6});
\begin{scope}
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\draw[scale=0.5,domain=-10:10,samples=150,smooth,variable=\x,blue]
plot ({\x},{3-\x*(exp(\x)+1)/(exp(\x)-1)});
\end{scope}
\end{tikzpicture}
\end{document}


Please note that if you ignore error messages in LaTeX, you cannot expect to get reasonable output. (While I am aware of the fact that overleaf is good at hiding errors, but I do not support this practice.) Anyway, I'd recommend pgfplots.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,xlabel=$x$,ylabel=$y$,
domain=-5:5,ymax=2,ymin=-3,xtick=\empty,ytick=\empty,axis equal,
smooth,samples=150,clip=false]
\draw[dotted] (-2.5,-2) -- (-2.5,2);
\draw (-2.5,0) node[below]{-5};
\draw[dotted] (2.5,-2) -- (2.5,2);
\draw (2.5,0) node[below]{5};
node[pos=1,sloped,above left]{$y=1$};
node[pos=1,sloped,below left] {$y=1-\tfrac16x^2$};
node[pos=1,sloped,above left] {$y=f(x)$};

If you want to use an odd number of samples, which usually is a good choice for symmetric plots, you need to, as pointed out by C.F.G, come up with an analytic continuation of the function at x=0.
• Thanks a lot! The labels are because I'm using a scale of 0.5... I agree that using pgfplots seems easier. Commented Apr 2, 2020 at 20:30