Message Boards Message Boards

0
|
178080 Views
|
6 Replies
|
6 Total Likes
View groups...
Share
Share this post:

How to resolve "function value X is not a number" issue?

Posted 9 years ago

Dear Community,

I am just new to Mathematica that came with my Raspberry Pi2. For a while now I have been looking for a program, which is able to minimize a discontinuous and nonlinear problem. Up to now I created the following formulas:

f1[x_] := 
 Piecewise[{{0.00000000000252413096488599 x^4 - 
     0.0000000295981935774312 x^3 + 0.0001268180409138660 x^2 - 
     0.239339875030011 x + 368.934089585833, x > 0}, {0, x <= 0}}]

f2[x_] := 
 Piecewise[{{0.00000000000327848026710691 x^4 - 
     0.0000000379764365996404 x^3 + 0.0001584280843487410 x^2 - 
     0.284905693712486 x + 386.639837124850, x > 0}, {0, x <= 0}}]

f3[x_] := 
 Piecewise[{{0.00000000000273770414832564 x^4 - 
     0.0000000245466477791088 x^3 + 0.0000876247218487314 x^2 - 
     0.151272246878742 x + 298.178569447775, x > 0}, {0, x <= 0}}]

f4[x_] := 
 Piecewise[{{0.00000000000151824463118546 x^4 - 
     0.0000000164748347339070 x^3 + 0.0000716029554621768 x^2 - 
     0.140270401260495 x + 293.998348739492, x > 0}, {0, x <= 0}}]

NMinimize[{f1[w] + f2[x] + f3[y] + f4[z], 
  Plus[w, x, y, z] == 7000}, {w \[Element] Interval[{0, 4000}], 
  x \[Element] Interval[{0, 4000}], y \[Element] Interval[{0, 3000}], 
  z \[Element] Interval[{0, 3000}]}]

Unfortunately now I get the following error messages three times before the calculation is aborted:

During evaluation of In[8]:= NMinimize::nnum: The function value {812.066} is not a number at {Subscript[w, 1],Subscript[x, 1],Subscript[y, 1],Subscript[z, 1]} = {1052.02,1848.71,2479.67,1619.59}.

Is there any quick way of resolving this issue? Did I enter something in a wrong way? At least the sum of the resulting values for w,x,y, and z being 7000 is correct, maybe 812,066 is a local minimum already. But why is there an error message? I also tried to use Minimize and different Methods, but the outcome is the same in all cases.

And it is just a LOCAL minimum: As one might be able to see from the equation, fx[0]=0. A minimum value, f.e. for the entered w+x+y+z==7000, should incorporate that some of the functions are set to zero! Is there any way to include this possibility in a fast way? Or do I have to write a minimization for all 16 possibilities and minimize these again?

Best regards, Peter

POSTED BY: Peter Bergmann
6 Replies
Posted 9 years ago

I edited the problem and get pretty good results with:

f[reqpower_] := MininmalBy[{
  Check[NMinimize[{ae1[w], {w == reqpower, 0 <= w <= maxae1}}, {w}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae2[x], {x == reqpower, 0 <= x <= maxae2}}, {x}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae3[y], {y == reqpower, 0 <= y <= maxae3}}, {y}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae4[z], {z == reqpower, 0 <= z <= maxae4}}, {z}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae1[w] + ae2[x], {w + x == reqpower, 
      0 <= w <= maxae1, 0 <= x <= maxae2}}, {w, x}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae1[w] + ae3[y], {w + y == reqpower, 
      0 <= w <= maxae1, 0 <= y <= maxae3}}, {w, y}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae1[w] + ae4[z], {w + z == reqpower, 
      0 <= w <= maxae1, 0 <= z <= maxae4}}, {w, z}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae2[x] + ae3[y], {x + y == reqpower, 
      0 <= x <= maxae2, 0 <= y <= maxae3}}, {x, y}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae2[x] + ae4[z], {x + z == reqpower, 
      0 <= x <= maxae2, 0 <= z <= maxae4}}, {x, z}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae3[y] + ae4[z], {y + z == reqpower, 
      0 <= y <= maxae3, 0 <= z <= maxae4}}, {y, z}, 
    Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae1[w] + ae2[x] + ae3[y], {w + x + y == reqpower, 
      0 <= w <= maxae1, 0 <= x <= maxae2, 0 <= y <= maxae3}}, {w, x, 
     y}, Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae1[w] + ae2[x] + ae4[z], {w + x + z == reqpower, 
      0 <= w <= maxae1, 0 <= x <= maxae2, 0 <= z <= maxae4}}, {w, x, 
     z}, Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae1[w] + ae3[y] + ae4[z], {w + y + z == reqpower, 
      0 <= w <= maxae1, 0 <= y <= maxae3, 0 <= z <= maxae4}}, {w, y, 
     z}, Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae2[x] + ae3[y] + ae4[z], {x + y + z == reqpower, 
      0 <= x <= maxae2, 0 <= y <= maxae3, 0 <= z <= maxae4}}, {x, y, 
     z}, Method -> "RandomSearch"], 999999],
  Check[NMinimize[{ae1[w] + ae2[x] + ae3[y] + 
      ae4[z], {w + x + y + z == reqpower, 0 <= w <= maxae1, 
      0 <= x <= maxae2, 0 <= y <= maxae3, 0 <= z <= maxae4}}, {w, x, 
     y, z}, Method -> "RandomSearch"], too much power needed]},
  First]

