python - TypeError: 'NoneType' object is not iterable. Why do I get this error? -


the function compute_root uses newton's method of successive approximation find enough approximations of zeroes of polynomials (herein lies problem). function evalpoly computes value of polynomial @ particular x value, , function ddx2 computes derivative of polynomial.

poly2 = (2,3,1,5) #poly2 represents polynomial 5x^3+x^2+3x+1  def evalpoly(poly,x):     degree = 0     ans = 0     index in poly:         ans += (index * (x**degree))         degree += 1     return ans    def ddx2(tpl):     lst = list(tpl)     in range(len(lst)):         lst[i] = lst[i]*i         if != 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))         # 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 call function *compute_root* 

when call function 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     index in poly: typeerror: 'nonetype' object not iterable 

i know python functions return none default. think error produced because value none being passed parameter poly in evalpoly. why happening?

i felt prudent include everything, function ddx2 (which hasn't been called yet in example) because don't know if need it. know compute_root needs lot of work, first step. thanks!!!

update!!!

it has been brought attention getting error because function ddx2 lacked return value, of course returning value none, of course not iterable. thank you!!

update2!!!

i have complete working program here posting in hope may sometime. spent lot of hours on this. it's mit open courseware's electrical engineering , computer science 6.00sc professor john guttag, problem set 2.

poly5 = (-13.39, 0.0, 17.5, 3.0, 1.0) # represents polynomial:        # x^4+3x^3+17.5x^2-13.39    def evalpoly(poly,x):     degree = 0     ans = 0     index in poly:         ans += (index * (x**degree))         degree += 1      return float(ans)     return degree     def ddx2(tpl):     lst = list(tpl)     in range(len(lst)):         lst[i] = lst[i]*i         if != 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 program?) finds 1 real root, if 1 exists, suppose sufficient exercise.

your function ddx2(tpl) lacks return value.


Comments

Popular posts from this blog

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

wordpress - (T_ENDFOREACH) php error -

Using django-mptt to get only the categories that have items -