# 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);
% Finding the weights
% Formula for weights is given at
% 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;
end
fprintf(‘Quad point rule for n=%g \n’,n)
disp(‘  ‘)
disp(‘Abscissas’)
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.
Posted on Categories Integration

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

1. Mike "Pomax" Kamermans says:

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. Mike "Pomax" Kamermans says:

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)

1. Autar Kaw says:

Thank you. This would be very useful to many people out there.

3. Blancmange says:

Mr Kamermans’ tables are indeed useful. Now I can make hats!

4. Blancmange says:

Mr Kamermans’ tables are indeed useful. Now I can make hats!

5. lakshman sondhi says:

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

6. lakshman sondhi says:

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

7. JUSTINoneNonly says:

thank you so much for this! 🙂

8. JUSTINoneNonly says:

thank you so much for this! 🙂

9. Hasnain Bangash says:

plz can someone send me matlab codes for guassion quadrature

10. Hasnain Bangash says:

plz can someone send me matlab codes for guassion quadrature