4

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?

nixeagle
  • 2,263
  • 1
  • 18
  • 24
s.s.o
  • 4,559
  • 2
  • 27
  • 42
  • 1
    NMinimize[f[x], x], i.e. f[x] instead of f as first argument. – F'x Mar 25 '12 at 22:45
  • 3
    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 09: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
  • @acl I tried to explain my point of view here : http://meta.mathematica.stackexchange.com/questions/312/what-should-be-done-with-trivial-code-checking-questions/317#317 – Artes Mar 26 '12 at 21:58
  • @F'x Thanks for directing my attention to that thread. – Artes Mar 26 '12 at 22:07
  • @Artes I'd think that a look at any single example on the NMinimize doc page should make it clear how NMinimize must be used... I agree with F'x that this is unlikely to help future visitors. – Szabolcs Mar 27 '12 at 11:37
  • @Szabolcs Yes, I don't mean the question is very good, however especially for this kind of questions there may appear really interesting answers, so that's why I think it shouldn't be closed. Now, since it's reopened it doesn't seem to be interesting to anyone. – Artes Mar 27 '12 at 12:31
  • I,m new to this forums. I could have asked the question in an other way but I preferred to explain my problem in a simple manner. I wanted to check my approach with alternative ways of thinking and go step by step. Mainly, I did solve it with matlab I could have asked in a different way. I have problems with NMinimize function... I had developed H-infinity norm for mathematica and I calculate the norm of control system defined. By using Norm-inf I want to minimize the control system response. Some how the NMinimize works as in infite loop.So, I was tracing my approach with a better alternative – s.s.o Mar 27 '12 at 12:56

2 Answers2

4

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

István Zachar
  • 47,032
  • 20
  • 143
  • 291
3

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

acl
  • 19,834
  • 3
  • 66
  • 91
  • 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 00: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
  • http://mathematica.stackexchange.com/questions/333/combined-numerical-minimization-and-maximization similar type of problem and solutions... – s.s.o Mar 27 '12 at 23:38