3

When i plot the graph of $(x^2+x+1)/(x+1)$, there appears to be a solid vertical line in the plot which happens to be the vertical asymptote, is there a way to make that line dotted and also include the oblique asymptote in a similar fashion?

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines = center,
    xlabel = $x$,
    ylabel = {$y$},
    xmax = {5},
    xmin = {-5},
    ymax = {5},
    ymin = {-5},
    legend pos = outer north east
]
\addplot [
    domain=-10:10, 
    samples=100, 
    color=black,
]
{(x^2+x+1)/(x+1)};
\addlegendentry{2 turning points}
\end{axis}
\end{tikzpicture}
\end{document}
6
  • Do you want only the vertical line as dotted? Or the whole plot? – Alenanno Feb 7 '16 at 12:15
  • I would like to have the vertical line dotted, and also include the dotted oblique asymptote, thank you. – Tay Yong Qiang Feb 7 '16 at 12:25
  • Do you mean like this? – Alenanno Feb 7 '16 at 12:26
  • No, the curve should be solid, while the vertical asymptote dotted or dashed, also include a dotted/ dashed diagonal line y=x-1 as the oblique asymptote. How can i do that? – Tay Yong Qiang Feb 7 '16 at 12:32
  • The black line currently appearing is not really the asymptote, but the program simply joining the last negative value to the first positive value (remove the ymax and ymin lines and see for yourself. real asymptotes do not touch the curve). The program does not know that this function has a corresponding asymptote. You may have to manually calculate the x value at which the function switches from -infinity to +infinity and plot the graph in two separate pieces and then add an asymptote separately. – AJN Feb 7 '16 at 12:35
2

You can use restrict y to domain=-10:10 to remove any datapoints outside this range, and thus get rid of the vertical asymptote as part of the main plot. Also, I took the liberty to reduce the function domain to -5:5 (same values as xmin and xmax).

To plot the oblique asymptote, add another plot with the function {x}.

To plot the vertical asymptote you can make use of the axis' relative coordinate system, so that the asymptote takes up the full height of the plot even if you decide to change the axis limits.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines = center,
    xlabel = $x$,
    ylabel = {$y$},
    xmax = {5},
    xmin = {-5},
    ymax = {5},
    ymin = {-5},
    restrict y to domain = -10:10,
    legend pos = outer north east
]
\addplot [
    domain=-5:5,
    samples=100,
    color=black,
]
{(x^2+x+1)/(x+1)};
\addlegendentry{2 turning points}

% Oblique asymptote at y=x
\addplot[dashed] {x};
% Vertical asymptote at x=-1
\draw[dashed] ({axis cs:-1,0}|-{rel axis cs:0,0}) -- ({axis cs:-1,0}|-{rel axis cs:0,1});
\end{axis}
\end{tikzpicture}
\end{document}

Output

7
  • Thanks for answer, it really helped! But why did you restrict the domain though, i don't see how it affects the plot. – Tay Yong Qiang Feb 7 '16 at 13:05
  • @TayYongQiang To remove the solid line of the function at x=-1. Try without this restriction to see the difference. – sodd Feb 7 '16 at 13:06
  • Could you briefly explain how this line works? Say i now want the asymptote to be at x=\pi, how should i go about editing this line: \draw[dashed] ({axis cs:-1,0}|-{rel axis cs:0,0}) -- ({axis cs:-1,0}|-{rel axis cs:0,1}) ? – Tay Yong Qiang Feb 7 '16 at 13:22
  • 1
    axis cs means the axis coordinate system, i.e. -1,0 is the point (-1,0) in the plot. rel axis cs is the relative axis coordinate system, where (0,0) is the bottom left corner and (1,1) is the upper right corner. To have the asymptote at x=π, simply replace {axis cs:-1,0} with {axis cs:pi,0}. Also, see section 4.17.1 of the pgfplots manual for more details about the coordinate systems. – sodd Feb 7 '16 at 13:31
  • @TayYongQiang Did this solve your problem? – sodd Feb 7 '16 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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