Finding Complex Roots From Set Of Non-linear Equations In Python
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 equ
Solution 1:
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 *
deff(x):
# Takes a complex-valued vector of size 2 and outputs a complex-valued vector of size 2return [x[0]-3*x[1]+1j+2, x[0]+x[1]] # <-- for exampledefreal_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])]
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.
Solution 2:
Here append() and extend() methods can be used to make it automatic and easily extendable to N number of variables
def real_eqns(y1):
y=[]
for i in range(N):
y.append(y1[2*i+0]+1j*y1[2*i+1])
real_eqns1 = eqns(y)
real_eqns=[]
for i in range(N):
real_eqns.extend([real_eqns1[i].real,real_eqns1[i].imag])
return real_eqns
Post a Comment for "Finding Complex Roots From Set Of Non-linear Equations In Python"