Can't find maximum of a function on a region

for h=0.2

I'm trying to find the maximum value of

(-h^2/3)*(-1/x^2)


subject to

8.1 < x < 8.5


I've tried pretty much every combination of:

Functions:

FindMaximum[], MaxValue[]

Region (reg):

x \ [Element] Interval[{8.1,8.5}]
x \ [Element] Interval[8.1,8.5]
{x,8.1,8.5}
x, Interval[{8.1,8.5}]
x, Interval[8.1,8.5]
x \ [Element] Interval[{8.1,8.5}], x
x \ [Element] Interval[8.1,8.5], x


I even tried using 81/10 and 85/10 but that didn't work either..

Expression:

(-h^2/3)*(-1/x^2)
{(-h^2/3)*(-1/x^2) , reg}
Assuming[reg , -h^2/(3*x^2)


I'm getting errors like:

The variable x\[Element]Interval[{81/10,17/2}] cannot be localized so that it can be assigned to numerical values.

The variable x\[Element]Interval[{8.099999999,8.500000001}] cannot be localized so that it can be assigned to numerical values.

Constraints in {x\[Element]Interval[{8.099999999,8.500000001}]} are not all equality or inequality constraints. With the exception of integer domain constraints for linear programming, domain constraints or constraints with Unequal (!=) are not supported.

One time it told me the max value of this function was -7.34642395*10^-35.

Honestly I know I'm doing something really stupid but I can't figure out what.

• With[{h = 0.2}, FindMaximum[{(-h^2/3)*(-1/x^2), 8.1 <= x <= 8.5}, {x, 8.3}]]? Commented Feb 29, 2016 at 19:09
• This doesn't seem to be working. I had to tweek it a bit for what I was using it for after I posted the question. It works better than what I have but I have:............... With[{h = 0.2}, FindMaximum[{Abs[(h^2/3)*(-1/x^2)], 8.3 <= x <= 8.7}, x]] = {0.000193497, {x -> 8.30103}} ...............But............... Abs[(-0.2^2/(3*8.3^2))] = 0.0001935452654
– Kvo
Commented Feb 29, 2016 at 19:31
• This boils down to finding the maximum of 1/x^2 (for a given real value of h function). On a closed interval on positive x the left end point must be the maximum. The errors are reflection of interval issue as per@kirma and @MarcoB Commented Apr 28 at 4:40

You should be aware of the fact that your problem may be somewhat ill-posed. Your function simplifies to the following expression:

Simplify[(-h^2/3)*(-1/x^2)]

(* Out: h^2/(3 x^2) *)


The difference of the function's values at the two extrema of your interval is ca. $0.0005\ h^2$. Since $h^2$ is a positive quantity (for $h\ne0$) and the function is monotonically decreasing (plot it), its maximum is attained at $x=8.1$, i.e. infinitesimally outside the region defined by your constraints; in that case Maximize & co. will return the supremum and the closest specifiable point (from the docs).

Many approaches seem to produce a result when correct syntax is used:

FindMaximum[{(-h^2/3)*(-1/x^2), 8.1 < x < 8.5}, x]
Maximize[{(-h^2/3)*(-1/x^2), 8.1 < x < 8.5}, x]


However, I was intrigued by the idea of using an Interval as a region specification, although technically it is NOT equivalent to your desired constraints, because Interval[{8.1, 8.5}] represents the closed interval that includes both end points (i.e. $8.1\le x\le8.5$, whereas you want them excluded.

Maximize[(-h^2/3)*(-1/x^2), x ∈ Interval[{8.1, 8.5}]]


It turns out that the accepted syntax for specifying that $x$ belongs to that interval is {x} ∈ Interval[{8.1, 8.5}]. Using that form, the optimization works fine:

Maximize[(-h^2/3)*(-1/x^2), {x} ∈ Interval[{8.1, 8.5}]]
(* Out: {0.000203221, {x -> 8.1}} *)


This did not strike me as immediately obvious, nor did I find it referenced in the documentation for Interval or other optimizers that accept geometric regions. I am still wondering whether I am missing something and I should have expected that. Hopefully I'll update if I find out more.

• the requirement for the { } is inconsistent with the docs for Element. Possibly should be labeled as a bug? Commented Feb 29, 2016 at 19:54
• @george2079 Use of vectors ({ }) is consistent when you consider Interval to be a geometric region when Element is used with it. Points in geometric regions are always coordinates that are described by vectors, even when those vectors are of only one dimension. Plain x is also valid, but at that case it's considered to be a symbolic vector, which needs to be handled accordingly (for instance, with Indexed[x, 1] in equations, instead of being treated as a number. Commented Feb 29, 2016 at 20:12
• For symbolic vector case, consider Maximize[(-h^2/3)*(-1/Indexed[x, 1]^2), x \[Element] Interval[{8.1, 8.5}]] (* {0.000203221, {x -> {8.1}}} *) Commented Feb 29, 2016 at 20:15
• Makes sense, but I still find the docs lacking. Commented Feb 29, 2016 at 21:16

This is really an extended comment about region-based constrained variables.

When you see Element in the variable section of a function such as Maximize, it always means you are dealing with geometric regions, although it is not necessarily immediately obvious. Values in geometric regions are always vectors, even when that vector has a length of one. So it happens to be in the case of Interval, when it is interpreted to be a region. Like some graphics primitives, Interval has second meaning in this context. This kind of constrained variable is declared in a form like Element[{x}, Interval[{0, 1}]]. Compare this with Element[{x, y}, Disk[]].

So, such declarations should have curly braces, right? Not quite. There are also symbolic vectors which are a feature that isn't documented very well. They don't have curly braces, even if they involve both Element and a geometric region. These are such as Element[v, Interval[{0, 1}]] or Element[v, Disk[]]. Sometimes these symbolic vectors can be used directly also on the equation side; consider Maximize[Norm[v], Element[v, Disk[]]], for instance. (Sadly mixing normal and symbolic vectors doesn't work as nicely as one might hope.)

This form allows for accessing individual components of symbolic vectors using Indexed, which is used like this: Maximize[Indexed[x, 1] - Indexed[x, 1]^2, Element[x, Interval[{0, 1}]]].

Note that in case of list-based coordinates, results list those variables as individual scalars:

Maximize[y + x^2, Element[{x, y}, Disk[]]]

(* {5/4, {x -> -(Sqrt[3]/2), y -> 1/2}} *)


... but in the case of symbolic vectors, as vector values:

Maximize[Indexed[v, 2] + Indexed[v, 1]^2, Element[v, Disk[]]]

(* {5/4, {v -> {-(Sqrt[3]/2), 1/2}}} *)


Interval is just one of these regions that can be used, and its elements are also vectors.

EDIT: A side note: FindMaximum doesn't understand region constraints in the variable declaration part. This is understandable, but unfortunately it doesn't understand that in its constraints either, which saddens me. This does work, though (as it expands to normal inequalities):

With[{h = 0.2},
FindMaximum[{(-h^2/3)*(-1/x^2),
RegionMember[Interval[{8.1, 8.5}], {x}]}, x]]


In 14.0 on Windows 10

h = 0.2; Maximize[{(-h^2/3)*(-1/x^2), x \[Element] Interval[{8.1, 8.5}]}, x]


Maximize::objfs: The objective function {0.0133333/Subscript[x, 1]^2} should be scalar-valued. 0.000203221, {x -> 8.1}}

FindMaximum[{(-h^2/3)*(-1/x^2), x\[Element] Interval[{8.1, 8.5}]}, x]


{0.000203221, {x -> 8.1}}

MaxValue[{(-h^2/3)*(-1/x^2),x \[Element] Interval[{8.1, 8.5}]}, x]


MaxValue::objfs: The objective function {0.0133333/Subscript[x, 1]^2} should be scalar-valued. 0.000203221

• How is this a solution if it gives an error message? And how is it supposed to be different from MarcoB's answer? Commented Apr 27 at 20:08
• @Domen: Warnings are not errors. MarcoB uses {x} ∈ Interval[{8.1, 8.5}], whereas I use x\[Element] Interval[{8.1, 8.5}]}. BTW, the notation {x} ∈ Interval[{8.1, 8.5}] is not correct: {x}~ is a set, not an element. Commented Apr 28 at 9:27
• The error message clearly tells you that MarcoB's usage is correct and yours is not. Compare the result of your wrong usage 0.5 ∈ Interval[{0, 1}] or 0.5 ∈ Interval[{0, 1}] // Reduce with the correct usage {0.5} ∈ Interval[{0, 1}]. Commented Apr 28 at 18:49
• @Domen: Indeed, the result of {0.5} \[Element] Interval[{0, 1}] is True. This means that Mathematica's math is not traditional, no more and no less. That's all. Commented Apr 30 at 4:24