[Tex/LaTex] Graphing a rational function and a vertical line using pgfplots

tikz-pgf

I would like the code that instructs TikZ to plot the graph for the rational function y=(x^{2}+5x+6)/(x^{2}+2x-3). I tried using addplot[very thin,blue]{frac(x^{2}+5x+6)/(x^{2}+2x-3)}. This graph has a vertical asymptote x = 1. What is the code for plotting this line – as a dashed line – with arrowheads?

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}


\begin{document}

\hspace*{\fill}
\begin{tikzpicture}
\begin{axis}[axis equal image,
          xmax=8,ymax=7,
          axis lines=middle,
          restrict y to domain=-7:7,
          enlargelimits={abs=1cm},
          axis line style={latex-latex},
          ticklabel style={fill=white},
          ytick=\empty,
          xtick={-3}
          %xlabel=$x$,ylabel=$y$,
]
\addplot[domain=-10:10,mark=none,samples=10] {frac{x + 2}{x - 1} node [above left, yshift=3pt]{$\scriptstyle{y}=\frac{x^{2}+5x+6}{x^{2}+2x-3}$};
\draw [fill=white] (-3,0) circle [radius=1.5pt];
\end{axis}
\end{tikzpicture}
\hspace{\fill}

\end{document}

Best Answer

With my version of PGFplots, the \addplot command you provide doesn't even compile, so I had to guess what you want to achieve. In PGFplots you can just write fractions like this: 1/(x^2).

With Matthew's comment to Jake's answer to this question you can drawn an asymptote at x=1 by using the key vasymptote=1. In order to reliably truncate the plot without using many samples (orange curve), you can plot the two parts separately, specifying the domain so that is ends slightly before x=1 (black curve).

Some curve

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\pgfplotsset{vasymptote/.style={
    before end axis/.append code={
        \draw[densely dashed] ({rel axis cs:0,0} -| {axis cs:#1,0})
        -- ({rel axis cs:0,1} -| {axis cs:#1,0});
    }
}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis equal image,
    axis lines=middle,
    xmin=-10,xmax=10,
    ymin=-10,ymax=10,
    enlargelimits={abs=1cm},
    axis line style={latex-latex},
    ticklabel style={fill=white},
    ytick={-10,10},
    xtick={-3,-10,10},
    vasymptote=1,
]
% This doesn't clip to y=-10:10 nicely
% because there are too few samples near the asymptote:
\addplot[very thick, orange, domain=-10:10,samples=200, restrict y to domain=-10:10]
    {(x^2+5*x+6)/(x^2+2*x-3)};

% Draw the two parts separately with individual domains:
\addplot[samples=50,domain=-10:1-0.27] {(x^2+5*x+6)/(x^2+2*x-3)};
\addplot[samples=50,domain=1+0.33:10]  {(x^2+5*x+6)/(x^2+2*x-3)};

\draw [fill=white] (-3,0) circle [radius=1.5pt]; % What is this?
\end{axis}
\end{tikzpicture}
\end{document}
Related Question