This is a follow-up question to this question.
Let's say we want to minimize the following function f wrt. x and y. The approach is to minimize first wrt. to y and then wrt. x. If this approach makes sense is not part of the question.
f[x_, y_] := (x - 2)^2 + x ( y - 2)^2
dx[x_, y_] := Block[{xs}, D[f[xs, y], xs] /. xs -> x]
dy[x_, y_] := Block[{ys}, D[f[x, ys], ys] /. ys -> y]
fy[x_?NumericQ] := y /. FindRoot[dy[x, y] == 0, {y, 20}]
dxnoy[x_] := dx[x, fy[x]]
Now using FindRoot on dxnoy yields the correct result:
FindRoot[{dxnoy[x] == 0}, {{x, 5}}]
(* {x -> 2.} *)
However, the process seems to be inefficient. Using EvaluationMonitor in FindRoot reveals that the exact same points x are evaluated multiple times:
Last[Reap[FindRoot[{dxnoy[x] == 0}, {{x, 5}}, EvaluationMonitor :> Sow[x]]]]
(* {{5., 5., 5., 2., 2., 2.}} *)
Why is that? Or better: How can it be avoided? It seems to me that the performance could be a factor 3 better if it wouldn't evaluate the points x=5 and x=2 three times each.
NumberForm[%, 15]yields{{{5.}, {5.}, {5.000000074505806}, {2.}, {2.}, {2.0000000298023224}}}, so the pointsx == 5.andx == 2.actually are computed only twice each. Still, this is an interesting question. – bbgodfrey Nov 23 '16 at 18:10