2
$\begingroup$

I am plotting two functions in two variables and one of them is a piecewise function as given below. I am trying to get different colors for each of the sub-function of the piecewise function.

func1 = -(Subscript[n, 1]^2 + Subscript[n, 2]^2) + 100
cb1[[1]] = 1.1245;
cb2[[1]] = 0.7153;
p1 = 
 Plot3D[{func1, 
   Piecewise[{{cb1[[1]] + 100, 
       Subscript[n, 1] <= 5 && Subscript[n, 2] <= 5}}, cb2[[1]]] + 
    75}, {Subscript[n, 1], 0, 10}, {Subscript[n, 2], 0, 10}, 
  AxesLabel -> Automatic]

Generate figure

The entire piecewise function is in color blue as shown in the figure. Is there a way to have sub-functions in different colors? Also, is there way to have the origin on the bottom left?

$\endgroup$
3
$\begingroup$
func1 = -(n1^2 + n2^2) + 100
pw = Piecewise[{{cb1[[1]] + 100,n1 <= 5 && n2 <= 5}}, cb2[[1]]] + 75;

plt2 = Plot3D[pw,
  {n1, 0, 10}, {n2, 0, 10}, 
  MeshFunctions -> {#3 &}, 
  Mesh -> {{100}}, 
  MeshShading -> {Opacity[.6, Red], Opacity[.6, Green]}, 
  ExclusionsStyle -> Opacity[.6, Blue],
  BoundaryStyle -> None];

Show[Plot3D[func1, {n1, 0, 10}, {n2, 0, 10}, AxesLabel -> Automatic], plt2]

enter image description here

Alternatively, construct two functions from your Piecewise function:

pwe = PiecewiseExpand[pw];
{pw1, pw2} = {ConditionalExpression @@ pwe[[1, 1]], 
   ConditionalExpression[pwe[[2]], Not @ pwe[[1, 1, 2]]]};

Plot3D[{func1,  pw1, pw2}, {n1, 0, 10}, {n2, 0, 10}, 
   AxesLabel -> Automatic, 
  PlotStyle -> {Automatic, Opacity[.5, Red], Opacity[.5, Green]}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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