Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

TuringTest

  • 3 years ago

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

  • This Question is Closed
  1. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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...

  2. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  3. KonradZuse
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  4. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. KonradZuse
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    is # a comment?

  6. TuringTest
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes and never mind, problem solved!

  7. ishesh
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  8. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy