2

I am trying to plot a function involving sqrt but I don't understand why it is not plotting until sqrt(0).

The blue curve should reach y=0 at x=8 but it stops before, which does not happen with the red curve. Does someone know how to force the blue curve to plot on the full domain between x=0 and x=8?

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

    \begin{tikzpicture}

    \begin{axis}[
    scale only axis, % To ensure same size on all pictures axis
    restrict y to domain=-12:12,
    axis x line=center,
    axis y line=center,
    ticks = none,
    samples=150]

    \addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

    % Plot curves
    \addplot[blue, very thick, domain=0:8]{sqrt(20/2.8*(8-x))};
    \addplot[red, very thick, domain=0:9.5]{sqrt(20/2.8*(11-x))-1.3};
    \addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};

    \end{axis}

    \end{tikzpicture}

\end{document}
1

It is not the case that a vertical line with \addplot coordinates .... causes problems. The reason why the blue plot is not complete is that there are rounding errors. It suffices to wrap the argument of the square root in abs.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

    \begin{tikzpicture}

    \begin{axis}[
    scale only axis, % To ensure same size on all pictures axis
    restrict y to domain=-12:12,
    axis x line=center,
    axis y line=center,
    ticks = none,
    samples=150]

    \addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

    % Plot curves
    \addplot[blue, very thick, domain=0:8]{sqrt(abs(20/2.8*(8-x)))};
    \addplot[red, very thick, domain=0:9.5]{sqrt(20/2.8*(11-x))-1.3};
    \addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};

    \end{axis}

    \end{tikzpicture}

\end{document}

enter image description here

2
  • Your solution works to complete the blue curve (you wrote red in your answer). I accepted your answer. – JuCa May 28 '20 at 16:00
  • 2
    @JuCa You are right, that is a typo. Thanks! BTW, if you use xmax=12,ymax=10,, you can drop \addplot [no markers] coordinates {(12,10)};. – user194703 May 28 '20 at 16:03
1

Use a simple plot for the vertical line segment instead. The slope of a vertical line is ±inf which can cause problems with pgfplots.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

    \begin{tikzpicture}

    \begin{axis}[
    scale only axis, % To ensure same size on all pictures axis
    restrict y to domain=-12:12,
    axis x line=center,
    axis y line=center,
    %ticks = none,
    samples=150]

    \addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

    % Plot curves
    \addplot[blue, very thick, domain=0:8]{sqrt(20/2.8*(8-x))};
    \addplot[red, very thick, domain=0:9.5]{sqrt(20/2.8*(11-x))-1.3};
    %\addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};
    \draw [red, very thick](9.5,0) -- (9.5,1.98);

    \end{axis}

    \end{tikzpicture}

\end{document}

enter image description here

Edit:

Another option to increase the accuracy is to increase the number of samples, as a plus, your curve will look better.

\addplot[blue, very thick, samples=1000, domain=0:8,]{sqrt(20/2.8*(8-x))};

enter image description here

5
  • I have not understood the question...but I like your answer. – Sebastiano May 28 '20 at 15:54
  • 1
    Thanks for being very supportive ;-) – AboAmmar May 28 '20 at 15:55
  • Thank you but this does not solve my problem. It only hides it by changing the red curve which is here only to show the problem. Outside of the minimum example, I need the blue curve to be plotted entirely, not stopped before sqrt(0). For clarification, I would need your axis to be plotted until the origin at (0,0). – JuCa May 28 '20 at 15:56
  • OK, I added another option to correct the problem and make the curve much better. – AboAmmar May 28 '20 at 16:14
  • Insufficient number of samples makes the curve looks like line segments at the vicinity of (8,0). – AboAmmar May 28 '20 at 16:20

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.