Asymptotes in a plot

TikZ doesn't actually draw the asymptotes: it simply connects the two points immediately before and after the jump. Because you use so many samples, you don't see that the line is actually slightly slanted.

Two suggestions: For drawing the plots, I would use the PGFplots package, which extends the plotting capabilities of TikZ/PGF substantially. It allows you to filter out coordinates above or below a certain threshold, and interrupt the plot line at that point. For drawing the asymptotes, I would use a normal \draw command. If you use PGFplots, you can specify the line in terms of the "data coordinate system" and in terms of the "axis coordinate system", so you can draw a vertical line that runs from the bottom of the plot at a specified horizontal coordinate to the top of the plot.

I've defined a new style ejes=<xmin>:<xmax> <ymin>:<ymax> which sets all the required parameters and draws the axes, and a new command \vasymptote{<xpos>} which draws a vertical dashed line at the desired position. It takes an optional argument which will be passed to the line, so you can change the color or thickness or dash pattern as you please.

With the style and the command, you could recreate your first image using

\tikz{\begin{axis}[ejes=-4:4 -4:4, title={$y=f(x+1)=\dfrac{1}{x+1}$}]
    \addplot {1/(x+1)};
    \vasymptote {-1}
\end{axis}}

which will yield

Here's the complete code:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}

\newcommand{\vasymptote}[2][]{
    \draw [densely dashed,#1] ({rel axis cs:0,0} -| {axis cs:#2,0}) -- ({rel axis cs:0,1} -| {axis cs:#2,0});
}

\pgfplotsset{
    double y domain/.code 2 args={
        \pgfmathsetmacro\doubleymin{#1*2}
        \pgfmathsetmacro\doubleymax{#2*2}
    },
    ejes/.style args={#1:#2 #3:#4}{
        double y domain={#3}{#4},
        domain=#1:#2,
        ymin=#3,ymax=#4, restrict y to domain=\doubleymin:\doubleymax,
        samples=100,
        enlargelimits=false,
        axis lines=middle,
        xtick={#1,...,#2}, ytick={#3,...,#4},
        xticklabels=\empty, yticklabels=\empty,
        every axis plot post/.style={
            black,
            mark=none,
            smooth
        },
        scale only axis,
        width=4cm,
        height=4cm
    }
}


\tikz{\begin{axis}[ejes=-4:4 -4:4,title={$y=f(x+1)=\dfrac{1}{x+1}$}]
    \addplot {1/(x+1)};
    \vasymptote {-1}
\end{axis}}

\end{document}

With PSTricks.

enter image description here

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

\begin{document}
\begin{pspicture}(-4,-4)(2,4)   
    \psline[linecolor=gray,linestyle=dashed](-1,-4)(-1,3.5)
    \psaxes{->}(0,0)(-4,-4)(1.5,3.5)[$x$,0][$y$,90]
    \psset{algebraic,linecolor=blue}
    \psplot{-3.5}{-1.3}{\f(x+1)}
    \psplot{-0.7}{1}{\f(x+1)}
\end{pspicture}
\end{document}

EDIT:

Based on the comment given by texenthusiast below,

I upvoted thinking your answer is different from Herbert's as i have no idea. It would be better to mention it what's the difference.

here are the differences:

  • I used the default plotpoints=50. It should be enough. Increasing this value makes the resulting PDF bigger in file size.
  • I split the graph into 2 parts with the asymptotic line as the separator. I chose an offset of 0.3 (by inspection) to shift the domain a bit to avoid too large y values. For the current PSTricks implementation, the end points of the asymptotic graph near its asymptotic line oscillate. Fluctuation can be controlled by plotpoints key. Splitting it into 2 parts and offsetting the domain freeing me from increasing plotpoints from its default. Herbert used a single \psplot but with plotpoints=1000 to minimize the fluctuation. Thus the differences are pessimistic and optimistic approaches.

Alternative approach to plot asymptote using gnuplottex package with automatic underhood gnuplot computation.

Works with: -shell-escape enabled and gnuplot 4.4,

code compiled with: pdflatex,frozen texlive distro 2012 on Linux

self descriptive gnuplot code:

\documentclass[preview=true,12pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{lmodern} 
% http://www.ctan.org/pkg/gnuplottex
% amsmath for math labels
% code compiled with pdflatex engine via frozen texlive 2012 on Linux  
\usepackage{gnuplottex,amsmath} % need shell-escape enabled and gnuplot 4.4
\begin{document} 
\begin{gnuplot}[terminal=epslatex,terminaloptions=color]
                                        # means comments in gnuplot syntax
  unset border                          # border off
  unset grid                            # grid off
  unset key                             # dataset legends off
  set format x ''                       # xtics without labels
  set format y ''                       # ytics without labels
  set size square                       # figure into square size
  # xy co-ordinates range
  xmin=-3;xmax=3;ymin=-15;ymax=15;
  set samples 100                       # no of divisions xaxis xmin to  xmax
  set zeroaxis                          # x axis and y-axis aligned to origin
  set xrange [xmin:xmax]                # plot range x and y
  set yrange [ymin:ymax]
  set arrow from xmin,0 to xmax,0  filled   # xaxis starting (x,y) to ending
  set arrow from 0,ymin to 0,ymax  filled   # yaxis starting to ending
  # dotted line at discontinuity
  set arrow from -1,ymin to -1,ymax  nohead linestyle 2 linewidth 3 linecolor rgb 'black' 
  set xtics axis                        # x major ticks 
  set ytics axis                        # y major ticks 
  set title 'Asymptote $y=f(x+1)=\dfrac{1}{x+1}$ function in gnuplot'
  #set xlabel "$x$"
  #set ylabel "$f(x+1)$" rotate by 0 offset 20 
                                         # Piecewise func with ternary operator
  a(x) = x<-1  ? 1.0/(x+1) : 1/0         #  continous in x < -1
  #b(x) = x=-1  ?      inf    : 1/0      #  will not plot at discontinous x= -1
  c(x) = x>-1  ? 1.0/(x+1) : 1/0         #  continous in x > -1
  plot a(x) linetype 1 linewidth 3 linecolor rgb 'gray',c(x) linetype 1 linewidth 3 linecolor rgb 'gray'
\end{gnuplot}
\end{document}

output:

enter image description here

Tags:

Tikz Pgf