A MATLAB program to find quadrature points and weights for Gauss-Legendre Quadrature rule


Recently, I got a request how one can find the quadrature and weights of a Gauss-Legendre quadrature rule for large n.  It seems that the internet has these points available free of charge only up to n=12.  Below is the MATLAB program that finds these values for any n.  I tried the program for n=25 and it gave results in a minute or so.  The results output up to 32 significant digits.
_______________________________________________________

% Program to get the quadrature points
% and weight for Gauss-Legendre Quadrature
% Rule
clc
clear all
syms x
% Input n: Quad pt rule
n=14;
% Calculating the Pn(x)
% Legendre Polynomial
% Using recursive relationship
% P(order of polynomial, value of x)
% P(0,x)=1; P(1,x)=0;
% (i+1)*P(i+1,x)=(2*i+1)*x*P(i,x)-i*P(i-1,x)
m=n-1;
P0=1;
P1=x;
for i=1:1:m
    Pn=((2.0*i+1)*x*P1-i*P0)/(i+1.0);
    P0=P1;
    P1=Pn;
end
if n==1
    Pn=P1;
end
Pn=expand(Pn);
quadpts=solve(vpa(Pn,32));
quadpts=sort(quadpts);
% Finding the weights
% Formula for weights is given at
% http://mathworld.wolfram.com/Legendre-GaussQuadrature.html
% Equation (13)
for k=1:1:n
    P0=1;
    P1=x;
    m=n;
    % Calculating P(n+1,x)
    for i=1:1:m
        Pn=((2.0*i+1)*x*P1-i*P0)/(i+1.0);
        P0=P1;
        P1=Pn;
    end
    Pn=P1;
    weights(k)=vpa(2*(1-quadpts(k)^2)/(n+1)^2/ …
                                   subs(Pn,x,quadpts(k))^2,32);
end
    fprintf(‘Quad point rule for n=%g \n’,n)
disp(‘  ‘)
disp(‘Abscissas’)
disp(quadpts)
disp(‘  ‘)
disp(‘Weights’)
disp(weights’)_______________________________________________________ 

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu, the textbook on Numerical Methods with Applications available from the lulu storefront, the textbook on Introduction to Programming Concepts Using MATLAB, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.
Advertisements

Published by

Autar Kaw

Autar Kaw (http://autarkaw.com) is a Professor of Mechanical Engineering at the University of South Florida. He has been at USF since 1987, the same year in which he received his Ph. D. in Engineering Mechanics from Clemson University. He is a recipient of the 2012 U.S. Professor of the Year Award. With major funding from NSF, he is the principal and managing contributor in developing the multiple award-winning online open courseware for an undergraduate course in Numerical Methods. The OpenCourseWare (nm.MathForCollege.com) annually receives 1,000,000+ page views, 1,000,000+ views of the YouTube audiovisual lectures, and 150,000+ page views at the NumericalMethodsGuy blog. His current research interests include engineering education research methods, adaptive learning, open courseware, massive open online courses, flipped classrooms, and learning strategies. He has written four textbooks and 80 refereed technical papers, and his opinion editorials have appeared in the St. Petersburg Times and Tampa Tribune.

7 thoughts on “A MATLAB program to find quadrature points and weights for Gauss-Legendre Quadrature rule”

  1. I wrote something similar, using mathematica instead, after watching your videos on applying the Gauss quadrature rules. I decided to offer the tabulated result on the page, too, in case it turns out to be useful for people who just want the quick values, with a download for the high precision numbers. Perhaps of interest, it’s up on http://processingjs.nihongoresources.com/bezierinfo/legendre-gauss-values.php (I used it as a resource for computing the arc length integral using legendre-gauss approximation, for http://processingjs.nihongoresources.com/bezierinf)

    Like

  2. Sir kindly send me source code in MAT LAB for gaussian quadrature formula where [-1,1] is mapped into [0,1]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s