2

我需要计算一个函数的根,我正在使用 scipy.optimize.fsolve。但是,当我调用 fsolve 时,有时它会输出一个错误,提示“函数调用的结果不是正确的浮点数组”。

这是我正在使用的输入示例:

In [45]: guess = linspace(0.1,1.0,11)

In [46]: alpha_old = 0.5

In [47]: n_old = 0

In [48]: n_new = 1

In [49]: S0 = 0.9

In [50]: fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: array cannot be safely cast to required type
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/home/andres/Documents/UdeA/Proyecto/basis_analysis/<ipython-input-50-f1e9a42ba072> in <module>()
----> 1 fsolve(bb.alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))

/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.pyc in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag)
    123             maxfev = 200*(n + 1)
    124         retval = _minpack._hybrd(func, x0, args, full_output, xtol,
--> 125                 maxfev, ml, mu, epsfcn, factor, diag)
    126     else:
    127         _check_func('fsolve', 'fprime', Dfun, x0, args, n, (n,n))

error: Result from function call is not a proper array of floats.

In [51]: guess = linspace(0.1,1.0,2)

In [52]: fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
Out[52]: array([ 0.54382423,  1.29716005])

In [53]: guess = linspace(0.1,1.0,3)

In [54]: fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
Out[54]: array([ 0.54382423,  0.54382423,  1.29716005])

在那里你可以看到,对于 In[46] 中定义的“guess”,它会输出一个错误,但是对于 In[51] 和 In[53] 中定义的“guess”,它可以正常工作。据我所知,In[46]、In[51] 和 In[53] 都是相同类型的数组,那么我在 In[50] 中出现错误的原因是什么?

以下是我正在调用的函数,以防它们是问题的原因:

def alpha_eq(alpha2,n1,alpha1,n2,S0):
    return overlap(n1,alpha1,n2,alpha2) - S0

def overlap(n1,alpha1,n2,alpha2):
    aux1 = sqrt((2.0*alpha1)**(2*n1+3)/factorial(2*n1+2))
    aux2 = sqrt((2.0*alpha2)**(2*n2+3)/factorial(2*n2+2))
    return aux1 * aux2 * factorial(n1+n2+2) / (alpha1+alpha2)**(n1+n2+3)

(函数 linspace、sqrt 和 factorial 是从 scipy 导入的)

这是我试图找到根源的函数图。 阴谋

在我看来,这是 fsolve 的一个错误,但是我想确保在报告之前我没有犯愚蠢的错误。

如果我的代码有问题,请告诉我。谢谢!

4

1 回答 1

2

我已经修改了您overlap的调试功能,如下所示:

def overlap(n1,alpha1,n2,alpha2):
    print n1, alpha1, n2, alpha2
    aux1 = sqrt((2.0*alpha1)**(2*n1 + 3)/factorial(2*n1 + 2))
    aux2 = sqrt((2.0*alpha2)**(2*n2 + 3)/factorial(2*n2 + 2))
    ret = aux1 * aux2 * factorial(n1+n2+2) / (alpha1+alpha2)**(n1+n2+3)
    print ret, ret.dtype
    return ret

当我尝试重现您的错误时,会发生以下情况:

>>> scipy.optimize.fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
0 0.5 1 [ 0.1   0.19  0.28  0.37  0.46  0.55  0.64  0.73  0.82  0.91  1.  ]
[ 0.11953652  0.34008953  0.54906314  0.71208678  0.82778065  0.90418052
  0.95046505  0.97452352  0.98252708  0.97911263  0.96769965] float64

...

0 0.5 1 [ 0.45613162  0.41366639  0.44818267  0.49222515  0.52879856  0.54371741
  0.50642005  0.28700652 -3.72580492  1.81152096  1.41975621]
[ 0.82368346+0.j          0.77371428+0.j          0.81503304+0.j
  0.85916030+0.j          0.88922137+0.j          0.89992643+0.j
  0.87149667+0.j          0.56353606+0.j          0.00000000+1.21228156j
  0.75791881+0.j          0.86627491+0.j        ] complex128

因此,在求解方程的过程中,正在计算负数的平方根,这会导致complex128dtype 和您的错误。

sqrt使用您的功能,如果您只对零感兴趣,我认为如果您提高S0到 4 次方,您可以摆脱s:

def alpha_eq(alpha2,n1,alpha1,n2,S0):
    return overlap(n1,alpha1,n2,alpha2) - S0**4

def overlap(n1,alpha1,n2,alpha2):
    aux1 = (2.0*alpha1)**(2*n1 + 3)/factorial(2*n1 + 2)
    aux2 = (2.0*alpha2)**(2*n2 + 3)/factorial(2*n2 + 2)
    ret = aux1 * aux2 * factorial(n1+n2+2) / (alpha1+alpha2)**(n1+n2+3)
    return ret

现在:

>>> scipy.optimize.fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
array([ 0.92452239,  0.92452239,  0.92452239,  0.92452239,  0.92452239,
        0.92452239,  0.92452239,  0.92452239,  0.92452239,  0.92452239,
        0.92452239])
于 2013-03-11T20:34:01.277 回答