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?

  • 1
    $\begingroup$ NMinimize[f[x], x], i.e. f[x] instead of f as first argument. $\endgroup$ – F'x Mar 25 '12 at 22:45
  • 3
    $\begingroup$ 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. $\endgroup$ – Artes Mar 25 '12 at 23:19
  • $\begingroup$ @Artes we are currently discussing the policy which should be adopted for such questions on the meta site $\endgroup$ – F'x Mar 26 '12 at 9:22
  • $\begingroup$ @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. $\endgroup$ – acl Mar 26 '12 at 11:11
  • $\begingroup$ @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 . $\endgroup$ – Artes Mar 26 '12 at 12:25

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}}

| 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

| improve this answer | |
  • $\begingroup$ 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 ? $\endgroup$ – s.s.o Mar 25 '12 at 23:51
  • $\begingroup$ 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? $\endgroup$ – acl Mar 25 '12 at 23:53
  • $\begingroup$ 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... $\endgroup$ – s.s.o Mar 26 '12 at 0:03
  • $\begingroup$ 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... $\endgroup$ – s.s.o Mar 26 '12 at 23:32
  • $\begingroup$ mathematica.stackexchange.com/questions/333/… similar type of problem and solutions... $\endgroup$ – s.s.o Mar 27 '12 at 23:38

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.