MIT 6.00 Newton's Method in Python 3
<p>This is part of the second problem set for MIT's OCW 6.00 Intro to Computation and Programming using Python. First, I created a function that evaluates a polynomial for a given x value. Then a function that computes the derivative for a given polynomial. Using those, I created a function that evaluates the first derivative for a given polynomial and x value. </p>

<p>Then I tried to create a function to estimate the root of any given polynomial within a tolerance (epsilon). </p>

<p>Test case is at bottom with expected output.</p>

<p>I am new to programming and new to python, so I have included some comments in the code to explain what I think the code should be doing. </p>

<pre><code>def evaluate_poly(poly, x):
""" Computes the polynomial function for a given value x. Returns that value."""
answer = poly[0]
for i in range (1, len(poly)):
    answer = answer + poly[i] * x**i
return answer


def compute_deriv(poly):
"""
#Computes and returns the derivative of a polynomial function. If the
#derivative is 0, returns (0.0,)."""
dpoly = ()
for i in range(1,len(poly)):
    dpoly = dpoly + (poly[i]*i,)

return dpoly

def df(poly, x):
"""Computes and returns the solution as a float to the derivative of a polynomial function
"""
dx = evaluate_poly(compute_deriv(poly), x)
#dpoly = compute_deriv(poly)
#dx = evaluate_poly(dpoly, x)
return dx




def compute_root(poly, x_0, epsilon):
"""
Uses Newton's method to find and return a root of a polynomial function.
Returns a float containing the root"""
iteration = 0
fguess = evaluate_poly(poly, x_0) #evaluates poly for first guess
print(fguess)
x_guess = x_0 #initialize x_guess
if fguess &gt; 0 and fguess &lt; epsilon: #if solution for first guess is close enough to root return first guess
    return x_guess
else: 
    while fguess &gt; 0 and fguess &gt; epsilon:
        iteration+=1
        x_guess = x_0 - (evaluate_poly(poly,x_0)/df(poly, x_0))
        fguess = evaluate_poly(poly, x_guess)
        if fguess &gt; 0 and fguess &lt; epsilon:
            break #fguess where guess is close enough to root, breaks while loop, skips else, return x_guess
        else:
            x_0 = x_guess #guess again with most recent guess as x_0 next time through while loop
print(iteration)
return x_guess




#Example:
poly = (-13.39, 0.0, 17.5, 3.0, 1.0)    #x^4 + 3x^3 + 17.5x^2 - 13.39
x_0 = 0.1
epsilon = .0001
print (compute_root(poly, x_0, epsilon))
#answer should be 0.80679075379635201
</code></pre>

<p>The first 3 functions return correct answers, but compute_root (Newton's method) does not seem to enter the <code>while</code> loop because when I run the cell <code>print(iteration)</code> prints 0. I would think that since <code>if fguess &gt; 0 and fguess &lt; epsilon:</code> should return <code>false</code> for the test case (statement <code>print(fguess)</code> prints <code>-13.2119</code>), the interpreter would go to <code>else</code> and enter the <code>while</code> loop until it finds a solution that is within epsilon of 0. </p>

<p>I have tried eliminating the first <code>if</code> <code>else</code> conditions so that I only have one <code>return</code> statement and I get the same problem. </p>

<p>What could be causing the function to skip the <code>else</code> case / <code>while</code> loop altogether? I'm stumped!</p>

<p>Thanks for looking and/or helping!</p>

<p>It seems to be just a small oversight. Notice how <code>fguess</code> is printed with a value of -13.2119. In your <code>while</code> condition (in <code>else</code> from <code>compute_root</code>) you require <code>fguess &gt; 0 and fguess &lt; epsilon</code>, which is not met so nothing is done further and you exit without iterations.</p>

<p>Instead:</p>

<pre><code>while fguess &lt; 0 or fguess &gt; epsilon:
</code></pre>

<p>Will give you what you need:</p>

<pre><code>-13.2119
7
0.806790753796352
</code></pre>

python python-3.x newtons-method