1

FindRoot seems to fail for most examples of the form

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

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

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

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Art Gower
  • 586
  • 2
  • 11

1 Answers1

4

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

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

(* Same thing happens with NumericQ since then f[x] doesn't evaluate *)
f[x_?NumericQ] := 1
f[x][[1]]
(* 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. } *)
ssch
  • 16,590
  • 2
  • 53
  • 88