python fsolve:' list'对象不可调用

时间:2015-05-21 15:34:12

标签: python scipy

我似乎遇到了scipy.optimize.fsolve()的问题。错误消息是:

TypeError: 'list' object is not callable.

代码:

def eqs(P, z1):

    x1 = z1[0]
    y1 = z1[1]
    x2 = 1 - x1 
    y2 = 1 - y1

    fl1 = Fug(0, fsolve(PR(x1, P), 0.5), x1, P)
    fl2 = Fug(1, fsolve(PR(x1, P), 0.5), x2, P)
    fg1 = Fug(0, fsolve(PR(y1, P), 150), y1, P)
    fg2 = Fug(1, fsolve(PR(y1, P), 150), y2, P)

    error = (x1*fl1 - y1*fg1) + ((1-x1)*fl2 - (1-y1)*fg2)
    phiphi = x1*fl1 - y1*fg1

return [phiphi, error]


def main():

    P = 18
    x1 = (19.99 - P) / (19.99 - 3.59)
    y1 = IdealCase_y1(x1)
    z1 = [x1, y1]

    soln = fsolve(eqs(P, z1), z1)   
    x1, y1 = soln

return soln

我之前在fsolve中使用了列表(带有[]' s的列表)没有问题,所以我不确定在哪里查找问题。

函数eqs(P, z1),以及Fug(...)以及IdealCase_y1运行没有问题,但是当运行main()时,python返回上面提到的错误。我的想法是创建一个函数main(),我可以输入不同的P值并返回x1,y1作为输出。主要的x1 = ...是"通知猜测",告诉fsolve在哪里查看,然后从该值生成y1猜测。

edit:IdealCase_y1(x1)是用于估计fsolve所需的y1值的函数。追溯是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
  File "C:/Users/ShahuN/Documents/Project/q2_v0.3.py", line 229, in <module>
test = main()
  File "C:/Users/ShahuN/Documents/Project/q2_v0.3.py", line 223, in main
soln = fsolve(eqs(P, z1), [x1, y1])   
  File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 140, in fsolve
    res = _root_hybr(func, x0, args, jac=fprime, **options)
  File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 197, in _root_hybr
    shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
  File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 20, in _check_func
     res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
 TypeError: 'list' object is not callable

如果那是追溯的意思。

1 个答案:

答案 0 :(得分:1)

soln = fsolve(eqs(P, z1), z1) 

1)  func应该是一个可调用的函数。

2) 期望的参数(可选)是tuple不是列表,您必须在调用fsolve()时将列表转换为元组

scipy.optimize.fsolve(func, x0, args=(), fprime=None, full_output=0, col_deriv=0, xtol=1.49012e-08, maxfev=0, band=None, epsfcn=None, factor=100, diag=None)[source]

func : callable f(x, *args)
A function that takes at least one (possibly vector) argument.

args : tuple, optional
Any extra arguments to func.

修改 您以错误的方式调用函数eqs,它应如下所示:

soln = fsolve(eqs, P, args=(z1))
x1, y1 = soln