Skip to content

Commit 0bfc2dc

Browse files
Create lgwt.m
1 parent a0cd585 commit 0bfc2dc

File tree

1 file changed

+36
-0
lines changed
  • Numerical Methods/Gauss Legendre Quadrature

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function [x, w] = lgwt(N, a, b)
2+
% lgwt.m - Compute Gauss Legendre nodes and weights
3+
% From Greg von Winckel - 02/25/2004
4+
N = N-1;
5+
N1 = N+1; N2 = N+2;
6+
7+
xu = linspace(-1,1,N1)';
8+
9+
% Initial guess
10+
y = cos((2*(0:N)' + 1)*pi/(2*N+2)) + (0.27/N1)*sin(pi*xu*N/N2);
11+
12+
% Legendre-Gauss Vandermonde Matrix
13+
L = zeros(N1,N2);
14+
% Derivative
15+
Lp = zeros(N1,1);
16+
17+
% Iterate using Newton's method
18+
y0 = 2;
19+
while max(abs(y - y0)) > eps
20+
L(:,1) = 1;
21+
L(:,2) = y;
22+
23+
for k = 2:N1
24+
L(:,k+1) = ((2*k - 1).*y.*L(:,k) - (k - 1)*L(:,k-1))/k;
25+
end
26+
27+
Lp = N2*(L(:,N1) - y.*L(:,N2))./(1 - y.^2);
28+
29+
y0 = y;
30+
y = y0 - L(:,N2)./Lp;
31+
end
32+
33+
% Linear map from [-1,1] to [a,b]
34+
x = (a*(1 - y) + b*(1 + y))/2;
35+
w = (b - a)./((1 - y.^2).*Lp.^2)*(N2/N1)^2;
36+
end

0 commit comments

Comments
 (0)