Function Repository Resource:

GaussianQuadratureWeights

Source Notebook

Get a numerically sorted list of abscissa-weight pairs for Gaussian quadrature

Contributed by: Paul Abbott (additional contributions by Wolfram Research staff)

ResourceFunction["GaussianQuadratureWeights"][n,{a,b}]

gives a list of the n pairs {xi,wi} of the elementary n-point Gaussian formula for quadrature on the interval a to b, where wi is the weight of the abscissa xi.

ResourceFunction["GaussianQuadratureWeights"][n,{a,b},prec]

uses the working precision prec.

Details and Options

Gaussian quadrature approximates the value of an integral as a linear combination of values of the integrand evaluated at optimal abscissas xi: .
The abscissas are optimal in the sense that the quadrature formula is exact for all polynomials up to degree 2n-1.
The precision argument acts similarly to the WorkingPrecision option used in many Wolfram Language numeric functions; it is not at all like the PrecisionGoal option. If the n argument is too small to allow for a high-precision result, extra precision in a result will not be meaningful.

Examples

Basic Examples (3) 

The abscissas and weights for a 10-point quadrature on a given interval:

In[1]:=
ResourceFunction["GaussianQuadratureWeights"][10, {-2., 9.}]
Out[1]=

The exact abscissas and weights:

In[2]:=
ResourceFunction["GaussianQuadratureWeights"][3, {-1, 1}]
Out[2]=

Use the specified precision:

In[3]:=
ResourceFunction["GaussianQuadratureWeights"][3, {-1, 1}, 5]
Out[3]=

Applications (3) 

Use the Gaussian quadrature to approximate the area under a curve:

In[4]:=
f = x |-> Cos[x] + 3 x^3 - x + 3
Out[4]=

Plot the curve over a given interval:

In[5]:=
Plot[f[x], {x, -0.5, 1.5}, Filling -> Axis, AxesOrigin -> {0, 0}]
Out[5]=

Approximate the area under the curve for a specific n:

In[6]:=
area[n_] := Module[{a, w}, {a, w} = Transpose@
    ResourceFunction["GaussianQuadratureWeights"][n, {-0.5, 1.5}]; w . (f /@ a)]
In[7]:=
area[5]
Out[7]=

Compare to the output of NIntegrate:

In[8]:=
NIntegrate[f[x], {x, -0.5, 1.5}]
Out[8]=
In[9]:=
Abs[% - area[n]]
Out[9]=

The difference in Gaussian quadratures of two different orders:

In[10]:=
Abs[area[6] - area[5]]
Out[10]=

The abscissas and weights for an n-point quadrature on {-1,1} for n=2 5:

In[11]:=
Dataset@Table[
  Association[
   n -> RootReduce[
     ResourceFunction["GaussianQuadratureWeights"][n, {-1, 1}]]], {n, 2, 5}]
Out[11]=

The triangle of polynomials whose roots determine the weights for n up to 8:

In[12]:=
Dataset@Table[
  Association[
   n -> TraditionalForm@
     MinimalPolynomial[
      ResourceFunction["GaussianQuadratureWeights"][n, {-1, 1}][[1, 2]], x]], {n, 8}]
Out[12]=

Properties and Relations (3) 

For the interval {-1,1}, abscissas for the n-point Gaussian quadrature are the roots of the Legendre polynomial Pn(x):

In[13]:=
n = 5;
In[14]:=
{xx, ww} = Transpose[
   ResourceFunction["GaussianQuadratureWeights"][n, {-1, 1}]];
In[15]:=
xx // Simplify
Out[15]=
In[16]:=
% === ((x /. {ToRules[Roots[LegendreP[n, x] == 0, x]]}) // Simplify //
    NumericalSort)
Out[16]=

The weights at abscissas xi are 2(1-xi2)/((n+1)2Pn+1(xi))2):

In[17]:=
ww // Simplify
Out[17]=
In[18]:=
% === (Table[(
    2 (1 - xx[[i]]^2))/((n + 1)^2 LegendreP[n + 1, xx[[i]]]^2), {i, n}] // Simplify)
Out[18]=

The sum of the weights is exactly 2:

In[19]:=
Total[ww] // Simplify
Out[19]=

Resource function GaussianQuadratureError gives an upper bound of the error in the weights used for the approximation:

In[20]:=
ResourceFunction["GaussianQuadratureError"][5, g, {-0.5, 1.5}]
Out[20]=

The abscissas and weights for the elementary n-point Gaussian quadrature are related to the eigensystem of the nn symmetric tridiagonal Gaussian quadrature matrix:

In[21]:=
n = 6;
In[22]:=
{xx, ww} = Transpose[
   ResourceFunction["GaussianQuadratureWeights"][n, {-1, 1}]];
In[23]:=
(m = ResourceFunction["GaussianQuadratureMatrix"][n]) // MatrixForm
Out[23]=

Eigenvalues of the matrix determine the abscissas:

In[24]:=
Eigenvalues[m] // NumericalSort
Out[24]=
In[25]:=
% - xx // Simplify
Out[25]=

Eigenvectors of the matrix determine the weights:

In[26]:=
{Eigenvalues[m], 2 (Normalize /@ Eigenvectors[m])[[All, 1]]^2} // Transpose // NumericalSort // Transpose;
In[27]:=
%[[2]] - ww // Simplify
Out[27]=

Version History

  • 2.0.0 – 06 November 2020
  • 1.0.0 – 20 November 2019

Related Resources

License Information