0

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 list a. Also, x is 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]

5
  • 2
    The error is because of this part: float(a[0:k]) in which you are grabbing all elements between 0 and k in your list a. 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? Commented Oct 19, 2018 at 2:42
  • 3
    a[0:k] is a list, and so is x[0:k]. What are you trying to accomplish with these lists? Commented Oct 19, 2018 at 2:43
  • The error calculation for Newton interpolation is: b0*(x-x0), b1*(x-x0)*(x-x1), b2*(x-x0)*(x-x1)*(x-x2) and so on (for this case, it runs to b5 for a 5th degree polynomial). The "b" values are contained in list a. Also, x is 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]. Commented Oct 19, 2018 at 2:49
  • Apologies, let me reword -- the error calculation 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). Commented Oct 19, 2018 at 3:00
  • Relevant convert-list-or-numpy-array-of-single-element-to-float-in-python Commented Oct 19, 2018 at 6:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.