# How to find the global maximum for complicated expressions with NMaximize?

Posted 10 years ago
9082 Views
|
3 Replies
|
0 Total Likes
|
 Hello, I have a complicated expression and I'd like to know where the global maximum locates.  To find the global maximum numerically in a certain range, I used NMaximize in Mathematica, but, since my equation is too complicated and might generate complex numbers with some ranges of values, Mathematica stops running my script and returns error message whenever it meets complex numbers during computation (it assumes all the parameters are real numbers in NMaximize by default). In order to exclude imaginary numbers, I constrained all the cube root and square root terms in my expression to be real. However, the computation was still stopped by complex numbers and returned error message. I was wondering has anyone here come across the same problem or know what might be the reason?Thanks for the help. Below is my code:  NMaximize[{-(10.3024 b (-72. a1^3 b^2 B + 6.9282 Sqrt[a1^6 b^3 (2. (b - 2. ^3 + 108 b B^2)])^(1/3) N0 (-1. + PE) (a1^2 B^2 (-0.5 + 1. PE) + 0.109198 (-72. a1^3 B^3 PE + 6.9282 Sqrt[a1^6 B^6 (2. + PE (-12. + (132. - 16. PE) PE))])^(2/3)))/(B (a1^2 b (-4.57886 b + 9.15771  + 1. (-72. a1^3 b^2 B +6.9282 Sqrt[a1^6 b^3 (2. (b - 2. ^3 + 108 b B^2)])^(2/3)) (0.125 + 1. PE) (-72. a1^3 B^3 PE + 6.9282 Sqrt[a1^6 B^6 (2. + PE (-12. + (132. - 16. PE) PE))])^(1/3) \[Rho]2), 1 >= PE >= 0 && a1 > 0 && b > 0 && B > 0 && N0 > 0 && \[Rho]2 > 0 &&   Element[Sqrt[a1^6 b^3 (2. (b - 2. ^3 + 108 b B^2)], Reals] &&   Element[(-72. a1^3 b^2 B +6.9282 Sqrt[a1^6 b^3 (2. (b - 2. ^3 + 108 b B^2)])^(1/3), Reals] &&   Element[ (-72. a1^3 B^3 PE +6.9282 Sqrt[a1^6 B^6 (2. + PE (-12. + (132. - 16. PE) PE))])^(2/3), Reals] &&  Element[Sqrt[a1^6 B^6 (2. + PE (-12. + (132. - 16. PE) PE))],Reals] && Element[(-72. a1^3 b^2 B +6.9282 Sqrt[a1^6 b^3 (2. (b - 2. ^3 + 108 b B^2)])^(2/3),Reals] && Element[Sqrt[a1^6 b^3 (2. (b - 2. ^3 + 108 b B^2)], Reals] && Element[(-72. a1^3 B^3 PE +6.9282 Sqrt[a1^6 B^6 (2. + PE (-12. + (132. - 16. PE) PE))])^(1/3), Reals] && Element[Sqrt[a1^6 B^6 (2. + PE (-12. + (132. - 16. PE) PE))],Reals]}, {PE, a1, b, B, N0, \[Rho]2}]And this is the error message it returned:NMaximize::nrnum: The function value 1.10779 +1.35959 I is not a real number at {a1,b,B,N0,PE,\[Rho]2} = {1.94959,1.24058,1.25931,1.9308,0.306576,1.2403}. >> Then I subsituded the set of values that error message provided back to constrained terms and found there are two cubic root terms that still generated imaginary numbers after they were constrained: (-72. a1^3 b^2 B + 6.9282 Sqrt[a1^6 b^3 (2. (b - 2. B)^3 + 108 b B^2)])^(1/ 3) /.   {a1 -> 1.9495938054894624, b -> 1.2405752948531126,    B -> 1.2593144096464886, N0 -> 1.9307969865057644,    PE -> 0.30657581321875266, \[Rho]2 -> 1.2403032542691323}  >>1.08472 + 1.8788 I  (-72. a1^3 b^2 B + 6.9282 Sqrt[a1^6 b^3 (2. (b - 2. B)^3 + 108 b B^2)])^(2/3) /.   {a1 -> 1.9495938054894624, b -> 1.2405752948531126,   B -> 1.2593144096464886, N0 -> 1.9307969865057644,   PE -> 0.30657581321875266, \[Rho]2 -> 1.2403032542691323}>>-2.35325 + 4.07595 I
3 Replies
Sort By:
Posted 10 years ago
 First, you have some unbalanced parentheses in the typed expression above, making it difficult to be sure we are working with the same function.  I put the extra needed closing parens where I thought they were needed, but you will know better where they actually go.Second, to make it more readable and separate the constraints handling etc, I wrote a named functionmyExpression[a_, b_, c_, d_, e_, f_]:= N[Re[ --- equation ---]]--- so that we are making sure the function always returns a real value with machine precision.When I then as for NMaximize[myExpression[a,b,c,d,e,f], {a,b,c,d,e,f}] I get a failure to converge after the iteration limit, because Mathematica thinks it may be unbounded, with the function obtaining the value -2.34199x10^15at the point -a -> 2.15529, b->0.994733, c-> 0.744313, d-> -0.282101, e-> 0.463114, f-> 2.14101x10^-16Note the last argument being very close to but not exactly equal to zero is what gave the high function value.I do not know if that apparently "pole" is an artifact of my putting the missing parens in the wrong place, however.I hope this helps.
Posted 10 years ago
 In the NMaximize documentation there is a note on how to specify a starting interval, which might help in your problem.NMaximize[{-Cos + Exp[(x - 0.5) y], x^2 + y^2 < 1}, {{x, -1, 0},   y}]
Posted 10 years ago
 In Mathematica 9 there is a CubeRoot function that returns Reals for negative arguments. In[2]:= ?CubeRoot                                                                         CubeRoot gives the real-valued cube root of x.