Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a user defined function h[i_,j_] and I would like to find its minimum value over all integers $1 \leq i \leq 100$ and $1 \leq j \leq 100$ and $j \leq i$. I would just like Mathematica to try all the possibilities rather than attempt any symbolic minimization. How can you do that?

share|improve this question
Have a look at Table. – b.gatessucks Apr 16 '13 at 18:20
up vote 4 down vote accepted

If you like loops, you could just go through all combinations and memorize the minimum value and position:

h[x_, y_] := ((x - 30)^2 + (y - 10)^2);

min = h[1, 1];
pos = {1, 1};
Do[If[h[i, j] < min, min = h[i, j]; pos = {i, j}], {i, 100}, {j, 100}];

And then min is 0 and pos is {30,10} as expected. With this approach you don't need to allocate the memory as in the Table example.

Not so fast for this specific example but definitely more beautiful is using Minimize although you don't wanted this

Minimize[{h[x, y], 1 <= x <= 100 && 1 <= y <= 100}, {x, y}, Integers]
share|improve this answer
Thank you. The problem with Minimize is that it doesn't do anything in my case. – Anush Apr 16 '13 at 19:31

As suggested in the comments, Table can be used to generate the matrix. To find the {i,j} pair corresponding to the minimum value, a combination of Min and Position can be used.

tbl = Table[h[i, j], {i, 1, 100}, {j, 1, i}];
Position[tbl, Min@tbl, {2}]
share|improve this answer

If you don't need to find the parameters, but only the minimum value, you could save memory by finding the minimum for each row. Compare:

h[x_, y_] := ((x - 30)^2 + (y - 10)^2);
n = 1500;
Min @ Table[Min @ Array[h[i, #] &, i], {i, n}]

Min @ Table[h[i, j], {i, n}, {j, i}];

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.