Here is my block of code:
x = [1.6, 2, 2.5, 3.2, 4, 4.5]
y = [2, 8, 14, 15, 8, 2]
r = 2.8
e = []
def err(x, y, r):
n = len(x) - 1
a = y.copy()
for i in range(1, n + 1):
for j in range(n, i - 1, -1):
a[j] = float(a[j] - a[j-1])/float(x[j] - x[j-i])
for k in range(n):
e[k] = float(a[0:k])*float(x[0:k] - r)
return e
It's the error calculation for Newton polynomial interpolation. Here's the complete error message:
TypeError Traceback (most recent call last)
<ipython-input-3-dafb0d988dac> in <module>()
----> 1 err(x, y, r)
<ipython-input-2-7d51080e7243> in err(x, y, r)
9 a[j] = float(a[j] - a[j-1])/float(x[j] - x[j-i])
10 for k in range(n):
---> 11 e[k] = float(a[0:k])*float(x[0:k] - r)
12 return e
TypeError: float() argument must be a string or a number, not 'list'
I've been at this for hours, have not been able to resolve this issue. I hope someone here can help (I'm relatively new to Python). Thanks in advance.
The error calculation for Newton interpolation is:
a0*(r-x0), a1*(r-x0)*(r-x1), a1*(r-x0)*(r-x1)*(r-x2) .... a5*(r-x0)*(r-x1)*(r-x2)*(r-x3)*(r-x4)*(r-x5)(for this case, it runs to a5 for a 5th degree polynomial).
The "a" values are contained in lista. Also,xis a fixed value (r in this case = 2.8), and x0, x1, x2, etc. are from x = [1.6, 2, 2.5, 3.2, 4, 4.5]
float(a[0:k])in which you are grabbing all elements between0andkin your lista. You cannot convert a list to a float, so what exactly are you trying to do by that statement and the similar one next to it?a[0:k]is a list, and so isx[0:k]. What are you trying to accomplish with these lists?