Question: Dividing by zero

This thread stimulated me into playing with a few ideas,

I pieced together this little newton-raphson procedure...

> newton := proc( f, # the function x0 # the initial guess n, # step count limit tol # error tolerance)

local x, g, k;

g := D(f); # the derivative f’(x)

x[0] := evalf(x0); # initialize the iteration

for k from 1 to n do # loop for newton’s iteration

# Newton’s iteration formula

x[k] := evalf( x[k-1] - f(x[k-1])/g(x[k-1]) );

# exit early if possible, with luck

if abs(x[k]-x[k-1]) < tol then

return x[k];

end if;

end do;

return "Newton’s iteration fails"

end proc:

If f'(x) at some stage gets very close to or becomes 0 the procedure returns "Newton’s iteration fails". I would like to modify the program some how such that when f'(x[k])x[k]+0.00001 and resumes to normal calculations. Any Ideas where or how I could put in this conditional statement? Or should I use while?

Thank you in advance!

Please Wait...