Contents Previous Next Subchapters

Computing Gauss-Legendre Quadrature Weights
Syntax gaussleg(naoutwout)
See Also gaussq , ode4rk

Description
Determines the Gauss-Legendre weights, wout and abscissas aout such that
      +1
     /
     | f(xdx  =  wout  f(aout ) + ... + wout  f(aout )
     /                 1       1              n       n
     -1
where f(x) is any polynomial of degree n - 1 or less.

The integer scalar n specifies the number of points in the quadrature. The input value of aout does not matter. Its output value is a double-precision column vector of length n containing the abscissas for the quadrature. The input value of wout does not matter. Its output value is a double-precision column vector of length n containing the weights for the quadrature.

Example
The following example integrates the cosine function between zero and pi/2 using one quadrature interval. The result of the integral is printed in the command window.

clear
n     = 5                       # number of quadrature points
aout  = novalue                 # quadrature points
wout  = novalue                 # quadrature weights
gaussleg(n, aout, wout)
ratio = (pi / 2d0) / 2d0        # ratio of [0, pi/2] / [-1, +1] 
x     = ratio * (aout + 1d0)    # abscissas in [0, pi/2]
print "approximation for the integral =", ratio * wout' * cos(x)