2
$\begingroup$

I have been having some trouble with NMinimize. I am trying

NMinimize[f[x], x]

where f is a complicated function. The whole reason I am using NMinimize is because f is too complicated to be able to compute for an arbitrary x, but for a fixed value of x it is fine.

Well NMinimize seemed to be taking up an outrageous amount of time and memory, so I added a print statement inside f

f[x_]:= Module[{},
Print[x];
...
]

and ran the NMinimize code again. To my surprise, Mathematica prints x$4770, the name of a local variable. Why is NMinimize calling my function with non-numeric values? How can I stop it from doing so?

$\endgroup$
1
  • 3
    $\begingroup$ Just a wild guess, try adding ?_NumericQ to the argument of the function called. see support.wolfram.com/kb/3820 $\endgroup$
    – Nasser
    Nov 17, 2013 at 0:32

1 Answer 1

4
$\begingroup$

It seems that putting

NMinimize[Hold[f[x]], x]

solved the issue.

$\endgroup$
1
  • $\begingroup$ While this works, it most probably is by accident and not documented, so it could change in future versions (although that is very unlikely). It has worked like that for many versions now. For a more detailed discussion see eg. here. The "supported"/documented solution for this problem is to re-define f to ClearAll[f]; f[x_?NumericQ]:=... as Nasser mentioned in his comment. $\endgroup$ Mar 29, 2022 at 7:02

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.