我一直在尝试使用python中的scipy软件包将Voight配置文件与一组数据相匹配。具体来说,导入scipy.optimize包。我有一个旧的代码使用wolfz函数可以做到这一点,但我现在不能使用wolfz函数,因为我需要适合的函数实际上并不是一个Voight配置文件。实际上有两个不同的洛伦兹人组合在一起。无论如何,为了让我的程序开始,我尝试从头开始编写我自己的常规voight配置文件。对于我的所有测试参数,它产生与wolfz算法完全相同的输出。但是,当我尝试将其安装到数据集时,与wolfz算法不同,它给了我这个错误。
Traceback (most recent call last):
File "test_two.py", line 99, in <module>
output = opt.curve_fit(TWO_VOIGHT,x_arr,y_arr, guess)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 581, in curve_fit
res = leastsq(func, p0, args=args, full_output=1, **kw)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 371, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 20, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 447, in _general_function
return function(xdata, *params) - ydata
File "test_two.py", line 46, in TWO_VOIGHT
output = integrate.quad(lambda x: VOIGHT_INTEGRAND(x,det,shift,alpha,gamma_a),-np.inf,0.0)[0]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 311, in quad
points)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 378, in _quad
return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
quadpack.error: Supplied function does not return a valid float.
这是我的代码......
import numpy as np
import scipy.optimize as opt
import os
import re
import math
from scipy import integrate
from matplotlib import pyplot as plt
from scipy.special import wofz
def VOIGHT_INTEGRAND(x,x_func,x0,alpha,gamma_a):
output = np.exp(-alpha*((x-x0)**2.0))
output = ((alpha/pi)**(1.0/2.0))*output
output = output*(gamma_a/(2.0*pi))
output = output/(((x-x_func)**(2.0)) + ((gamma_a/2.0)**2.0))
return output
def TWO_VOIGHT(det,gamma_a,shift,scale):
sigma = SIGMA_CALC(1.3e-6, (88.0 * 1.67e-27),4.34829121312334e14*2.0*pi)
sigma = sigma/gamma
alpha = 1.0/(2.0*sigma*sigma)
output = integrate.quad(lambda x: VOIGHT_INTEGRAND(x,det,shift,alpha,gamma_a),-np.inf,np.inf)[0]
output = output*scale
return output
def SIGMA_CALC(temp,mass, hertz):
sigma = math.sqrt((hertz**2.0) * (1.381e-23) * temp / (mass * (3.0e8 * 3.0e8)))
return sigma
f = open('out_gauss_1.0E+17_N_400.out','r')
x_arr = []
y_arr = []
for line in f:
line_arr = line.split()
x_arr.append(float(line_arr[0]))
y_arr.append(float(line_arr[1]))
guess = np.array([1.0,0.0,1.0])
output = opt.curve_fit(TWO_VOIGHT,x_arr,y_arr, guess)
print 'Gamma 1:', output[0][0]/width, n_atoms
print 'Gamma 2:', output[0][1]/width
我无法弄清楚为什么会这样。你们有什么想法吗?
我打开的文件的内容是:
-6.4 0.237952863717
-6.2 0.268912617983
-6.0 0.305446644483
-5.8 0.3485094196
-5.6 0.398708815217
-5.4 0.456942840517
-5.2 0.524574736517
-5.0 0.602806918033
-4.8 0.692499762417
-4.6 0.794458526383
-4.4 0.909341570333
-4.2 1.03784998038
-4.0 1.18085462442
-3.8 1.33867094077
-3.6 1.51124577498
-3.4 1.69824987307
-3.2 1.8986205928
-3.0 2.11082341933
-2.8 2.33307233833
-2.6 2.5631992475
-2.4 2.79829104117
-2.2 3.0351678375
-2.0 3.2708957095
-1.8 3.50152844917
-1.6 3.72206345267
-1.4 3.928262486
-1.2 4.11657091433
-1.0 4.2838813515
-0.8 4.426546224
-0.6 4.54028968367
-0.4 4.62285975717
-0.2 4.67327511933
0.0 4.690377612
0.2 4.673365306
0.4 4.622312377
0.6 4.5382663485
0.8 4.42294278633
1.0 4.2796806435
1.2 4.11257992183
1.4 3.92422074583
1.6 3.71751059817
1.8 3.49728613283
2.0 3.267883908
2.2 3.03266881417
2.4 2.79548030417
2.6 2.56023351717
2.8 2.33020427217
3.0 2.10810391283
3.2 1.89634314437
3.4 1.69664525222
3.6 1.50996288777
3.8 1.3371250582
4.0 1.17885436362
4.2 1.03533114692
4.4 0.906425218067
4.6 0.7918292208
4.8 0.690636432067
5.0 0.6017171318
5.2 0.524059884217
5.4 0.456797620467
5.6 0.398844547883
5.8 0.348953529967
6.0 0.306220038467
6.2 0.269773789833
该函数似乎返回一个标量。我用这个检查过:
x_arr = np.linspace(-6,6,100)
y1_arr = []
y2_arr = []
for x in x_arr:
y1_arr.append(TWO_VOIGHT(x,1.0,0.0,1.0))
print TWO_VOIGHT(x,1.0,0.0,1.0)