I have a function I would like to find the roots to:
import numpy as np
def k_xy_eq(kxy):
# exponential term
exp = np.exp(2j*np.sqrt(eps_2*k0**2 - kxy**2) * d)
# numerator of second term
num = (np.sqrt(eps_2*k0**2 - kxy**2) + eps_2 * np.sqrt(k0**2 - kxy**2)) * (eps_2 * np.sqrt(eps_1 * k0**2 - kxy**2) + eps_1 * np.sqrt(eps_2 * k0**2 - kxy**2))
#denominator of second term
denom = (np.sqrt(eps_2*k0**2 - kxy**2) - eps_2 * np.sqrt(k0**2 - kxy**2)) * (eps_2 * np.sqrt(eps_1 * k0**2 - kxy**2) - eps_1 * np.sqrt(eps_2 * k0**2 - kxy**2))
return exp*denom + num
k0 = np.array([7459927, 8165527, 9574340, 9695577])[0]
eps_1 = 1.45**2
eps_2 = np.array([-26.69+1.31j, -20.88+1.16j, -12.76+1.13j, -12.22+1.14j])[0]
d = 1e-9
initial_guess = 7603702-6832j
I've had a look at several similar questions with no luck unfortunately. Everything I have tried either doesn't converge or straight up results in an error. I started with the first values of k0 and eps_2 with the aim of obtaining the solutions for all 4 cases.
I initially tried a root_scalar approach, which didn't converge:
import scipy.optimize
kxy_secant = scipy.optimize.root_scalar(k_xy_eq, x0 = initial_guess/2, x1 = initial_guess, method = 'secant', maxiter=int(1e4))
I tried several findroot methods as well all if which raised the error "TypeError: loop of ufunc does not support argument 0 of type mpc which has no callable sqrt method"
from mpmath import findroot
secant_root = findroot(k_xy_eq, initial_guess)
Finally I attempted using cxroots
from cxroots import Circle, Rectangle
C = Circle(0, np.real(2*initial_guess))
roots = C.roots(k_xy_eq)
result = Rectangle([-5*np.real(initial_guess),5*np.real(initial_guess)], [-5*np.imag(initial_guess),5*np.imag(initial_guess)]).roots(k_xy_eq)
Both cases raised an integration error.
Any help would be appreaciated, thanks.