Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to plot the function f(x) = 1/(x-2) + 3 using TikZ, however I get that the domain is undefined.

What code should I type?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

You can use pgfplots instead:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[axis lines=middle,samples=200]
\addplot[blue,domain=-3:1.85] {1/(x-2) +3 };
\addplot[blue,domain=2.15:6] {1/(x-2) + 3};
\draw[red!20,dashed] (axis cs:2,-4) -- (axis cs:2,10);
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Adding some more labels:

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{every tick label/.style={inner sep=0pt,font=\scriptsize}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  samples=200,
  xtick={-3,...,6},
  ytick={-3,...,9}
]
\addplot[blue,domain=-3:1.85] {1/(x-2) +3 };
\addplot[blue,domain=2.15:6] {1/(x-2) + 3};
\draw[red!20,dashed] (axis cs:2,-4) -- (axis cs:2,10);
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
thanks; how can I make the axis are numbered from $1$ to $8$? (not just even numbers) –  user10 Nov 6 '12 at 1:06
    
@user10 please see my updated answer. –  Gonzalo Medina Nov 6 '12 at 1:14

Whatever your problem was, split the function at x = 2.

In my answer I provide two ways: pgfplots and vanilla TikZ.

MWE

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ymin=-5,
    ymax=5,
    xmin=-10.1,
    xmax=10.1,
    axis on top=true,
    axis x line=middle,
    axis y line=middle,
    ]
    \addplot [forget plot, samples=100, domain=-10:1.99] {1/(x-2) + 3};
    \addplot [             samples=100, domain=2.01:10] {1/(x-2) + 3};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[scale=.3]
    \draw plot [
        samples=100,
        domain=-10:1.9
        ] (\x,{1/(\x-2) + 3});
    \draw plot [
        samples=100,
        domain=2.1:10
        ] (\x,{1/(\x-2) + 3});
\end{tikzpicture}
\end{document}

Output (pgfplots)

pgfplots

Output (vanilla TikZ)

vanilla TikZ

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.