The AlgorithmFor a general function, f, let x[n] be the approximation to the root at the næî step of Newton's method. The formula for the tangent line at x[n] is
Clear[f] tangent[x,x[n]]
If we find the zero of this linear function we get the formula for Newton's method.
Simplify[Solve[ tangent[t,x[n]] == 0, t]]
Incorporating this formula into a loop, we get
Clear[newton]
newton[f_,x0_?NumberQ,tolerance_:(N[10^-20])] :=
(* default value of tolerance is 10^-20 *)
Module[{
count = 1,
approxX = N[x0,30],
oldApproxX = N[x0 + 1000 tolerance,30]
},
While[Abs[approxX - oldApproxX] > tolerance,
Print[count, ": ", N[approxX,20]];
oldApproxX = approxX;
approxX = approxX - f[approxX]/f'[approxX];
++count
];
Print[count, ": ", N[approxX,20]];
approxX (* return result *)
]However, Newton's method can also be implemented recursively.
recursiveNewton[f_,x_?NumberQ,previousX_:0
,tolerance_:(N[10^-20])] :=
If[Abs[x - previousX] > tolerance,
Print[N[x,20]];
recursiveNewton[
f, N[x - f[x]/f'[x],30], x, tolerance],
x (* return result *)
]Reference: These functions are adapted from Nancy Bachman, The Mathematica Journal, Volume 2, Issue 2, 1992.
Mathematica has been optimized for functional programs, such as recursiveNewton, rather than for the more familiar procedural programs, such as newton. Lisp and Prolog are functional programming languages; whereas, Fortran, C, Pascal, and Basic are procedural programming languages. Mathematica allows programming in either style.
Let's perform timings on these implementations of Newton's method to compare the two styles.
g[x_] := x^2 - 4
newton[g,1]//Timing
recursiveNewton[g,1,2]//Timing
Mathematica uses Newton's method for its built-in function, FindRoot. Let's see how fast it is compared to our implementations.
FindRoot[x^2 - 4 == 0,{x,1}]//Timing