从python中的非线性方程组中找到复​​杂的根 [英] Finding complex roots from set of non-linear equations in python

查看:106
本文介绍了从python中的非线性方程组中找到复​​杂的根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在测试一种已发表在文献中的算法,该算法涉及在Matlab和Python中求解一组"m"个非线性方程.非线性方程组涉及包含复数的输入变量,因此所得的解也应是复数.到目前为止,通过使用以下代码行,我已经在Matlab中获得了不错的效果:

I have been testing an algorithm that has been published in literature that involves solving a set of 'm' non-linear equations in both Matlab and Python. The set of non-linear equations involves input variables that contain complex numbers, and therefore the resulting solutions should also be complex. As of now, I have been able to get pretty good results in Matlab by using the following lines of code:

lambdas0 = ones(1,m)*1e-5;
options = optimset('Algorithm','levenberg-marquardt',...
'MaxFunEvals',1000000,'MaxIter',10000,'TolX',1e-20,...
'TolFun',1e-20);

Eq = @(lambda)maxentfun(lambda,m,h,g);
[lambdasf]  = fsolve(Eq,lambdas0,options);

其中h和g分别是复数矩阵和向量.该解决方案在很宽的初始值范围内都能很好地收敛.

where h and g are a complex matrix and vector, respectively. The solution converges very well for a wide range of initial values.

我一直试图在Python中模仿这些结果,但是收效甚微.数值求解器的设置似乎大不相同,并且'levenburg-marquardt'算法存在于函数根下.在python中,此算法无法处理复杂的根,当我运行以下行时:

I have been trying to mimic these results in Python with very little success however. The numerical solvers seem to be set up much differently, and the 'levenburg-marquardt' algorithm exists under the function root. In python this algorithm cannot handle complex roots, and when I run the following lines:

lambdas0 = np.ones(m)*1e-5

sol = root(maxentfun, lambdas0, args = (m,h,g), method='lm', tol = 1e-20, options = {'maxiter':10000, 'xtol':1e-20})

lambdasf = sol.x

我收到以下错误:

minpack.error: Result from function call is not a proper array of floats.

我尝试使用其他一些算法,例如'broyden2'和'anderson',但是它们比Matlab逊色得多,并且只有在考虑了初始条件后才能给出好的结果.函数"fsolve"也不能处理复杂的变量.

I have tried using some of the other algorithms, such as 'broyden2' and 'anderson', but they are much inferior to Matlab, and only give okay results after playing around with the initial conditions. The function 'fsolve' also cannot handle complex variables either.

我想知道我是否应用不正确,是否有人对如何正确地用Python解决复杂的非线性方程式有想法.

I was wondering if there is something I am applying incorrectly, and if anybody has an idea on maybe how to properly solve complex non-linear equations in Python.

非常感谢您

推荐答案

遇到此类问题时,我尝试将我的函数重写为实部和虚部的数组.例如,如果f是您的函数,它需要复杂的输入数组x(为简单起见,假设x的大小为2)

When I encounter this type of problem I try to rewrite my function as an array of real and imaginary parts. For example, if f is your function which takes complex input array x (say x has size 2, for simplicity)

from numpy import *
def f(x):
    # Takes a complex-valued vector of size 2 and outputs a complex-valued vector of size 2
    return [x[0]-3*x[1]+1j+2, x[0]+x[1]]  # <-- for example

def real_f(x1):
    # converts a real-valued vector of size 4 to a complex-valued vector of size 2
    # outputs a real-valued vector of size 4
    x = [x1[0]+1j*x1[1],x1[2]+1j*x1[3]]
    actual_f = f(x)
    return [real(actual_f[0]),imag(actual_f[0]),real(actual_f[1]),imag(actual_f[1])]

新功能real_f可以在fsolve中使用:同时求解该函数的实部和虚部,将输入自变量的实部和虚部视为独立的.

The new function, real_f can be used in fsolve: the real and imaginary parts of the function are simultaneously solved for, treating the real and imaginary parts of the input argument as independent.

这篇关于从python中的非线性方程组中找到复​​杂的根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