6

Evening!

Posting a quick message here cause I have a small problem with TiKZ. I've been trying to plot the function 1/sqrt(x), and it just... won't work.

Here is the code I've been typing:

\begin{tikzpicture}[scale = 1]
\begin{axis}[
    domain=0:100,
    xscale=1,yscale=1,
    xmin=0, xmax=100,
    ymin=0, ymax= 1,
    samples=10000,
    axis lines=center,
]
    \draw[thick, samples = 1000, blue] plot[domain=0:100] ({\x, 1/sqrt(\x)});
\end{axis}
\end{tikzpicture}

But here's what I get:

enter image description here

I tried with other functions, still have problems. For example, if I just plot sqrt(x), here's what I get:

enter image description here

At least, the shape is now right... but the values are completely off.

So if anyone should have any idea on how to correct that code, I'd be really grateful, thanks :)

  • 1
    Welcome to TeX-SE! 1/sqrt(\x) has a singularity at 0 and should fall for larger \x. But the technical reason why it does not work is that you set the braces wrongly, (\x,{ 1/sqrt(\x)}) is correct but still has the singularity. – user121799 Jun 13 at 20:22
  • 2
    Possibly the values are off too because you didn't specify the (correct) \pgfplotssetcompatibility (which is why it's important to provide a fully functional MWE ;). – JJM Driessen Jun 13 at 20:33
18

Diagram of the function 1/sqrt(x) is the following:

enter image description here

In your MWE you have mix of pgfplotsand plain tikz syntax. Better is stick to just one of them. For example to pgfplots:

\documentclass[border=3mm]{standalone}% <-- changed
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    \begin{tikzpicture}
\begin{axis}
    ymin=0, ymax=1,
    axis lines=center,
            ]
    \addplot [thick, domain=0:100, samples=100] {1/sqrt(\x)};
\end{axis}
    \end{tikzpicture}
\end{document}
  • Oh, ok, thanks for the answer. I tried it, it works great! :) – Arthur Jun 13 at 20:48
10

As mentioned in the comments above, the syntax was incorrect. Since you seem to be using pgfplots, here is the way to do it. I exclude the singularity, and in pgfplots there is no need to put a backslash before x. Also I reduced the number of samples.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[scale = 1]
\begin{axis}[domain=0:100,
    xscale=1,yscale=1,
    xmin=0, xmax=100,
    ymin=0, ymax= 1,
    samples=501,
    axis lines=center,
]
    \addplot[domain=0.0001:100] {1/sqrt(x)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks for both your answers. Both work really well, I really appreciate it. Have a nice evening! – Arthur Jun 13 at 20:48

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.