3

To be honest, the following example not exactly works as I had expected.

\documentclass{scrartcl}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
% taken from http://tex.stackexchange.com/questions/15475/using-ifthenelse-in-pgfmath
\pgfmathdeclarefunction{ifthenelsefpu}{3}{%
  \pgfmathparse{#1*#2 + !#1*#3}%
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    scale only axis,
  ]
    \addplot3[
      surf,
      samples = 20,
      domain = -1:1,
      y domain = -1:1,
    ] ({x}, {y}, {ifthenelsefpu({(x==0) && (y==0)},1,0)});
  \end{axis}
\end{tikzpicture}
\end{document}

The result should be single peak at (0,0), but ...

[here should be a picture, but at the moment the upload is not working for me - sorry]

Jürgen
  • 2,160

1 Answers1

2

With 20 samples in each direction the sample points in the interval from -1 to 1 miss the value 0. Changing to 21 samples gives

Sample output

It is perhaps easiest to think about the case of 2 samples, these are -1 and 1 vs. 3 samples, which are -1,0 and 1. In the 20 sample case, first sample is -1 and the other are spaced 2/19 apart, so the list of values is

-1, -17/19, -15/19, -13/19, -11/19, -9/19, -7/19, -5/19, -3/19, -1/19

and the corresponding positive values.

\documentclass{scrartcl}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
% taken from http://tex.stackexchange.com/questions/15475/using-ifthenelse-in-pgfmath
\pgfmathdeclarefunction{ifthenelsefpu}{3}{%
  \pgfmathparse{#1*#2 + !#1*#3}%
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    scale only axis,
  ]
    \addplot3[
      surf,
      samples = 21,
      domain = -1:1,
      y domain = -1:1,
    ] ({x}, {y}, {ifthenelsefpu({(x==0) && (y==0)},1,0)});
  \end{axis}
\end{tikzpicture}
\end{document}
Andrew Swann
  • 95,762
  • Thanks, that is correct. But this was not my problem and it does not change it, alas. Let me describe the resulting picture: Not only the value at (0,0) equals 1 but all the values for (x<0,y<0)! So the complete quadrant is "high". Maybe this is due to an old version of pgfplots (1.10)? I think I should update ... – Jürgen Jun 25 '15 at 10:06
  • The above is produced with pgfplots version 1.12. So I suggest you update. – Andrew Swann Jun 25 '15 at 10:09
  • Yes! That was exactly the cause, thanks for the hint. – Jürgen Jun 25 '15 at 10:17