七点二阶多项式滑动拟合代码
时间: 2023-09-03 22:11:22 浏览: 200
下面是一个使用Python实现的七点二阶多项式滑动拟合的代码示例:
```python
import numpy as np
import matplotlib.pyplot as plt
def sliding_polyfit(x, y, window_size):
n_points = len(x)
poly_order = 2
fitted_curves = []
for i in range(n_points - window_size + 1):
window_x = x[i:i+window_size]
window_y = y[i:i+window_size]
coefficients = np.polyfit(window_x, window_y, poly_order)
fitted_curve = np.polyval(coefficients, x)
fitted_curves.append(fitted_curve)
return np.array(fitted_curves)
# 示例数据
x = np.linspace(0, 10, 100)
y = 2 * x**2 + 3 * x + 1 + np.random.randn(100)
# 使用滑动窗口大小为7进行拟合
window_size = 7
fitted_curves = sliding_polyfit(x, y, window_size)
# 绘制原始数据和拟合曲线
plt.scatter(x, y, label='Original Data')
for curve in fitted_curves:
plt.plot(x, curve, alpha=0.3)
plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Sliding Polynomial Fit')
plt.show()
```
您可以根据自己的数据和需求进行修改和调整。这段代码使用了numpy库进行多项式拟合和计算,matplotlib库用于绘制数据和拟合曲线。
阅读全文