Integrating to Infinity Numerically

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

Discussion Overview

The discussion revolves around the numerical integration of functions over an infinite domain, specifically the integral $$\int_0^\infty \frac{e^{-x}}{\sqrt{x}}\,dx$$. Participants explore various numerical methods and techniques to handle improper integrals, addressing challenges related to infinite limits and singularities.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant notes that using Python for the integral directly may lead to NaN or INF due to floating point limitations and suggests using Wolfram Alpha as an alternative.
  • Another participant proposes integrating to a large finite number instead of infinity, arguing that the integral converges quickly and emphasizes checking error estimates for numerical methods.
  • A suggestion is made to use Gauss–Laguerre quadrature as a method for handling the integral.
  • Concerns are raised about the code provided, particularly regarding the treatment of variables that involve infinity, which could lead to undefined behavior.
  • A participant references "Numerical Recipes" to explain how to separate the integral into two parts to address the singularity at zero and the infinite limit, suggesting specific substitutions for each part to facilitate numerical integration.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to numerically integrate the function over an infinite domain. There is no consensus on a single method, and multiple strategies are proposed and discussed.

Contextual Notes

Participants highlight limitations related to handling infinite limits and singularities in numerical integration, as well as the need for careful consideration of the numerical methods employed.

Who May Find This Useful

This discussion may be useful for individuals interested in numerical methods for integration, particularly in the context of improper integrals and handling infinite domains in computational settings.

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
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
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