Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
1  
Related: Integrating a list of functions –  ssch Oct 19 '13 at 16:25
add comment

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.

1 Answer

up vote 4 down vote accepted

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. } *)
share|improve this answer
    
Thanks, I get it now. Great answer. –  Artur Gower Oct 19 '13 at 18:10
add comment

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