1

I would like to fit data and discover three parameters (a1, a2 and a3) with an integral function. I found a question just like mine (python fitting curve with integral function). However, even though basicaly copiyng the solution changing the function and data, I`m getting an error that was not explored in this topic. I am a physicist trying to learn python for simulations and plotting data, but finding discourage in the syntax. Here is the code:

import pandas as pd
import numpy as np
from scipy import integrate
import scipy.optimize
import matplotlib.pyplot as plt

hanlepos = pd.read_table("hanlepos.dat")
hanleposxlist=hanlepos.iloc[:,0]
hanleposylist=hanlepos.iloc[:,1]

hanleposxdata = np.array(hanleposxlist)
hanleposydata = np.array(hanleposylist)

parameter_initial = np.array([0.012,0.2,0.005]) # a1, a2, a3

def func(x,a1,a2,a3): 
    integrand = lambda T : a1*np.exp(-T-a2/T)*np.cos(a3*x*T)/np.sqrt(T)
    num = np.asarray([integrate.quad(integrand,0,np.inf)[0]])
    den = np.exp(-2*np.sqrt(a2))*np.sqrt(np.pi)[0]
    res = num/den
    return res

parameter_optimal, cov = scipy.optimize.curve_fit(func, hanleposxdata, hanleposydata, p0=parameter_initial) 
print("paramater =", parameter_optimal)
y = func(hanleposxdata, *parameter_optimal)
plt.plot(hanleposxdata, hanleposydata, 'o')
plt.plot(hanleposxdata, y, '-')
plt.show()

The error is:

  File "C:\Users\Vinicius\Anaconda3\lib\site-packages\scipy\integrate\quadpack.py", line 450, in _quad
return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)

TypeError: only size-1 arrays can be converted to Python scalars

0 Answers0