4
$\begingroup$

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;

calling

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?

$\endgroup$
10
  • 1
    $\begingroup$ NMinimize[f[x], x], i.e. f[x] instead of f as first argument. $\endgroup$
    – F'x
    Commented Mar 25, 2012 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
    Commented Mar 25, 2012 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
    Commented Mar 26, 2012 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
    Commented Mar 26, 2012 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
    Commented Mar 26, 2012 at 12:25

2 Answers 2

4
$\begingroup$

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:

f

f

{OwnValues[f], DownValues[f]}

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

$\endgroup$
3
$\begingroup$

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

$\endgroup$
5
  • $\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
    Commented Mar 25, 2012 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
    Commented Mar 25, 2012 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
    Commented Mar 26, 2012 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
    Commented Mar 26, 2012 at 23:32
  • $\begingroup$ mathematica.stackexchange.com/questions/333/… similar type of problem and solutions... $\endgroup$
    – s.s.o
    Commented Mar 27, 2012 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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