I am dealing with a non-linear function of three variables, namely R0k, R1 and alpha. To find its maximum value, I divided the three variables into grids and maximized the function by plotting the maximum value of the function on the grid of alpha, on all the grid points of the two variables. Now I want the value of all those alpha at which the function attained its maximum value. How do I find it? Here is my code:

tbl = Table[
  Flatten[{R0k, R1, 
    Max[Table[bTransp61[(i - 1) Pi 45/180/78]^2, {i, 0, 79}]]}], 
   {R1, 0, 21.43, .1}, {R0k, 2*(R1/R1el)^2*R0kel - R0kel, 19.71, .1}]
tbl >> "datafile.nb"
Export["tbl.dat", tbl]
tbl2 = Table[Flatten[tbl, 1], {1}]
ListPlot3D[tbl2, PlotRange -> {0, 0.0004}]
  • $\begingroup$ Did you try FindMaximum? $\endgroup$ – freddieknets Jun 11 '12 at 15:40
  • 5
    $\begingroup$ You code is not helpful when it's not possible to execute it. $\endgroup$ – halirutan Jun 11 '12 at 16:00

Your question is not fully clear to me, but I think Position might be what you're looking for. You can use it to find elements in a list that match a certain criterion. In this case being equal to the maximum value(s) of the list.

m = RandomInteger[20, {10, 10}]; (* generate a random array *)

posMax = Position[m, Max[m]]; (* Find the positions of the maximum value(s) *)

Grid[m, Background -> {None, None, (# -> Pink) & /@ posMax}] (* Display results *)

enter image description here

Position works on ragged lists too:

m = Table[RandomInteger[10], {i, 10}, {j, i}];
posMax = Position[m, Max[m]];
Grid[m, Background -> {None, None, (# -> Pink) & /@ posMax}]

Mathematica graphics

which is useful in your case as one of the Table boundaries seems to depend on the other.

It works in 3D and any other number of dimensions too.

m = RandomInteger[20, {4, 4, 4}];
posMax = Position[m, Max[m]];
Graphics3D[{{Opacity[0.1], {Sphere[#, 0.3] & /@ posMax}}, 
             MapIndexed[Text[#1, #2] &, m, {3}]}]

enter image description here

So, if you hadn't used the Max function in the first line of your code Position would be able to find the max values directly. You now first have to find the positions of the maximum values in 2D and then search for the alpha value that created them.

Please note that Mathematica has quite a few function for finding maximum values. You might want to start reading this tutorial.

  • 3
    $\begingroup$ I'm getting dizzy... $\endgroup$ – faleichik Jun 11 '12 at 20:58

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.