我试着用余弦函数作为我的模型来拟合数据,有些是不起作用的.
以下是我的代码:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def model(x,a,psi,b):
return a*np.cos(x + psi)+b
err_y = 0.5
err_x = 2
x = np.arange(10, 370,20)
x_r = x* (np.pi/180)
y = np.array([5.7, 7.9, 8, 6.6, 4.4, 1.3, 0.09, 1.2, 2.9, 5.9, 7.8, 9, 7, 4.2, 1.5, 0.17, 0.57, 2.5])
error = err_y * np.ones(y.size)
popt, pcov = curve_fit(model, x_r, y,p0 = (4, 3.14, 4), sigma = error,absolute_sigma = True)
print(popt)
plt.scatter(x,y)
plt.errorbar(x, y, yerr=err_y, fmt='none', c='k')
plt.xlabel('Angle(degree)')
plt.ylabel('Readout (V)')
plt.plot(x, model(x_r, *popt), color='red')
我try 了许多不同的参数初始猜测,但都不起作用.