1

I am trying to plot a simple function: y = sqrt(x+4)-2:

\begin{tikzpicture}
   \begin{axis}[ 
        xlabel=$x$,
        ylabel={$y$},
        axis x line=center, axis y line=center
        ] 
        \addplot[domain=-5:5,
        color=red] {sqrt(x+4)-2}; 
    \end{axis}
 \end{tikzpicture}

I get:

enter image description here

The min y value should be -2 at x = -4, but I get -1.5. How is that? Thank you!

5
  • Well, you ask pgfplots to plot this all the way down to -5 by saying domain=-5:5. What should it plot below x=-4?
    – user121799
    Commented Feb 23, 2018 at 1:38
  • @marmot I don't understand because -5 < -4, no? It shouldn't plot anything below -4 because the function would not be defined.
    – Johnathan
    Commented Feb 23, 2018 at 1:41
  • 1
    restrict your domain to -4:4. now the function is drawn to about x=-3.5 because your number of samples is to small than can follows slope of function. or define samples=500 for example.
    – Zarko
    Commented Feb 23, 2018 at 1:46
  • @Zarko Thank you very much, it worked! I thought that it would plot like a graphic calculator (or perhaps the calculator uses the same strategy).
    – Johnathan
    Commented Feb 23, 2018 at 1:50
  • @Johnathan, you actually have math problem: how to draw complex function on real plane :-). see my answer below.
    – Zarko
    Commented Feb 23, 2018 at 1:55

2 Answers 2

0
  • defining domain=-5:5 require, that pgfplots below -4 draw complex function, what is not possible. so sensible is, that the lower domain limit is -4.
  • tangent to function at x=-4 is orthogonal, so the default number of samples is to small that can follows to function change

the simplest solution is

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
   \begin{axis}[
        xlabel=$x$,
        ylabel={$y$},
        axis lines=center,
       %samples=400% for more smuth curve
            ]
   \addplot[domain=-4:5, color=red] {sqrt(x+4)-2};
   \end{axis}
 \end{tikzpicture}
 \end{document}

which gives

enter image description here

2
  • Ok, so because we approach a limit (x = -4), we need more points for latex to estimats how the curve behaves at that point, right?
    – Johnathan
    Commented Feb 23, 2018 at 2:03
  • @Johnathan, yes. you can help pgfplots with splitting plots on two parts: \adplot[domain:-4:-3,5, samples=70,red] {sqrt(x+4)-2};\adplot[domain:-3.5:5, samples=70,red] {sqrt(x+4)-2};
    – Zarko
    Commented Feb 23, 2018 at 2:10
2

You do not need any large number of samples if you just realize that what you are drawing is a flipped parabola.

\documentclass[margin=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
   \begin{axis}[
        xlabel=$x$,
        ylabel={$y$},
        axis lines=center]
   \addplot[domain=-2:1.5, color=red] ({(x+2)^2-4},{x});
   \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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