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://nm.mathforcollege.com, 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://nm.mathforcollege.com/videos.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

0 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)

  2. 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)

Leave a Reply