1
$\begingroup$

Consider a quadratic form in 3 variables: $f(A,B,C)= A^2 + C^2 - B^2 - 4 A B + 2 A C$

I have a plot like so:

plot = ContourPlot3D[
  A^2 + C^2 - B^2 - 4 A B + 2 A C == 0, {A, 0, 100}, {B, 0, 100}, {C, 
   0, 100}, AxesLabel -> Automatic]

I want to label the regions where $f>0$, $f=0$ and $f<0$.

I was trying using Inset but was not able to come up with a proper way to implement it.

Edit 1:

My original question was answered @kglr in the comment. I have a follow up question. What if I now had another such function, say $g(A,B,C)=A^2 + C^2 -9 B^2 - 4 A B + 2 A C$. How to plot and label the four regions:

  1. $ f>0 , g>0$
  2. $f>0 , g<0$
  3. $f<0,g>0$
  4. $f<0,g<0$
$\endgroup$
6
  • 1
    $\begingroup$ Show[RegionPlot3D[ 0 <= A <= 100 && 0 <= B <= 100 && 0 <= C <= 100, {A, 0, 100}, {B, 0, 100}, {C, 0, 100}, AxesLabel -> Automatic, BoundaryStyle -> Opacity[0], MeshFunctions -> {#^2 + #3^2 - #2^2 - 4 # #2 + 2 # #3 &}, Mesh -> {{0}}, MeshShading -> {Opacity[.25, Yellow], Opacity[.25, Blue]}], plot, Graphics3D@Text["f > 0", {20, 20, 75}], Graphics3D@Text["f < 0", {80, 80, 25}]]? $\endgroup$
    – kglr
    Commented May 22, 2018 at 5:34
  • 1
    $\begingroup$ see also: “Covering up” text in Graphics $\endgroup$
    – kglr
    Commented May 22, 2018 at 5:53
  • $\begingroup$ @kglr Please see updated question. $\endgroup$
    – Subho
    Commented May 22, 2018 at 6:04
  • 1
    $\begingroup$ does this work: ClearAll[f, g] f[a_, b_, c_] := a^2 + c^2 - b^2 - 4 a b + 2 a c; g[a_, b_, c_] := a^2 + c^2 - 9 b^2 - 4 a b + 2 a c; RegionPlot3D[ 0 <= a <= 100 && 0 <= b <= 100 && 0 <= c <= 100, {a, 0, 100}, {b, 0, 100}, {c, 0, 100}, AxesLabel -> Automatic, BoundaryStyle -> None, MeshFunctions -> {f[#, #2, #3] &, g[#, #2, #3] &}, Mesh -> {{0}, {0}}, MeshShading -> {{Opacity[.5, Green], Opacity[.5, Blue]}, { Opacity[.5, Orange], Opacity[.5, Red]}}] for the updated question? $\endgroup$
    – kglr
    Commented May 22, 2018 at 6:27
  • $\begingroup$ @kglr Yeah. They look fine except for the labels. As I am just starting out with Graphics in Mathematica, I need a bit of explanation on MeshFunction. How are the various regions I mentioned in my question identified? $\endgroup$
    – Subho
    Commented May 22, 2018 at 6:43

1 Answer 1

3
$\begingroup$

Update: Labeling the 4 regions defined by 2 functions:

ClearAll[f, g, h]
f = #^2 + #3^2 - #2^2 - 4 # #2 + 2 # #3 &;
g = #^2 + #3^2 - 9 #2^2 - 4 # #2 + 2 # #3 &;
h = And @@ Thread[0 <= {#, #2, #3} <= 100] &;
regions = Tuples[And[#@f[a, b, c] & /@ #, #@g[a, b, c] & /@ #]] &@{Positive, Negative};
labels = {"f > 0\ng > 0", "f > 0\ng < 0", "f < 0\ng > 0", "f < 0\ng < 0"};
SeedRandom[1]
positions = N[Mean /@ 
  ({a, b, c} /. FindInstance[# && h[a, b, c], {a, b, c}, Reals, 7] & /@ regions)];
labelsandpositions = Select[Transpose[{labels, positions}], FreeQ[#, a + b + c] &];
insets = Graphics3D[Inset[ Graphics[Text[Style[#, 16]]], #2] & @@@ labelsandpositions];
cp = Show[ContourPlot3D[#[a, b, c] == 0, {a, 0, 100}, {b, 0, 100}, {c, 0, 100}, 
  ContourStyle -> Texture[Graphics[Text[Style[#2, 48, Black]], Background -> #3]]] & @@@
    Transpose[{{f, g}, {"f == 0", "g == 0"}, Opacity[.7, #] & /@ {Red, Blue}}]];
rp = RegionPlot3D[h[a, b, c], {a, 0, 100}, {b, 0, 100}, {c, 0, 100}, 
   AxesLabel -> Automatic, BoundaryStyle -> None, 
   MeshFunctions -> {f, g}, Mesh -> {{0}, {0}}, 
   MeshShading -> {Opacity[.5, #]&/@{Green, Yellow}, Opacity[.5, #]&/@{Purple, Cyan}}];
Show[rp, cp, insets, PlotRange -> All]

enter image description here

Note: For the given functions f and g the region f < 0 && g > 0 is empty.

Original answer:

ClearAll[f, h, a, b, c]
f = #^2 + #3^2 - #2^2 - 4 # #2 + 2 # #3 &;
h = 0 <= # <= 100 && 0 <= #2 <= 100 && 0 <= #3 <= 100 &;
plot = ContourPlot3D[f[a, b, c] == 0, {a, 0, 100}, {b, 0, 100}, {c, 0, 100}, 
  ContourStyle -> Texture[Graphics[Text[Style["f = 0", 64, Cyan]], Background -> Red]]]; 
Show[RegionPlot3D[h[a, b, c], {a, 0, 100}, {b, 0, 100}, {c, 0, 100}, 
   AxesLabel -> Automatic, BoundaryStyle -> None, 
   MeshFunctions -> {f}, Mesh -> {{0}}, 
   MeshShading -> {Opacity[.25, Yellow], Opacity[.25, Blue]}],
 plot, Graphics3D[{Inset[Graphics[Text[Style["f > 0", 20]]], {25, 25, 85}],
    Inset[Graphics[Text[Style["f < 0", 20]]], {85, 85, 25}]}]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Really nice implementation! Could you check my updated question? $\endgroup$
    – Subho
    Commented May 22, 2018 at 6:16
  • $\begingroup$ When saving as an eps file, the "f=0" text turns into a solid blue rectangle. Any fix for that? $\endgroup$
    – Subho
    Commented May 22, 2018 at 7:27
  • $\begingroup$ @Subho95, can think of a fix; you might want to check related q/a's on this site $\endgroup$
    – kglr
    Commented May 22, 2018 at 7:38

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.