# latex code for graphing rational function

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}$?

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}


Same as mbork's answer but with pgfplots, just for academic purposes.

\documentclass{article}

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
grid=major,
xmin=-3.1,xmax=3.9,
ymin=-4.1,ymax=3.9,
restrict y to domain=-4:4,
%unbounded coords=jump,
xlabel=$x$,
ylabel=$y$,
clip=false]
\addplot[color=orange,domain=-3:3,samples=100] {x+1/x} node[right] {$y = x+\frac{1}{x}$};
\end{axis}
\end{tikzpicture}

\end{document} 