Passing variable from an array to scipy.integrate.quad() in python

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()

Answers


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

Need Your Help

Remove default right margin of a div

html css css3

I need to remove default right margin of a div:

JavaFX layouts question

listview javafx layout hbox vbox

I am having some problem understanding layouts in JavaFX. Consider following code.