I am trying to solve an equation that contains the minimzer of a function. A simple example is:
x[p_] := xx /. NMinimize[(xx - p)^2, xx][[2]]
NSolve[x[p] == 0, p]
During evaluation of In[1]:= NMinimize::nnum: The function value (-0.829053-p)^2 is not a number at {xx} = {-0.829053}. >>
During evaluation of In[1]:= NMinimize::nnum: The function value (-0.829053-p)^2 is not a number at {xx} = {-0.829053}. >>
During evaluation of In[1]:= NMinimize::nnum: The function value (-0.829053-p)^2 is not a number at {xx} = {-0.829053}. >>
During evaluation of In[1]:= General::stop: Further output of NMinimize::nnum will be suppressed during this calculation. >>
During evaluation of In[1]:= ReplaceAll::reps: {xx} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
During evaluation of In[1]:= ReplaceAll::reps: {xx} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
Out[2]= {}
Does anyone know how to do this? (Of course the example is trivial, the actual functions I am using are much more complicated and require NMinimize and the same error arises there.)
This one works (thanks Michael!):
x[p_?NumericQ] := xx /. NMinimize[(xx - p)^2, xx][[2]]
FindRoot[x[p], {p, 0}]
Out[7]= {p -> 0.}
x[p_?NumericQ] :=.... See http://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/26037#26037. – Michael E2 Aug 02 '16 at 18:24NSolve[]is not really meant for handling those. – J. M.'s missing motivation Aug 02 '16 at 18:25NSolvemight not be the best choice.FindRootmay work better on your actual function. – Michael E2 Aug 02 '16 at 18:25