9

I try to draw the graph of sin(1/x) on domain (0,1]; using pgfplots, it seems to work, but with TikZ plot-function, it doesn't. What am I doing wrong?

My code:

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

The output:

The output

2
  • 4
    Your conversion between radian and degree is not correct. Use \draw[blue,domain=0.01:1,samples=5000] plot (\x, {sin((1/\x)r)});. Sep 12, 2013 at 5:49
  • Ah...stupidity strikes again...imagine me ashamed and grateful for the answer!
    – Mark
    Sep 12, 2013 at 6:22

1 Answer 1

10

First issue: your conversion between radian and degree is not correct.

Second issue: by default, plot uses 100 samples.

Solution:

\draw[blue,domain=0.01:1,samples=5000] plot (\x, {sin((1/\x)r)});

enter image description here

\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}
3
  • 2
    For the gap between the axis and the blue curve, a cheap cheat is (\x-0.01,...).
    – percusse
    Sep 12, 2013 at 13:54
  • 1
    an other cheap cheat is use \clip (0,-1) rectangle (1,1); to cut out some bad points outside [-1,1] strip.
    – Black Mild
    Oct 17, 2018 at 19:56
  • applying options line join=round and smooth to the last \draw may avoid bad points outside [-1,1] in some degree.
    – jiaopjie
    Dec 4, 2021 at 11:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .