I am trying to solve a maximization problem, where my variable can only take a limited number of values. (Probably) the easiest example would be

dom = {0, 1/2}; NMaximize[{x^2, Element[x, dom]}, {x}]

While the Element[x,dom] syntax works just fine for dom = Integers, I keep getting error messages for any other domain. (edit: this seems to be because my chosen domain is invalid to use with ∈)

edit: I also tried the MemberQ syntax as stated below:

dom = {0, 1/2}; NMaximize[{x^2, MemberQ[dom, x]}, {x}]

This seems to be an acceptable constraint, but for some reason Mathematica seems unable to find a value that meets the constraint, as I get the following message:

NMaximize::nsol: There are no points that satisfy the constraints {False}.

Any other ideas?

edit: just to clarify. I need to restrict the variable(s) to a finite set of real numbers, hence I cannot use ">=/<=" constraints to get the job done. The above is just the simplest application I could think of.

  • 2
    $\begingroup$ Select Element and press F1 for an overview in the documentation. $\endgroup$
    – user21
    Commented Sep 4, 2013 at 15:15
  • $\begingroup$ Thanks, rubenko! I gather that my chosen domain was invalid to use the [Element] syntax. Another option I can think of is MemberQ, which unfortunately does not work either (I updated the post accordingly). So, I am out of ideas again... $\endgroup$
    – Andreas
    Commented Sep 4, 2013 at 15:26
  • $\begingroup$ NMaximize[{x^2, 0 < x < .5, Element[x, Reals]}, {x}] something like this ? $\endgroup$ Commented Sep 4, 2013 at 15:35
  • $\begingroup$ Unfortunately, this will not work, as I need to restrict the variable to a set of discrete options. (Obviously it would work for this example, but my real application has some 300 variables and >500 other constraints, so there is no way I could guarantee a border solution like above) $\endgroup$
    – Andreas
    Commented Sep 4, 2013 at 15:41

2 Answers 2


For multiple domain range check than you can use something like this,

Map[(NMaximize[{x^2, #[[1]] < x < #[[2]], 
     Element[x, Reals]}, {x}]) &, {{.4, .5}, {.4, .6}}]

{{0.25, {x -> 0.5}}, {0.36, {x -> 0.6}}}

You can put all your ranges as list.


Since Element accepts RegionQ objects as domains, you can create a custom domain with:

NMaximize[{x^2, {x} \[Element] Point[List /@ {0, 1/2}]}, x]

{0.25, {x -> 0.5}}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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