When evaluating this formula with f[reqpower], the result looks like this:

MininmalBy[{999999, 999999, 999999, 999999, {396.497, {w -> 3160.39, 
   x -> 1839.61}}, {390.376, {w -> 2515.03, 
   y -> 2484.97}}, {389.794, {w -> 3140.25, 
   z -> 1859.75}}, {389.996, {x -> 2293.23, 
   y -> 2706.77}}, {389.955, {x -> 3241.48, 
   z -> 1758.52}}, {383.048, {y -> 2730.85, 
   z -> 2269.15}}, {602.931, {w -> 1733.48, x -> 1638.86, 
   y -> 1627.66}}, {600.422, {w -> 1755.15, x -> 1655.01, 
   z -> 1589.84}}, {596.013, {w -> 1752.93, y -> 1659.67, 
   z -> 1587.4}}, {592.798, {x -> 1674.27, y -> 1707.44, 
   z -> 1618.29}}, {602.931, {w -> 1733.48, x -> 1638.86, 
   y -> 1627.66, z -> 0.}}}, First]

Is there a trick to get the final result without having to look for the minimum list of values manually? According to the help database, MinimalBy[Arg,First] should do it...? Thanks a lot to whoever supports me on this! :)

EDIT: Corrected brackets, but no change in the outcome...

POSTED BY: Peter Bergmann
Posted 9 years ago

Okay, I found the mistake... It was a typo (MininmalBy instead of MinimalBy), sorry for bothering. -.-

POSTED BY: Peter Bergmann
Posted 9 years ago

This is great, Bill, thanks a lot! Is there any Explanation for this behavior? Just want to be able to avoid this in the future...

POSTED BY: Peter Bergmann
Posted 9 years ago

Realize the software has no "sense", it doesn't look at your problem, perhaps graph it, think a bit and realize it should check all the boundaries, just in case.

Without the Method -> "RandomSearch" it (mostly) looks at some how the values are changing in the immediate neighborhood of some point, calculates in which direction the function is decreasing most rapidly, steps off in that direction by some distance and does this all over again. After repeating this again and again it finds itself in a place where everywhere around it the function is either the same or even increases and it declares that it is done.

With the Method -> "RandomSearch" it (mostly) throws a dart up in the air, goes over and measures the function at that point, if it is smaller than that last best value it keeps this as the new last best value. After a while it doesn't find any better last best values and declares that it is done.

Now imagine in the vast space that your function covers you have one or a few places where the function grows to a very high sharp peak, except at exactly those peaks you have drilled a deep hole of infinitely small diameter.

Without the Method -> "RandomSearch" the software will attempt to run down hills and get far away from peaks. It will not "think" I am going up this peak and making this worse and worse, but I suppose I should keep making it worse, even though you just asked me to minimize this, just in case I find something better up there.

With the Method -> "RandomSearch" there is almost no chance your dart will happen to hit that infinitely small diameter hole in the vast space of your function.

You, on the other hand, can think and realize that your problem includes special cases at the boundaries where your function is not "smooth" and you can check all the special points.

POSTED BY: Bill Simpson
Posted 9 years ago

Hi Bill, thanks for this very visual explanation! But the meaning of the RandomSearch Method was relatively clear for me. I should have staded my question more precisely, because I referred to writing the constraints with 0<=x<=4000 instead of using intervals or something like x,y,z >=0 and x,y<=3000, which I had before and also resulted in the same error message...?

POSTED BY: Peter Bergmann
Posted 9 years ago

This works without error

In[6]:= NMinimize[{f1[w] + f2[x] + f3[y] + f4[z], {w + x + y + z == 7000,
   0 <= w <= 4000, 0 <= x <= 4000, 0 <= y <= 3000, 0 <= z <= 3000}}, {w, x, y, z}]

Out[6]= {793.412, {w -> 1829.54, x -> 1709.12, y -> 1791.87, z -> 1669.47}}

For this problem this happens to find a smaller minimum

In[7]:= NMinimize[{f1[w] + f2[x] + f3[y] + f4[z], {w + x + y + z == 7000,
   0 <= w <= 4000, 0 <= x <= 4000, 0 <= y <= 3000, 0 <= z <= 3000}}, {w, x, y, z}, 
   Method -> "RandomSearch"]

Out[7]= {583.291, {w -> 2153.49, x -> 0., y -> 2663.08, z -> 2183.44}}

And this finds an even smaller minimum.

In[8]:= NMinimize[{f1[w] + f2[x] + f3[y], {w + x + y == 7000, 
   0 <= w <= 4000, 0 <= x <= 4000, 0 <= y <= 3000}}, {w, x, y}, 
 Method -> "RandomSearch", MaxIterations -> 10^4]

Out[8]= {384.535, {w -> 4000., x -> 0., y -> 3000.}}

I suspect almost any minimization method is going to have problems where some or all your functions have smoothly growing very large values adjacent to isolated points where the functions are much smaller. So if you have functions like that then I suspect that your idea of checking all the isolated points, in addition to using a general minimization method, might be the best approach.

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract