I need help on the Newton question (#3) on the problem set 2! I use the eval_poly and compute_deriv function from the first few questions. Those check out, but I just can't get the while loop to work. It runs forever, for reasons I don't understand. Here's the code: deriv = compute_deriv(poly) numguess = 0 while abs(eval_poly(poly, x_0)) >= epsilon: numguess += 1 x_0 = x_0 - (eval_poly(poly, x_0))/(eval_poly(deriv, x_0)) else: print (eval_poly(poly, x_0), numguess) return
not sure, looks okay to me try inserting a print statement or two: deriv = compute_deriv(poly) numguess = 0 while abs(eval_poly(poly, x_0)) >= epsilon: numguess += 1 x_0 = x_0 - (eval_poly(poly, x_0))/(eval_poly(deriv, x_0)) print x_0, (eval_poly(poly, x_0)), (eval_poly(deriv, x_0)) else: print (eval_poly(poly, x_0), numguess) return that ought to be enlightening
btw it should return the root x_0, not eval_poly(poly, x_0)
Thanks for your help! I put in the print command and I found that the values don't seem to be converging. 0.373076923077 0.671405325444 -0.238461538462 2.44249379653 23.7823154313 16.6549627792 1.01455212253 6.11705227303 8.08731273516 0.25817574964 1.71631565239 3.54905449784 -0.225422249047 0.701601073002 0.647466505718 -1.30903208339 3.52263081924 -5.85419250033 -0.707304207333 1.08622931047 -2.243825244 -0.223207099783 0.703050028615 0.660757401303 -1.28721338595 3.39632813098 -5.72328031567 -0.693790044146 1.05645378778 -2.16274026488 This is using the test values: poly = (1, 2, 3) x_0 = .1 epsilon = .0001 Weirdly enough, though, when I put in the original test values from the problem: poly = (-13.39, 0.0, 17.5, 3.0, 1.0) x_0 = 0.1 epsilon = .0001 the program works. I'm not sure what to make of this, because I don't quite understand Newtons formula (x_1 = f(x_0)/f'(x_0) itself.
if it works with the test values then there is a mathematical explanation to why the other one is not working: newton's method fails for certain initial values of x_0 let me try it in my program and see what happens...
Thanks. I suspected it was an issue with restricted application of the formula itself. I need to read up on it, since the problem set provides no info.
yes, the same exact thing happens to mine, I even get the same print values for x_0 it is a result of the fallibility of newtons method, and has to do with having a bad initial choice for x_0 If you want to understand what is happening you could look at the section on Newton's method in the single variable calc section of OCW
I will try to draw what is happening something like|dw:1351214431286:dw|
|dw:1351214499069:dw|
|dw:1351214573121:dw|
Join our real-time social learning platform and learn together with your friends!