3% lgwt.m
4%
5% This script is for computing definite integrals using Legendre-Gauss
6% Quadrature. Computes the Legendre-Gauss nodes and weights on an interval
7% [a,b] with truncation order N
8%
9% Suppose you have a continuous function f(x) which is defined on [a,b]
10% which you can evaluate at any x in [a,b]. Simply evaluate it at all of
11% the values contained in the x vector to obtain a vector f. Then compute
12% the definite integral using sum(f.*w);
13%
14% Written by Greg von Winckel - 02/25/2004
15N=N-1;
16N1=N+1; N2=N+2;
18xu=linspace(-1,1,N1)';
20% Initial guess
21y=cos((2*(0:N)'+1)*pi/(2*N+2))+(0.27/N1)*sin(pi*xu*N/N2);
23% Legendre-Gauss Vandermonde Matrix
24L=zeros(N1,N2);
26% Derivative of LGVM
27Lp=zeros(N1,N2);
29% Compute the zeros of the N+1 Legendre Polynomial
30% using the recursion relation and the Newton-Raphson method
32y0=2;
34% Iterate until new points are uniformly within epsilon of old points
35while max(abs(y-y0))>eps
38 L(:,1)=1;
39 Lp(:,1)=0;
41 L(:,2)=y;
42 Lp(:,2)=1;
44 for k=2:N1
45 L(:,k+1)=( (2*k-1)*y.*L(:,k)-(k-1)*L(:,k-1) )/k;
46 end
48 Lp=(N2)*( L(:,N1)-y.*L(:,N2) )./(1-y.^2);
50 y0=y;
51 y=y0-L(:,N2)./Lp; % Newton's iteration
53end
55% Linear map from [-1,1] to [a,b]
56x=(a*(1-y)+b*(1+y))/2;
58% Compute the weights
59w=(b-a)./((1-y.^2).*Lp.^2)*(N2/N1)^2;