In Wellin's Programming with Mathematica book, here's one of his implementations of the Newton method, where the iteration runs until the error tolerance is reached.


Clear[findRoot]
findRoot[expr_, {var_, init_}, ϵ_] :=
Module[{xi = init, fun = Function[fvar, expr]},
While[Abs[fun[xi]] > ϵ,
xi = N[xi - fun[xi]/fun'[xi]]];
{var -> xi}]
findRoot[x^3 - 2, {x, 2.0}, 0.0001]
(* {x -> 2.} *)
As you could see the result is clearly wrong. I think it's because of the presence of fvar in the body of Function, which was never defined. I think he meant to use var. I tried that, and it works, but there was a warning that "The variable name has been used twice in a nested scoping construct, in a way that is likely to be an error", with var highlighted in red.

Should I be concerned about this warning? In what circumstances would that be an issue? Please feel free to come up with examples of your own.
Edit: Here's a way that I found that avoid the scoping warning:

Is this a better way? I think the main reason of using Function in Wellin's case and defining a function within the Module here is to make a regular expression (which can't take in variable directly) become a function that could take in a value (xi in this function). What's the best way to do it? This is closely related to this question here.


varinstead of the typofvar, then was he aware of the scoping warning. As a result, my question also asks if it's a good idea to construct the function that way, usingFunction. In other words, do you or other MMA experts on here consider that a good practice? I should have worded my title a little bit better. – seismatica Aug 12 '14 at 23:48fun = Function[Evaluate[var], expr]. – Daniel Lichtblau Aug 13 '14 at 15:55