# 3D Plotting with Piecewise function

I'm trying to plot the following function:

$f(x,y)= \left\{\begin{matrix} (x+y) & (x,y) \in T \\ (x+y)^2 & (x,y) \notin T \end{matrix}\right.$

where $T= \{ y\leq -x+1, x\geq 0, y\geq 0 \}$

I'm trying without success something like:

Plot3D[Piecewise[{{x + y, {y <= -x + 1, x>=0, y>=0}}, {(x + y)^2, y > -x + 1}},
0], {x, -4, 4}, {y, -4, 4}, Boxed -> False, AxesOrigin -> {0, 0, 0}]


I don't know if the Piecewise function supports multiple conditions like ${y \le -x + 1, x \ge 0, y \ge 0}$ for some piece.

• You need to combine the conditions with And instead of making a list. Piecewise[{{x + y, y <= -x + 1 && x >= 0 && y >= 0}, {(x + y)^2, True}}] will do. – Rahul Jun 9 '14 at 0:16

Try this.

p1 = Plot3D[(x + y), {x, -3, 3}, {y, -3, 3},
RegionFunction -> Function[{x, y}, y <= -x + 1 && y >= 0 && x >= 0]];

p2 = Plot3D[(x + y)^2, {x, -3, 3}, {y, -3, 3},
RegionFunction -> Function[{x, y}, 1 <= y + x || y + x <= 0 || y <=  0 || x <=  0]];

Show[p1, p2] You can also use UnitStep to define your region and to generate one function.

You can do with Piecewise:

f[x_, y_] :=
Piecewise[{{x + y, y <= -x + 1 && x >= 0 && y    >= 0}, {(x + y)^2, True}}]
plt = Plot3D[f[x, y], {x, -4, 4}, {y, -4, 4}, PlotRange -> {0, 64},
MeshFunctions -> {(#1 + #2 &), #1 &, #2 &},
Mesh -> {{1.}, {0}, {0}}, MeshStyle -> Red, BoundaryStyle -> None,
Boxed -> False, AxesOrigin -> {0, 0, 0}] 