Legendre-Gauss Quadrature formula

The Legendre-Gauss Quadrature formula or Gauss-Legendre quadrature is the numeric approximation of a definite integral,

\int_{-1}^1 f(x)\,dx \approx \sum_{i=1}^N w_i f(x_i). \qquad\qquad\qquad\qquad(1) $$ It is possible to choose quadrature points xi and weights wi, so that polynomial functions of degree smaller than 2N are integrated exactly by equation (1).

The Legendre-Gauss quadrature formula is a special case of Gaussian quadratures which allow efficient approximation of a function with known asymptotic behavior at the edges of the interval of integration. Gauss quadrature is especially recommended if the integrand is holomorphic in a neighbourhood of integration interval.

Points and weights
The points xi in equation (1) are zeros of the Legendre polynomial  PN(x) of order N:
 * $$ P_N(x_i)=0.\qquad\qquad\qquad\qquad(2)$$

It can be shown that the zeros are real and lie in the interval (&minus;1, 1):
 * $$ -1<x_1<x_2< ... <x_N <1 \qquad\qquad\qquad\qquad(3)$$

The weights wi in equation (1) can be expressed by


 * $$ w_i = \frac{2}{\left( 1-x_i^2 \right) (P'_N(x_i))^2}, \qquad\qquad\qquad\qquad(4)$$

where the prime indicates a first derivative. There is no straightforward expression for the zeros xi; they can be approximated to many decimal places through only few iterations, solving numerically equation (2) with the initial values
 * $$ x_i\approx \cos\left(\pi \frac{\frac{1}{2} +i}{N}\right)\qquad\qquad\qquad\qquad(5) $$

These formulas are described in the books

Precision of the approximation
If the integrand is a holomorphic function along the path of integration, then the error of approximation decreases exponentially in the number N. If the integrand is a polynomial of degree 2N&minus;1 or less, then the N-point Legendre-Gauss quadrature yields the exact answer.

If the integrand contains a singularity, then the formula still can be used, but the approximation is poor. In that case, one may consider to use another Gaussian quadrature, more suitable for the specific function. Alternatively, it may be possible to deform the contour of integration to avoid the singularity if it is inside the integration interval. Another possibility is to do a substitution that makes the integrand regular.

Examples
In Fig.1, the base-10 logarithm of the modulus of the residual of the approximation of integral with Gaussian quadrature is shown versus number of terms in the sum, for four examples of the integrand.
 * $$ f(x)=x^{16}\, $$ (black)
 * $$ f(x)=\sqrt{1+x^2}\, $$ (red)
 * $$ f(x)=1/(1+x^2)\, $$ (green)
 * $$ f(x)=1/(3+x)\, $$ (blue)

The first of these functions is integrated exactly at N > 8, and the residual is determined by the rounding errors of the long double arithmetic. The second function (red) has branch points at the end of the interval; therefore, the approximation does not improve quickly at the increase of number terms in the sum. The last two functions are analytic within the range of integration; the residual decreases exponentially, and the precision of evaluation of the integral is limited only by the rounding errors. The logarithm of the modulus of the residual is plotted versus number of points in the quadrature formula. If the residual is positive, it is represented as a circle; if it is negative, it is represented as a dot. Out of the grid, at the bottom of the figure, the precision is not sufficient for reliable plotting.


 * See Legendre-Gauss Quadrature formula/code for the code that generated figure 1.