Try the Evaluated -> False
option:
FindRoot[Print[x]; Sin[x] - Cos[x], {x, .5}, Evaluated -> False]
During evaluation of In[3]:= 0.5
During evaluation of In[3]:= 0.5
During evaluation of In[3]:= 0.5
During evaluation of In[3]:= 0.793408
During evaluation of In[3]:= 0.793408
During evaluation of In[3]:= 0.793408
During evaluation of In[3]:= 0.785398
During evaluation of In[3]:= 0.785398
During evaluation of In[3]:= 0.785398
During evaluation of In[3]:= 0.785398
During evaluation of In[3]:= 0.785398
During evaluation of In[3]:= 0.785398
Out[3]= {x -> 0.785398}
As to the Plot
, try
Plot[Print[x]; Sin[x], {x, Pi/4, Pi/2}, Evaluated -> True];
During evaluation of In[5]:= x
As you see, the behavior is exactly the same as it is for FindRoot
by default. The difference in default behavior can be explained by default values of the Evaluated
option:
Options[#, Evaluated] & /@ {Plot, FindRoot}
{{Evaluated -> Automatic}, {Evaluated -> True}}
It seems that the Automatic
value is equivalent to False
in this case.
Another approach
As Albert Retey mentioned in the comment, the "standard" (and documented) way to monitor evaluations is to define objective function as black-box function by restricting its argument to numerical values only:
In[1]:= f[x_?NumericQ] := (Print[x]; Sin[x] - Cos[x])
FindRoot[f[x], {x, .5}]
During evaluation of In[1]:= 0.5
During evaluation of In[1]:= 0.5
During evaluation of In[1]:= 0.5
During evaluation of In[1]:= 0.793408
During evaluation of In[1]:= 0.793408
During evaluation of In[1]:= 0.793408
During evaluation of In[1]:= 0.785398
During evaluation of In[1]:= 0.785398
During evaluation of In[1]:= 0.785398
During evaluation of In[1]:= 0.785398
During evaluation of In[1]:= 0.785398
During evaluation of In[1]:= 0.785398
Out[2]= {x -> 0.785398}
Additional comparisons
Here I switch off autocompilation for not loading the corresponding package (it affects output of Trace
):
In[1]:= ClearAll[f, x]; f[x_] := x - 1;
Trace[FindRoot[f[x], {x, .5}, Evaluated -> False, Compiled -> False],
TraceInternal -> True] // LeafCount
Trace[FindRoot[f[x], {x, .5}, Evaluated -> True, Compiled -> False],
TraceInternal -> True] // LeafCount
Out[2]= 181
Out[3]= 111
In[4]:= ClearAll[f, x]; f[x_?NumericQ] := x - 1;
Trace[FindRoot[f[x], {x, .5}, Evaluated -> False, Compiled -> False],
TraceInternal -> True] // LeafCount
Trace[FindRoot[f[x], {x, .5}, Evaluated -> True, Compiled -> False],
TraceInternal -> True] // LeafCount
Out[5]= 217
Out[6]= 261
One can see that in simple cases the option Evaluated -> True
reduces number of evaluations but in more complicated cases (black-box function) it cannot help.
Plot
, probably as well asFindRoot
don't necessarily evaluate the whole first argument, as is, in every iteration. They sometimes partially evaluate first, and I am not sure in what cases. So you probably simply shouldn't put there what you want evaluated every time. Probably sinceEvaluationMonitor
exists they have taken the liberty to put some optimizations in the expression preprocessing, where they felt like it. $\endgroup$