Python: Newston-Raphson I am getting too many iterations and my answer is off in the millions decimal place. Any ideas?
How many is too many? What level of accuracy do you actually want?
i posted that q by mistake the actual question is how do i use newtons method fr findin roots of a polynomial ?
First u need a guess and the guess must be reasonably close to the actual root.
So let your polynomial be some function of x, f(x) and your initial guess be x_0. Then x_1 = x_0 - f(x_0) / f ' (x_0) and iterate till u reach a desired accuracy.
tried this
def computeRoot(poly,x_0,epsilon): count=0 while abs(evaluatePoly(poly,x_0))>epsilon: x1=x_0-(evaluatePoly(poly,x_0)/computeDeriv(poly,x_0)) x_0=x1 count=count+1 return [x1,count]
print computeRoot([-13.39, 0.0, 17.5, 3.0, 1.0], 0.1, .0001)
heres the error Traceback (most recent call last): File "<pyshell#64>", line 1, in <module> print computeRoot([-13.39, 0.0, 17.5, 3.0, 1.0], 0.1, .0001) File "<pyshell#63>", line 5, in computeRoot x1=x_0-(evaluatePoly(poly,x_0)/computeDeriv(poly,x_0)) ZeroDivisionError: float division by zero
Well, if computeDeriv(poly,x_0) (ie the value of the derivative) is zero, you will get this error, no?
What is the polynomial and what is your initial guess?
Join our real-time social learning platform and learn together with your friends!