在Python中,可以使用scipy库中的curve_fit函数来拟合Cos函数到整数列表中,同时不缩小X或Y轴。具体步骤如下:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def cos_func(x, a, b, c):
return a * np.cos(b * x + c)
x = np.array([0, 1, 2, 3, 4, 5]) # X轴整数列表
y = np.array([1, 0, -1, 0, 1, 0]) # 对应的Y轴整数列表
popt, pcov = curve_fit(cos_func, x, y)
x_fit = np.linspace(0, 5, 100) # 生成更密集的X轴数据
y_fit = cos_func(x_fit, *popt) # 使用拟合参数计算对应的Y轴数据
plt.plot(x, y, 'o', label='data') # 绘制原始数据点
plt.plot(x_fit, y_fit, label='fit') # 绘制拟合曲线
plt.legend()
plt.show()
这样,就可以在不缩小X或Y轴的情况下将Cos函数拟合到整数列表中。
领取专属 10元无门槛券
手把手带您无忧上云