# Graphing a rational function and a vertical line using pgfplots

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}$};
\end{axis}
\end{tikzpicture}
\hspace{\fill}

\end{document}

• There has to be a better code for plotting this rational function. I do not know most of the commands in the code that you provided: double y domain/.code 2 args={} and ejes/.style args={}. That code was given years ago. There must be an updated version to pgfplots. – user143462 Sep 23 '14 at 16:31
• Just a remark: As far as I know, adding arrows on both ends of an axis is mathematically wrong. Normally you only add an arrow on the end which points in the positive direction (upwards and to the right, in your plot). – Fritz Sep 23 '14 at 17:07
• It is not wrong to put arrowheads at both ends. In textbooks, I almost always see each axis drawn with arrowheads at both ends. In case arrowheads are not at both ends, there are no arrowheads drawn. – user143462 Sep 24 '14 at 14:50
• Okay, maybe the word "wrong" is too strong. However, I have never seen anyone put arrows on both ends, and in school and university I was taught that only the positive end should be marked in order to prevent confusion. Otherwise the arrows are pure decoration and convey no information at all. See also: math.stackexchange.com/questions/52515/… – Fritz Sep 24 '14 at 15:27

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).

\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:

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

• Now, I will reply to your code instead of our discussion on decorating the axes with arrowheads. I am not familiar with the \pgfplotsset{vasymptote/.style={} command. I would like to use the draw[dashed] command to draw a vertical asymptote, the equation of which is x = 1. I would also like to use the draw[dashed] command to draw a horizontal asymptote, the equation of which is y = 1. Unbelievably, I did not find the code in the manual for drawing lines. Oh, I would like arrowheads on these lines. I think it is something like draw[dashed,<->] but I got an error. – user143462 Sep 24 '14 at 16:03
• You asked me why I had the command \draw [fill=white] (-3,0) circle [radius=1.5pt]. The function has a removable discontinuity at -3. That is the reason for having xtick{-3} in the code. Can I have "-3" typeset in a smaller font such as scriptstyle? How do I label the axes? I would like to have "x" to be printed below and to the right of the right arrowhead, and I would like to have "y" to be printed above and to the right of the top arrowhead. – user143462 Sep 24 '14 at 16:12
• Ah, that makes sense. For the tick font size you can use tick style={font=\small} or similar. Otherwise, please have a look at the PGFplots manual. Just try entering texdoc pgfplots in a terminal, or enter the same term into a search engine of your choice. There is a whole chapter called "Axis Descriptions" and a section called "Axis Lines" which explains everything with nice pictures and examples. If you get stuck, simply ask another question. – Fritz Sep 24 '14 at 16:30

Mark Wibrow's solution will produce a permanent catastrophic when plotpoints=14*(2^n)+1 (for any non-negative integer n).

So the following solution can be used to overcome such an issue (just for fun).

\documentclass[pstricks,border=20pt,12pt]{standalone}
\usepackage{pst-plot}
\def\f{(x+2)/(x-1)}

\begin{document}
\begin{psgraph}[algebraic,plotpoints=1000,Dy=5,Dx=2]{->}(0,0)(-11,-30)(11,35){20cm}{20cm}
\psset{linecolor=red}
\psplot{-10}{0.9}{\f}
\psplot{1.1}{10}{\f}
\psset{linestyle=dashed,linecolor=blue}
\psline(1,-30)(1,35)
\psline(-11,1)(11,1)
\end{psgraph}
\end{document}


Just for fun, with PSTricks :O

\documentclass[pstricks,border=5]{standalone}
\usepackage{pst-plot}
\begin{document}
\psset{xunit=20pt,yunit=20pt}
\begin{pspicture}*(-7.5,-7.5)(7.5,7.5)
\psgrid[gridlabels=0,gridcolor=gray!25,subgridcolor=gray!10](-7,-7)(7,7)
\psaxes[labels=none,ticksize=-2pt 2pt]{<->}(0,0)(-7,-7)(7,7)[$x$,-90][$y$,0]
\psplot[linecolor=blue,plotstyle=line,algebraic, yMaxValue=7,plotpoints=1000]
{-7}{7}{(x^2+5*x+6)/(x^2+2*x-3)}
\psline[linestyle=dashed](1,-7)(1,7)
\rput(4.5,4){$\displaystyle f(x)=\frac{x^2+5x+6}{x^2+2x-3}$}
\end{pspicture}
\end{document}


• It will fail when plotpoints=14*(2^n)+1 for any positive integer n. – kiss my armpit Sep 23 '14 at 17:57
• Non negative integers n (I meant). – kiss my armpit Sep 23 '14 at 18:12
• Just out interest: is there some non-negative integer n for which 14*(2^n)+1=1000? – Mark Wibrow Sep 23 '14 at 19:00
• No. There is not such n. – kiss my armpit Sep 23 '14 at 19:04
• Good, I was so very concerned that when I used plotpoints=1000 in my answer the plot would fail and the function would not appear. Then I would not have an image to post. – Mark Wibrow Sep 23 '14 at 19:10

All you need are a couple of addplot commands:

  \addplot[domain=-10:10,samples=1000]
{(x^2+5*x+6)/(x^2+2*x-3)};
coordinates{(1,-6) (1,6)};


The first one should plot your real function, the second one draws a vertical line using the coordinates. the latex-latex option adds latex arrow heads to either end. If you don't want arrow heads on both ends, just remove the appropriate latex. If you want to the vertical line to be longer, just replace -6 and 6 in the coordinates with something bigger, eg. -15 and 15.

here's a full example:

\begin{tikzpicture}
\begin{axis}[axis equal image,
xmin=-8, xmax=8,
ymin=-7, ymax=7,
axis lines=middle,
restrict y to domain=-10:10,
enlargelimits={abs=1cm},
axis line style={-latex},
ytick=\empty,
xtick={-3},
%xlabel=$x$,ylabel=$y$,
]