I want to evaluate an integral numerically that contains one singularity. The software I use for this is Python. The actual integral I want to evaluate is quite long with a lot of other constants so I decided to present a different integral here which I think has the same problem. $$ I = \int_{-10}^{10} \frac{1}{x-2} dx $$
I tried to evaluate the integral in python using this code:
w = 2
def integrand(x,w):
return 1/(x - w)
ResultIntegral = quad(integrand,-10,10, args=(w))
Python indeed gives a warning when you run this code. Does someone know how to calculate such an integral numerically? What theorem or algorithm should I look into?
My solution attempt:
I have the idea that you should add an infinitesimal small imaginary number to the denominator of the integrand. (I saw this in a physics book, but the book does not go deeper into the matter than this). The integral would then become something like this:
$$ I = \lim_{\epsilon \rightarrow 0} \int_{-10}^{10} \frac{1}{x-2+i\epsilon } dx $$
Somehow when you do something like this you can evaluate the integral. I tried modifying my python code:
w = 2
epsilon = 0.01*1j
def integrand(x,w,epsilon):
return 1/(x - w + epsilon)
Sigma = quad(integrand,-10,10, args=(w,epsilon))
I get an error in Python, this does not seem to work.
Attempting to evaluate the integral $$\int_{-10}^{10} \frac{1}{x-2}\ \mathrm dx=\int_{-12}^{8} \frac{1}{t}\ \mathrm dt$$ $$=\lim\limits_{\alpha\to 0}\int_{-12}^{\alpha} \frac{1}{t}\ \mathrm dt+\lim\limits_{\beta\to 0}\int_{\beta}^{8} \frac{1}{t}\ \mathrm dt$$ Both integrals diverge, which implies that $$\int_{-10}^{10} \frac{1}{x-2}\ \mathrm dx=\mbox{nonexistent}$$ Evaluating the Cauchy principal value of the integral
$$\mathrm{PV}\int_{-10}^{10} \frac{1}{x-2}\ \mathrm dx=\mathrm{PV}\int_{-12}^{8} \frac{1}{t}\ \mathrm dt$$ $$=\lim\limits_{\gamma\to 0^+}\left[\int_{-12}^{-\gamma} \frac{1}{t}\ \mathrm dt+\int_{\gamma}^{8} \frac{1}{t}\ \mathrm dt\right]$$ $$=\lim\limits_{\gamma\to 0^+}\left[\int_{\gamma}^{8} \frac{1}{t}\ \mathrm dt-\int_{\gamma}^{12} \frac{1}{t}\ \mathrm dt\right]$$ $$=\lim\limits_{\gamma\to 0^+}\left[\int_{\gamma}^{8} \frac{1}{t}\ \mathrm dt-\int_{\gamma}^{8} \frac{1}{t}\ \mathrm dt-\int_{8}^{12} \frac{1}{t}\ \mathrm dt\right]$$ $$=-\int_{8}^{12} \frac{1}{t}\ \mathrm dt=-\ln\left(\frac{3}{2}\right)=\ln\left(\frac{2}{3}\right)$$ Which method should you use in your code?
In this case the improper integral simply does not exist. Therefore your only option is to assign the nonexistent integral a value via the method of Cauchy principal value.