[Tex/LaTex] How to refine this tikz diagram

tikz-pgf

I'm working on this diagram:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
        \clip (-0.1,-0.1) rectangle (5,3);
        \draw[help lines,->] (0,0) -- (4.2,0);
        \draw[help lines,->] (0,0) -- (0,3);% draw axis lines
        \draw[gray,dashed] (0,2) -- (4.2,2); % draw asymptote
        \draw[domain=0.1:4.6,very thick,red,->,samples=400] plot ({\x - 0.4},{1/(-\x) + 2} );% draw plot
    \draw[help lines,->] (1,0) -- (1,1.2);
    \draw[help lines,->] (2,0) -- (2,1.5);
    \draw[help lines,->] (3,0) -- (3,1.65);
    \draw[help lines,->] (4,0) -- (4,1.7);

    % scale to fit marginfigure
\end{tikzpicture}
\end{document}

Asymptote drawing

It gets the job done, more or less, but I'm wondering whether there's a cleaner or more elegant way to do it. Specifically, I'm wondering:

  1. Is there a way to draw the curved line as a ray with an endpoint at (0,0)? The problem with drawing a demihyperbola and then clipping is that I want to label several points on that path (which is what the vertical arrows are for). I can't do that if I have to clip the diagram so closely.

  2. Is there a way to describe the height of the vertical lines as relative to the curved plot (i.e., can I tell tikz, "Draw a vertical line, with an arrowhead, from (1,0) until it intersects the curved line")?

Best Answer

If I understand correctly, this is what you're asking for:

 \documentclass[border=2mm]{standalone}
 \usepackage{tikz}   
 \usetikzlibrary{calc}
 \begin{document}
   \begin{tikzpicture}
       \clip (-0.1,-0.1) rectangle (5,3);                                   
       \draw[help lines,->] (0,0) -- (4.2,0);                               
       \draw[help lines,->] (0,0) -- (0,3);   % draw axis lines             
       \draw[gray,dashed] (0,2) -- (4.2,2);   % draw asymptote              
       \draw[domain=0.5:4.6,very thick,red,->,samples=400] plot ({\x - 0.5},{1/(-\x) + 2} );% draw plot

   \foreach \x  in {1,2,3,4}                                                
     {%%                                                                    
       \draw[help lines,->] (\x,0) -- ($(\x,{1/(-(\x+0.5)) +2})-(0,0.6pt)$);
     }%%                                                                    

 \end{tikzpicture}                                                          
 \end{document}

enter image description here

Using the tikzlibrary calc allows you to take into account the thickness of the red curve (hence the offset of 0.6pt).

You could write a macro to handle the y-coordinate:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}   
\usetikzlibrary{calc}
\def\mycurve#1{{1/(-(\x+#1))+2}}
\begin{document}
  \begin{tikzpicture}
    \clip (-0.1,-0.1) rectangle (5,3);                                   
    \draw[help lines,->] (0,0) -- (4.2,0);                               
    \draw[help lines,->] (0,0) -- (0,3);   % draw axis lines             
    \draw[gray,dashed] (0,2) -- (4.2,2);   % draw asymptote              
    \draw[domain=0.5:4.6,very thick,red,->,samples=400] plot ({\x - 0.5},\mycurve{0} );% draw plot

    \foreach \x  in {0.25,0.5,...,4}                                                
      {
        \draw[help lines,->] (\x,0) -- ($(\x,\mycurve{0.5})-(0,0.6pt)$);
      }

\end{tikzpicture}                                                          
\end{document}

enter image description here

Related Question