在Python

时间:2016-03-08 01:14:31

标签: python

我尝试使用scipy函数fsolve计算函数的根,但错误会持续标记:

TypeError: 'numpy.array' object is not callable

我认为将方程式定义为函数可能更容易,但我已经尝试了几次但没有用。

代码:

import scipy
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize

# Constants
wavelength = 0.6328
ncore = 1.462420
nclad = 1.457420
a = 8.335

# Mode Order
l = 0

# Mode parameters
V = (2 * np.pi * a / wavelength) * np.sqrt(ncore**2 - nclad**2)
U = np.arange(0, V, 0.01)
W = np.sqrt(V**2-U**2)

func = U * scipy.special.jv(l+1, U) / scipy.special.jv(l, U) - W * scipy.special.kv(l+1, W) / scipy.special.kv(l, W)

from scipy.optimize import fsolve
x = fsolve(func,0)
print x

堆栈跟踪:

Traceback (most recent call last):

  File "<ipython-input-52-081a9cc9c0ea>", line 1, in <module>
    runfile('/home/luke/Documents/PythonPrograms/ModeSolver_StepIndex/ModeSolver_StepIndex.py', wdir='/home/luke/Documents/PythonPrograms/ModeSolver_StepIndex')

  File "/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 580, in runfile
    execfile(filename, namespace)

  File "/home/luke/Documents/PythonPrograms/ModeSolver_StepIndex/ModeSolver_StepIndex.py", line 52, in <module>
    x = fsolve(func,0)

  File "/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.py", line 140, in fsolve
    res = _root_hybr(func, x0, args, jac=fprime, **options)

  File "/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.py", line 197, in _root_hybr
    shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))

  File "/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.py", line 20, in _check_func
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))

TypeError: 'numpy.ndarray' object is not callable

2 个答案:

答案 0 :(得分:1)

这是因为fsolve将一个函数作为参数。 试试这个,注意你仍然会遇到一些运行时错误,你必须检查你是否正确构造了你的func返回,我会留下你的想法。

import scipy
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize

# Constants
wavelength = 0.6328
ncore = 1.462420
nclad = 1.457420
a = 8.335

# Mode Order
# l = 0

# Mode parameters
V = (2 * np.pi * a / wavelength) * np.sqrt(ncore**2 - nclad**2)
U = np.arange(0, V, 0.01)
W = np.sqrt(V**2-U**2)

def func(l):
    return U * scipy.special.jv(l+1, U) / scipy.special.jv(l, U) - W * scipy.special.kv(l+1, W) / scipy.special.kv(l, W)

from scipy.optimize import fsolve
x = fsolve(func,0)
print x

答案 1 :(得分:0)

您需要将函数传递给fsolve而不是array

如果我只打印你的功能:

func
array([ -1.04882076e+01,  -1.04881526e+01,  -1.04879876e+01,
        -1.04877125e+01,  -1.04873274e+01,  -1.04868321e+01,
        -1.04862266e+01,  -1.04855109e+01,  -1.04846847e+01,
        -1.04837481e+01,  -1.04827008e+01,  -1.04815428e+01,
        -1.04802738e+01,  -1.04788938e+01,  -1.04774024e+01,
        -1.04757996e+01,  -1.04740850e+01,  -1.04722585e+01,
        -1.04703198e+01,  -1.04682686e+01,  -1.04661046e+01,
        -1.04638275e+01,  -1.04614371e+01,  -1.04589330e+01,
        -1.04563147e+01,  -1.04535820e+01,  -1.04507345e+01,
        -1.04477718e+01,  -1.04446934e+01,  -1.04414988e+01,
... ]

这是一个数组,但你想要一个函数。这样的事情有效:

def linear(x):
    return 2*x+4

fsolve(linear, 0)

不知道如何定义你的功能。