Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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