Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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 for Linux x86.

share|improve this question

marked as duplicate by m_goldberg, Artes, Mr.Wizard Oct 20 '13 at 5:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Related: Integrating a list of functions – ssch Oct 19 '13 at 16:25
up vote 4 down vote accepted

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

(* x *)

(* Same thing happens with NumericQ since then f[x] doesn't evaluate *)
f[x_?NumericQ] := 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. } *)
share|improve this answer
Thanks, I get it now. Great answer. – Artur Gower Oct 19 '13 at 18:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.