scipy.optimize.fsolve'正确的浮点数'错误

时间:2013-03-10 15:49:58

标签: scipy root

我需要计算函数的根,我正在使用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]是相同类型的数组,那么我在[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导入的)

这是我试图找到根的功能图。 plot

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

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

1 个答案:

答案 0 :(得分: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

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

使用您的功能,如果您只对零感兴趣,我认为如果您将sqrt提升到第四种力量,您可以摆脱S0

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])