2
$\begingroup$

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}]

enter image description here

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}$

$\endgroup$

3 Answers 3

4
$\begingroup$

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}]
$\endgroup$
5
  • $\begingroup$ The expression f is as in my question, is not so? $\endgroup$
    – user64494
    Jan 27, 2019 at 19:05
  • $\begingroup$ 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. $\endgroup$
    – user64494
    Jan 27, 2019 at 19:10
  • $\begingroup$ @user64494 Sorry, I should have mentioned that. Yes, I just copied your definition of f. $\endgroup$
    – MassDefect
    Jan 27, 2019 at 19:10
  • $\begingroup$ @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. $\endgroup$
    – MassDefect
    Jan 27, 2019 at 19:15
  • $\begingroup$ Think of three variables, for example. $\endgroup$
    – user64494
    Jan 27, 2019 at 20:15
0
$\begingroup$

If you replot the function

Plot3D[f, {x, -10,10}, {y, -10, 10}, Exclusions -> None, MaxRecursion ->5,AxesLabel -> {x, y, f}]  

enter image description here

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!

enter image description here

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – user64494
    Jan 27, 2019 at 17:34
  • $\begingroup$ You may want to look at the output of Plot3D[f, {x, -2, 3}, {y, 0, 11}, Exclusions -> None, MaxRecursion -> 7] . $\endgroup$
    – user64494
    Jan 27, 2019 at 17:43
0
$\begingroup$

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

Plot of Im[f]

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[2], {x -> 3}}
{3 Sqrt[3], {x -> -2}}
$\endgroup$
2
  • $\begingroup$ Unfortunately, the third solution {3 Sqrt[3], {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. $\endgroup$
    – user64494
    Jan 27, 2019 at 17:57
  • $\begingroup$ 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. $\endgroup$ Jan 30, 2019 at 9:57

Your Answer

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

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