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 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.

share|improve this question
Select Element and press F1 for an overview in the documentation. – user21 Sep 4 '13 at 15:15
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... – Andreas Sep 4 '13 at 15:26
NMaximize[{x^2, 0 < x < .5, Element[x, Reals]}, {x}] something like this ? – Rorschach Sep 4 '13 at 15:35
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) – Andreas Sep 4 '13 at 15:41

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.

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.