# How to find the maximum?

I have in mind $$\max\left( \sqrt{(x-1) (y-x)}+\sqrt{(1-x) (7-y)}+\sqrt{(y-7) (x-y)}\right)$$ for $$x\geq -2\land x\leq 3\land y\geq 0\land y\leq 11$$, of course, taking into account real values of the roots only. In order to avoid complex numbers I consider

f=(Sqrt[(x - 1)*(y - x)] + Sqrt[(7 - y)*(1 - x)] + Sqrt[(x - y)*(y - 7)])*
Boole[(x - 1)*(y - x) >= 0]* Boole[(7 - y)*(1 - x) >= 0]*Boole[(x - y)*(y - 7) >= 0]


Unfortunately, its plot does not give any prompt to me.

Plot3D[f, {x, -2,  3}, {y, 0, 11}] Both

NMaximize[{f, x >= -2 && x <= 3 && y >= 0 && y <= 11},{x, y},AccuracyGoal-> 3,MaxIterations->200]


NMaximize::cvmit: Failed to converge to the requested accuracy or precision within 200 iterations. {0., {x -> 3., y -> 11.}}

and

FindMaximum[{f, x >= -2 && x <= 3 && y >= 0 && y <= 11}, {x, y}]


{0., {x -> 0.914707, y -> 9.31719}}

do not produce a correct answer in view of

f /. {x -> 1, y -> 3}


$$2 \sqrt{2}$$

In general, maximization and minimization algorithms will probably have difficulty when attempting to find a max/min when the landscape is mostly flat except at single values. Most of these algorithms take into account the shape of the landscape to direct them towards their goal, and if the function yields 0 almost everywhere, then it has no idea which direction to take.

I would say that doing a little bit of the work by hand first like Ulrich suggested is going to be the most successful route even though it's not as automated as you'd like. For this problem, however, I found the following worked well:

NMaximize[{f, -2 <= x <= 3, 0 <= y <= 11}, {x, y},
MaxIterations -> 500, Method -> "DifferentialEvolution"]


Set the Method to "DifferentialEvolution" and increase the MaxIterations.

EDIT: The following also works and provides exact solutions.

Maximize[{f, -2 <= x <= 3, 0 <= y <= 11}, {x, y}]

• The expression f is as in my question, is not so? – user64494 Jan 27 '19 at 19:05
• You wrote "I would say that doing a little bit of the work by hand first like Ulrich suggested is going to be the most successful route even though it's not as automated as you'd like". Unfortunately, the Ulrich's plot does not show the optimal solution at all. – user64494 Jan 27 '19 at 19:10
• @user64494 Sorry, I should have mentioned that. Yes, I just copied your definition of f. – MassDefect Jan 27 '19 at 19:10
• @user64494 The first result of Ulrich's is x = 1, y = 4, f(x, y) = 3 which is the same as what I got. I just meant that the answer I provided above may not always work depending on how difficult the problem is. Ulrich's answer decomposes the problem into 3 real-valued, continuous lines for which Mathematica is almost guaranteed to find maximal solutions. – MassDefect Jan 27 '19 at 19:15
• Think of three variables, for example. – user64494 Jan 27 '19 at 20:15

If you replot the function

Plot3D[f, {x, -10,10}, {y, -10, 10}, Exclusions -> None, MaxRecursion ->5,AxesLabel -> {x, y, f}] you can see that the maximum requires y==x!

Plot[f /. y -> x, {x, -5, 10}]


maximum lies on the left or right border of given x-intervall! • Thank you. Unfortunately, your statement " that the maximum requires y==x!" is built on the sand. BTW, I don't see the point $(1,3)$ where $f$ takes $2\sqrt{2}$ in your plot. Please pay your attention that $y$ should be nonegative. – user64494 Jan 27 '19 at 17:34
• You may want to look at the output of Plot3D[f, {x, -2, 3}, {y, 0, 11}, Exclusions -> None, MaxRecursion -> 7] . – user64494 Jan 27 '19 at 17:43

Well, this is not very suprising. Your function has imaginary parts pretty much everywhere, so the product of the Boole is zero practically everywhere.

Consider the function without the Boole part and plot its imaginary part

f = (Sqrt[(x - 1)*(y - x)] + Sqrt[(7 - y)*(1 - x)] + Sqrt[(x - y)*(y - 7)])
Plot3D[Im[f], {x, -2, 3}, {y, 0, 11}, PlotPoints -> 50]


You'll get something like this So there are purely real numbers only along the lines $$y=7 \land 1\leq x \leq3$$, $$x=1 \land 1\leq y\leq 7$$ and $$x=y \land 0\leq x \leq 1$$

This is near impossible to catch with a general purpose optimizer. You'd be much better of by considering the three segments where f is real independently

f1 = f /. x -> 1;
f2 = f /. y -> 7;
f3 = f /. y -> x;

Maximize[ {f1, 0 <= y <= 11}, y]
Maximize[ {f2, -2 <= x <= 3}, x]
Maximize[ {f3, -2 <= x <= 3}, x]


And you'll get

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

• Unfortunately, the third solution {3 Sqrt, {x -> -2}} is not admissible as $y$ should be nonnegative. Also your statement "You'd be much better of by considering the three segments where f is real independently" is built on the sand. I am interested in an automated solution. – user64494 Jan 27 '19 at 17:57
• You are right, the third solution is not valid. As far as an automated solution to problems like that go - good luck. You are trying to find a maximum on a discontinuous function which is one of the nastier problems in optimisation. – Oliver Jennrich Jan 30 '19 at 9:57