Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to call the NMinimize function for my own defined calculations but I got an error. Below is a simple example.

NMinimize[x^4 - 3 x^2 - x, x] returns
{-3.51391, {x -> 1.30084}} but

f[x_] := x^4 - 3 x^2 - x;


NMinimize[f, x] gives an error

NMinimize::nnum: The function value f is not a number at {x} = {-0.829053}.

What's wrong with this approach?

share|improve this question
NMinimize[f[x], x], i.e. f[x] instead of f as first argument. –  F'x Mar 25 '12 at 22:45
This question shouldn't be closed, since there is a reasonable answer even to an intermediate user. One thinks in a purely mathematical sense about NMinimize[f, x] and there should be an answer why it doesn't work in Mathematica. I don't find this to localized since it adresses the very basics of the Mathematica core and the current answer may be misleading until it does not emphasize the reasons of the error. –  Artes Mar 25 '12 at 23:19
@Artes we are currently discussing the policy which should be adopted for such questions on the meta site –  F'x Mar 26 '12 at 9:22
@Artes Just to be clear, and while I have no strong opinion either way, the reason one could consider it too localized is that it's phrased in a localized way. If someone did Plot[f,{x,-10,10}] and it didn't work, would they have found the solutions below by searching in this site? No. Hence, "too localized"; only someone with the identical problem would have found the current form of this question useful. At least, this is how I view it. –  acl Mar 26 '12 at 11:11
@acl I think there could be a good answer (although this issue can be easily explained) why NMinimize[f, x] produces an error. I find Mathematica frequentely involves mathematically inconvenient notation and this is the core of what I meant. I am sorry for too concise explaining of my point, I have no time at the moment . –  Artes Mar 26 '12 at 12:25

2 Answers 2

up vote 2 down vote accepted

The symbol f is the name of the function, and calling f with the proper argument structure (i.e. 1 argument, like x) replaces it with the function value (the definition on the right hand side). Since f is not called with an argument in your example in NMinimize, it is not replaced by the right hand side, thus a symbol is left which cannot be minimized.

If you define f as a function of y and not x, or even as a pure function:

f = #^4 - 3 #^2 - # &;

it is still not enough, as NMinimize then is replaced as:

NMinimize[f, x] --> NMinimize[#^4 - 3 #^2 - # &, x]

where # and x are not bound, as x now you can see that x does not appear at all in the function. On the other hand, this works:

NMinimize[foo^4 - 3 foo^2 - foo, foo]

{-3.51391, {foo -> 1.30084}}

Also note, that if you define your function in the standard way like this:

f[x_] := x^4 - 3 x^2 - x;

then calling f on its own returns the symbol f itself, as f does not have any OwnValue (only DownValues), therefore NMinimize[f, x] does not make sense:



{OwnValues[f], DownValues[f]}

{{}, {HoldPattern[f[x_]] :> x^4 - 3 x^2 - x}}

share|improve this answer

Syntax. Try

f[x_] := x^4 - 3 x^2 - x;
NMinimize[f[x], x]

and it works. That is, you call f like so: f[3], so you need f[x] in the first argument of NMinimize

share|improve this answer
f[x_, yyy_] := 2 x^2 + 3 z + 5 /. z -> yyy; NMinimize[f[x, y], {x, y}] As in this example I was trying to define a fitness function to optimize. It was long calculation. When I interrupted I sow that the formula turned to 2 x^2 + 3 y + 5... So, I was trying to figure out the behavior of NMinimum. So it first symbolicly solves and then assigns a value to optimize is that correct ? –  s.s.o Mar 25 '12 at 23:51
no, I don't think it first solves it symbolically. it is a bit more complicated than that (and I am not sure there is space in the comments). perhaps you could post a separate question? –  acl Mar 25 '12 at 23:53
Ok, thx. Before I send a new post I'll check the functions and work on them a bit more. May be later I'll send a new post... –  s.s.o Mar 26 '12 at 0:03
Somewhere I red that some important commands, such as Solve, NSolve, FindRoot, FindMinimum, Minimize, NMinimize, FindFit, DSolve, and NDSolve, give the result in the form of a transformation rule. I thing NMinimize it first assigns variables then finds or transforms to values. It may require to redefine my functions because my fitness functions contain sub functions which are pure numeric calculations... –  s.s.o Mar 26 '12 at 23:32
mathematica.stackexchange.com/questions/333/… similar type of problem and solutions... –  s.s.o Mar 27 '12 at 23:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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