-1

I create with Table an array containing the function value of some function and the n1and n2arguments. Afterwards i want to look for the minimum function value within this array and find the corresponding arguments. And since i need that information for about 30 different functions that differ only slightly (in one parameter) from eachother, i want to create a code, that creates a list of functions and thus a list of minima and corresponding coordinates. I've written a code for that, but it's unbelievable slow. Maybe you guys know faster and more elegant ways to solve that problem. Sin[n1]Sin[n2]is of course only an examplary function, my actual function has only one global minimum.

There is a condition to the code. I can't use FindMinimum or something similar on my function (don't want to get into that here), it needs to be a list of data points and one looks for a minimum value inside that list.

functiontable = 
  Table[{n1, n2, Sin[n1]Sin[n2]}, {i, 1, 5}];

temptabletable = 
  Table[Table[functiontable[[i]], {n1, 0, 5, 1}, {n2, 0, 5, 1}], {i, 
    1, 5}];

temppositiontable = 
  ux = Table[
    Position[temptabletable[[i]], Min[temptabletable[[i]]]], {i, 1, 
     5}];

temppositiontablen1 = 
  Table[Take[Flatten[temppositiontable[[i]]], 1], {i, 1, 5}];

temppositiontablen2 = 
  Table[Take[Flatten[temppositiontable[[i]]], {2, 2}], {i, 1, 5}];

selectmatrixentrytable = 
  Table[{temppositiontablen1[[i]], temppositiontablen2[[i]]}, {i, 1, 
    5}];

minimumkoordinatentable = 
 Table[Flatten[{Take[Flatten[selectmatrixentrytable[[i]]], 1], 
    Take[Flatten[selectmatrixentrytable[[i]]], {2, 2}]}], {i, 1, 5}]

Min /@ temptabletable
11drsnuggles11
  • 363
  • 1
  • 11

1 Answers1

5

I really don't know what all your complicated code is supposed to do and I'm too tired to find out. However from your problem statement this should help:

f = Sin[#] Sin[#2] &;

a = Array[f, {6, 6}, 0];

min = Min[a]

Position[a, min]
Sin[2] Sin[5]

{{3, 6}, {6, 3}}

For values rather than coordinates consider SortBy or for somewhat better efficiency Ordering:

f2 = Sinc[#] + Cos[#2] &;

triplets = Join @@ Table[{x, y, f2[x, y]}, {x, 0, 5, 0.1}, {y, 0, 5, 0.1}];

SortBy[triplets, Last][[1]]

# ~Extract~ Ordering[#[[All, 3]], 1] & @ triplets
{4.5, 3.1, -1.21636}

{4.5, 3.1, -1.21636}

If you are using version 10 (or later) there is also MinimalBy:

MinimalBy[triplets, Last]
{{4.5, 3.1, -1.21636}}

Unlike the methods above this will return all triplets that have the (same) minimal value rather than only the first. If you want only the first you can use:

MinimalBy[triplets, Last, 1]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371