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.
Download
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:
Evaluation of Legendre Polynomials
With the exception of very low degree, n, the direct evaluation of a Legendre polynomial [math]\displaystyle{ P_n(x) }[/math] 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 [math]\displaystyle{ P_n(x) }[/math], which is the nth order Legendre polynomial evaluated at x, where [math]\displaystyle{ -1\le x \le 1 }[/math] and n>=0.
Function LegendreP_deriv(n,x)
Computes [math]\displaystyle{ {{d P_n(x)}\over{d x}} }[/math], which is the derivative of the nth order Legendre polynomial evaluated at x, where [math]\displaystyle{ -1\le x \le 1 }[/math] and n>=0.
The Roots (zeros) of the Legendre Polynomial
The roots or zeros of a Legendre polynomial are the points where [math]\displaystyle{ P_n(x)=0 }[/math]. All polynomials for n>1 have n roots between -1 and 1.
Function Legendre_kth_root(n,k)
Computes the kth root of [math]\displaystyle{ P_n }[/math], where [math]\displaystyle{ 1\le k\le n }[/math].
Legendre polynomial coefficients
A Legendre polynomial of degree n is a polynomial of the form:
- [math]\displaystyle{ P_n(x) = \sum_{k=0}^n c_k x^k }[/math]
The coefficients [math]\displaystyle{ c_k }[/math] 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 [math]\displaystyle{ x }[/math]. The 3rd is the coefficient for [math]\displaystyle{ x^2 }[/math], etc. The values of the K
index do not effect the returned result.
See also
- Legendre polynomials on Wikipedia
- Area() and Integrate() functions (built-in functions that use the Trapezoid rule)
- Category:Integration (Quadrature) functions
Enable comment auto-refresher