两点三次埃尔米特插值

python代码及截图

import math
import numpy as np
import matplotlib.pyplot as plt
def square(data):
return data * data
def Two_point_cubic_Hermite_interpolation(arr_x, arr_y, arr_m, x):
temp1 = arr_y[0] * (1 + 2 * (x - arr_x[0]) / (arr_x[1] - arr_x[0])) * square((x - arr_x[1]) / (arr_x[0] - arr_x[1]))
temp2 = arr_y[1] * (1 + 2 * (x - arr_x[1]) / (arr_x[0] - arr_x[1])) * square((x - arr_x[0]) / (arr_x[1] - arr_x[0]))
temp3 = arr_m[0] * (x - arr_x[0]) * square((x - arr_x[1]) / (arr_x[0] - arr_x[1]))
temp4 = arr_m[1] * (x - arr_x[1]) * square((x - arr_x[0]) / (arr_x[1] - arr_x[0]))
return temp1 + temp2 + temp3 + temp4
x_arr = [1, math.e]
y_arr = [0, 1]
m_arr = [1, 0.36788]
original_x = np.arange(0.1, 3, 0.01)
original_y = [0.0 for j in range(len(original_x))]
for i in range(len(original_y)):
original_y[i] = math.log(original_x[i])
x = np.arange(0.1, 3, 0.01)
y = [0.0 for j in range(len(original_x))]
for i in range(len(x)):
y[i] = Two_point_cubic_Hermite_interpolation(x_arr, y_arr, m_arr, x[i])
plt.plot(original_x, original_y, label='f(x) = lnx')
plt.scatter(x_arr, y_arr)
plt.plot(x, y, label='point cubic Hermite interpolation')
plt.scatter(2, Two_point_cubic_Hermite_interpolation(x_arr, y_arr, m_arr, 2), color='red', label='Ln2 approximation')
plt.title("Two point cubic Hermite interpolation")
plt.legend(loc="upper left")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
误差分析
