繁体   English   中英

SciPy curve_fit 不会尝试改变洛伦兹的峰值

[英]SciPy curve_fit doesn't try to shift Lorentzian's peak

我有一些洛伦兹数据,我正在尝试拟合并且遇到问题。 我想也许 scipy 太吵了,找不到合适的,所以我生成了一个理想化的 Lorentzian 来代替,令我惊讶的是,scipy 也无法适应它,初始猜测几乎完美。 在这种情况下,scipy 需要做的就是将 Lorentzian 的峰值移动不到 1%,尽管它会因任意准确的初始猜测而失败。

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

def lorentzian(freq, gamma, center_freq, amplitude):
    return amplitude * gamma**2 / ( gamma**2 + ( freq - center_freq )**2) + 1

frequencies = np.arange(4.95e9, 5.05e9, 0.1e9/1000)

guess = [15000000, 4.997e9, 28]
fake = [15000000, 5.02e9, 28]
test_data = lorentzian(frequencies, *fake)
popt, pcov = curve_fit(lorentzian, test_data, frequencies, maxfev = 100000, p0 = guess)
fit_lorentzian = lorentzian(frequencies, *popt)
guess_lorentzian = lorentzian(frequencies, *guess)
plt.plot(frequencies, test_data)
plt.plot(frequencies, fit_lorentzian)

测试数据与拟合

scipy警告我OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn('Covariance of the parameters could not be estimated',并将初始猜测返回为popt。pcov是一个3x3的inf数组。这是怎么回事?

正如评论中指出的,curve_fit 有参数(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=(- inf, inf), method=None, jac=None, **kwargs) ; 即,我交换了 x 数据和 y 数据的顺序。 popt, pcov = curve_fit(lorentzian, frequencies, test_data, maxfev = 100000, p0 = guess)解决它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM