首页 文章

如何在Python中进行非线性复杂根发现

提问于
浏览
7

我想对以下非线性方程进行根搜索,我在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 result = scipy.optimize.fsolve(func,[1 1j,1 1j])

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

2 回答

  • 1

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

    它看起来像你直接支持 - 但你可以尝试从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]
    

    应该工作,虽然直接在实数上做它可能要快得多,而不是重复包装到复杂和展开 .

  • 6

    你可以试试mpmath的findroot(sympy):

    from mpmath import findroot
    
    #Your code here
    
    ans = findroot([z1,z2],(0,0))
    print(ans)
    

    返回:

    [(-0.302169479251962 - 0.651084739625981j)]
    [(-0.348915260374019 - 0.174457630187009j)]
    

    这是您系统的解决方案 .
    Mpmath是一个多精度库,因此它的例程通常较慢,但你可以尝试一下!

相关问题