Integrating to Infinity Numerically

In summary, the conversation discusses the topic of integrating functions over an infinite domain, using the specific example of integrating e^(-x)/sqrt(x). The individual has already attempted using Gauss-Legendre quadrature and Romberg integration, but has encountered issues with NaN and infinite limits. Suggestions are made to use other methods such as Gauss-Laguerre quadrature or to separate the problem into two integrals and use specific substitutions for each. The conversation also references the book "Numerical Recipes" for further guidance on handling improper integrals numerically.
  • #1
member 428835
Hi PF!

I am trying to integrate functions over an infinite domain. One example is $$\int_0^\infty \frac{e^{-x}}{\sqrt{x}}\,dx$$ I know the substitution ##u = \sqrt{x}## reduces this problem to integrating ##\exp(-x^2)##, but if I want to integrate the function as is, how would I do this?

I've already tried Gauss-Legendre quadrature and Romberg integration. GL reports NaN and Romberg is evidently unable to handle the infinite limits.

Python:
import numpy as np
import scipy
import scipy.linalg# SciPy Linear Algebra Library
from matplotlib import pyplot as plt# plotting
from scipy import integrate

f = lambda x: np.exp(-x)/np.sqrt(x)# function to integrate
a = 0# lower bound
b = np.inf# upper bound

toler = 10e-3# tolerance

exact = 1.772453850# exact value of integral

# Romberg Integration
I = integrate.romberg(f, a, b, rtol=toler, show=True, divmax=25)

# Gauss-Legendre Quadrature Integration
deg = 1# degree of Legendre poly
gauss = 0# initial guess
while abs(exact-gauss) > toler:
    x, w = np.polynomial.legendre.leggauss(deg)
    # Translate x values from the interval [-1, 1] to [a, b]
    t = 0.5*(x + 1)*(b - a) + a
    gauss = sum(w * f(t)) * 0.5*(b - a)
    deg = deg + 1
   
print gauss
print deg
 
Technology news on Phys.org
  • #2
NaN or INF are what you get when you go beyond the range of floating point. As you wrote it, you will not get an answer using python. You can always use wolfram alpha, it will give you an answer. There are other approximations and workarounds.

This will help you to bypass some FP issues in the future:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
 
  • #3
joshmccraney said:
I've already tried Gauss-Legendre quadrature and Romberg integration. GL reports NaN and Romberg is evidently unable to handle the infinite limits.
Right, because actually integrating from 0 to infinity would take an infinite amount of time. You should instead integrate to a very large number. The integral should converge very fast since this is a Gaussian integral; you won't have to go that far out to get a very good approximation. If you are conserned with just how accurate the numerical solution is, you should look up the error estimates for the numerical integration methods you are using. I think for Gaussian Quadrature, it goes something like
$$\Delta f(\eta_{n})=\frac{2^{2n+1}(n!)^{4}}{(2n+1)[(2n)!]^{3}}f^{(2n)}(\eta_{n})$$
 
  • Like
Likes Valdis
  • #5
Looking more closely at your code. It is not clear to me what is happening with the variables named t and gauss. They both have a term ##(b-a)## in them but since ##b=\infty## these values will also be infinity.
 
  • #6
joshmccraney said:
I am trying to integrate functions over an infinite domain. One example is $$\int_0^\infty \frac{e^{-x}}{\sqrt{x}}\,dx$$ I know the substitution ##u = \sqrt{x}## reduces this problem to integrating ##\exp(-x^2)##, but if I want to integrate the function as is, how would I do this?

The book "Numerical Recipes" explains how to handle this type of improper integral. There are two "problems" for numerical integration: 1) the integrand blows up at ##x=0##; the region of integration is infinite. Separate the problems, i.e., write
$$I = \int_0^\infty \frac{e^{-x}}{\sqrt{x}}dx = \int_0^1 \frac{e^{-x}}{\sqrt{x}}dx + \int_1^\infty \frac{e^{-x}}{\sqrt{x}}dx = I_1 + I_2.$$

Since ##I_1## blows up like ##x^{-1/2}## as ##x## goes to zero, "Numerical Recipes" says to make the substitution ##x=u^2## in ##I_1##. Because the region of integration is infinite in ##I_2##, "Numerical Recipes" says to make the substitution ##u = e^{-x}## in ##I_2##. These substitutions easily give
$$I = 2 \int_0^1 e^{-u^2}du + \int_0^{1/e} \frac{du}{\sqrt{- \ln u}}.$$

It is easy to integrate numerically each of the integrals. As you say, the first substitution turns the whole question into the integral of a Gaussian, but the idea here is to illustrate techniques that can be used on improper integrals.
 
  • Like
Likes member 428835
  • #7
Thanks a lot everyone! I always find your advise very helpful!
 

1. What is numerical integration to infinity?

Numerical integration to infinity is a method used to approximate the value of a definite integral where the upper limit of integration is infinity. It involves breaking the integral into smaller intervals and summing up the values at each interval to get an approximate value of the integral.

2. Why is numerical integration to infinity important?

Numerical integration to infinity is important because it allows us to compute the value of integrals that cannot be solved analytically. It is also useful in many fields of science and engineering where integration is needed to solve complex problems.

3. What are some common methods used for numerical integration to infinity?

Some common methods used for numerical integration to infinity are the trapezoidal rule, Simpson's rule, and the Gaussian quadrature method. These methods differ in their accuracy and complexity, but all aim to approximate the value of the integral.

4. How do I know which method to use for numerical integration to infinity?

The choice of method for numerical integration to infinity depends on the function being integrated and the desired level of accuracy. Some methods may be more suitable for certain types of functions or may require fewer intervals to achieve a certain level of accuracy. It is important to consider the properties of the function and the limitations of each method before choosing one.

5. What are some limitations of numerical integration to infinity?

Numerical integration to infinity has some limitations, such as the dependence on the number of intervals and the choice of method, which can affect the accuracy of the approximation. It may also be computationally expensive for highly oscillatory or singular functions, and may not be suitable for functions with infinite discontinuities.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
794
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
1
Views
750
Back
Top