# How to make Maximize function successfully maximize in general?

Having looked at

I understand that the Maximize function often needs some "help" with domain and implicit assumptions. What's really confusing me here is: In general, what is the right way to give the function the "help" it needs?

As a simple example, I'm stumped about how to explain the behavior below:

In:= Maximize[{Log[x*y], x + y == 10}, {x, y}, Reals]
Out= {Log, {x -> 5, y -> 5}}

In:= Maximize[{Log[x] + Log[y], x + y == 10}, {x, y}, Reals]
Out= Maximize[{Log[x] + Log[y], x + y == 10}, {x, y}, Reals]

(*OK, maybe M doesn't like the risk that x or y is nonpositive and can't simplify.*)
In:= Maximize[{Log[x] + Log[y], x + y == 10 && x > 0 && y > 0}, {x, y}, Reals]
Out= Maximize[{Log[x] + Log[y], x + y == 10 && x > 0 && y > 0}, {x, y}, Reals]

(*Maybe this still isn't enough information? Let me try to be doubly redundant*)
In:= Assuming[{x, y} \[Element] Reals && x > 1 && y > 1,
Refine[Maximize[{Log[x] + Log[y], $Assumptions && {x, y} \[Element] Reals && x + y == 10 && x > 1 && y > 1}, {x, y}, Reals]]] Out= Maximize[{Log[x] + Log[y], x + y == 10}, {x, y}, Reals] (*Maybe this is just too hard?*) In:= Assuming[a \[Element] Reals && a > 0, Maximize[{Log[x] + a, x <= 10}, {x}, Reals]] Out= Maximize[{a + Log[x], x <= 10}, {x}, Reals] In:= Assuming[a \[Element] Reals && a > 0, Maximize[{Log[x] + a,$Assumptions && x <= 10}, {x}, Reals]]
Out= Maximize[{a + Log[x], a \[Element] Reals && a > 0 && x <= 10}, {x}, Reals]

(*And yet without that little a,*)
In:= Maximize[{Log[x], x <= 10}, {x}, Reals]
Out= {Log, {x -> 10}}

(*In fact according to the documentation Mathematica can
maximize algebraically in terms of parameters:*)

In:= Maximize[{a*x^2 + b*x + c, x <= 10}, {x}, Reals]
Out= [too long to print, a little strange,
but a reasonable characterization of the solution space]


So my observations are:

1. Mathematica is capable of symbolically maximizing a function
2. including transcendental functions
3. including when introducing a boundary
4. (from other examples) and can even optimize over multiple variables in a multi-dimensional bounded space

5. Yet in certain circumstances where the solution should be "obvious," simply introducing a parameter breaks Mathematica's ability to solve the problem, even when handling that parameter seems to be within its ability in other contexts (namely the Log[x]+a example versus a*x^2+b*x+c example)

So, I'm missing something, but I'm not sure what. My question is the general one I put in bold above; if that question is not specific enough, then a specific piece of my question is "how do I explain the behavior observed above and in particular avoid breaking Maximize's ability to maximize when giving it expressions that seem within its ability to handle based on similar examples yet break it for reasons that I cannot understand," or even more narrowly but still relatedly, "what is the reason [that I have been unable to discern] for why Maximize breaks under certain inputs and not others?"

My hope is that if I figure out why this doesn't work, I can generalize to figure out why the actual maximization I want to do isn't working. In particular, I have many more free parameters, and I want Mathematica to maximize a sum of functions in terms of those parameters, and I know that a solution exists provided that those parameters satisfy certain inequalities, and I want Mathematica to categorize the solutions-in-terms-of-parameters-under-what-inequality-conditions automatically. The reason I need it to do this for me even though I've done it by hand is that I want to generalize to a high-dimension case that I don't want to solve by hand, and wanted to see Mathematica reproduce my results on the low-dimension case first, but so far I've failed even to make it do that. I can explain more of the general problem if necessary, but I believe the first step is for me to understand how to use Maximize correctly.

Although, as the OP noted,

Maximize[{Log[x] + Log[y], x + y == 10 && 0 < x && 0 < y}, {x, y}, Reals]


returns unevaluated,

Maximize[{Log[x] + Log[y], x + y == 10 && 0. < x && 0. < y}, {x, y}, Reals]
(* {3.21888, {x -> 5., y -> 5.}} *)


works. On the other hand,

Maximize[{Log[x] + Log[y], x + y == 10 && 1 < x && 1 < y}, {x, y}, Reals]


returns unevaluated, even though it looks a lot like the second example above. This can be overcome with

Maximize[{Log[x] + Log[y], x + y == 10 && 1 < x < 10 && 1 < y < 10}, {x, y}, Reals]
(* {2 Log, {x -> 5, y -> 5}} *)


It is worth noting the statement in the Maximize documentation that "If the maximum is achieved only infinitesimally outside the region defined by the constraints, or only asymptotically, Maximize will return the supremum and the closest specifiable point." In this case, it may mean that Maximize looked at x = 0, even though it violates the constraint x > 0.

• This is interesting. In your example 2 the routine seems to work because it falls back to numerical optimization (which I need to avoid as I want a symbolic categorization of the solution space). In your example 3 you'd think Mathematica could "infer" the "<10" constraints from the ">1" and "sum to 10" constraint, but it didn't and your explicit statement of them helped Mathematica along. Is this because the resulting space had finite area? I'm trying to extrapolate a general rule from this...perhaps I just need to find a sufficiently large finite-volume bounding box for all my variables. – Philip Nov 2 '15 at 18:26
• @Philip Any upper bound, 10 or greater, combined with any lower bound greater than zero (for instance, 1/1000) seems to work. This is consistent with the closing paragraph in my answer. The region over which Maximize works need (in this case) to be bounded away from zero and infinity. – bbgodfrey Nov 2 '15 at 18:35
• Apologies for the delay. This is very useful. I'm not sure if, strictly speaking, it answers the fully general question, but absent further suggestions I'm going to accept it now. Thanks! – Philip Jan 7 '16 at 17:35