5

I draw the graph of 1/|x| with the code:

\begin{tikzpicture}[>=latex, scale=0.3]
        \draw[->] (-5,0)--(5,0)node[right]{$x$};
        \draw[->] (0,-1)--(0,9)node[right]{$y$};
        \draw [domain=-5:-1/8, samples=500, thick]
            plot (\x, {-1/(\x)});
        \draw [domain=1/8:5, samples=500, thick]
            plot (\x, {1/(\x)});
        \fill[black] (0,3) circle (4pt);
    \end{tikzpicture}

But the resulting picture looks like this:

enter image description here=5x5

As you can see, it is not symmetric at the highest point. I wonder what causes this happen? And how can we fix this?

A similar problem also happens when I draw the graph of sin(1/x):

\begin{tikzpicture}[>=latex, scale=1.5]
        \draw[->] (-2,0)--(2,0)node[right]{$x$};
        \draw[->] (0,-2)--(0,2)node[right]{$y$};
        \draw [domain=-1:-0.03, samples=1000, thick]
        plot (\x, {sin(1/(\x) r)});
        \draw [domain=0.03:1, samples=1000, thick]
        plot (\x, {sin(1/(\x) r)});
    \end{tikzpicture}

The resulting picture is:

enter image description here

The problem is that it doesn't look "soomth" when getting close to x=0. Is there a way to fix this?

New contributor
Timo Chang is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • Welcome to TeX.SE, good question! For the second one, I would recommend using something like \draw [domain=0.03:100, samples=1000, ultra thin] plot (1/\x, {sin(deg(\x))});.
    – Jasper
    Commented 20 hours ago
  • For the first one, the domain is excluding the right endpoint for the left portion of the graph (draw a line at y=8 to see)
    – Jasper
    Commented 20 hours ago
  • 1
    See also tex.stackexchange.com/questions/280595/… Commented 10 hours ago

2 Answers 2

6

I strongly recommend you to use the pgfplots package to draw functions.

Example (using, for sin(1/x), the 1/x tricks from Jasper's answer):

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
      axis lines=middle,
      xmin=-5,
      xmax=5,
      ymin=-1,
      ymax=9,
      xlabel={$x$},
      xlabel style={below},
      ylabel={$y$},
      xtick=\empty,
      ytick=\empty,
      ]
      \addplot [domain=-5:-1/8, samples=500, thick] { -1/x };

      \addplot [domain=1/8:5, samples=500, thick] { 1/x };

  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[
      axis lines=middle,
      xmin=-2,
      xmax=2,
      ymin=-2,
      ymax=2,
      xlabel={$x$},
      xlabel style={below},
      ylabel={$y$},
      xtick=\empty,
      ytick=\empty,
      trig format=rad,
      ]
      \addplot [smooth, domain=-1:-30, samples=1000, thick] ( { 1/x }, { sin(x) } );

      \addplot [smooth, domain=1:30, samples=1000, thick] ( { 1/x }, { sin(x) } );

  \end{axis}
\end{tikzpicture}

\end{document}

Example

4

Credit: https://tex.stackexchange.com/a/728029/319072, https://asymptote.sourceforge.io/asymptote_tutorial.pdf


\documentclass{article}
\usepackage{tikz}
\begin{document}

For the first one, you simple switch the bounds of one of them, 
since it omits the last one for some reason.

\begin{tikzpicture}[>=latex, scale=0.3]
    \draw[->] (-5,0)--(5,0)node[right]{$x$};
    \draw[->] (0,-1)--(0,9)node[right]{$y$};
    \draw [domain=-1/8:-5, samples=500, thick]
        plot (\x, {-1/(\x)});
    \draw [domain=1/8:5, samples=500, thick]
        plot (\x, {1/(\x)});
    \fill[black] (0,3) circle (4pt);
\end{tikzpicture}

If you want to include the endpoint, you can use a foreach statement:

\begin{tikzpicture}[>=latex, scale=0.3]
    \draw[->] (-5,0)--(5,0)node[right]{$x$};
    \draw[->] (0,-1)--(0,9)node[right]{$y$};
    
    \draw [domain=1/8:5, samples=500]
        plot (\x, {1/(\x)});
        \draw (-2,8) -- (2,8);

    \pgfmathsetmacro{\lastix}{-5}
    \pgfmathsetmacro{\lastiy}{1/abs(-5)}
    \foreach[parse=true,evaluate=\x,count=\i] \x[
        evaluate=\x as \x using {\x}
        ,evaluate=\x as \y using {1/abs(\x)}
        ,remember=\x as \lastx (initially \lastix)
        ,remember=\y as \lasty (initially \lastiy)
    ] in {-5,-5+(-1/8-(-5))/500,...,-1/8}{
        \draw[red] (\lastx,\lasty) -- (\x,\y);
        
        \ifnum\i=500
        \draw[red](\lastx,\lasty) -- (-1/8,8);
        \fi
    }
\end{tikzpicture}

For the last one, you can make it smooth  by making the first component 1/x:

\begin{tikzpicture}[>=latex, scale=1.5]
    \draw[->] (-2,0)--(2,0)node[right]{$x$};
    \draw[->] (0,-2)--(0,2)node[right]{$y$};
    \draw [domain=1:100, samples=1000, ultra thin]
    plot (1/\x, {sin(deg(\x))});
\end{tikzpicture}
\end{document}

output graph

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .