Tikz 绘图精度

Tikz 绘图精度

你好,我想使用 tikz 绘制以下函数 $3-x\cdot\frac{e^x+1}{e^x-1}$。

这是我的代码

\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}

下面是输出

在此处输入图片描述

显然这不是正确的图表,我相信问题出在精度上。我该如何提高图表的精度?看来简单的调整samples并不能解决问题。

谢谢!

答案1

我不太确定我理解了为什么正负 2.5 处的点被标记为正负 5,但以下内容至少没有dimension too large错误。只需打开fpu该图即可。

\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}

在此处输入图片描述

请注意,如果您忽略 LaTeX 中的错误消息,则无法获得合理的输出。(虽然我知道 overleaf 擅长隐藏错误,但我不支持这种做法。)无论如何,我建议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};
        \addplot[color=gray,samples=2] {1} 
            node[pos=1,sloped,above left]{$y=1$};
        \addplot[color=red] {1-x*x/6}
            node[pos=1,sloped,below left] {$y=1-\tfrac16x^2$};
        \addplot[color=blue] {3-x*(exp(x)+1)/(exp(x)-1)}
            node[pos=1,sloped,above left] {$y=f(x)$};
 \end{axis}         
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果你想使用奇数个样本(这通常是对称图的不错选择),你需要CFG 指出,得出在处的函数的解析延拓x=0

相关内容