3

I'm trying to draw in tikz the graph of f(x)=x*cos(1/x) and I'm using the following code:

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-0.45754230805133705,0.0) -- (0.6210968701809529,0.0);
\foreach \x in {-0.4,-0.30000000000000004,-0.20000000000000004,-0.10000000000000003,0.19999999999999998,0.3,0.4,0.5,0.6}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[->,color=black] (0.0,-0.340872843559345) -- (0.0,0.38634286875659907);
\foreach \y in {-0.30000000000000004,-0.20000000000000004,-0.10000000000000003,0.19999999999999998,0.3}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-0.45754230805133705,-0.340872843559345) rectangle (0.6210968701809529,0.38634286875659907);
\draw[smooth,samples=100,domain=-0.45754230805133705:0.6210968701809529] plot(\x,{(\x)*\sigma\upsilon\nu((1.0/(\x))*180/pi)});
\end{tikzpicture}
\end{document}

However it does not compile. I guess that the problem is that the function is not defined at zero. The code above was given to me by Geogebra. Any ideas? I would like to have the complete code!

Jesse
  • 29,686
Tolaso
  • 165

2 Answers2

8

I'd simply do the plot using the pgfplots package, which builds on pgf but is a more 'natural' interface for plotting:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[samples = 1000]
    \addplot[mark = none] {x * cos ( 180 / ( x / pi ) ) };
  \end{axis}
\end{tikzpicture}
\end{document}

I needed to up the samples here otherwise there is not enough detail: I'm not quite sure what part of the plot you are actually after!

(Note that the pgf maths system expects angles in degrees, so as in the auto-generated code in the question I've had to convert from a value of x in radians.)

Moving the axes from a 'box' (common scientific plot style) to central (more common for plotting formulae), and 'zooming in' can be achieved relatively easily

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[axis lines = middle, domain = -1:1, samples = 1000]
    \addplot[mark = none] {x * cos ( 180 / ( x / pi ) ) };
  \end{axis}
\end{tikzpicture}
\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • First of all, thank you... But , again, it does not compile. It doesn't identify that \pgfplotsset{compat=1.11} – Tolaso Sep 22 '14 at 08:07
  • 2
    Since PGFPlots version 1.11, you can also set trig format plots=rad to avoid the need for the conversion of the angles. – Jake Sep 22 '14 at 08:08
  • @Tolaso I've got an up-to-date TeX system with the latest release of pgfplots. If you have an older version then it won't know 1.11: try commenting out that line. – Joseph Wright Sep 22 '14 at 08:08
  • By, the way I just compiled it without that.. It's ok! However, it would be better for me without the frame , and that the axis (both of them) were shown and that I had some more zoom. – Tolaso Sep 22 '14 at 08:09
  • @Jake Not in my opinion a good plan: either an FPU uses degrees or it uses radians, and the input should reflect that. – Joseph Wright Sep 22 '14 at 08:09
  • @Tolaso As I've tried to indicate, the auto-generated code you've got is not really that easy to follow (as well as not working!) so I just went with some easy-to-input defaults. I can soon move the axes: I'm not sure how much 'zoom' you want! – Joseph Wright Sep 22 '14 at 08:10
  • @JosephWright The zoom I desire is to be as good as to be seen in a book, so that the reader can follow the graph. – Tolaso Sep 22 '14 at 08:12
  • 2
    @JosephWright: I don't see why the user interface should reflect an implementation detail like that. Most graphing calculators allow you to switch between radians and degrees, so I think it's only appropriate that a graphing system like PGFPlots does the same. – Jake Sep 22 '14 at 08:16
  • @JosephWright All right... Thanks a lot... That will do! It's perfect! – Tolaso Sep 22 '14 at 08:30
  • @JosephWright: In the second document, it's a good idea to also set domain = -1:1 in addition to xmin = -1, xmax = 1. Otherwise, 80 % of the samples are only computed but not shown in the plot, because the default domain runs from -5 to 5. – Jake Sep 22 '14 at 08:58
  • @Jake Thanks for the point about the domain: will alter. On the radians/degrees business, I guess this come down to the fact that I'm a LaTeX kernel programmer. As such, I tend to think that an interface should be well defined and predictable. A function such as cos here either takes degrees or takes radians, as defined in its documentation. Having this variable in use seems pretty risky to me: what is the defined semantics of the function? – Joseph Wright Sep 22 '14 at 10:06
4

A just-for-fun solution with PSTricks.

\documentclass[pstricks,border=20pt,12pt,dvipsnames]{standalone}
\usepackage{pst-plot}

\def\f{x*cos(1/x)}

\begin{document}

\begin{psgraph}[algebraic,Dx=0.025,Dy=0.01,plotpoints=1000]{->}(0,0)(-.1,-.1)(.1,.1){15cm}{!}
    \psplot[linecolor=Red]{-.1}{-0.005}{\f}
    \psplot[linecolor=Red]{0.005}{.1}{\f}
\end{psgraph}
\end{document}

enter image description here

Zoomed-in animation

\documentclass[pstricks,border=20pt,12pt,dvipsnames]{standalone}
\usepackage{pst-plot}
\usepackage[nomessages]{fp}

\def\f{x*cos(1/x)}

\begin{document}
\foreach \i in {1.5,1.4,...,0.1}{%
\FPeval\D{round(\i/5:2)}%
\begin{psgraph}[algebraic,Dx=\D,Dy=\D,plotpoints=1000]{->}(0,0)(-\i,-\i)(\i,\i){20cm}{!}
    \psplot[linecolor=Red]{-\i}{\D\space 10 div neg}{\f}
    \psplot[linecolor=Red]{\D\space 10 div}{\i}{\f}
\end{psgraph}}
\end{document}

enter image description here