Python中的Hermite插值

时间:2015-04-19 01:28:21

标签: python interpolation numerical-methods hermite

我有这个用于计算Hermite插值的程序。

问题是,它的表现非常糟糕。 interpolation for 35 Chebyshev nodes

这是35个Chebyshev节点的图表。如果我添加更多点,则开头的峰值将更高(使用此节点数量约为10 ^ 7)。

我使用拉格朗日方法(绿色,它移位以便可以看到)插入这个相同的函数,你可以看到它看起来很好。

以下是代码:

def hermit_interpolate(input): #input is list of tuples [(x1,y1),(x2,y2)...] xi are Chebyshev nodes
points = [(input[0][0], input[0][1] - 0), (input[0][0], calculate_f_p_x(input[0][0]))] #"input[0][1] - 0" this is just to change type of second element
                                                                                    #calculate_f_p_x returns value of derivative
for k in range(1, len(input)): #Divided differences and derivatives in one list alternately
    points.append((input[k][0], (input[k][1] - input[k - 1][1]) / (
        input[k][0] - input[k - 1][0])))
    points.append((input[k][0], calculate_f_p_x(input[k][0])))
x, c = zip(*points)
x = list(x)
c = list(c)
n = len(points)
for i in range(2, n): #calculating factors
    for j in range(n - 1, i - 1, -1):
        c[j] = (c[j] - c[j - 1]) / (x[j] - x[j - i])

def result_polynomial(xpoint): #here is function to calculate value for given x
    val = c[0]
    factor = 1.0
    for l in range(1, n):
        factor *= (xpoint - x[l - 1])
        val += (c[l] * factor)
    return val

return result_polynomial

我不明白这里有什么问题。 谢谢!

1 个答案:

答案 0 :(得分:0)

此代码实际上有效:

def hermit_interpolate(input):  #input is list of tuples [(x1,y1),(x2,y2),...,(xn,yn)] xi are Chebyshev nodes
n = len(input)
points = numpy.zeros(shape=(2 * n + 1, 2 * n + 1))
X, Y = zip(*input)
X = list(X)
Y = list(Y)

for i in range(0, 2 * n, 2):
    points[i][0] = X[i / 2]
    points[i + 1][0] = X[i / 2]
    points[i][1] = Y[i / 2]
    points[i + 1][1] = Y[i / 2]

for i in range(2, 2 * n + 1):
    for j in range(1 + (i - 2), 2 * n):
        if i == 2 and j % 2 == 1:
            points[j][i] = calculate_f_p_x(X[j / 2]);

        else:
            points[j][i] = (points[j][i - 1] - points[j - 1][i - 1]) / (
                points[j][0] - points[(j - 1) - (i - 2)][0])

def result_polynomial(xpoint):  #here is function to calculate value for given x
    val = 0
    for i in range(0, 2 * n):
        factor = 1.
        j = 0
        while j < i:
            factor *= (xpoint - X[j / 2])
            if j + 1 != i:
                factor *= (xpoint - X[j / 2])
                j += 1
            j += 1
        val += factor * points[i][i + 1]
    return val
return result_polynomia