13

I need to plot f(x) = 1 / x in LaTeX and am trying to with Tikz. I haven't found a way to set the vertical range, so the values go out of bound around x=0.

Using gnuplot directly doesn't seem to be an option because the fix presented here fails.

\begin{frame}
    \begin{tikzpicture}
        \draw[->] (-10, 0) -- (10, 0) node[right] {$x$};
        \draw[->] (0, -10) -- (0, 10) node[above] {$y$};
        \draw[color=red, domain=-10:10] plot[id=x] function{1/x} node[right] { $f(x) = x$ };
    \end{tikzpicture}
\end{frame}

I'm not set on Tikz, if there are other options I am open to them.

3 Answers 3

13

With TikZ version 2 :

\documentclass{article}    
\usepackage{tikz}

\begin{document}

      \begin{tikzpicture}[scale=.5] 
        \draw[->] (-10, 0) -- (10, 0) node[right] {$x$};
        \draw[->] (0, -10) -- (0, 10) node[above] {$y$};
        \draw[color=red, domain=-10:-0.1,samples=200] plot[id=x1] function{1/x};
        \draw[color=red, domain=0.1:10,samples=200] plot[id=x2] function{1/x} node[below right = 6pt] { $f(x) =1/x$ };
    \end{tikzpicture}

\end{document}

enter image description here

7
  • You can use gnuplot but not with gnuplottex. You can see the manual to see how to plot a function with gnuplot. After you can try pgfplots or tkz-fct. Commented Jun 16, 2011 at 16:37
  • doesn't work here: ! Number too big. \pgftemp@y. I'm using the beamer package Commented Jun 16, 2011 at 16:49
  • @Otto Allmendinger Altermundus example compiles on my system (TeX Live 2009). It also compiles if I change the documentclass to beamer.
    – N.N.
    Commented Jun 16, 2011 at 17:46
  • @Otto Allmendinger What is your version of pgfmath ? You can also modify the domains : -10:-0.1 and 0.1:10 Commented Jun 16, 2011 at 17:51
  • @Otto Allmendinger the last version is better I change the domains and remove the clip and put at the right place the name of the function. Commented Jun 16, 2011 at 18:00
15

The pgfplots package has a plot drawing function that takes the parameter restrict y to domain. It takes care of the clipping and doesn't connect the points on the left and right of the singularity.

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[
        restrict y to domain=-10:10,
        samples=1000,
        minor tick num=1,
        xmin = -10, xmax = 10,
        ymin = -10, ymax = 10,
        unbounded coords=jump,
        axis x line=middle,
        axis y line=middle]

      \addplot[mark=none, domain=-10:10] {(1-x)/(x^2-1)};
    \end{axis}
  \end{tikzpicture}

\end{document}

enter image description here

0

What I tend to do is to clip all the graphs to match the same dimensions given by coordinate axes or grid. In your case, adding a line

\clip (-10,-10) rectangle (10,10);

before the draw function would prevent the graph from being drawn any further than wanted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .