Difference between revisions of "Legendre Library"
(Gauss_Quadrature_Pts) |
(Download) |
||
Line 7: | Line 7: | ||
* Calculation of the zeros of Legendre polynomials. | * Calculation of the zeros of Legendre polynomials. | ||
* Calculation of the Legendre polynomial coefficients. | * Calculation of the Legendre polynomial coefficients. | ||
+ | |||
+ | == Download == | ||
+ | You can download the library here: | ||
+ | |||
+ | '''Download:''' [[media:Legendre library.ana|Legendre library.ana]] | ||
+ | |||
+ | The library was created using [[Analytica 5.5]], 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 == | == 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). | 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'') === | === Function Gauss_Quadrature_Pts(K'', lb, ub'') === | ||
Line 19: | Line 28: | ||
:[[Index]] K := 1..n; | :[[Index]] K := 1..n; | ||
:[[Local]] (xi,wi) := [[Gauss_Quadrature]](K, a, b) Do [[Sum]]( wi * F(xi), K )</code> | :[[Local]] (xi,wi) := [[Gauss_Quadrature]](K, a, b) Do [[Sum]]( wi * F(xi), K )</code> | ||
+ | |||
+ | == Evaluation of Legendre Polynomials == | ||
+ | With the exception of very low degree, ''n'', the direct evaluation of a Legendre polynomial <math>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>P_n(x), which is the nth order Legendre polynomial evaluated at x, where <math>-1\le x \le 1</math> and n>=0. | ||
+ | |||
+ | === Function LegendreP_deriv(n,x) === | ||
+ | |||
+ | Computes <math>{{d P_n(x)}\over{d x}}</math>, which is the derivative of the nth order Legendre polynomial evaluated at x, where <math>-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>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>P_n</math>, where <math>1\le k\le n</math>. | ||
+ | |||
+ | == Legendre polynomial coefficients == | ||
+ | A Legendre polynomial of degree ''n'' is a polynomial of the form: | ||
+ | :<math>P_n(x) = \sum_{k=0}^n c_k x^k</math> | ||
+ | The coefficients <math>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, <code>K:=0..n</code>. 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>x</math>. The 3rd is the coefficient for <math>x^2</math>, etc. The values of the <code>K</code> index do not effect the returned result. | ||
+ | |||
+ | == See also == | ||
+ | * [https://en.wikipedia.org/wiki/Legendre_polynomials Legendre polynomials] on Wikipedia | ||
+ | * [[Area]]() and [[Integrate]]() functions (built-in functions that use the Trapezoid rule) |
Revision as of 06:04, 18 October 2020
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 5.5, 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. 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(K, a, b) Do Sum( wi * F(xi), K )
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), which is the nth order Legendre polynomial evaluated at x, where \lt math\gt -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)
Enable comment auto-refresher