I have some data that I am trying to fit with a model that includes and definite integral equation. My strategy was to use the optimize.leastsq and integrate.quad, I keep getting a type error: "only length-1 arrays can be converted to Python scalars"
Any help would be greatly appreciated.
Here's the relevant part of my code (keep in mind that self.vvals and self.bvals are 1-D arrays, self.L is a float):
def NLFit(self):
'''fits data to the NL formula'''
L=self.L
def model(m0,m1,m2,B): #m0=So, m1=D, m2=NLtau
return scipy.integrate.quad(lambda t: -m0/(math.sqrt(4*math.pi*m1*t))*math.exp(- L**2/(4*m1*t))*math.exp(-t/m2)*math.cos(g*muB*B*t/h) , 0, 1e-9)
def residuals(p,y,x):
m0,m1,m2=p
err=y-model(m0,m1,m2,x)
return err
def peval(x,p):
return model(p[0],p[1],p[2],x)
#initial conditions
p0=[1,1,1]
#find fit
B=self.bvals
V=self.vvals
plsq=scipy.optimize.leastsq(residuals,p0,args=(V,B))
print plsq[0]