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