The function compute_root uses Newton's method of successive approximation to find good enough approximations of the zeroes of polynomials (herein lies the problem). The function evalPoly computes the value of a polynomial at a particular x value, and the function ddx2 computes the derivative of a polynomial.
poly2 = (2,3,1,5) #poly2 represents the polynomial 5x^3+x^2+3x+1
def evalPoly(poly,x):
degree = 0
ans = 0
for index in poly:
ans += (index * (x**degree))
degree += 1
return ans
def ddx2(tpl):
lst = list(tpl)
for i in range(len(lst)):
lst[i] = lst[i]*i
if i != 0:
lst[i-1] = lst[i]
del lst[-1]
tpl = tuple(lst)
def compute_root(poly,x_0):
epsilon = .001
numGuesses = 1
if abs(evalPoly(poly,x_0)) <= epsilon:
ans = (evalPoly(poly,x_0),numGuesses)
print ans
return ans
else:
x_1 = x_0 - (evalPoly(poly,x_0)/evalPoly(ddx2(poly),x_0))
# This is Newton's method of getting progressively better /
# "guesses"
compute_root(poly,x_1)
x_0 = x_1
numGuesses += 1
return x_0
return poly
compute_root(poly2,2) #Here I call the function *compute_root*
When I call the function I get this error:
Samuels-MacBook:python barnicle$ python problemset2.py
Traceback (most recent call last):
File "problemset2.py", line 160, in <module>
compute_root(poly2,x_0)
File "problemset2.py", line 156, in compute_root
x_1 = x_0 - (evalPoly(poly,x_0)/evalPoly(ddx2(poly),x_0))
File "problemset2.py", line 126, in evalPoly
for index in poly:
TypeError: 'NoneType' object is not iterable
I know python functions return none by default. I think that the error is produced because the value none is being passed into the parameter poly in evalPoly. Why is this happening?
I felt it would be prudent to include everything, even the function ddx2 (which hasn't been called yet in this example) because I don't know if you need it. I know compute_root needs a lot of work, this is just the first step. Thanks!!!
UPDATE!!!
It has been brought to my attention that I was getting the error because my function ddx2 lacked a return value, so it was of course returning the value none, which is of course not iterable. Thank you!!
UPDATE2!!!
I have my complete working program here which I am posting in the hope that it may help someone sometime. I spent a lot of hours on this. It's from MIT open courseware's electrical engineering and computer science 6.00sc with Professor John Guttag, problem set 2.
poly5 = (-13.39, 0.0, 17.5, 3.0, 1.0) # represents the polynomial:
# x^4+3x^3+17.5x^2-13.39
def evalPoly(poly,x):
degree = 0
ans = 0
for index in poly:
ans += (index * (x**degree))
degree += 1
return float(ans)
return degree
def ddx2(tpl):
lst = list(tpl)
for i in range(len(lst)):
lst[i] = lst[i]*i
if i != 0:
lst[i-1] = lst[i]
del lst[-1]
tpl = tuple(lst)
return tpl
def compute_root(poly,x_0,numGuesses):
epsilon = .001
ans = []
if abs(evalPoly(poly,x_0)) <= epsilon:
ans.append(x_0)
ans.append(numGuesses)
ans = tuple(ans)
print ans
return ans
else:
numGuesses += 1
x_0 = x_0 - (evalPoly(poly,x_0)/evalPoly(ddx2(poly),x_0))
compute_root(poly,x_0,numGuesses)
compute_root(poly5,.1,1)
Output: Samuels-MacBook:python barnicle$ python problemset2.py (0.806790753796352, 8)
This program (is this a program?) only finds one real root, if one exists, but I suppose it is sufficient for an exercise.
Aucun commentaire:
Enregistrer un commentaire