# NMinimize applied to a function which depends on a complicated code

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?

• You need to make range only evaluate for numerical values. Try using range[x_?NumericQ] := Range[1, x]. – Chris K Apr 11 at 13:31
• 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 – Bob Hanlon Apr 11 at 13:37
• See mathematica.stackexchange.com/questions/18393/… for more info – Chris K Apr 11 at 13:40