Legendre Library

Legendre Polynomials Library

This library includes:

  • Gauss-Legendre Quadrature
  • Numerically stable, accurate and efficient calculation of an order n Legendre Polynomial at values -1<=x<=1
  • Numerically stable calculation of the derivative of an order n Legendre Polynomial at x.
  • Calculation of the zeros of Legendre polynomials.
  • Calculation of the Legendre polynomial coefficients.


You can download the library here:

Download: Legendre library.ana

The library was created using Analytica 6.0, but doesn't use any recently introduced features, so is expected to work fine in any post 5.0 build (perhaps even earlier).

Gauss-Legendre Quadrature

Also called Gaussian Quadrature, is commonly used to compute definite integrals of a function F(x) with high accuracy using a small number of points where F(x) is evaluated. The method using n points is exact when F(x) is a polynomial of degree 2*n-1 or less, and is very accurate when F(x) can be accurately approximated by a polynomial of degree n (i.e., by its Taylor series with n terms).

Note that an example of Quadrature is included with the library.

Function Gauss_Quadrature_Pts(K, lb, ub)

The function used to compute a Gauss-Legendre quadrature. To use, supply and index, K:=1..n. The length of K is used for n, the number of points used. The function has two return values, both indexed by K:

  • The points where you should evaluate your function.
  • The weights

The bounds for the integration, «lb» and «ub» default to -1 to 1 if not specified. Both bounds much be finite. The following pattern illustrates how this function is used to integrate F(x) from a to b using n points:

Index K := 1..n;
Local (xi,wi) := Gauss_Quadrature_Pts(K, a, b) Do Sum( wi * F(xi), K )

An example is included with the library. In the example, Gaussian quadrature achieves 6 digits of precision with 12 points (i.e., evaluates of F(x)) and 12 digits with 17 points, whereas the trapezoidal rule has almost 4 digits of precision with 100 terms and the trend suggests it would take thousands of points to get to 6 digits of precision. Here is the function to be integrated in the example and the accuracy achieved as a function of the number of points used:

Graph of example F Gauss Quadrature.png Accuracy gauss quadrature example.png

Evaluation of Legendre Polynomials

With the exception of very low degree, n, the direct evaluation of a Legendre polynomial $ P_n(x) $ using the polynomial coefficients is numerically unstable and results is low accuracy. Hence, when you need to evaluate a Legendre Polynomial, you should use the numerically stable, efficient and accurate evaluation function, LegendreP. For the first derivative, use LegendreP_deriv.

Function LegendreP(n,x)

Computes $ P_n(x) $, which is the nth order Legendre polynomial evaluated at x, where $ -1\le x \le 1 $ and n>=0.

Function LegendreP_deriv(n,x)

Computes $ {{d P_n(x)}\over{d x}} $, which is the derivative of the nth order Legendre polynomial evaluated at x, where $ -1\le x \le 1 $ and n>=0.

The Roots (zeros) of the Legendre Polynomial

The roots or zeros of a Legendre polynomial are the points where $ P_n(x)=0 $. All polynomials for n>1 have n roots between -1 and 1.

Function Legendre_kth_root(n,k)

Computes the kth root of $ P_n $, where $ 1\le k\le n $.

Legendre polynomial coefficients

A Legendre polynomial of degree n is a polynomial of the form:

$ P_n(x) = \sum_{k=0}^n c_k x^k $

The coefficients $ c_k $ are the coefficients.

Be aware that evaluation of polynomials using the coefficients for large n often has a lot of numeric round-off. It is better to use the LegendreP function to evaluate the polynomial at a point, especially for large n.

Function LegendreP_Coefs(K, n)

To obtain the numeric coefficients for the nth Legendre polynomial (n>=0), supply an index, K:=0..n. The n+1 coefficients are return indexed by K. The first item in the returned array is the constant. The 2nd item is the coefficient for $ x $. The 3rd is the coefficient for $ x^2 $, etc. The values of the K index do not effect the returned result.

See also


You are not allowed to post comments.