使用 lmfit

问题描述

我正在尝试使用 lmfit 拟合测量数据。 我的目标是通过等效电路图获得电容器的参数。 所以,我想创建一个带有参数(C、R1、L1、...)的模型并将其拟合到测量数据中。

我知道谐振频率处于全局最小值,并且还必须有 R1。也被称为 C. 所以我可以修复参数 C 和 R1。有了共振频率,我也可以计算出 L1。

我创建了模型,但拟合不正常。

也许有人可以帮我解决这个问题。 提前致谢。

from lmfit import minimize,Parameters
from lmfit import report_fit
params = Parameters()

params.add('C',value = 220e-9,vary = False)
params.add('L1',value = 0.00001,min = 0,max = 0.1)
params.add('R1',value = globalmin,vary = False)
params.add('Rp',value = 10000,max = 10e20)
params.add('Cp',value = 0.1,max = 0.1)

def get_elements(params,freq,data):
    C = params['C'].value
    L1 = params['L1'].value
    R1 = params['R1'].value
    Rp = params['Rp'].value
    Cp = params['Cp'].value
    
    XC = 1/(1j*2*np.pi*freq*C)
    XL = 1j*2*np.pi*freq*L1
    XP = 1/(1j*2*np.pi*freq*Cp)
    
    Z1 = R1 + XC*Rp/(XC+Rp) + XL
    
    real = np.real(Z1*XP/(Z1+XP))
    imag = np.imag(Z1*XP/(Z1+XP))
    
    model = np.sqrt(real**2 + imag**2)
    
    #model = np.sqrt(R1**2 + ((2*np.pi*freq*L1 - 1/(2*np.pi*freq*C))**2))
    #model = (np.arctan((2*np.pi*freq*L1 - 1/(2*np.pi*freq*C))/R1)) * 360/((2*np.pi))
    
    return data - model

out = minimize(get_elements,params,args=(freq,data))

report_fit(out)


#make reconstruction for plotting
C = out.params['C'].value
L1 = out.params['L1'].value
R1 = out.params['R1'].value
Rp = out.params['Rp'].value
Cp = out.params['Cp'].value


XC = 1/(1j*2*np.pi*freq*C)
XL = 1j*2*np.pi*freq*L1
XP = 1/(1j*2*np.pi*freq*Cp)
Z1 = R1 + XC*Rp/(XC+Rp) + XL
real = np.real(Z1*XP/(Z1+XP))
imag = np.imag(Z1*XP/(Z1+XP))

reconst = np.sqrt(real**2 + imag**2)
reconst_phase = np.arctan(imag/real)* 360/(2*np.pi)




'''
PLOTTING
'''

#plot of filtred signal vs measered data (AMPLITUDE)
fig = plt.figure(figsize=(40,15))
file_title = 'Measured Data'
plt.subplot(311)
plt.xscale('log')
plt.yscale('log')
plt.xlim([min(freq),max(freq)])
plt.ylabel('Amplitude')
plt.xlabel('Frequency in Hz')
plt.grid(True,which="both")
plt.plot(freq,z12_fac,'g',alpha = 0.7,label = 'data')


#Plot Impedance of model in magenta
plt.plot(freq,reconst,'m',label='Reconstruction (Model)')
plt.legend()


#(PHASE)
plt.subplot(312)
plt.xscale('log')
plt.xlim([min(freq),max(freq)])
plt.ylabel('Phase in °')
plt.xlabel('Frequency in Hz')
plt.grid(True,z12_deg,label = 'data')

#Plot Phase of model in magenta
plt.plot(freq,reconst_phase,label='Reconstruction (Model)')
plt.legend()


plt.savefig(file_title)
plt.close(fig)

measured data equivalent circuit diagram (model)

编辑 1: 拟合报告:

[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 28
    # data points      = 4001
    # variables        = 3
    chi-square         = 1197180.70
    reduced chi-square = 299.444897
    Akaike info crit   = 22816.4225
    Bayesian info crit = 22835.3054
##  Warning: uncertainties could not be estimated:
    L1:  at initial value
    Rp:  at boundary
    Cp:  at initial value
    Cp:  at boundary
[[Variables]]
    C:   2.2e-07 (fixed)
    L1:  1.0000e-05 (init = 1e-05)
    R1:  0.06375191 (fixed)
    Rp:  0.00000000 (init = 10000)
    Cp:  0.10000000 (init = 0.1)

编辑 2: 数据可以在这里找到: https://1drv.ms/u/s!AsLKp-1R8HlZhcdlJER5T7qjmvfmnw?e=r8G2nN

编辑 3: 我现在已经将我的模型简化为一个简单的 RLC 系列。使用另一组数据,这非常有效。看这里的情节 with another set of data

def get_elements(params,data):
    C = params['C'].value
    L1 = params['L1'].value
    R1 = params['R1'].value
    #Rp = params['Rp'].value
    #Cp = params['Cp'].value
    #k = params['k'].value
    
    #freq = np.log10(freq)
    
    XC = 1/(1j*2*np.pi*freq*C)
    XL = 1j*2*np.pi*freq*L1
    # XP = 1/(1j*2*np.pi*freq*Cp)
    
    # Z1 = R1*k + XC*Rp/(XC+Rp) + XL
    
    # real = np.real(Z1*XP/(Z1+XP))
    # imag = np.imag(Z1*XP/(Z1+XP))
    
    
    
    Z1 = R1 + XC + XL
    real = np.real(Z1)
    imag= np.imag(Z1)
    
    model = np.sqrt(real**2 + imag**2)
    
    return np.sqrt(np.real(data)**2+np.imag(data)**2) - model

out = minimize(get_elements,data))

报告: 卡方真的很高...

[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 25
    # data points      = 4001
    # variables        = 2
    chi-square         = 5.0375e+08
    reduced chi-square = 125968.118
    Akaike info crit   = 46988.8798
    Bayesian info crit = 47001.4684
[[Variables]]
    C:   3.3e-09 (fixed)
    L1:  5.2066e-09 +/- 1.3906e-08 (267.09%) (init = 1e-05)
    R1:  0.40753691 +/- 24.5685882 (6028.56%) (init = 0.05)
[[Correlations]] (unreported correlations are < 0.100)
    C(L1,R1) = -0.174

使用我最初的数据集,我得到了这个: plot original data (complex) 这还不错,但也不好。这就是为什么我想让我的模型更详细,这样我也可以适应更高的频率区域...... 本次报道:

[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 25
    # data points      = 4001
    # variables        = 2
    chi-square         = 109156.170
    reduced chi-square = 27.2958664
    Akaike info crit   = 13232.2473
    Bayesian info crit = 13244.8359
[[Variables]]
    C:   2.2e-07 (fixed)
    L1:  2.3344e-08 +/- 1.9987e-10 (0.86%) (init = 1e-05)
    R1:  0.17444702 +/- 0.29660571 (170.03%) (init = 0.05)

请注意:我也更改了模型的输入数据。现在我给模型复数值,然后计算振幅。也可以在这里找到:https://1drv.ms/u/s!AsLKp-1R8HlZhcdlJER5T7qjmvfmnw?e=qnrZk1

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...