I need to maximize a function that involves an alternating sum and a set of constraints. I have tried the following code:

NMaximize[{(-1)^{m}*n!, n + m == 7, m > 0, n > 0}, {m, n}]

However, the error message

NMaximize: The objective function {(-1)^m n!} should be scalar-valued


Are there any numerical maximization methods that can be used for functions like this?


(1) Change (-1)^{m} to (-1)^(m) and (2) add the constraint that m and n are integers:

NMaximize[{(-1)^(m)*n!, n + m == 7, m > 0, n > 0, Element[{m, n}, Integers]}, {m, n}]

{6., {m -> 4, n -> 3}}

  • $\begingroup$ Thank you! I am glad it was such an easy fix. $\endgroup$
    – smallscot
    Aug 20 '19 at 13:33
  • $\begingroup$ @smallscot, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Aug 20 '19 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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