6

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

  • 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)});. – Paul Gaborit Sep 12 '13 at 5:49
  • Ah...stupidity strikes again...imagine me ashamed and grateful for the answer! – Mark Sep 12 '13 at 6:22
5

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}
  • For the gap between the axis and the blue curve, a cheap cheat is (\x-0.01,...). – percusse Sep 12 '13 at 13:54
  • 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 '18 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.