Ask your own question, for FREE!
Computer Science 6 Online
OpenStudy (turingtest):

Python: Newston-Raphson I am getting too many iterations and my answer is off in the millions decimal place. Any ideas?

OpenStudy (turingtest):

poly=(-13.39,0.0,17.5,3.0,1.0) x=0.1 epsilon=0.001 def evaluate_poly(poly, x): #evaluates a polynomial input as a tuple for a specific x value value=0 power=0 for i in poly: value+=i*x**power power+=1 return value def compute_derivative(poly): #returns a tuple representing the derivative #of the input polynomial as a tuple power=0 deriv=() for i in poly: deriv+=(i*power,) power+=1 return deriv def compute_root(poly, x, epsilon): #find the root of the polynomial poly using #Newton-Raphson method iterations=0 deriv=compute_derivative(poly) derivValue=evaluate_poly(deriv, x) #evaluates the current x value of the derivative of the polynomial value=evaluate_poly(poly, x) while abs(value) > epsilon: value=evaluate_poly(poly, x) derivValue=evaluate_poly(deriv, x) x-=value/derivValue iterations+=1 return x, iterations #outputs a the root and number of iterations as a tuple I get an output of (0.80679329312..., 139) the answer is supposed to be (0.806790679..., 8) My answer for the root is darn close, but why so am I getting so many iterations? I tried debugging it but all the numbers seem normal on every iteration, I'm just not getting there fast enough somehow...

OpenStudy (turingtest):

my answer is within epsilon of the given answer, so I'm really just worried about the iterations

OpenStudy (konradzuse):

A million decimal places.....? or the millionths place? :P

OpenStudy (turingtest):

The millionth place, actually I had the wrong value for epsilon, it should have been 0.0001 with that correction I get (0.8067887806273476, 141) and the given answer is (0.80679075379635201, 8) Since my answer and the given answer differ by a number less than epsilon=0.0001 the answer is not what I'm worried about, it's the fact that my algorithm uses 141 iterations when it's only supposed to use 8 ! I'm going to do this painfully on paper and see if I can debug this to find which variable(s) is/are misbehaving.

OpenStudy (konradzuse):

is # a comment?

OpenStudy (turingtest):

yes and never mind, problem solved!

OpenStudy (anonymous):

if the problem is solved could u pls giv me the soln??

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!