0
我想用matplotlib呈現一個6度函數,它與我找到的這段代碼一起工作,但不能和python3一起工作。matplotlib在python3中失敗
import numpy as np
from matplotlib import pyplot as plt
def langrange_polynomial(X, Y):
def L(i):
return lambda x: np.prod([(x-X[j])/(X[i]-X[j]) for j in range(len(X)) if i != j]) * Y[i]
Sx = [L(i) for i in range(len(X))] # summands
return lambda x: np.sum([s(x) for s in Sx])
# cut something
# Here i get the points with a function
(X, Y) = [1,2,3,4,5,6,7],[0,20,10,4,3,40,4]
F = langrange_polynomial(X, Y)
x_range = np.linspace(X[0], X[-1], 100)
plt.plot(X, Y, 'ro')
plt.plot(x_range, map(F, x_range))
plt.xlabel(r'$x$')
plt.ylabel(r'$F(x)$')
plt.title('Lagrange polynomial interpolation')
plt.grid(True)
plt.show()
得到這個錯誤:
raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
在該行:
plt.plot(x_range, map(F, x_range))
我讀到一些有關聲明X,Y座標到np.array,但沒有反正工作。我在Python3.5中必須做什麼?
從來沒有想到,;-)非常感謝! –