3
$\begingroup$

I'm trying to maximize a (quite simple) polynomial inside a sphere. The command is simply:

f[x1_, x2_, x3_] := -(x1/36) + x1^3/67 + x2/25 - (x1^2  x2)/12 + (x1  x2^2)/15 - x2^3/30 + 
   x3/27 - (2  x1^2  x3)/11 + (x1  x2  x3)/35 - (x2^2  x3)/11 + 
   x1^2  x2^2  x3 + (x1  x3^2)/22 - (x2  x3^2)/18 - x3^3/49;
Maximize[{f[x1, x2, x3], x1^2 + x2^2 + x3^2 <= 1}, {x1, x2, x3}] // N

The result I get is:

(* {-149457., {x1 -> -0.873639, x2 -> 64.5, x3 -> -48.}} *)

which grossly violates the constraint x1^2 + x2^2 + x3^2 <= 1, and even the function's value is incoherent, since it has positive values both within the constraint and outside.

To me, this seems like a textbook usage of Maximize. Is this a Mathematica bug?

$\endgroup$
1
  • $\begingroup$ As @Domen showed, this is not a bug. Maximize returns a result that satisfies the constraint. But numericizing that result is prone to cancelllation error. $\endgroup$ Commented Sep 12 at 16:37

1 Answer 1

10
$\begingroup$

If you drop N, you will see that the result is a very long and complex expression, full of high-degree Root objects. I would say that the result isn't really a bug, but shows an accumulation of numerical errors, which you can avoid by using arbitrary-precision calculations (set by the second argument of N), for example,

Maximize[{f[x1, x2, x3], x1^2 + x2^2 + x3^2 <= 1}, {x1, x2, x3}] // N[#, 5]&
(* {0.057092, {x1 -> -0.87364, x2 -> -0.026513, x3 -> -0.48585}} *)

Alternatively, you can use numerical maximization with NMaximize:

NMaximize[{f[x1, x2, x3], x1^2 + x2^2 + x3^2 <= 1}, {x1, x2, x3}]
(* {0.0570927, {x1 -> -0.873678, x2 -> -0.0266629, x3 -> -0.485778}} *)
$\endgroup$
5
  • $\begingroup$ I would have thought that N[Maximize[...]] and Maximize[...] // N would do the same thing in Mathematica and yield the same results. Why don't they? $\endgroup$ Commented Sep 12 at 11:47
  • $\begingroup$ @MichaelSeifert, hmm, have you perhaps missed that there is a second argument: N[Maximize[...], 5], where 5 is the desired precision? $\endgroup$
    – Domen
    Commented Sep 12 at 12:58
  • $\begingroup$ Ah, I did miss that. So Maximize[... , 5] // N should yield the same result. $\endgroup$ Commented Sep 12 at 14:06
  • $\begingroup$ @MichaelSeifert, I've edited the answer to emphasize that. But I am not really sure what you mean by Maximize[... , 5] // N ... $\endgroup$
    – Domen
    Commented Sep 12 at 14:22
  • $\begingroup$ That was a mistake on my part, trying to figure out how one would get the same results using the postfix notation. The command you used in your revised version is what I had in mind but failed to express correctly. $\endgroup$ Commented Sep 12 at 15:41

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.