JiangPQ的个人博客分享 http://blog.sciencenet.cn/u/JiangPQ

博文

Numerical integration/Gauss-Legendre Quadradure by MATLAB

已有 5895 次阅读 2013-10-19 13:11 |系统分类:科研笔记

Numerical integration/Gauss-Legendre Quadrature

In a general Gaussian quadrature rule, a definite integral
of
f(x) is first approximated over the interval [-1, 1] by a
polynomial approximatedfunction
g(x) and a known
weighting  function
W(x).

$\int_{-1}^{1}f(x)dx=\int_{-1}^{1}W(x)g(x)dx$

Those are then approximated by a sum of function values
at  specified points
$x_{i}$ multiplied by some weights wi:

$\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  
function
W(x) = 1, so we can approximate an integral of
 
f(x)  with:

$\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
called "nodes", are roots of n-th order Legendre  
Polynomials
Pn(x). Legendre polynomials are
defined  by the following recursive rule:

P0(x)  = 1

P1(x)  = x
nPn(x) = (2n − 1)xPn −  1(x) − (n − 1)Pn − 2(x)

There is also a recursive equation for their
derivative:

${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
analytically solvable, so they have to be
approximated numerically, for  example by
Newton-Raphson iteration:

$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
polynomial
Pn can be given by

$x_{0}=cos(\pi \frac{i-0.25}{n+0.5})$

After we get the nodes xi, we compute  the
appropriate weights by:

  $w_{i}=\frac{2}{(1-x_{i}^{2})[{P}'_{n}(x_{i})]^{2}}$

After we have the nodes and the weights for a
n-point  quadrature rule, we can approximate an
integral over any interval [
a,b]  by

$\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
   L=zeros(N,N+1);                     % Legendre-Gauss Matrix

   x0=2;
   while max(abs(x-x0))>eps
        L(:,1)=1;
        L(:,2)=x;
   
         
for k=2:N
               L(:,k+1)=((2*k-1)*x.*L(:,k)-(k-1)*L(:,k-1))/k;
% Legendre polynormial Pn defined by recursive rule
         end
   
        Lp=N*(x.*L(:,N+1)-L(:,N))./(x.^2-1);       % Derivative Pn'
   
        x0=x;
        x=x0-L(:,N+1)./Lp;          
% Newton-Raphson method for the roots
   
   end

           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

 

 

 



https://wap.sciencenet.cn/blog-756363-734186.html

上一篇:信号的频域分析——傅里叶变换
下一篇:XPS方法测量自生氧化层厚度
收藏 IP: 137.132.250.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-25 23:03

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部