我有一个非常非线性的函数,其中有两个参数,curve_fit无法拟合:它适合第一个参数,但不更改第二个参数。
我也得到了经典
.../.local/lib/python3.6/site-packages/scipy/optimize/minpack.py:794: OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)
这是我要适合的功能:
def tand(x):
return np.tan(x*np.pi/180.)
def sind(x):
return np.sin(x*np.pi/180.)
def cosd(x):
return np.cos(x*np.pi/180.)
def coeffAx(A0, alpha):
return A0*cosd(alpha)**2.
def coeffBx(B0, alpha):
return B0*cosd(alpha)**2.
def coeffAy(A0,alpha):
return (1./2.)*A0*cosd(alpha)*sind(alpha)
def coeffBy(B0,alpha):
return (1./2.)*B0*cosd(alpha)*sind(alpha)
def Growth_rate(k,alpha,A0,B0,mu,r):
c = (r**2.-1.)/r**2.
return (k**2./(1.+(k*cosd(alpha))**2.))*(cosd(alpha)*(coeffBx(B0,alpha) - cosd(alpha)/(mu*r**2.)) + sind(alpha)*(coeffBy(B0,alpha) - sind(alpha)/(mu*r))*c - k*cosd(alpha)*(cosd(alpha)*coeffAx(A0,alpha) + sind(alpha)*coeffAy(A0,alpha)*c))
def Get_most_unstable(Sigma,alpha,k):
SigMax = np.amax(Sigma)
Coord = np.argwhere(Sigma == SigMax)
kmax = k[Coord[:,1]]
amax = alpha[Coord[:,0]]
return np.array([SigMax, kmax, amax])
def lambda_fit(V, C1, C2):
A0 = 3.5
B0 = 2
mu = tand(35)
# R = C2 * (V -1) + 1
k = np.linspace(0., 0.6, 1001)
alpha = np.array([0])
K,ALPHA = np.meshgrid(k,alpha)
kM = []
for v in V:
Sigma = Growth_rate(K,ALPHA,A0,B0,mu, C2 * (v - 1) + 1)
kM.append(Get_most_unstable(Sigma,alpha,k)[1])
return 2*np.pi*C1/np.array(kM)
这是数据:
V = np.array([1.0398639 , 1.13022518, 1.27846 , 1.31943454, 1.3898527 ,1.42114085])
Lambda_trans = [18.56117382616553, 13.747212426683717, 12.149968490349218, 12.034763392608163, 11.944807729994983, 12.6708866218023]
这就是我得到的:
p, pconv = curve_fit(lambda_fit, V, Lambda_trans, p0 = [1,10], check_finite = True)
/home/gadal/.local/lib/python3.6/site-packages/scipy/optimize/minpack.py:794: OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)
>>> p
array([ 0.69145457, 10. ])
>>> pconv
array([[inf, inf],
[inf, inf]])
如您所见,第一个参数已安装,但第二个参数未安装。奇怪的是,我可以使用9.5和10之间的第二个参数的值来获得非常好的拟合。我不明白为什么curve_fit无法做到..?有任何想法吗 ?我尝试将边界添加为bounds = ([0.5,8], [1.2,12])
,但结果是相同的。