# Passing f[x][] to FindRoot [duplicate]

FindRoot seems to fail for most examples of the form

f[x_?NumericQ] := {x - 3 , x^3};
FindRoot[f[x][], {x, 3}]

{x -> 0.}


I expected {x -> 3}. The same occurs for FindMinimum, even for this case

FindMinimum[{f[x][], x > 1}, {x, 3}]

{1., {x -> 1.}}


What is going here guys? I'm using Mathematica 9.0.1.0 for Linux x86.

You can take parts of expressions just like you can with a list, so what happens here is:

Clear[f];
f[x][]
(* x *)

(* Same thing happens with NumericQ since then f[x] doesn't evaluate *)
f[x_?NumericQ] := 1
f[x][]
(* x *)


To get around this you need to make sure Part doesn't try to extract until it has the result of f, for instance like:

f[x_?NumericQ] := {x - 3, x^3}
g[x_?NumericQ, i_Integer] := f[x][[i]]
FindRoot[g[x,1], {x,987}]
(* {x -> 3. } *)

(* Or a bit more general: *)
nPart[v_List, p__] := With[{res = v[[p]]}, res /; NumericQ[res]]
FindRoot[nPart[f[x], 1], {x, 987}]
(* {x -> 3. } *)

• Thanks, I get it now. Great answer. Oct 19 '13 at 18:10