Integrating to Infinity Numerically

  • Context: Python 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Infinity Integrating
Click For Summary
SUMMARY

This discussion focuses on techniques for numerically integrating functions over infinite domains, specifically the integral $$\int_0^\infty \frac{e^{-x}}{\sqrt{x}}\,dx$$. Users report unsuccessful attempts using Gauss-Legendre quadrature and Romberg integration due to issues with NaN outputs and infinite limits. The conversation highlights the importance of using substitutions and separating the integral into manageable parts, as suggested in "Numerical Recipes," to effectively handle improper integrals.

PREREQUISITES
  • Understanding of numerical integration techniques, specifically Gauss-Legendre quadrature and Romberg integration.
  • Familiarity with Python programming, particularly the SciPy library for numerical computations.
  • Knowledge of improper integrals and convergence behavior of integrals.
  • Basic understanding of Gaussian functions and their properties.
NEXT STEPS
  • Learn about Gauss–Laguerre quadrature for integrating functions over infinite domains.
  • Study error estimates for numerical integration methods, particularly for Gaussian quadrature.
  • Explore the techniques outlined in "Numerical Recipes" for handling improper integrals.
  • Implement substitutions for improper integrals in Python using SciPy's integration functions.
USEFUL FOR

Mathematicians, data scientists, and software developers involved in numerical analysis, particularly those working with integrals over infinite domains.

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
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
 
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   Reactions: Valdis
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.
 
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   Reactions: member 428835
Thanks a lot everyone! I always find your advise very helpful!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K