python中quad_将变量从数组传递给python中的scipy.integrate.quad()

I'm using python to fit function to my dataset. My code worked and fitted function with curve_fit before I added integral scipy.integrate.quad() to the definition of function. I checked why does it give me an error "Supplied function does not return a valid float." and it turns out that the code works fine if I don't pass a variable from dataset over which I'm fitting my curve. If I set arbitrary value like 5. here: scipy.integrate.quad(args(5.)) instead of Xi it works perfectly again. Here is my code, help me please!:

from scipy import integrate

calka = lambda z, vz, t: np.exp(-1.*0.001-(z*0.001+vz*t*2.48138957816e-05))**2*1./((20.)**2)*np.exp(-(1.*1.42060911e-05-z*1.42060911e-05)**2*1./((20.)**2))

z_lin = linspace(0,zmax,npoints/2)

def func(Xi, vx, vz):

z=0.0

f=0.

t=Xi

f=integrate.quad(calka, z_lin[0], z_lin[3998], args=(vz,Xi))[0] #it works fine with arbitrarily set value

q=(1.7*np.pi*4./1.569)**2*0.000024813

v=(0.000024813)**2/((13.)**2)

p=2.*2.*np.pi/1.569*0.000024813

return np.exp(-vx*v*Xi)*np.exp(-(Xi*q*1.3))*np.cos(p*Xi*vz)*f

xdata = np.linspace(0, 1000, 1000)

print len(xdata)

ydata= autokowariancja[0,:]

popt, pcov = curve_fit(func, xdata, ydata)

print popt, pcov

plt.figure(figsize=(8,5))

pylab.plot(func(xdata, popt[0], popt[1]), 'b')

pylab.plot(autokowariancja[0,:], 'r')

legend("NoWin","Win")

pylab.show()

解决方案

I solved the problem. Xi is an array and integrate.quad() takes float only, so I splitted Xi array by enumerating it and I created an array the size of Xi and calculated integral for every element of Xi separately and passed it into an array:

def func(Xi, vx, vz):

z=0.0

f=0.

t=Xi

for i, item in enumerate(Xi):

integral[i]=integrate.quad(calka, z_lin[0], z_lin[3998], args=(vz, Xi[i]))[0]

q=(1.7*np.pi*4./1.569)**2*0.000024813

v=(0.000024813)**2/((13.)**2)

p=2.*2.*np.pi/1.569*0.000024813

return np.exp(-vx*v*Xi)*np.exp(-(Xi*q*1.3))*np.cos(p*Xi*vz)*integral

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值