我怎样才能正确地绘制10^9*ln(1+10^{-9}/(x+2))
从-1
到的图1
?
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=0.7]
\begin{axis}[
xmin=-2.1,
xmax=1.39,
ymin=-1.25,
ymax=1.25,
unit vector ratio={1 1},
axis lines=center,
%axis equal image,
xlabel={$x$},
xlabel style={anchor=west},
ylabel={$f(x)$},
ylabel style={anchor=south},
samples=200,
]
%
\addplot[
color=blue,
domain=-1:1,
] {1/(x+2)};
\addplot[
color=red,
domain=-1:1,
] {10^9*ln(1+1/(10^9*(x+2))};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
正如 caverac 指出的那样他的回答这很可能是因为 (La)TeX 的计算限制。除了使用 gnuplot 作为计算引擎外,您还可以使用 Lua,方法是添加compat
1.12 或更高级别,并使用 LuaLaTeX 进行编译,以获得正确/所需的结果。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% use this `compat' level or higher to make use of Lua for the computations
% (in case it is used LuaLaTeX as engine)
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2.1,
xmax=1.39,
ymin=-1.25,
ymax=1.25,
unit vector ratio={1 1},
axis lines=center,
%axis equal image,
xlabel={$x$},
xlabel style={anchor=west},
ylabel={$f(x)$},
ylabel style={anchor=south},
samples=51, % (decreased number of samples)
domain=-1:1, % (moved common `\addplot' option here)
no markers, % (you used the default colors, thus adding this options
% gives the same result)
]
% (removed optional `\addplot' arguments)
\addplot {1/(x+2)};
% (slightly rewritten formula (and balanced braces))
\addplot {1e9 * ln(1 + 1e-9/(x+2))};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
我认为问题在于精度,您可以gnuplot
使用更精确的数值引擎
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[scale=0.7]
\begin{axis}[
xmin=-2.1,
xmax=1.39,
ymin=-1.25,
ymax=1.25,
unit vector ratio={1 1},
axis lines=center,
%axis equal image,
xlabel={$x$},
xlabel style={anchor=west},
ylabel={$f(x)$},
ylabel style={anchor=south},
samples=200,
]
%
\addplot[
color=blue,
domain=-1:1,
] {1/(x+2)};
\addplot gnuplot[
color=red,
domain=-1:1,
] {10^9*log(1+10^(-9)/(x+2))};
\end{axis}
\end{tikzpicture}
\end{document}