pgf-tikz / pgfplots

pgfplots - A TeX package to draw normal and/or logarithmic plots directly in TeX in two and three dimensions with a user-friendly interface and pgfplotstable - a TeX package to round and format numerical tables. Examples in manuals and/or on web site.
http://pgfplots.sourceforge.net/
187 stars 33 forks source link

Function coordinates unexpectedly claimed to be out of bounds #453

Closed Benezivas closed 1 year ago

Benezivas commented 1 year ago
This is pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022/Arch Linux) (preloaded format=pdflatex)
 restricted \write18 enabled.
Package: pgfplots 2021/05/15 v1.18.1 Data Visualization (1.18.1)

I am trying to plot a function inside a narrow domain interval, in which pgfplots claims

NOTE: coordinate (1Y5.9998997e-1],3Y0.0e0]) has been dropped because it is unbo
unded (in y). (see also unbounded coords=jump).

for all coordinates of the domain. This is unexpected, since the function is defined and bounded in the given interval. I tried plotting the same function in wolframalpha and maxima:

plot2d (((1+x)*(sqrt(-1+x^2)+sqrt(-17-16*x+x^2-16*3)))/(4*sqrt(-1+x^2)), [x, sqrt(21)-4, 3/5])$

which both output the expected plot.

Here is a minimal example file to reproduce the issue:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}
    \centering
      \begin{tikzpicture}[scale=1.0]
        \begin{axis}[ymin=1, ymax=10, xmin=0, xmax=0.8, xlabel=$x$, ylabel=$f(x)$, grid=major, samples=50] %

          \addplot[domain=sqrt(21)-4:3/5] {((1+x)*(sqrt(-1+x^2)+sqrt(-17-16*x+x^2-16*3)))/(4*sqrt(-1+x^2))};

        \end{axis}
      \end{tikzpicture}
    \end{figure}
\end{document}

I checked the pgf documentation regarding mathematical notation and have not found any problems, but it may of course still be the case that the syntax I used is incorrect. Any help is appreciated.

hmenke commented 1 year ago

Just by eyeballing, there is a subexpression sqrt(-1+x^2) but when plugging in the upper bound of 3/5 this evaluates to sqrt(-1+9/25) = 4/5*sqrt(-1) which is not a real number. Keep in mind that pgfplots is not a computer algebra system and does not perform any simplifications on the expression you provide.

Indeed, when you rewrite the expression by taking out a sqrt(-1) from the numerator and the denominator it works just fine:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}
    \centering
      \begin{tikzpicture}[scale=1.0]
        \begin{axis}[ymin=1, ymax=10, xmin=0, xmax=0.8, xlabel=$x$, ylabel=$f(x)$, grid=major, samples=50] %
          \addplot[domain=sqrt(21)-4:3/5] {((1+x)*(sqrt(1-x^2)+sqrt(17+16*x-x^2+16*3)))/(4*sqrt(1-x^2))};
        \end{axis}
      \end{tikzpicture}
    \end{figure}
\end{document}

image

Benezivas commented 1 year ago

You are correct, I should have spotted this myself. I was so focused on finding an issue with the syntax of the expression that I did not stop to check the individual expressions extensively.