How to do nonlinear complex root finding in Python

為{幸葍}努か 提交于 2019-12-01 04:04:56

fsolve finds zeros of functions from R^n -> R. The similar function root finds zeros of functions from R^n -> R^m.

It looks like you're trying to find zeros of a function from C^2 -> C^2, which as far as I know scipy.optimize doesn't support directly - but you could try writing it a function from R^4 -> R^4 and then using root. For example, something along the lines of:

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]

should work, though it might be significantly faster to do it directly on the real numbers instead of repeatedly wrapping into complex and unwrapping.

You could try mpmath's findroot(sympy):

from mpmath import findroot

#Your code here

ans = findroot([z1,z2],(0,0))
print(ans)

Returns:

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

which is a solution of your system.
Mpmath is a multiprecision library so it's routines are generally slower, but you could give it a try!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!