I am trying to implement Newton's method for a given function f[x] and a starting x-value xval. The code should print out each new x-value as it approaches one of the zeroes of the functions. g[x] is a function that returns the new x-value given an old x-value.
f[x_] = x^3 - x; g[x_] = x - (f[x]/D[f[x], x]); zeroes = (x /. Solve[f[x] == 0, x]); xval = 1000; shouldStop = False; While[Not[shouldStop], distances = g[xval] - zeroes; results = Map[Less[Abs[#], 0.00001] &, Abs[distances]]; xval = g[xval]; Print[N[xval]]; shouldStop = Or @@ results]
I am not sure why, but as the while loop continues, each iteration takes longer to run. Any help?