TransWikia.com

Syntax error in NMaximize despite following official documentation

Mathematica Asked on June 1, 2021

This is the code I’m trying to run:

NMaximize[ {k, (a + b + c + d + e + f + g + h + i + j + k == 
     1) && (b + 2*c + 3*d + 4*e + 5*f + 6*g + 7*h + 8*i + 9*j + 10*k ==
      6) &&  (b + 4*c + 9*d + 16*e + 25*f + 36*g + 49*h + 64*i + 
      81*j + 100*k == 42)}, {a , b, c, d, e, f, g, h, i, j, 
   k} [Element] NonNegativeReals ]

This is the error I’m getting:

NMaximize::ivar: (a|b|c|d|e|f|g|h|i|j|k)[Element][DoubleStruckCapitalR]&&a>=0&&b>=0&&c>=0&&d>=0&&e>=0&&f>=0&&g>=0&&h>=0&&i>=0&&j>=0&&k>=0 is not a valid variable.

The syntax I’m using is NMaximize[{f,cons},x∈reg], which is suggested on the official website.

The f (function) part is just k, the "x∈reg" (region restriction) part is

{a , b, c, d, e, f, g, h, i, j, 
       k} [Element] NonNegativeReals

and the cons (constraints) part is

(a + b + c + d + e + f + g + h + i + j + k == 
         1) && (b + 2*c + 3*d + 4*e + 5*f + 6*g + 7*h + 8*i + 9*j + 10*k ==
          6) &&  (b + 4*c + 9*d + 16*e + 25*f + 36*g + 49*h + 64*i + 
          81*j + 100*k == 42)

So where’s the issue?

2 Answers

Try this

NMaximize[{k, (a + b + c + d + e + f + g + h + i + j + k == 
     1) && (b + 2*c + 3*d + 4*e + 5*f + 6*g + 7*h + 8*i + 9*j + 
      10*k == 6) && (b + 4*c + 9*d + 16*e + 25*f + 36*g + 49*h + 
      64*i + 81*j + 100*k == 42),
  ## & @@ Thread[{a, b, c, d, e, f, g, h, i, j, k} >= 0]}, {a, b, c, d,
   e, f, g, h, i, j, k}, Reals]
{0.266667, {a -> 0., b -> 0., c -> 0., d -> 0., e -> 0.333333, 
  f -> 0.4, g -> 0., h -> 0., i -> 0., j -> 0., k -> 0.266667}}

And you can directly use Maximize, get

{4/15, {a -> 0, b -> 0, c -> 0, d -> 0, e -> 1/3, f -> 2/5, g -> 0, h -> 0, i -> 0, j -> 0, k -> 4/15}}

Correct answer by wuyudi on June 1, 2021

NonNegativeIntegers is not a region,it is just a algebra field.

You can use

NMaximize[{k, a + b + c + d + e + f + g + h + i + j + k == 1, 
  b + 2*c + 3*d + 4*e + 5*f + 6*g + 7*h + 8*i + 9*j + 10*k == 6, 
  b + 4*c + 9*d + 16*e + 25*f + 36*g + 49*h + 64*i + 81*j + 100*k == 
   42}, {a, b, c, d, e, f, g, h, i, j, k} ∈ 
  ImplicitRegion[
   Thread[{a, b, c, d, e, f, g, h, i, j, k} >= 0], {a, b, c, d, e, f, 
    g, h, i, j, k}]]

Or simpely

NMaximize[{k, a + b + c + d + e + f + g + h + i + j + k == 1, 
  b + 2*c + 3*d + 4*e + 5*f + 6*g + 7*h + 8*i + 9*j + 10*k == 6, 
  b + 4*c + 9*d + 16*e + 25*f + 36*g + 49*h + 64*i + 81*j + 100*k == 
   42, {a, b, c, d, e, f, g, h, i, j, k} >= 0}, {a, b, c, d, e, f, g, 
  h, i, j, k}]

Or

NMaximize[{k, a + b + c + d + e + f + g + h + i + j + k == 1, 
  b + 2*c + 3*d + 4*e + 5*f + 6*g + 7*h + 8*i + 9*j + 10*k == 6, 
  b + 4*c + 9*d + 16*e + 25*f + 36*g + 49*h + 64*i + 81*j + 100*k == 
   42, {a, b, c, d, e, f, g, h, i, j, k} ∈ 
   NonNegativeReals}, {a, b, c, d, e, f, g, h, i, j, k}]

etc.

Answered by cvgmt on June 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP