# pgf: 3D Graphing sqrt(-x-y^2+8) results in a jagged surface

The plot of this specific elliptic paraboloid, sqrt(-x-y^2+8) comes out looking like this. I've tried parameterizing the surface, writing the equation in different forms, and messed around with the domain. The error I get most of the time is something about the z axis and having a mismatched number of rows and cols. Can someone help me by figuring out to make the surface smooth and well just not look like this?

\usepackage{pgfplots}
\usetikzlibrary{3d, calc}
\pgfplotsset{compat=1.18}
\usepackage{tikz-3dplot}

\begin{document}

\begin{tikzpicture}
\begin{axis}[axis lines=center]
\end{axis}
\end{tikzpicture}

\end{document}

• Welcome! Please can you add a class so this can be compiled?
– cfr
Commented Oct 2, 2023 at 2:10

## The issues

Because PGFPlots samples points within the domain [−3,3] × [−3,3] then constructs a plot from evaluating z-coordinates, there are two unfortunate problems we encounter when trying to plot this particular surface. First, the equation z = sqrt(−x − y2 + 8) is complex on a subset of the domain, but PGFPlots cannot plot complex values. We can use unbounded coords=jump to resolve this first problem—this tells PGFPlots to make jumps where coordinates are nan.

The second problem is a more of a hindrance to handle. PGFPlots does not sample evenly along the lower bound of the surface, which occurs where z = 0. This will result in a series of jagged-looking spikes along the bottom of the graph when PGFPlots attempts to stitch the evaluated points into a complete graph. A workaround for this problem is to use an if-statement that sets sufficiently small values (less than 0.2) to zero, which removes some of the roughness from the bottom of the graph.

## Code

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{3d, calc}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines = center,
xmin = -4,
xmax =  4,
ymin = -4,
ymax =  4,
axis equal image,
unbounded coords = jump
]
domain  = -3:3,
samples = 80,
surf,
] {-x-y^2+8 < 0.2 ? 0 : sqrt(-x-y^2+8)};
\end{axis}
\end{tikzpicture}

\end{document}


To be honest, I’m not sure whether there’s a way to change PGFPlots’ sampling intervals for the second problem, but I’d be happy for feedback in the comments if this can be improved.

• Hey, just want to thank you @gz839918 for answering my question. Obviously, adding the < 0.2 ? 0 argument will make some parts outside the "real" (complex) domain of the function show a flat surface rather than being empty. Is there any way to eliminate that or is it not possible? Commented Oct 2, 2023 at 16:18
• @tangula I'm glad it helped! I'm not sure about the answer to your question, but it may be possible to perform a custom sample of points, by using a for-loop to sample points on z = 0 then passing all evaluated points with \addplot3 coordinates. I wouldn't know for sure until somebody tries it, though... Commented Oct 3, 2023 at 1:21
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
%axis lines=center,
]