我尝试使用 pgfplots 在域 (0,1]; 上绘制 sin(1/x) 的图形,它似乎有效,但使用 TikZ 绘图函数,却不行。我做错了什么?
我的代码:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0.01, xmax=1,
xticklabel style={/pgf/number format/.cd,fixed},
width=6cm, height=4cm,
grid = major,
grid style={gray!30},
ymin=-1,
ymax= 1,
axis background/.style={fill=white},
ylabel={$\sin (1/x)$},
xlabel={$x$},
tick align=outside
]
\addplot[domain=0.01:1, red, thick,samples=5000] {sin(deg(1/(x)))};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\draw[step=.5,lightgray,ultra thin] (-0.5,-1.5) grid (1.5,1.5);
\draw[->] (0,0) -- (1.1,0) node[right] {$x$};
\draw[->] (0,-1) -- (0,1.1) node[above] {$\sin (1/x)$};
\draw[blue, domain=0.01:1] plot (\x, {sin(1/ (\x r))});
\end{tikzpicture}
\end{document}
输出:
答案1
第一个问题:弧度和度数之间的转换不正确。
第二个问题:默认情况下plot
使用100个样本。
解决方案:
\draw[blue,domain=0.01:1,samples=5000] plot (\x, {sin((1/\x)r)});
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=5cm]
\draw[xstep=.2,ystep=.5,lightgray,ultra thin] (-0.1,-1.5) grid (1.1,1.5);
\draw[->] (0,0) -- (1.1,0) node[right] {$x$};
\draw[->] (0,-1) -- (0,1.1) node[above] {$\sin (1/x)$};
\draw[blue,domain=0.01:1,samples=5000] plot (\x, {sin((1/\x)r)});
\end{tikzpicture}
\end{document}