0
$\begingroup$

If on one hand:

N@Maximize[{Sqrt[y^2 - x^2], x + y^2 == 0 && 0 < y < 2 && -y < x < y}, {x, y}]

{0.5, {x -> -0.5, y -> 0.707107}}

which is what is desired, on the other hand:

NMaximize[{Sqrt[y^2 - x^2], x + y^2 == 0 && 0 < y < 2 && -y < x < y}, {x, y}]

produces a warning:

NMaximize::nrnum: The function value 4.15623 -1.34058 I is not a real number at {x,y} = {-1.35352,0.186697}.

and a consequent incorrect result:

{0.285088, {x -> -0.681805, y -> 0.739008}}

I can't understand why this behavior. Could you explain it to me? Thank you!

$\endgroup$
1
  • 1
    $\begingroup$ NMaximize[{Sqrt[y^2 - x^2]^2, y^2 - x^2 >= 0, x + y^2 == 0 && 0 < y < 2 && -y < x < y}, {x, y}] $\endgroup$
    – cvgmt
    Commented Nov 14, 2022 at 8:20

1 Answer 1

1
$\begingroup$

Sqrt isn't handled properly by NMaximize, I think.

Workaround

NMaximize[{Sqrt@RealAbs[y^2 - x^2], x + y^2 == 0 && 0 < y < 2 && -y < x < y}, {x, y}]
(*{0.5, {x -> -0.5, y -> 0.707107}}*)

gives the expected result.

$\endgroup$
0

Your Answer

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

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