3

I'm trying to plot the function $-log(x^2+y^2)$ in a circular domain centered in (0,0). With addplot3 I can plot it on the square domain $[-1,1]^2$.

With what I found here pgfplots and ifthenelse, I can choose to set the value to 0 outside of the disk, but I'd like nothing to be drawn at all.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\pgfmathdeclarefunction{ifthenelsefpu}{3}{%
  \pgfmathparse{#1*#2 + !#1*#3}%
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
      \addplot3[
        surf,
        samples = 60,
        domain = -1:1,
        y domain = -1:1,
      ] ({x}, {y}, {ifthenelsefpu({(x^2+y^2<=1)}, -ln(sqrt(x^2 + y^2)), 0)});
    \end{axis}
  \end{tikzpicture}
\end{document}

Here is the current result enter image description here.

We see on the external part of the disk that the plot is flat.

When I replace the 0 by nan in the macro ifthenelsefpu, I get the error Package pgfplots: An internal error occured during z buffer reorderings: the rows/cols where not balanced! I have rows= 60, cols=60. If this happens to be wrong, you might want to provide rows and cols manually..

How can I achieve to make this figure ?

0

2 Answers 2

6

I can suggest using polar coordinates. This way the domain is rectangulsr:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\pgfmathdeclarefunction{ifthenelsefpu}{3}{%
  \pgfmathparse{#1*#2 + !#1*#3}%
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
      \addplot3[
        surf,
        samples = 60,
        domain = 0.1:1,
        y domain = -180:180,
        ] ({x*cos(y)}, {x*sin(y)}, {-ln(x)});
    \end{axis}
  \end{tikzpicture}
\end{document}

The result: enter image description here

4

I get no error with ifthenelsefpu({(x^2+y^2<=1)}, -ln(sqrt(x^2 + y^2)), nan)}. You also have to set unbounded coords=jump to avoid distortions when the surfaces are drawn.

Example:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\pgfmathdeclarefunction{ifthenelsefpu}{3}{%
  \pgfmathparse{#1*#2 + !#1*#3}%
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
      \addplot3[
        surf,
        samples = 60,
        domain = -1:1,
        y domain = -1:1,
        unbounded coords=jump
      ] ({x}, {y}, {ifthenelsefpu({(x^2+y^2<=1)}, -ln(sqrt(x^2 + y^2)), nan)});
    \end{axis}
  \end{tikzpicture}
\end{document}

Example

You must log in to answer this question.

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