3
$\begingroup$

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?

$\endgroup$
4
$\begingroup$
Minimize[{x + y, AnyTrue[{2, 3, 4, 5}, EqualTo[x]], 
  AnyTrue[{2, 3, 4, 5}, EqualTo[y]]}, {x, y}]

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

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ – sgdsgyhetwaraw Oct 14 '20 at 2:07
  • $\begingroup$ 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 . $\endgroup$ – cvgmt Oct 14 '20 at 2:50
  • $\begingroup$ Minimize[{x + y, Or @@ Thread[ConstantArray[x, 4] == {2, 3, 4, 5}], Or @@ Thread[ConstantArray[y, 4] == {2, 3, 4, 5}]}, {x, y}] $\endgroup$ – cvgmt Oct 14 '20 at 13:46
2
$\begingroup$

One way.

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

Min[x + y]
(*4*)
$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ – sgdsgyhetwaraw Oct 14 '20 at 0:19
1
$\begingroup$

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}}   *)
$\endgroup$
0
$\begingroup$

Another option,

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

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.