|
Numerical integration/Gauss-Legendre Quadrature
In a general Gaussian quadrature rule, a definite integral | $\int_{-1}^{1}f(x)dx=\int_{-1}^{1}W(x)g(x)dx$ |
Those are then approximated by a sum of function values | $\int_{-1}^{1}W(x)g(x)dx\approx \sum_{i=1}^{n}w_{i}g(x_{i})$ |
In the case of Gauss-Legendre quadrature, the weighting | $\int_{-1}^{1}f(x)dx\approx \sum_{i=1}^{n}w_{i}f(x_{i})$ |
For this, we first need to calculate the nodes and the weights, but after we have them, we can reuse
them for numerious integral evaluations, which greatly speeds up thecalculation.
The n evaluation points xi for a n-point rule, also | P0(x) = 1 P1(x) = x |
There is also a recursive equation for their | ${P_{n}}'(x)=\frac{n}{x^{2}-1}\left [ xP_{n}(x)-P_{n-1}(x) \right ]$ |
The roots of those polynomials are in general not | $x_{n+1}=x_{n}-\frac{f(x_{n})}{{f}'(x_{n})}$ |
The first guess x0 for the i-th root of a n-order | $x_{0}=cos(\pi \frac{i-0.25}{n+0.5})$ |
After we get the nodes xi, we compute the | $w_{i}=\frac{2}{(1-x_{i}^{2})[{P}'_{n}(x_{i})]^{2}}$ |
After we have the nodes and the weights for a | $\int_{a}^{b}f(x)dx\approx \frac{b-a}{2}\sum_{i=1}^{n}w_{i}f(\frac{b-a}{2}x_{i}+\frac{b+a}{2})$ |
The matlab program to calculate the nodes and weights of a n-order Gauss-Legendre quadrature is:
function [w,x]=lgwt(N) x=cos(pi*((1:N)'-0.25)/(N+0.5)); % Initial guess x0=2; w=2./((1-x.^2).*Lp.^2); % Compute the weights |
Another function GaussInt is to call lgwt function and compute the inegral over interval [a,b]:
function int=GaussInt(f,a,b,n) % f - function to be integrated; a - lower limit; b - upper limit; n - order of Legendre [w0 x0]=lgwt(n); w=(b-a)/2*w0; x=(b-a)/2*x0+(b+a)/2; int=w'*f(x); |
Finally, in the main program, we can define the function to be integrated, then call GaussInt function and do the integration. For example, we can compute:
$\int_{-3}^{3}exp(x)dx\approx \sum_{i=1}^{40}w_{i}exp(x_{i})\approx 20.0357$
The main program is:
f=@(x) exp(x); y=GaussInt(f,-3,3,40); |
Reference: http://rosettacode.org/wiki/Numerical_integration/Gauss-Legendre_Quadrature
http://pomax.github.io/bezierinfo/legendre-gauss.html#n37
http://keisan.casio.com/exec/system/1329114617
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-28 16:55
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社