# How can I plot sin(1/x) with a logarithmic x-axis?

I would like to plot the function f(x) = sin(1/x) and it should look like this. I get this so far:

You might notice, that my image doesn't look as good as the linked one because of the axis.

How can I get a logarithmic axis for my plot?

Why does it currently produce an error with a logarithmic axis?

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}

\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning, calc, intersections}

\begin{document}

\begin{preview}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
width=15cm, height=15cm,     % size of the image
grid = major,
grid style={dashed, gray!30},
%xmode=log,log basis x=10, % <- doesn't work:
% ! Missing number, treated as zero.
%                    p
% l.33 ..., red, thick,samples=500] {sin(deg(1/x))};
%
% ?
% ! Emergency stop.
%                    p
% l.33 ..., red, thick,samples=500] {sin(deg(1/x))};
%
% !  ==> Fatal error occurred, no output PDF file produced!
%ymode=log,log basis y=10,
xmin=-0.7000, % start the diagram at this x-coordinate
xmax=-0.0062, % end   the diagram at this x-coordinate
ymin=-1,      % start the diagram at this y-coordinate
ymax= 1,      % end   the diagram at this y-coordinate
axis background/.style={fill=white},
ylabel=y,
xlabel=x,
%xticklabels={,,},
%yticklabels={,,},
tick align=outside,
tension=0.08]
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}

If you plot it with a logarithmic y-axis it looks ... hmm ... lets call it interesting.

The latest LaTeX-Code of this plot can be found on GitHub.

-
The y-axis has negative numbers, you cant take the log of negative numbers. –  zeroth Sep 18 '12 at 13:33
Thanks! (argh ... I should have thought of that) –  moose Sep 18 '12 at 14:04

The problem is that the logarithm isn't real for negative numbers, so you'll have to use some trickery: Plot the function for the positive domain, but flip the direction of the x axis and the sign of the tick labels:

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}

\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

\begin{document}

\begin{preview}
\begin{tikzpicture}
\begin{axis}[
xmode=log, % Logarithmic x axis
xmin=0.01, xmax=1, % Positive domain...
x dir=reverse, % ... but increasing to the left
xticklabel=\pgfmathparse{-exp(\tick)}\pgfmathprintnumber{\pgfmathresult}, % Reverse the sign of the labels
xticklabel style={/pgf/number format/.cd,fixed}, % Use fixed point notation
width=15cm, height=8cm,     % size of the image
grid = major,
grid style={gray!30},
ymin=-1,      % start the diagram at this y-coordinate
ymax= 1,      % end   the diagram at this y-coordinate
axis background/.style={fill=white},
ylabel=y,
xlabel=x,
tick align=outside
]
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}
-

run with xelatex

\documentclass{article}
\usepackage{pst-plot}
\pagestyle{empty}
\begin{document}

\psset{xunit=10,yunit=3}
\begin{pspicture}(0.05,-1.1)(-1.05,1.1)
\psaxes[axesstyle=frame,ticksize=0 5pt,Dx=0.2,Dy=0.5,ylabelPos=right]{->}(0,0)(0,-1.25)(-1.1,1.25)[$\log(x)$,180][$sin(1/\log(x))$,90]
\psplot[plotpoints=10000,linecolor=red,algebraic]{-0.1}{-0.001}{sin(1/(log(1-x)))}
\psplot[plotpoints=1000,linecolor=red,algebraic]{-0.99}{-0.1}{sin(1/(log(1-x)))}
\end{pspicture}

\end{document}

-
I was about to say "Wow! PSTricks is so powerful that it can take logs of negative numbers!" Then I looked at the actual function that you're plotting ... –  Loop Space Sep 18 '12 at 14:45