高山听流水
2019-01-27 00:42
采纳率: 50%
浏览 1.3k

scipy解非线性方程组报错TypeError: 'int' object is not callable

scipy解非线性方程组报错TypeError: 'int' object is not callable。网上说是参数和函数重名的缘故,但貌似我的并没有重名,代码如下:

from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import root,fsolve

def fun(x):
    for i in range(0,len(x)):
        x[i] = float(x[i])
    fy = np.array([(x[0]*2010**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2010**x[3])+(-75.2303)**2/(x[4]*2010**x[5])-6259)),(x[0]*2011**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2011**x[3])+(-75.2303)**2/(x[4]*2011**x[5])-5958)),(x[0]*2012**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2012**x[3])+(-81.4421)**2/(x[4]*2012**x[5])-2624)),(x[0]*2014**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2014**x[3])+(-75.2303)**2/(x[4]*2014**x[5])-4809)),(x[0]*2015**x[1])*np.exp(-1/2(39.701**2/(x[2]*2015**x[3])+(-84.2187)**2/(x[4]*2015**x[5])-3877)),(x[0]*2017**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2017**x[3])+(-81.4421)**2/(x[4]*2017**x[5])-7381))])
    return fy
res_fsolve = fsolve(fun,[1,1,1,1,1,1])
print(res_fsolve)

报错内容如下:

TypeError Traceback (most recent call last)
in
9 fy = np.array([(x[0]*2010**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2010**x[3])+(-75.2303)**2/(x[4]*2010**x[5])-6259)),(x[0]*2011**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2011**x[3])+(-75.2303)**2/(x[4]*2011**x[5])-5958)),(x[0]*2012**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2012**x[3])+(-81.4421)**2/(x[4]*2012**x[5])-2624)),(x[0]*2014**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2014**x[3])+(-75.2303)**2/(x[4]*2014**x[5])-4809)),(x[0]*2015**x[1])*np.exp(-1/2(39.701**2/(x[2]*2015**x[3])+(-84.2187)**2/(x[4]*2015**x[5])-3877)),(x[0]*2017**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2017**x[3])+(-81.4421)**2/(x[4]*2017**x[5])-7381))])
10 return fy
---> 11 res_fsolve = fsolve(fun,[1,1,1,1,1,1])
12 print(res_fsolve)

c:\users\anton\appdata\local\programs\python\python36\lib\site-packages\scipy\optimize\minpack.py in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag)
145 'diag': diag}
146
--> 147 res = _root_hybr(func, x0, args, jac=fprime, **options)
148 if full_output:
149 x = res['x']

c:\users\anton\appdata\local\programs\python\python36\lib\site-packages\scipy\optimize\minpack.py in _root_hybr(func, x0, args, jac, col_deriv, xtol, maxfev, band, eps, factor, diag, **unknown_options)
211 if not isinstance(args, tuple):
212 args = (args,)
--> 213 shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
214 if epsfcn is None:
215 epsfcn = finfo(dtype).eps

c:\users\anton\appdata\local\programs\python\python36\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):

in fun(x)
7 for i in range(0,len(x)):
8 x[i] = float(x[i])
----> 9 fy = np.array([(x[0]*2010**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2010**x[3])+(-75.2303)**2/(x[4]*2010**x[5])-6259)),(x[0]*2011**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2011**x[3])+(-75.2303)**2/(x[4]*2011**x[5])-5958)),(x[0]*2012**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2012**x[3])+(-81.4421)**2/(x[4]*2012**x[5])-2624)),(x[0]*2014**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2014**x[3])+(-75.2303)**2/(x[4]*2014**x[5])-4809)),(x[0]*2015**x[1])*np.exp(-1/2(39.701**2/(x[2]*2015**x[3])+(-84.2187)**2/(x[4]*2015**x[5])-3877)),(x[0]*2017**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2017**x[3])+(-81.4421)**2/(x[4]*2017**x[5])-7381))])
10 return fy
11 res_fsolve = fsolve(fun,[1,1,1,1,1,1])

TypeError: 'int' object is not callable

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 高山听流水 2019-01-31 15:32
    已采纳

    我后来发现,是我大意了,在函数里面,np.exp()里面1/2与后面的部分相乘,我把*号省略了。python里面的数字与数字之间的*是不能省略的。

    点赞 打赏 评论

相关推荐