2
$\begingroup$

In continuation to my previous question, I have (a slightly modified) function
f[x_,y_,z_,a_,b_,c_]:=x^2 a b + xy b c+ yz c a + z^3 abc , and I want to randomly choose values of x,y,z,a,b,c (with constraints that all $x,y,z$, and $a,b,c$ are positive, and Sqrt[x^2+y^2+z^2]<=1 and a+b+c=1) and plot f[x,y,z,a,b,c] against Sqrt[x^2+y^2+z^2] for say 100 points $(x,y,z,a,b,c)$.

I am editing this question with additional constraints given by four inequalities $|x\pm y| \le |1\pm z|$ which simplify to $z \ge x+y-1; ~z\le -x+y+1;~z\le x-y+1;~z \ge -x-y-1.$

How can one take into account these?

EDIT: It was "all ... and $a,b,c$ are positive" and not just $a$.

$\endgroup$

2 Answers 2

1
$\begingroup$
$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

f[x_, y_, z_, a_, b_, c_] := 
  x^2*a*b + x*y*b*c + y*z*c*a + z^3*a*b*c

FullSimplify[Abs[x + y] <= Abs[1 + z] &&
  Abs[x + y] <= Abs[1 - z] &&
  Abs[x - y] <= Abs[1 + z] &&
  Abs[x - y] <= Abs[1 - z] &&
  x > 0 && y > 0 && z > 0 &&
  Sqrt[x^2 + y^2 + z^2] <= 1]

(* x + y + z <= 1 && x > 0 && y > 0 && z > 0 *)

param = FindInstance[{x > 0, y > 0, z > 0, a > 0, 
    Sqrt[x^2 + y^2 + z^2] <= 1, a + b + c == 1, x + y + z <= 1}, 
  {x, y, z, a, b, c}, Reals, 100];

pts = Simplify[{Sqrt[x^2 + y^2 + z^2], f[x, y, z, a, b, c]} /. #] & /@
    param;

ListPlot[pts, Frame -> True,
 FrameLabel -> {Sqrt[x^2 + y^2 + z^2], 
   HoldForm@f[x, y, z, a, b, c]}]

enter image description here

EDIT: Expanding the PlotRange

ListPlot[pts, Frame -> True,
 FrameLabel -> {Sqrt[x^2 + y^2 + z^2],
   HoldForm@f[x, y, z, a, b, c]},
 PlotRange -> All]

enter image description here

Or

ListPlot[pts, Frame -> True,
 FrameLabel -> {Sqrt[x^2 + y^2 + z^2],
   HoldForm@f[x, y, z, a, b, c]},
 PlotRange -> All,
 ScalingFunctions -> "SignedLog"]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks @Bob Hanlon. Is it possible to avoid the symbols /. and /@ and /@# ? I mean I'm a beginner and find inconvenient working with this notation. $\endgroup$
    – seeker
    Commented Aug 11, 2022 at 15:04
  • $\begingroup$ Highlight the unfamiliar symbol in Mathematica and press F1 for help. The documentation will show you how to use the verbose form. $\endgroup$
    – Bob Hanlon
    Commented Aug 11, 2022 at 15:36
  • $\begingroup$ Yeah, I'm doing that and it helps! A doubt: & after # is supposed to "pump in" the expression into #. In the definition of pts, why is & followed by Map /@ ? I was expecting we simple pump the param into {Sqrt[x^2 + y^2 + z^2], f[x, y, z, a, b, c]}. $\endgroup$
    – seeker
    Commented Aug 11, 2022 at 15:43
  • $\begingroup$ The elements of param each look like {x -> 209/3334, y -> 19/54, z -> 26351/45009, a -> 23, b -> -4, c -> -18}, i.e., a list of rules. To create pts you want {Sqrt[x^2+y^2+z^2], f[x, y, z, a, b, c]} evaluated using each separate element of param in a ReplaceAll (/.). Consequently, the ReplaceAll is mapped (/@) onto param $\endgroup$
    – Bob Hanlon
    Commented Aug 11, 2022 at 16:04
0
$\begingroup$

Try

u=RandomReal[{0,1},{400,5}]; (*generate enough points to have >=100 acceptable points*)
v=Map[({x,y,z,a,b}=#;{x,y,z,a,b,1-a-b})&,u];(*c isn't really "random", calculate it*)
w=Take[Select[v,({x,y,z,a,b,c}=#;x^2+y^2+z^2<=1&&x+y-1<=z<=-x+y+1&&z>=-x-y-1)&],100];
f[x_,y_,z_,a_,b_,c_]:=x^2*a*b+x*y*b*c+y*z*c*a+z^3*a*b*c;
ListPlot[Map[({x,y,z,a,b,c}=#;{f[x,y,z,a,b,c], Sqrt[x^2+y^2+z^2]})&,w]]

Ah! Notice that I (or possibly the OP) made a mistake. He wrote "with constraints that all x,y,z, and a are positive" and I implemented "with constraints that all x,y,z,a,b are positive." If b really can be -9999999 then my code will need to be modified, perhaps in a similar way to how I generated c after the fact, to provide a different random range for b. And he didn't provide any bounds on the range of a, so it might possibly be 9999999. Both of those are going to make a VERY different plot. That just shows that you have to test my code VERY carefully to make certain that you have discovered all of my errors.

$\endgroup$
6
  • $\begingroup$ Thanks @Bill, can you explain what's happening inside Map in the definition of v? $\endgroup$
    – seeker
    Commented Aug 11, 2022 at 19:21
  • $\begingroup$ I would suggest using Apply[Function[{x,y,z,a,b},...]] instead of Function[{x,y,z,a,b}=#;...]. It is a lot easier to read (at least to me), and doesn't affect global variables. $\endgroup$
    – Lukas Lang
    Commented Aug 11, 2022 at 20:59
  • $\begingroup$ @seeker You wrote "I want to randomly choose values of x,y,z,a,b,c" Great. generate 6 random numbers each time. But then you add "a+b+c=1" and that will almost never be true for 6 random numbers. So generate 5 random numbers each time and use those to calculate the value of c thus turning a list of 5 numbers into a list of 6 numbers where c=1-a-b. Does that help any? Anything in MMA can be written in at least 12 different ways. One other way is v=Map[Append[#,1-#[[-2]]-#[[-1]]]&,u] But as I added above, there is no range given for either a or b maybe not limited between 0 and 1. Helps? $\endgroup$
    – Bill
    Commented Aug 11, 2022 at 21:35
  • $\begingroup$ Thanks @Bill, I understand your approach, it is just that I find it a bit hard to follow the Syntax like Map[({x,y,z,a,b}=#;{x,y,z,a,b,1-a-b})&,u]. I am totally lost by ( { }=#; { } ) &. I understand Map[f, expr] make f to act on each element on the first level in expr. $\endgroup$
    – seeker
    Commented Aug 11, 2022 at 23:35
  • $\begingroup$ @seeker It is very good that you Map. MMA programming has a culture of turning lots of things into cryptic punctuation characters and that takes time to learn to accept. What someone finds hard to follow is partly the result of how they defined the MMA language. Now ({x,y,z,a,b}=#;{x,y,z,a,b,1-a-b})& is the function. You can tell that by the (...)& You can look up Function in help, but that may not "help" you much. Or I could rewrite the code f[v_]:=({x,y,z,a,b}=v;{x,y,z,a,b,1-a-b});Map[f,u] Does that help? But that isn't the usual MMA way of writing this. Can you follow better now? $\endgroup$
    – Bill
    Commented Aug 12, 2022 at 12:39

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.