3
$\begingroup$

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.

$\endgroup$
1
  • 6
    $\begingroup$ 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. $\endgroup$
    – user484
    Commented Jun 9, 2014 at 0:16

2 Answers 2

7
$\begingroup$

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]

plot

You can also use UnitStep to define your region and to generate one function.

$\endgroup$
4
$\begingroup$

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}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.