SciPy curve_fit不完美适合数据?

时间:2014-04-07 22:01:26

标签: python scipy curve data-fitting

我一直在尝试使用Scipy.optimize curve_fit函数将一条线拟合到谐振腔中声学模式的数据,但结果并不像预期的那样。该函数返回值没有任何错误,但是当我根据数据绘制结果时,它的拟合非常差。这可能是由于缺乏数据/数据不佳/洛伦兹人在数据中重叠,还是我做错了什么?

我试图拟合的数据是两个声学模式,它们在频率上彼此相邻,数据应该适合Lorentzian,因此我已经定义了两个洛伦兹总和的模型函数。

以下是代码:

from scipy.optimize import curve_fit
from scipy.stats import cauchy, norm

def LorentzianSum(freq, x1,mu1,gamma1,x2,mu2,gamma2):
    ret = x1*cauchy.pdf(freq, loc=mu1, scale=gamma1)
    ret += x2*cauchy.pdf(freq, loc=mu2, scale=gamma2)
    return ret

#EMPTY
freq = arange(715, 955, 5)
freq = append(freq,arange(1000,1140, 5))

amp = array([10.0,10.8,11.2,12.8,14.4,15.6,16.8,17.2,17.2,16.8,16.8,16.8,17.6,18.8,21.2,24.0,28.4,33.6,42.8,56.4,69.2,60.0,42.4,29.2,21.2,15.2,10.0,8.8,13.2,25.6,47.6,58.4,46.4,36.4,13.0,26.0,23.6,21.2,19.6,18.8,17.6,17.2,16.4,15.6,15.6,16.0,16.4,16.0,18.8,20.0,20.0,20.8,21.6,22.8,23.6,25.2,27.6,29.6,33.2,36.0,40.8,47.6,56.0,70.2,98,170,92.0,103,154,176,136,110,98,90,90,94])
freq = freq[7:47]
amp = amp[7:47]

figure(4)
plot(freq,amp,'kx')
guess = [100.,815.,25.,90.,870.,25] # initial guess
popt, pcov = curve_fit(LorentzianSum, freq, amp, p0=guess)  # do fit
plot(freq, LorentzianSum(freq, *popt))
title('Empty Chamber')
xlabel('Frequency(Hz)')
ylabel('Amplitude(mV)')
grid()

0 个答案:

没有答案