-1
$\begingroup$

I have a function f which takes three arguments x, y and z:

 f[x_,y_,z_] := 2 x + 0.5 y + z

The arguments x, y and z must have a pre-defined domains, for example:

 -1 < x < 1
  3 <= y < 7
 -3 < z <= 3

I would like to run my function across x,y and z sets corresponding to some pre-defined domains as stated above and extract only those x, y and z sets which produce some arbitrary value, for example:

7, 12, 20 etc.

The expected output I seek is something like:

{{x,y,z}, result}

where {x,y,z} is a set of x,y,z arguments which were passed to my function and result is the associated result which is some pre-defined arbitrary value.

Edit

If I have a function in four parameters instead as f[x_,y_,z_,d_] := 2 x + 0.5 y + z+ d

and I want to run f only over x,y,z regions while fix d at say d=5 . I tried:

f[x_, y_, z_, d_] := 2 x + z + 0.5 y + d

x := Range[1, 5, 1]
y := Range[5, 9, 1]
z := Range[10, 14, 1]
d := 5

data = Outer[f, x, y, z, d]

But it gave output sample:

{{{f[1, 5, 10], f[1, 5, 11], f[1, 5, 12], f[1, 5, 13], f[1, 5, 14]}}}

So it seems doesn’t work. Any help ..

Note that I don't want to set d from the pre definition of the function, because my real calculation are so long and will be difficult to define the fixed parameter each time .

$\endgroup$
7
  • $\begingroup$ It is a bit unclear what you are asking. Could you please edit your question and provide more information? Also, the code provided above is not a valid wolfram syntax. It would help if you could provide code that you have tried $\endgroup$ May 17, 2016 at 10:16
  • $\begingroup$ DensityPlot, is that it? Use the option PlotRange for the second question. $\endgroup$
    – C. E.
    May 17, 2016 at 10:18
  • $\begingroup$ Are you trying to make a table of values, or do you want to make a plot? $\endgroup$
    – Jason B.
    May 17, 2016 at 10:25
  • $\begingroup$ Ok, I added f[x,y,z] to be as Mathematica syntax. Now, I don't want to to make a plot any more, I just want Mathematica to show a table of values for f[x,y,z], epically if I have a function in more than 2 parameters, say four or six parameters, in which each one has a range. $\endgroup$
    – S.S.
    May 17, 2016 at 10:28
  • $\begingroup$ What do you mean by "scan"? Do you just want to compute the values in given grid points? If so, use Table. $\endgroup$
    – Szabolcs
    May 17, 2016 at 10:39

1 Answer 1

1
$\begingroup$

Example:

(*arbitrary data*)
x = Range[1, 5, 1]
y = Range[5, 9, 1]
z = Range[10, 14, 1]

{1, 2, 3, 4, 5}
{5, 6, 7, 8, 9}
{10, 11, 12, 13, 14}

(*process*)
data = Outer[f, x, y, z];

Output (sample):

Domain: {1,5,10}, {1,5,11}, {1,5,12}, {1,5,13}, {1,5,14}

output sample

EDIT 1

This way may be not the best way how to achieve what you are after. There are some brilliant people out here who can do it with much better aesthetics and efficiency.

domains = Flatten[Outer[List, x, y, z], 2];
results = Flatten[Outer[f, x, y, z], 2];

Select[Transpose[{results, domains}], #[[1]] >= 20 &]

EDIT 2

If you decide to include any additional argument:

Declare domain of interest:

 d = Range[15, 19, 1]

The rest is the same:

domains = Flatten[Outer[List, x, y, z, d], 2];
results = Flatten[Outer[f, x, y, z, d], 2];

Select[Transpose[{results, domains}], #[[1]] >= 20 &]

NOTE:

Make sure to include d in your function declaration!

Reference

Outer + Animation

$\endgroup$
7
  • $\begingroup$ @ E.Doroskevic . data = Outer[f, x, y, z], gives me a sample as: {{16, 17, 18, 19, 20}, {17, 18, 19, 20, 21}}, which are the values of f over x,y,z ranges, but what about the second question, that I want to know which set of x, y, z values give for instance f larger than 20. $\endgroup$
    – S.S.
    May 17, 2016 at 10:51
  • $\begingroup$ Do you mean you want to select only those entries which are >= 20? Or do you want to know which {x,y,z} combination gives f >= 20? $\endgroup$ May 17, 2016 at 10:55
  • $\begingroup$ To know which {x,y,z} combination gives f >= 20 . $\endgroup$
    – S.S.
    May 17, 2016 at 11:02
  • $\begingroup$ A quick fix would be just embed 5 into your function and leave signature f[x_,y_,z_]. Otherwise, if you want to run f[x_,y_,z_,d_] across all domains of x, y, z and d all you have to do is just provide domain for d and supply Outer function with d - Outer[f, x, y, z, d] $\endgroup$ May 17, 2016 at 11:23
  • $\begingroup$ @ E.Doroskevic . well, E.Doroskevic thanks any way for your help. Now I try to use your example in case if ` f ` is a function in one extra parameter which has a fixed value, i.e., ` f[x_ ,y_ ,z_,d_ ] ` , and i want to scan, say ` f[x,y,z,5] `, with changing only x,y,z .. $\endgroup$
    – S.S.
    May 17, 2016 at 11:23

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.