[Tex/LaTex] latex code for graphing rational function

graphicspgfplotsplottikz-pgf

I've seen in TikZ and pgf Manual for version 1.18 a code for graphing some functions but it does not provide an example for a rational function.

Can someone help me graph $x+\frac{1}{x}$?

Best Answer

Here is a quick adaptation from example from p. 225 (section 19.5) of pgfmanual (for version 2.10). Notice that due to a singularity at zero I gave the formula twice; I do not know whether this can be avoided. (Well, it can, by giving the domain and the number of samples so that no sample is taken at zero, but this would be far from elegant!)

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw[very thin,color=gray] (-3.1,-4.1) grid (3.9,3.9);
  \draw[->] (-3.2,0) -- (4.2,0) node[right] {$x$};
  \draw[->] (0,-4.2) -- (0,4.2) node[above] {$y$};
  \draw[color=orange,domain=-3:-0.3] plot (\x,{\x+1/\x});
  \draw[color=orange,domain=0.3:3] plot (\x,{\x+1/\x}) node[right] {$y = x+\frac{1}{x}$};
\end{tikzpicture}

\end{document}