# Parameterized pgfplots not showing proper surface

I have a very simple function composed of two planes in the unit triangle. I managed to plot this function by parameterizing the coordinates. Now that I have the function, I would like to see how it looks like when multiplied with the polynomials, but then I get unexpected results. This is what I get

The middle result is when I multiply the function by 1-x-y and the second one by simply multiplying by x.

and this is the MWE:

\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % use color
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{pgfplots.groupplots, backgrounds}

\begin{document}
\begin{tikzpicture}[
declare function={upper(\x,\y) = \x + \y*(1-2*\x);},
declare function={lowerl(\x,\y)= \x + \y*(1-2*\x);},
declare function={lowerr(\x,\y)= \x + \y*(1-2*\x);},
]

\begin{groupplot}[
group style={
group size=3 by 1,
},
xmin=0,xmax=1,
ymin=0,ymax=1,
view={-30}{65},
axis line style={draw=none},
tick style={draw=none},
ticks=none,
]

\nextgroupplot[unbounded coords=jump,clip=false, variable=x,
variable y=y,]

\addplot3[patch,patch type=triangle,color=gray,faceted color=gray,fill opacity=0.1] coordinates {(0,0,0) (1,0,0) (0,1,0)};
samples=2, samples y=2,
domain=0:0.5, domain y=0:1,
] ({x},{upper(x,y)},{x});
samples=2, samples y=2,
domain=0:0.5, domain y=0:0.5,
] ({lowerl(x,y)},{y}, {y} );
samples=2, samples y=2,
domain=0.5:1, domain y=0:0.5,
] ({lowerr(x,y)}, {y}, {y} );
%% lines
samples=2, domain=0:1
]({0.5}, {0.5}, {x/2});
samples=101, domain=0:0.5
]({x}, {x}, {x});
samples=2, domain=0:0.5
]({0.5}, {x}, {x});
samples=2, domain=0:0.5
]({0.5}, {x}, {0});
samples=2, domain=0:0.5
]({x}, {x}, {0});

\nextgroupplot[unbounded coords=jump,clip=false, variable=x,
variable y=y,zmax=0.1]

%% background element
\addplot3[patch,patch type=triangle,color=gray,faceted color=gray,fill opacity=0.1] coordinates {(0,0,0) (1,0,0) (0,1,0)};
%% surfaces
samples=20, samples y=20,
domain=0:0.5, domain y=0:1,
] ({x},{upper(x,y)},{x*(1-x-y)});
samples=20, samples y=20,
domain=0:0.5, domain y=0:0.5,
] ({lowerl(x,y)},{y}, {y*(1-x-y)} );
samples=20, samples y=20,
domain=0.5:1, domain y=0:0.5,
] ({lowerr(x,y)}, {y}, {y*(1-x-y)} );
%%% lines
%    samples=2, domain=0:1
%]({0.5}, {0.5}, {x/2});
%    samples=101, domain=0:0.5
%]({x}, {x}, {x});
%    samples=2, domain=0:0.5
%]({0.5}, {x}, {x});
%    samples=2, domain=0:0.5
%]({0.5}, {x}, {0});
%    samples=2, domain=0:0.5
%]({x}, {x}, {0});

\nextgroupplot[unbounded coords=jump,clip=false, variable=x,
variable y=y,zmax=0.25]

\addplot3[patch,patch type=triangle,color=gray,faceted color=gray,fill opacity=0.1] coordinates {(0,0,0) (1,0,0) (0,1,0)};
samples=20, samples y=20,
domain=0:0.5, domain y=0:1,
] ({x},{upper(x,y)},{x*x});
samples=20, samples y=20,
domain=0:0.5, domain y=0:0.5,
] ({lowerl(x,y)},{y}, {y*x} );
samples=20, samples y=20,
domain=0.5:1, domain y=0:0.5,
] ({lowerr(x,y)}, {y}, {y*x} );
%%% lines
%    samples=2, domain=0:1
%]({0.5}, {0.5}, {x/2});
%    samples=101, domain=0:0.5
%]({x}, {x}, {x});
%    samples=2, domain=0:0.5
%]({0.5}, {x}, {x});
%    samples=2, domain=0:0.5
%]({0.5}, {x}, {0});
%    samples=2, domain=0:0.5
%]({x}, {x}, {0});

\end{groupplot}

\end{tikzpicture}
\end{document}


I tried different sampling rates but it doesn't improve. Is there an issue with my parametric mappings or is it just pgfplots that is unable to make these plots?

There are a couple of changes I'd suggest here.

1. You only need one parametrization function (the three you have are the same). This is not a problem, of course.
2. You need just two triangles for the domain of the parametrization. Not a problem either.
3. The real problem (I think) is mixing the original coordinates x,y with the parameters (which I going to call u,v). This is confusing and causes problems as x,y need different domains too (with this parametrization). This is the problem in your third plot, but not in the first or the second because there are symmetry in those two.

In my example I plotted only the domain (two) triangles in the first plot. The rest are your plots reducing the samples and changing surf to mesh to better visualize what's happening.

\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}
\usepackage    {pgfplots}
\pgfplotsset   {compat=1.17}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\begin{tikzpicture}[declare function={param(\x,\y) = \x + \y*(1-2*\x);}]
\begin{groupplot}[
group style={group size=2 by 2},
xmin=0,xmax=1,
ymin=0,ymax=1,
view={-30}{65},
axis line style={draw=none},
tick style={draw=none},
ticks=none,
]

% only the domain triangles (parametrized)
\nextgroupplot[unbounded coords=jump,clip=false,zmax=0.1]
samples=11, samples y=11,
domain=0:0.5, domain y=0:1,
] (u,{param(u,v)},0);
samples=11, samples y=11,
domain=0:1, domain y=0:0.5,
] ({param(u,v)},v,0);

% first plot
\nextgroupplot[unbounded coords=jump,clip=false]
\addplot3[patch,patch type=triangle,color=gray,faceted color=gray,fill opacity=0.1] coordinates {(0,0,0) (1,0,0) (0,1,0)};
samples=11, samples y=11,
domain=0:0.5, domain y=0:1,
] (u,{param(u,v)},u);
samples=11, samples y=11,
domain=0:1, domain y=0:0.5,
] ({param(u,v)},v,v);

% second plot
\nextgroupplot[unbounded coords=jump,clip=false]
\addplot3[patch,patch type=triangle,color=gray,faceted color=gray,fill opacity=0.1] coordinates {(0,0,0) (1,0,0) (0,1,0)};
samples=11, samples y=11,
domain=0:0.5, domain y=0:1,
] (u,{param(u,v)},{u*(1-u-param(u,v))});
samples=11, samples y=11,
domain=0:1, domain y=0:0.5,
] ({param(u,v)},v,{v*(1-param(u,v)-v)});

% third plot
\nextgroupplot[unbounded coords=jump,clip=false]
\addplot3[patch,patch type=triangle,color=gray,faceted color=gray,fill opacity=0.1] coordinates {(0,0,0) (1,0,0) (0,1,0)};
samples=11, samples y=11,
domain=0:0.5, domain y=0:1,
] (u,{param(u,v)},{u*u});

Or, with surf instead of mesh: