前提・実現したいこと
pythonで積分関数(フォークト関数)を使ってフィッティングしようとしています。
単純な関数ではフィッティングができたのですが、積分を組み込むとうまくいきません。
フォークト関数では、zについて積分をします。
パラメータは2つでparam1とparam2です。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-baa10670dc7d> in <module> 21 22 p0 = 0.5, 0.5 ---> 23 popt, pcov = curve_fit(func, array_x, array_y, p0) ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs) 750 # Remove full_output from kwargs, otherwise we're passing it in twice. 751 return_full = kwargs.pop('full_output', False) --> 752 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs) 753 popt, pcov, infodict, errmsg, ier = res 754 cost = np.sum(infodict['fvec'] ** 2) ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag) 381 if not isinstance(args, tuple): 382 args = (args,) --> 383 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) 384 m = shape[0] 385 ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape) 24 def _check_func(checker, argname, thefunc, x0, args, numinputs, 25 output_shape=None): ---> 26 res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) 27 if (output_shape is not None) and (shape(res) != output_shape): 28 if (output_shape[0] != 1): ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in func_wrapped(params) 456 if transform is None: 457 def func_wrapped(params): --> 458 return func(xdata, *params) - ydata 459 elif transform.ndim == 1: 460 def func_wrapped(params): <ipython-input-10-baa10670dc7d> in func(x, param1, param2) 17 def voigt(z): 18 return param1 * 0.8256 / np.pi**(3/2) / param2 / widthL * np.exp((-1) * (2 * z / param2)**2 * np.log(2)) / (1 + (2 * (x - z) / widthL)**2) ---> 19 integration = integrate.quad(voigt, -np.inf, np.inf)[0] 20 return integration 21 ~\Anaconda3\lib\site-packages\scipy\integrate\quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst) 339 if weight is None: 340 retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit, --> 341 points) 342 else: 343 retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel, ~\Anaconda3\lib\site-packages\scipy\integrate\quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points) 448 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) 449 else: --> 450 return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit) 451 else: 452 if infbounds != 0: TypeError: only size-1 arrays can be converted to Python scalars
該当のソースコード
python
1import numpy as np 2from scipy import integrate 3from scipy.optimize import curve_fit 4import math 5import pandas as pd 6 7Eins = 2.01 * 10**8 8Lorentzbroad = Eins / 2 / math.pi 9widthL = Lorentzbroad / 10**9 10 11# フィッティング用データの宣言 12df = pd.read_csv('book1.csv') 13array_x=np.array(df['t']) 14array_y=np.array(df['y']) 15 16def func(x, param1, param2): 17 def voigt(z): 18 return param1 * 0.8256 / np.pi**(3/2) / param2 / widthL * np.exp((-1) * (2 * z / param2)**2 * np.log(2)) / (1 + (2 * (x - z) / widthL)**2) 19 integration = integrate.quad(voigt, -np.inf, np.inf)[0] 20 return integration 21 22p0 = 0.5, 0.5 23popt, pcov = curve_fit(func, array_x, array_y, p0)
試したこと
補足情報(FW/ツールのバージョンなど)
python3 Anaconda3
回答3件
あなたの回答
tips
プレビュー