在Python中查找函数的根

在Python中查找函数的根,python,Python,我正在尝试使用scipy函数计算函数的根,但错误不断: TypeError: 'numpy.array' object is not callable 我想把方程定义为函数可能更容易,但我试过几次,都没有用 代码: 堆栈跟踪: Traceback (most recent call last): File "<ipython-input-52-081a9cc9c0ea>", line 1, in <module> runfile('/home/luke/D

我正在尝试使用scipy函数计算函数的根,但错误不断:

TypeError: 'numpy.array' object is not callable
我想把方程定义为函数可能更容易,但我试过几次,都没有用

代码:

堆栈跟踪:

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
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('/home/luke/Documents/PythonPrograms/ModeSolver_stepinex/ModeSolver_stepinex.py',wdir='/home/luke/Documents/PythonPrograms/ModeSolver_stepinex')
runfile中的文件“/usr/lib/python2.7/site packages/spyderlib/widgets/externalshell/sitecustomize.py”,第580行
execfile(文件名、命名空间)
文件“/home/luke/Documents/PythonPrograms/ModeSolver_stepinex/ModeSolver_stepinex.py”,第52行,在
x=fsolve(func,0)
fsolve中的文件“/usr/lib64/python2.7/site packages/scipy/optimize/minpack.py”,第140行
res=\u root\u hybr(func,x0,args,jac=fprime,**选项)
文件“/usr/lib64/python2.7/site packages/scipy/optimize/minpack.py”,第197行,在根目录下
shape,dtype=\u check\u func('fsolve','func',func,x0,args,n,(n,))
文件“/usr/lib64/python2.7/site packages/scipy/optimize/minpack.py”,第20行,在检查函数中
res=至少1d(thefunc(*(x0[:numput],)+args)))
TypeError:“numpy.ndarray”对象不可调用

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

如果我只是打印你的函数:

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)

但是,我不知道如何定义函数。

这是因为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

最好包括导致
TypeError
的行。添加错误堆栈跟踪。
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