2

I have tried increasing Samples to 700, and it helped a little. I saw that my green f(x) is closer to the asymptote x=-1,x=1. However, it is not close enough. Is there any way to make the green f(x) approaches x=+-1 until ymax and ymin?

Also, I am looking for the way to automatically label axis all digit numbers -7,-6,...,7 without specifying like this.

\begin{figure}[H]\centering\footnotesize
\begin{tikzpicture}
 \begin{axis}[axis lines=middle,xlabel=$ x $,ylabel=$ y 
 $,xmin=-7,xmax=7,ymin=-7,ymax=7,xtick={-6,-4,-2,-1,0,1,2,3,4,6},
 ytick={-0.9,0.9},scale=2, restrict y to domain=-7:7,
samples=700,]
 \addplot[domain=-7:0,draw=green,width=2pt] {x+ln((x-1)/(x+1))};
\addplot[domain=0:7,draw=green,width=2pt] {x+ln((x-1)/(x+1))};
\addplot[only marks] coordinates {(-2,-0.9)(2,0.9)};
\addplot[smooth,no marks,draw=blue] coordinates {(-1,-7)(-1,7)[x=-1]};
\addplot[smooth,no marks,draw=blue,width=2pt] coordinates {(1,-7)(1,7) 
[x=1]};
 \draw[green](3,4) node{$ f(x) $};
\end{axis}
\end{tikzpicture}\end{figure}

enter image description here

  • 2
    It seems no matter how far does the green curve go it IS covered by the blue line. Isn't that exactly what you want? – Symbol 1 Apr 8 '18 at 3:21
  • @Symbol1 the green line doesnt go as up, and down, as it should go. – Panha Apr 8 '18 at 4:47
  • Was my answer helpful to solve your question? If so, please consider upvoting (by clicking on the arrows next to the score) and/or marking it as the accepted answer (by clicking on the checkmark ✓). If not, please let us know what still is unclear. – Stefan Pinnow Apr 10 '18 at 20:36
7

The trick is to use the starred version of restrict y to domain and to use proper domains to get the desired result.

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=middle,
        xlabel=$x$,
        ylabel=$y$,
        xmin=-7,
        xmax=7,
        ymin=-7,
        ymax=7,
        xtick distance=2,
        ytick={-0.9,0.9},
        scale=2,
        % use the stared version of the command, which uses the given limit
        % value if the real value exceeds the given range instead of
        % not showing it at all
        restrict y to domain*=-7:7,
        % (no need to use that much samples)
        samples=101,
        smooth,
    ]

        % changed the domain limits to something a little bit "larger" than
        % the undefined points at x=-1 and x=1
        \addplot [ultra thick,green,domain=-7:-1.0001] {x+ln((x-1)/(x+1))};
        \addplot [ultra thick,green,domain=1.0001:7] {x+ln((x-1)/(x+1))};

        \draw [green](3,4) node{$ f(x) $};

        \addplot [only marks] coordinates {(-2,-0.9)(2,0.9)};

        \addplot [blue] coordinates {(-1,-7)(-1,7)};
        \addplot [blue] coordinates {(1,-7)(1,7)};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.