0

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.

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

Test case is at bottom with expected output.

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.

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 > 0 and fguess < epsilon: #if solution for first guess is close enough to root return first guess
    return x_guess
else: 
    while fguess > 0 and fguess > epsilon:
        iteration+=1
        x_guess = x_0 - (evaluate_poly(poly,x_0)/df(poly, x_0))
        fguess = evaluate_poly(poly, x_guess)
        if fguess > 0 and fguess < 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

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

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

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

Thanks for looking and/or helping!

Durf
  • 13
  • 2
  • A small but significant observation: you don't access the 1st (element with index `0`) of any array. It should be `for i in range (len(poly)):` not `for i in range (1, len(poly)):`. Change the code in your polynomial and derivative calculation methods to reflect this. – EvilTak Sep 17 '16 at 16:42
  • You are supposed to do the problem sets by yourself. – Padraic Cunningham Sep 17 '16 at 16:53
  • It should be `abs(fguess) < epsilon` as condition for the root. At the moment you exclude `-epsilon < fguess < 0` as approximate solutions. – Lutz Lehmann Sep 17 '16 at 20:03
  • @EvilTak: No, those loops are correct for polynomial evaluation resp. construction of derivative coefficients. – Lutz Lehmann Sep 17 '16 at 20:04
  • Padraic, I appreciate your dedication to academic integrity but given limited resources I decided to follow the policy: From the EdX Collaboration Guidelines: "It is ok to have someone show you a few steps of a solution where you have been stuck for a while, provided of course that you have attempted to solve it yourself first without success." From Collaboration Policy on ocw.mit.edu: "Our policy is simple: unless otherwise noted in the assignment itself, feel free to collaborate with each other on all the individual problem sets, but note with whom you collaborated." Thanks! – Durf Sep 18 '16 at 17:33

1 Answers1

0

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

Instead:

while fguess < 0 or fguess > epsilon:

Will give you what you need:

-13.2119
7
0.806790753796352
ptev
  • 183
  • 2
  • 7