我正在尝试使用 PGFPlots 绘制函数图形
f(x)=\exp(-\frac{x^2}{2})(x^2-1)
我的代码是
\begin{tikzpicture}
\begin{axis}[xmin = -8, xmax = 8,
ymin = -2, ymax = 3,xtick distance = 2.5,
ytick distance = 0.5,
grid = both,
minor tick num = 1,
major grid style = {lightgray},
minor grid style = {lightgray!25},
width = \textwidth,
height = 0.5\textwidth,
]
\addplot[smooth,
thick,
blue,] exp(-x^2/2)(x^2-1);
\end{axis}
\end{tikzpicture}
我得到的是
代替
请帮我...
答案1
实际上,我得到的是一个错误。绘制 x 函数时,你应该这样做
\addplot {<function>};
或者
\addplot [<option>] {<function>};
如果您要向情节添加任何选项。您已经完成了,而\addplot [...] <function>;
没有{}
。
此外,请注意,乘法必须用 明确指定*
,因此您需要
\addplot[smooth, thick,blue,] {exp(-x^2/2)*(x^2-1)};
看看 Overleaf 的截图,我怀疑你只是忘了*
。有了上面的内容\addplot
,我得到了
默认情况下,绘制的范围是-5:5
,这就是图形停止的位置。如果要将函数绘制到您指定的轴限值,请添加domain=-8:8
到axis
选项或选项。\addplot
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin = -8, xmax = 8,
ymin = -2, ymax = 3,xtick distance = 2.5,
ytick distance = 0.5,
grid = both,
minor tick num = 1,
major grid style = {lightgray},
minor grid style = {lightgray!25},
width = \textwidth,
height = 0.5\textwidth,
]
\addplot[smooth,
thick,
blue,] {exp(-x^2/2)*(x^2-1)};
\end{axis}
\end{tikzpicture}
\end{document}