python - 如何在 Python 中进行非线性复根查找

标签 python scipy mathematical-optimization

我想对以下非线性方程进行根搜索,我在 Python 中进行了搜索,但它不起作用。我的代码在下面

from pylab import *
import scipy
import scipy.optimize

def z1(x,y):
    temp=1+1j+x+2*y;
    return temp

def z2(x,y):
    temp=-1j-2*x+sqrt(3)*y;
    return temp

def func(x):
    temp=[z1(x[0],x[1])-1.0/(1-1.0/(z2(x[0],x[1]))),1-2.0/(z2(x[0],x[1])-4.0/z1(x[0],x[1]))]
    return temp

result=scipy.optimize.fsolve(func,[1+1j,1+1j])

print result

当我运行它时,它显示错误:

---> 30 结果=scipy.optimize.fsolve(func,[1+1j,1+1j])

C:\Python27\lib\site-packages\scipy\optimize\minpack.py 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))

最佳答案

fsolve 从 R^n -> R 中找到函数的零点。类似的函数 root从 R^n -> R^m 中找到函数的零点。

看起来你正试图从 C^2 -> C^2 中找到一个函数的零点,据我所知 scipy.optimize 不直接支持 - 但你可以尝试将它写成一个函数R^4 -> R^4 然后使用 root。例如,类似以下内容:

def func_as_reals(x):
    r1, c1, r2, c2 = x
    a, b = func([complex(r1, c1), complex(r2, c2)])
    return [a.real, a.imag, b.real, b.imag]

应该可以工作,尽管直接对实数执行此操作而不是重复包装到复数和解包中可能要快得多。

关于python - 如何在 Python 中进行非线性复根查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15213141/

相关文章:

python - 裁剪时在 PIL 中保留图像类型信息

Python BeautifulSoup 从 find_all() 返回错误的输入列表

python - 使用 Python [摘要] 读取 wav 文件的最简单方法是什么?

python - 你如何在 Numpy 中找到 IQR?

algorithm - 找到最接近另一组的点集

python - 解包集合时内存中会发生什么?

python - 如何在CSS中水平显示输入表单?

python - 如何找到 scipy.integrate.ode 的默认 atol 和 rtol?

algorithm - 最大化相邻数的乘积和

r - Matlab 的 fminunc 函数的 R 等价物是什么?