【问题标题】:Python: curve_fit not working for function with three fitting parameters, and an improper integralPython:curve_fit 不适用于具有三个拟合参数和不正确积分的函数
【发布时间】:2018-12-01 06:32:44
【问题描述】:

我正在尝试将数据集拟合到这个方程式怪物。我知道以前有人问过这个问题,但我不认为最初的猜测是我的问题,我也不能在我的拟合方程中添加更多项。

我的拟合方程。请注意,积分中的“u”与顶部定义的 u 不同。

顺便说一下,我的数据集是以 mA/um 为单位的。

我在一个函数F中实现了这个,它接受输入Vd、T、r和Vt。T、r和Vt是拟合参数。 T 和 r 范围从 0

我最初的几个程序非常适合(如果它甚至可以完成积分),所以我决定看看这个算法是否有效。该函数的实现如下:

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

#Constants
eSiO2 = 3.9 #Relative dielectric constant of SiO2
tox = 2e-9  #Gate oxide thickness in m
evac = 8.854e-12    #Vacuum permittivity, F/m
em = 0.2*9.11e-31   #Effective electron mass in kg
KT = 4.11e-21 #Thermal energy in joules
Mv = 2.5 #Degeneracy factor
q = 1.6e-19 #Electron charge, coulombs
hbar = 1.054e-34    #Reduced plancks constant
Vg = 1

def F(Vd,T,r,Vt):
    #Derived constants required for computation
    Ci = (eSiO2*evac)/tox #Oxide capacitance per area
    ved = (q*r*Vd)/(KT) #little Vd
    I0 = (np.sqrt(2)*q*(KT**1.5)*Mv*np.sqrt(em))/(np.pi*hbar)**2 #Leakage Current

    #Rho
    rho1 = 2*np.pi*hbar**2*Ci
    rho2 = q*KT*em*Mv
    rhoV = Vg-Vt
    rho = (rho1*rhoV)/rho2

    #u
    UA = 1 + np.exp(ved)
    UB = np.exp(ved)*(np.exp(rho)-1)
    Usq = np.sqrt(UA**2+4*UB)
    u = np.log(Usq-UA)-np.log(2)

    #Integrand of F(u)
    def integrand1(A,x):
        return (np.sqrt(A))/(1+np.exp(A-x))

    #Integrand of F(u-v)
    def integrand2(A,x):
        return (np.sqrt(A))/(1+np.exp(A-x))
    sum1 = 0
    sum2 = 0
    tempy1=[]
    tempy2=[]
    tempx2=[]

    #Tempx2 = dummy variable domain
    tempx2 = np.linspace(0,100,num=10000)

    #Fitting parameters are determined
    if Ready is True:
        #Evaluate the integrands for all the Vd values
        tempy1 = integrand1(tempx2,u)
        tempy2 = integrand2(tempx2,u-ved)

    #Fitting parameters are NOT determined
    else:
        print ("Calculating")
        #Evaluate the integrands for all the Vd values
        for i in range (0,len(u)):
            tempy1 = integrand1(tempx2,u[i])
            tempy2 = integrand2(tempx2,u[i]-ved[i]) 

    #Perform integration over dummy domain
    sum1 = integrate.simps(tempy1,tempx2,0.1)
    sum2 = integrate.simps(tempy2,tempx2,0.1)

    if Ready is False:
        print ("u=%s" %u,"ved=%s" %ved)
        print ("Sum1 is %s" %sum1)
    return I0*T*1e-3*(sum1-sum2)

如果指定了 T,r 和 Vt,该函数将计算 F(x,T,r,Vt)。所以我决定制作一个“样本”数据集,看看它是否能几乎完美地拟合自己:

#Create domain for reference curve
Ready = True
x = np.linspace(0,1.2,50)

y=[]
#Evaluate the reference curve domain
for j in range (0,50):
    y.append(F(x[j],0.2,0.147,0.45))

现在参考曲线已创建,现在将尝试拟合曲线。请注意我的 p0 值与实际值非常接近。

#Guesses for the curve fit
initial = [0.21,0.15,0.46]
Ready = False
#Attempt to fit the reference curve
popt, popc = curve_fit(F,x,y,initial,bounds=(0,1))

#Create the fit curve
fitdata=[]
Ready = True
for i in range (0,50):
    fitdata.append(F(x[i],popt[0],popt[1],popt[2]))

然后绘制参考曲线和拟合曲线。然而,即使 p0 值非常接近实际值,拟合曲线也很差。我在以前的 StackOverflow 帖子中看到人们对此有疑问。

plt.plot(x,y,label='Reference')
plt.plot(x,fitdata,label='Fit')
plt.legend()
plt.show()

剧情如下:

我发现至少选择一些参数然后手动猜测并检查最终拟合是有用的。太奇怪了,即使curve_fit基本上是接触距离的最佳拟合参数,它甚至都无法拟合自己。

由于这个拟合方程的复杂性,我必须这样做吗?我对真实数据使用二次拟合(针对不同的项目)进行了几乎完全相同的计算,并且获得合适的曲线是微不足道的。

【问题讨论】:

    标签: python numpy scipy


    【解决方案1】:

    您将丢弃 tempy1tempy2 的所有结果,最后一个除外。我想你想追加到列表中。

    变化

    for i in range (0,len(u)):
            tempy1 = integrand1(tempx2,u[i])
            tempy2 = integrand2(tempx2,u[i]-ved[i])
    

    for i in range (0,len(u)):
            tempy1.append(integrand1(tempx2,u[i]))
            tempy2.append(integrand2(tempx2,u[i]-ved[i]))
    

    导致两个图重叠。

    【讨论】:

      猜你喜欢
      • 2013-05-18
      • 2015-07-31
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多