Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

Hi..in python 6.00 Problem set 2, Implementing newton's method, problem 2, on running the solution code for this, i am not getting answer as expected. Any one else facing same problem? Why is it coming?

OpenStudy (anonymous):

Actually its problem 3 in problem set 2...

OpenStudy (anonymous):

Were you receiving an error, or it is calculating it incorrectly? The solution code I downloaded from the OCW website needed the compute_root function definition body indented. I also had to change the print statements in evaluate_poly and compute_deriv to return statements.

OpenStudy (anonymous):

def compute_root(poly, x_0, epsilon): #define root to make code simpler. root = evaluate_poly(poly,x_0) iterations = 0 #until root (evaluate_poly) is within our error range of 0... while (root > epsilon) or (root < -epsilon): #...apply newton's method, calculate new root, and count iterations. x_0 = (x_0 - ((root)/(evaluate_poly(compute_deriv(poly),x_0)))) root = evaluate_poly(poly,x_0) iterations = iterations + 1 return (x_0,iterations) Running this returns -13.2119 (-0.0, 0.0, 35.0, 9.0, 4.0) which is different from expected result (0.80679075379635201, 8.0) . Am I missing something here?

OpenStudy (anonymous):

Hi Theeconomist, thanks for your reply. I am also getting this error: Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> compute_root(poly, x_0, epsilon) File "C:\Documents and Settings\Administrator\Desktop\ps2-sol\ps2_newton.py", line 41, in compute_root x_0 = (x_0 - ((root)/(evaluate_poly(compute_deriv(poly),x_0)))) File "C:\Documents and Settings\Administrator\Desktop\ps2-sol\ps2_newton.py", line 11, in evaluate_poly for i in range(0,len(poly)): TypeError: object of type 'NoneType' has no len()

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!