# How to minimize equation over a list of values?

I tried to do something like Minimize[x+y, {x,y} ∈ {2,3,4,5}] but that doesn't work. How could I minimize an expression where I restrict the inputs to values from a list?

Minimize[{x + y, AnyTrue[{2, 3, 4, 5}, EqualTo[x]],
AnyTrue[{2, 3, 4, 5}, EqualTo[y]]}, {x, y}]


{4, {x -> 2, y -> 2}}

• Thanks, this works the way I wanted. This is not particularly important, but I tried replacing AnyTrue[{2, 3, 4, 5}, EqualTo[x]] to MemberQ[{2, 3, 4, 5}, x] but that didn't work. I thought it would be easier to remember. Do you know why it didn't work? – sgdsgyhetwaraw Oct 14 '20 at 2:07
• Minimize[x + y, {x, y} ∈ImplicitRegion[(x == 2 || x == 3 || x == 4 || x == 5) && (y == 2 ||y == 3 || y == 4 || y == 5), {x, y}]]  another way that is easy understand . – cvgmt Oct 14 '20 at 2:50
• Minimize[{x + y, Or @@ Thread[ConstantArray[x, 4] == {2, 3, 4, 5}], Or @@ Thread[ConstantArray[y, 4] == {2, 3, 4, 5}]}, {x, y}] – cvgmt Oct 14 '20 at 13:46

One way.

x = Range[2, 5]
y = Range[2, 5]

Min[x + y]
(*4*)

• That works for finding the minimum value, but Minimize also shows you the values of x and y that were used to generate the minimum value. – sgdsgyhetwaraw Oct 14 '20 at 0:19

For those who use older versions of MMA, where AnyTrue  is not implemented:

cond = Or @@ # & /@ And @@ Outer[Equal, {x, y}, {2, 3, 4, 5}]

(*   (x == 2 || x == 3 || x == 4 || x == 5) &&
(y == 2 || y == 3 || y == 4 || y == 5)   *)

Minimize[{x + y, cond}, {x, y}]

(*   {4, {x -> 2, y -> 2}}   *)


Another option,

xyVals = Tuples[ {Range[2, 5, 1], Range[2, 5, 1] }]
fxyVals = Apply[#1 + #2 &, xyVals, {1}]
Min[fxyVals]
(*4*)