1
我想用python製作一些涉及積分的非線性配件,積分的極限取決於自變量。代碼如下:用變量作爲積分極限的非線性最小二乘擬合
import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.integrate import quad
T,M=np.genfromtxt("zfc.txt", unpack=True, skiprows = 0) #here I load the data to fit
plt.plot(T,M,'o')
def arg_int1(x,sigma,Ebm):
return (1/(np.sqrt(2*np.pi)*sigma*Ebm))*np.exp(-(np.log(x/float(Ebm))**2)/(2*sigma**2))
def arg_int2(x,sigma,Ebm):
return (1/(np.sqrt(2*np.pi)*sigma*x))*np.exp(-(np.log(x/float(Ebm))**2)/(2*sigma**2))
def zfc(x,k1,k2,k3):
Temp=x*k2*27/float(k2/1.36e-16)
#Temp=k2*27/float(k2/1.36e-16) #apparently x can't be fitted with curve_fit if appears as well in the integral limits
A=sc.integrate.quad(arg_int1,0,Temp,args=(k3,k2))[0]
B=sc.integrate.quad(arg_int2,Temp,10*k2,args=(k3,k2))[0]
M=k1*(k2/1.36e-16*A/x+B)
return M
T_fit=np.linspace(1,301,301)
popt, pcov = curve_fit(zfc,T,M,p0=(0.5,2.802e-13,0.46))
M_fit=np.zeros(301)
M_fit[0]=zfc(100,0.5,2.8e-13,0.46)
for i in range (1,301):
M_fit[i-1]=zfc(i,popt[0],popt[1],popt[2])
plt.plot(T_fit,M_fit,'g')
的eror,我得到的是:
File "C:\Users\usuario\Anaconda\lib\site-packages\scipy\integrate\quadpack.py", line 329, in _quad
if (b != Inf and a != -Inf):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我不明白,既然功能是明確界定。我知道我的問題的解決方案是feeded參數(我已經適合mathematica)。我試圖尋找Bloch-Gruneisen函數的擬合(自變量也定義了積分極限),但我沒有找到任何線索。