1. 程式人生 > >牛頓插值公式擬合多項式

牛頓插值公式擬合多項式

#牛頓插值公式

x = []
y = []

step = []

n = input()
n = int(n)

for i in range(n) :
    xi = input()
    xi = int(xi)
    x.append(xi)


for i in range(n) :
    yi = input()
    yi = int(yi)
    y.append(yi)

temp = []

for i in range(1, 3) :
    temp.clear()
    for j in range(i, 3) :
        temp.append((y[j] - y[j - 1]) / (x[j] - x[j - i]))
    index = 0
    for j in range(i, 3) :
        y[j] = temp[index]
        index += 1


k = input()
k = float(k)

print(y[0] + y[1] * (k - x[0]) + y[2] * (k - x[0]) * (k - x[1]))