4
$\begingroup$

Given a function f[x_,y_,z_,a_]:=x^2 + xy+ yz + z^3+a^2 (just for example), how can I randomly choose values of x,y,z,a (with constraints that all $x,y,z$, and $a$ are positive, and Sqrt[x^2+y^2+z^2]<=1) and plot f[x,y,z,a] against Sqrt[x^2+y^2+z^2] for say 100 points $(x,y,z,a)$?

I am sure this is doable in Mathematica, but I have no idea how to proceed.

$\endgroup$
3
  • $\begingroup$ Side note: "Randomly choosing a value" is not well-defined for infinite domains (in particular, choosing a random a is not really possible, since a could be anywhere from 0 to infinity). If you care about the randomness of the chosen points, you'll have to decide on a distribution for a that is well-defined. $\endgroup$
    – Lukas Lang
    Commented Aug 8, 2022 at 18:44
  • $\begingroup$ It is recommend to answer a new question for the new edition and add this links to the new question. $\endgroup$
    – cvgmt
    Commented Aug 11, 2022 at 9:19
  • $\begingroup$ Alright @cvgmt, thanks for the info. $\endgroup$
    – seeker
    Commented Aug 11, 2022 at 9:29

3 Answers 3

4
$\begingroup$
(* number of random points *)
nPoints = 100;

(* parametrize {x,y,z} in spherical coordinates *)
(* with the constraints theta<=pi/2 and phi<=pi/4 *)
(* to meet the constraints that x,y,z>0 *)
r = RandomReal[{0, 1}, nPoints];
theta = RandomReal[{0, Pi/2}, nPoints];
phi = RandomReal[{0, Pi/4}, nPoints];
{x, y, z} = {r Sin[theta] Cos[phi], r Sin[theta] Sin[phi], 
   r Cos[theta]};

(* I'm assuming a is <=aMax *)
aMax = 1;
a = RandomReal[{0, aMax}, nPoints];

Clear[f]
f[x_, y_, z_, a_] := x^2 + x y + y z + z^3 + a^2

data = Transpose[{r, f @@@ Transpose[{x, y, z, a}]}];
ListPlot[data]
```
$\endgroup$
7
  • $\begingroup$ Thanks @H. Zhou. I have a similar situation where now in place of $a$, there is a set $a_1,a_2,a_3,a_4,a_5$ and they are real and also constrained to $\sum_{i=1}^{5} a_i = 1$. How to handle this situation? $\endgroup$
    – seeker
    Commented Aug 9, 2022 at 8:58
  • $\begingroup$ @seeker If you mean a linear relation between a_i's then I think you can first generate random numbers for a_{1,2,3,4}, then compute a5=1-a1-a2-a3-a4. $\endgroup$
    – H. Zhou
    Commented Aug 9, 2022 at 9:11
  • $\begingroup$ Thanks, @H. Zhou, that works! However, I have a final constraint $|x \pm y| \le |1 \pm z|$ arising due to some physical reasons. How can I take this into account? Should I ask it as a new question? $\endgroup$
    – seeker
    Commented Aug 10, 2022 at 19:08
  • $\begingroup$ @seeker For this one I would use Select, in a similar way as that suggested by @Rom38. $\endgroup$
    – H. Zhou
    Commented Aug 11, 2022 at 7:35
  • $\begingroup$ Thanks @H. Zhou, but Rom38's approach (with due respect to his expertise) seems hard to me. Could you incorporate this in your answer? $\endgroup$
    – seeker
    Commented Aug 11, 2022 at 8:45
6
$\begingroup$

How about

f[x_, y_, z_, a_] := x^2 + x y + y z + z^3 + a^2;
pts = RandomPoint[Ball[], n = 50000] //Abs// Transpose;
a = RandomReal[{0, 1}, n];
Map[{Norm[Most[#]], f @@ #} &, Transpose@Append[pts, a]] // ListPlot

enter image description here

$\endgroup$
2
$\begingroup$

First, let's define the array of random sets:

args = RandomReal[{0, 2}, {50000, 4}];

I've limited the coordinates up to 2 but it can be any. Let's take from args all the sets that fit the desired constrains are:

cnsts = Select[args, Norm[#[[1 ;; 3]]] <= 1 &];

The function is:

f = #[[1]]^2 + #[[1]] #[[2]] + #[[2]] #[[3]] + #[[3]]^3 + #[[4]]^2 &;

An array of values {Norm@cnsts[[i,1;;3]],f@cnsts[[i]]} is:

res = Table[{Norm[cnsts[[i, 1 ;; 3]]], f@cnsts[[i]]}, 
            {i, 1, Length@cnsts}];

And the plot is:

ListPlot[res, PlotLabel -> "Number of point = " <> ToString@Length@cnsts]

enter image description here

There 3315 random elements of args fit with your constrains.

P.S. I don't understand why the bottom Axis appeared without Ticks but this is in range {0,1} according the constrains

$\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.