0
$\begingroup$

I have a complicated code which puts out one value, which I want to minimize. Instead of copy pasting my code I present here a much simpler code which produces the same error.

range[x_] := Range[1, x]
f2[x_, y_, z_] := 
 Sum[(Sin[x] Exp[z] ArcTan[y])^n, {n, 1, x}] Norm[range[x]]

Please do not change this code above in your answer since it's not the real code. If I evaluate f2[x,y,z] for some positive and real x,y,z, I get a number. Just as the code I use.

However, if I do this:

NMinimize[{f2[x, y, z], 4 <= x <= 15, 2 <= y <= 4, 4 <= z <= 9}, {x, 
  y, z}]

I get an error message:

Range::range: Range specification in Range[1,x] does not have appropriate bounds.

How can I change Nminimize, or do something to the function f2 (without changing the code which creates f2) such that NMinimize works?

$\endgroup$
  • 4
    $\begingroup$ You need to make range only evaluate for numerical values. Try using range[x_?NumericQ] := Range[1, x]. $\endgroup$ – Chris K Apr 11 at 13:31
  • 4
    $\begingroup$ f2 should have its argument for x constrained to being numeric, i.e., f2[x_?NumericQ, y_, z_] := .... As pointed out in the error message, f2 is calling range with a symbolic value for x $\endgroup$ – Bob Hanlon Apr 11 at 13:37
  • 3
    $\begingroup$ See mathematica.stackexchange.com/questions/18393/… for more info $\endgroup$ – Chris K Apr 11 at 13:40

Your Answer

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

Browse other questions tagged or ask your own question.