# Newton's method of tangents

I am trying to solve this problem.I need to find a zero of a given function with Newton's method of tangents.The condition is f(x0)*f'(x0)>0 and then calculate xn+1=xn-(f(xn)/f'(xn)) I am now sure that this is the right way to find a zero with the Newton's method of tangents. If you can check.Thank you.

f[x_] := x^2 - 7*x + 6;
For[i = -10, i < 10, i++,
If[f[i]*f''[i] > 0, Print[i \[Minus] (f[i]/f'[i])]]]

• Note that when entering the definition of f into Mathematica's front end, you do not need to enter the explicit multiplication sign * in 7*x. And you do not need the terminal semicolon (;) there, either, since with the use of := you're using a SetDelayed rather than the Set that would be specified by just ='. – murray Feb 24 '20 at 21:03

Clear[f, i, x]

f[x_] := x^2 - 7*x + 6;


For comparison, the roots are

Solve[f[x] == 0, x]

(* {{x -> 1}, {x -> 6}} *)


Rather than using For, use NestList for a fixed number of iterations

NestList[# - f[#]/f'[#] &, 3., 20]

(* {3., -3., -0.230769, 0.796987, 0.992376, 0.999988, 1., 1., 1., 1., 1., 1., \
1., 1., 1., 1., 1., 1., 1., 1., 1.} *)

NestList[# - f[#]/f'[#] &, 7., 20]

(* {7., 6.14286, 6.00386, 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., \
6., 6., 6., 6., 6.} *)


Or use FixedPointList to stop once the algorithm has converged

FixedPointList[# - f[#]/f'[#] &, 3.]

(* {3., -3., -0.230769, 0.796987, 0.992376, 0.999988, 1., 1., 1.} *)

FixedPointList[# - f[#]/f'[#] &, 7.]

(* {7., 6.14286, 6.00386, 6., 6., 6., 6.} *)

• You might also want to use NestWhile or NestWhileList`to incorporate a stopping criterion, such as when two successive approximations are sufficiently near one another (absolutely or relatively). – murray Feb 24 '20 at 20:19