抛物线插值法 python
时间: 2023-10-25 17:07:34 浏览: 112
抛物线插值法是一种通过已知点来估计函数值的插值方法。在Python中,可以使用Lagrange插值函数实现抛物线插值。
以下是使用抛物线插值法实现抛物线插值的Python代码:
```python
import numpy as np
# 定义Lagrange插值函数
def lagrange_f(x, x1, x2, x3, y1, y2, y3):
l1 = y1 * (x - x2) * (x - x3) / ((x1 - x2) * (x1 - x3))
l2 = y2 * (x - x1) * (x - x3) / ((x2 - x1) * (x2 - x3))
l3 = y3 * (x - x1) * (x - x2) / ((x3 - x1) * (x3 - x2))
return l1 + l2 + l3
# 给定已知点的坐标
x1 = 0.32
y1 = np.round(np.sin(0.32), 6)
x2 = 0.34
y2 = np.round(np.sin(0.34), 6)
x3 = 0.36
y3 = np.round(np.sin(0.36), 6)
# 计算插值点的函数值
x_ = 0.3367
result = np.round(lagrange_f(0.3367, x1, x2, x3, y1, y2, y3), 6)
# 绘制原图和拟合图
import matplotlib.pyplot as plt
x = np.linspace(0, np.pi, 1000)
y = np.sin(x)
new_y = lagrange_f(x, x1, x2, x3, y1, y2, y3)
plt.figure(figsize=(10, 8), dpi=80)
plt.plot(x, y, 'b')
plt.plot(x, new_y, 'g')
plt.plot(x1, y1, 'r*')
plt.plot(x2, y2, 'r*')
plt.plot(x3, y3, 'r*')
plt.plot(x_, result, 'y*')
plt.show()
```
阅读全文