Adrian Keister had a wonderful answer to my question. In short, his answer allowed me to make a form of contour integration that picks up (some of the time) only real roots of a function $f(z)$. I started to code this in Python 3.x, but while I was debugging it, sometimes it would give me a negative answer or that there is no roots in an interval I know a root lies in. I can't see anything wrong with my code, so I'm hoping that someone else will point out my folly.
import cmath as cm
import math as m
start=150
end=181
subdiv=100
front=1/(2*cm.pi*1j)
d=1000
eps=1/d
def f(x):
return x**2 - 358*x + 32040
def f_1(x):
return 2*(-179 + x)
def g_1_1(x):
return start+(x*(end-start))+(1j*eps)
def g_1_2(x):
return (end-start)
def g_2_1(x):
return end+(1j*(eps-(2*x*eps)))
def g_2_2(x):
return -2*1j*eps
def g_3_1(x):
return end+(x*(start-end))-(1j*eps)
def g_3_2(x):
return (start-end)
def g_4_1(x):
return start+(1j*((-1*eps)+(2*x*eps)))
def g_4_2(x):
return 2*1j*eps
def c(g,g_1,x):
return (f_1(g(x))/f(g(x)))*g_1(x)
def quad(g,g_1):
total=0
for i in range(0,subdiv):
a=(i)*((1)/subdiv)
b=(i+1)*((1)/subdiv)
total+=(b-a)*((c(g,g_1,a)+c(g,g_1,b))/2)
return total
def g_c():
total=0
total+=quad(g_1_1,g_1_2)
total+=quad(g_2_1,g_2_2)
total+=quad(g_3_1,g_3_2)
total+=quad(g_4_1,g_4_2)
return front*total
print(g_c())
In an interval ($150$ to $190$) where two roots lie very close to each other ( both close to$179$), Python returns 0.0031720503705679384+5.624689839008629e-29j when I thought I would get 1.999986474+0.6746345e-99j
EDIT: Here is the new code:
def g_1_1(x):
return start+((1j*eps)*(1-(2*x)))
def g_1_2(x):
return -2*1j*eps
def g_2_1(x):
return start+(x*(end-start))-(1j*eps)
def g_2_2(x):
return (end-start)
def g_3_1(x):
return end+((1j*eps)*(-1+(2*x)))
def g_3_2(x):
return 2*1j*eps
def g_4_1(x):
return end+(x*(start-end))+(1j*eps)
def g_4_2(x):
return (start-end)
def c(g,g_1,x):
return (f_1(g(x))/f(g(x)))*g_1(x)
def quad(g,g_1):
total=0
for i in range(0,subdiv):
a=(i)*((1)/subdiv)
b=(i+1)*((1)/subdiv)
total+=(b-a)*((c(g,g_1,a)+c(g,g_1,b))/2)
return total
def g_c():
total=0
total+=quad(g_1_1,g_1_2)
total+=quad(g_2_1,g_2_2)
total+=quad(g_3_1,g_3_2)
total+=quad(g_4_1,g_4_2)
return front*total
print(g_c())
Now it gives me an answer of -0.0031720503705679384-0j.