scipy.optimize.curve_fit即使使用float64值也返回初始猜测

时间:2020-02-16 00:48:20

标签: python scipy curve-fitting least-squares scipy-optimize

我定义了函数:

wavelength = 546*10**(-9)
wattspervolt = 1.136*10**(-7)

def fraunhoferdoubleslit(thetas, a, d, I0, Ib, shift):
    freq = []
    for theta in thetas:
        if theta < shift:
            theta = -theta+shift
            freq.append((4*I0*(np.cos(np.pi*d/wavelength*np.sin(theta)))**2*((np.sin(np.pi*a/wavelength*np.sin(theta)))/(np.pi*a/wavelength*np.sin(theta)))**2)+Ib)
        else:
            theta -= shift
            freq.append((4*I0*(np.cos(np.pi*d/wavelength*np.sin(theta)))**2*((np.sin(np.pi*a/wavelength*np.sin(theta)))/(np.pi*a/wavelength*np.sin(theta)))**2)+Ib)
    return np.array(freq)

我从实验中获得了一些数据,并且一定要使用float64:

data = pd.read_table('double dark.txt')

thetas = np.array(data['Angle(V)'])
y = np.array(data['Diode(V)'])*wattspervolt*10**6

x = thetas.astype(np.float64)
y = y.astype(np.float64)

我做出的初步猜测看起来很接近,但并不完全正确:

shift1 = thetas[np.where(y==np.amax(y))[0][0]]
a1 = wavelength
d1 = 6.4*wavelength
I01 = max(y)/4
Ib1 = min(y)
p0 = [a1, d1, I01, Ib1, shift1]

我使用curve_fit:

param, param_cov = curve_fit(f=fraunhoferdoubleslit, xdata=x, ydata=y, p0=p0)

但是,all(p0 == param)返回True。我将数据转换为float64,但是我不确定该怎么做。

0 个答案:

没有答案