# Can I increase the accuracy of pgfplots?

Working on a pgfplots solution to this question, I faced the following problem. When I plot only one branch of a square root function, the plot seems to be OK; but when I add the minus part of the function, the plot seems to be shifted up at the value x=0.0.

%pdfLaTeX
\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[axis lines = center,samples=100]
\addplot [] {+sqrt(x)};
\addplot [] {-sqrt(x)};
\end{axis}

\end{tikzpicture}
\end{document}


This is what is gained by plotting only \addplot [] {+sqrt(x)};

This is what I have when I plot both functions;

Also, when I add to the amount of plotting samples, the problems seems to get be solved but it is highly dependent on the value of the samples and the problem is not solved completely.

This is what I have for samples=500

Why this happens? Is this because I need to define some accuracy in plotting functions? Why does not this happen when I only plot one function?

## 1 Answer

The problem is that you're not sampling the point at x=0, because the default domain runs form -5 to +5 and you're using an even number of samples. By setting samples=101 and/or setting domain=0:5, you'll at least get a connected plot. But the sampling isn't going to be dense enough where things are interesting (near x=0), so you'll still need to use a large number of samples.

In this case, however, you could simply cheat and instead plot the inverse of the function against the y axis:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[axis lines = center,samples=100]
\addplot [] (x^2, x);
\end{axis}

\end{tikzpicture}
\end{document}

• By defining an odd value for sampling 101 and a domain like domain=-5:11, the plot is not still connected at the boundary of x=0. What's wrong by defining both of the sampling and domain together? What causes such split in the plot? The reason is still vague for me. Jun 30 '15 at 20:06
• You need to get a sample point at x=0. If you choose a domain of -5:11 with 101 samples, you'll get samples at -5, -4.842, -4.683, ..., -0.089, 0.069, ..., 11, so you're not hitting x=0, and the two branches don't meet. Choosing samples=161, domain=-5:11 works, because you get samples at -5, -4.9, -4.8, ..., -0.1, 0, 0.1, ..., 11.
– Jake
Jun 30 '15 at 20:08
• How do you understand what is the correct sampling value for each function? For instance, having a function like sqrt(x-0.8) with sampling 351 and 0:11 domain still disconnects at its zero value with all those considerations. Jun 30 '15 at 20:16
• @EnthusiasticStudent: Just use domain=0.8:11 (with any number of samples), the start and end of your domain is always sampled. Or use domain=0:11, samples=111 (the domain covers 11 units, and you want to sample 0.8, so a step size of 0.1 would work. To get a step size of 0.1 over 11 units, you need to split the domain into 110 segments, which requires 111 samples).
– Jake
Jun 30 '15 at 20:19