-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlagpts.m
296 lines (242 loc) · 8.81 KB
/
lagpts.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
function [x w v] = lagpts(n,int,meth)
%LAGPTS Laguerre points and Gauss-Laguerre Quadrature Weights.
% LAGPTS(N) returns N Laguerre points X in (0,inf).
%
% [X,W] = LAGPTS(N) returns also a row vector W of weights for Gauss-Laguerre
% quadrature. [X,W,V] = LAGPTS(N) returns in addition a column vector V
% of the barycentric weights corresponding to X.
%
% LAGPTS(N,D) scales the nodes and weights for the semi-infinite domain D.
% D can be either a domain object or a vector with two components.
%
% [X,W] = LAGPTS(N,METHOD) allows the user to select which method to use.
% METHOD = 'GW' will use the traditional Golub-Welsch eigenvalue method,
% which is best for when N is small. METHOD = 'FAST' will use the
% Glaser-Liu-Rokhlin fast algorithm, which is much faster for large N.
% By default LEGPTS uses 'GW' when N < 128.
%
% See also chebpts, legpts, hermpts, and jacpts.
% Copyright 2011 by The University of Oxford and The Chebfun Developers.
% See http://www.maths.ox.ac.uk/chebfun/ for Chebfun information.
% 'GW' by Nick Trefethen, March 2009 - algorithm adapted from [1].
% 'FAST' by Nick Hale, March 2010 - algorithm adapted from [2].
%
% References:
% [1] G. H. Golub and J. A. Welsch, "Calculation of Gauss quadrature
% rules", Math. Comp. 23:221-230, 1969,
% [2] A. Glaser, X. Liu and V. Rokhlin, "A fast algorithm for the
% calculation of the roots of special functions", SIAM Journal
% on Scientific Computing", 29(4):1420-1438:, 2007.
method = 'default';
interval = [0 inf];
if n < 0
error('CHEBFUN:lagpts:n','First input should be a positive number.');
end
% Return empty vector if n == 0
if n == 0
x = []; w = []; v = []; return
end
% Check the inputs
if nargin > 1
if nargin == 3
interval = int; method = meth;
elseif nargin == 2
if ischar(int), method = int; else interval = int; end
end
if ~(strcmpi(method,'default') || strcmpi(method,'GW') || strcmpi(method,'fast'))
error('CHEBFUN:lagpts:inputs',['Unrecognised input string.', method]);
end
if isa(interval,'domain')
interval = interval.endsandbreaks;
end
if numel(interval) > 2,
warning('CHEBFUN:lagpts:domain',...
'Piecewise intervals not supported and will be ignored.');
interval = interval([1 end]);
end
end
if ~sum(isinf(interval)) == 1
error('CHEBFUN:lagpts:inf','lagpts only supports semi-infinite domains.');
end
% decide to use GW or FAST
if (n < 128 || strcmpi(method,'GW')) && ~strcmpi(method,'fast') % GW, see [1]
alpha = 2*(1:n)-1; beta = 1:n-1; % 3-term recurrence coeffs
T = diag(beta,1) + diag(alpha) + diag(beta,-1); % Jacobi matrix
[V,D] = eig(T); % eigenvalue decomposition
[x,indx] = sort(diag(D)); % Laguerre points
w = V(1,indx).^2; % Quadrature weights
v = sqrt(x).*abs(V(1,indx)).'; % Barycentric weights
v = v./max(v); v(2:2:n) = -v(2:2:n);
else % Fast, see [2]
[x ders] = alg0_Lag(n); % Nodes and L_n'(x)
w = exp(-x)./(x.*ders.^2); w = w'; % Quadrature weights
v = exp(-x/2)./ders; % Barycentric weights
v = -v./max(abs(v));
end
w = (1/sum(w))*w; % Normalise so that sum(w) = 1
% Nonstandard interval
if ~all(interval == [0 inf])
a = interval(1); b = interval(2);
if isinf(b)
x = x + a;
w = w*exp(-a);
else
x = - x + b;
w = w*exp(b);
end
end
% -------------------- Routines for FAST algorithm ------------------------
function [x ders] = alg0_Lag(n)
ders = zeros(n,1);
xs = 1/(2*n+1);
n1 = 20;
n1 = min(n1, n);
x = zeros(n,1);
for k = 1:n1
[xs ders(k)] = alg3_Lag(n,xs);
x(k) = xs;
xs = 1.1*xs;
end
[x ders] = alg1_Lag(x,ders,n,n1);
% --------------------------- UNSCALED VERSION ------------------------------
function [roots ders] = alg1_Lag2(roots,ders,n,n1)
m = 30;
u = zeros(1,m+1); up = zeros(1,m+1);
for j = n1:n-1
x = roots(j);
h = rk2_Lag(pi/2,-pi/2,x,n) - x;
r = x.*(n+.5 -.25*x); p = x.^2;
u(1:2) = [0; ders(j)];
u(3) = (-x*u(2)/2-r*u(1))/p;
u(4) = (-x*u(3)-(1+r)*u(2)/6-(n+.5-.5*x)*u(1))/p;
up(1:3) = [u(2) ; 2*u(3) ; 3*u(4)];
for k = 2:m-2
% u(k+3)=(-x*(2*k+1)*u(k+2)-(k*k+r)*u(k+1)-k*(n+.5-.5*x)*u(k)+.25*k*(k-1)*u(k-1))/p;
u(k+3)=(-x*(2*k+1)*(k+1)*u(k+2)-(k*k+r)*u(k+1)-(n+.5-.5*x)*u(k)+.25*u(k-1))/(p*(k+2)*(k+1));
up(k+2) = (k+2)*u(k+3);
end
hh = [1;cumprod(h+zeros(m,1))];
for l = 1:5
% hh = [1;cumprod(h*ones(m,1))];
hh = [1;cumprod(h+zeros(m,1))];
h = h - (u*hh)./(up*hh);
end
roots(j+1) = roots(j) + h;
ders(j+1) = up*[1;cumprod(h*ones(m,1))];
end
% --------------------------- SCALED VERSION ------------------------------
function [roots ders] = alg1_Lag(roots,ders,n,n1)
m = 30;
% Storage
hh1 = ones(m+1,1); zz = zeros(m,1); u = zeros(1,m+1); up = zeros(1,m+1);
x = roots(n1);
for j = n1:n-1
h = rk2_Lag(pi/2,-pi/2,x,n) - x;
M = 1/h; M2 = M^2; M3 = M^3; M4 = M^4;
r = x*(n + .5 - .25*x); p = x^2;
u(1:2) = [0; ders(j)/M];
u(3) = -.5*u(2)/(M*x)-(n + .5 - .25*x)*u(1)/(x*M^2);
u(4) = -u(3)/(M*x)+(-(1+r)*u(2)/6/M^2-(n+.5-.5*x)*u(1)/M^3)/p;
up(1:3) = [u(2) ; 2*u(3)*M ; 3*u(4)*M];
for k = 2:m-2
% u(k+3) = (-x*(2*k+1)*u(k+2)-(k*k+r)*u(k+1)-k*(n+.5-.5*x)*u(k)+.25*k*(k-1)*u(k-1))/p;
u(k+3) = (-x*(2*k+1)*(k+1)*u(k+2)/M-(k*k+r)*u(k+1)/M2-(n+.5-.5*x)*u(k)/M3+.25*u(k-1)/M4)/(p*(k+2)*(k+1));
up(k+2) = (k+2)*u(k+3)*M;
end
up(m+1) = 0;
% Flip for more accuracy in inner product calculation.
u = u(m+1:-1:1); up = up(m+1:-1:1);
% Newton iteration
hh = hh1; hh(end) = M; step = inf; l = 0;
if M == 1
Mhzz = (M*h)+zz;
hh = [M;cumprod(Mhzz)];
hh = hh(end:-1:1);
end
while (abs(step) > eps) && (l < 10)
l = l + 1;
step = (u*hh)/(up*hh);
h = h - step;
Mhzz = (M*h)+zz;
hh = [M;cumprod(Mhzz)]; % Powers of h (This is the fastest way!)
hh = hh(end:-1:1); % Flip for more accuracy in inner product
end
% Update
x = x + h;
roots(j+1) = x;
ders(j+1) = up*hh;
end
% --------------------------- SCALED VERSION ------------------------------
function [roots ders] = alg4_Lag(roots,ders,n,n1)
m = 30;
% Storage
hh1 = ones(m+1,1); zz = zeros(m,1); u = zeros(1,m+1); up = zeros(1,m+1);
x = roots(n1);
for j = n1:n-1
h = rk2_Lag(pi/2,-pi/2,x,n) - x;
M = 1/h; M2 = M^2; M3 = M^3; M4 = M^4;
r = x*(n + .5 - .25*x); p = x^2;
u(1:2) = [0; ders(j)/M];
u(3) = -.5*u(2)/(M*x)-(n + .5 - .25*x)*u(1)/(x*M^2);
u(4) = -u(3)/(M*x)+(-(1+r)*u(2)/6/M^2-(n+.5-.5*x)*u(1)/M^3)/p;
up(1:3) = [u(2) ; 2*u(3)*M ; 3*u(4)*M];
for k = 2:m-2
% u(k+3) = (-x*(2*k+1)*u(k+2)-(k*k+r)*u(k+1)-k*(n+.5-.5*x)*u(k)+.25*k*(k-1)*u(k-1))/p;
u(k+3) = (-x*(2*k+1)*(k+1)*u(k+2)/M-(k*k+r)*u(k+1)/M2-(n+.5-.5*x)*u(k)/M3+.25*u(k-1)/M4)/(p*(k+2)*(k+1));
up(k+2) = (k+2)*u(k+3)*M;
end
up(m+1) = 0;
% Flip for more accuracy in inner product calculation.
u = u(m+1:-1:1); up = up(m+1:-1:1);
% Newton iteration
hh = hh1; hh(end) = M; step = inf; l = 0;
if M == 1
Mhzz = (M*h)+zz;
hh = [M;cumprod(Mhzz)];
hh = hh(end:-1:1);
end
while (abs(step) > eps) && (l < 10)
l = l + 1;
step = (u*hh)/(up*hh);
h = h - step;
Mhzz = (M*h)+zz;
hh = [M;cumprod(Mhzz)]; % Powers of h (This is the fastest way!)
hh = hh(end:-1:1); % Flip for more accuracy in inner product
end
% Update
x = x + h;
roots(j+1) = x;
ders(j+1) = up*hh;
end
% -------------------------------------------------------------------------
function [x1 d1] = alg3_Lag(n,xs)
[u up] = eval_Lag(xs,n);
theta = atan(sqrt(xs/(n+.5-.25*xs))*up/u);
x1 = rk2_Lag(theta,-pi/2,xs,n);
% Newton iteration
step = inf; l = 0;
while (abs(step) > eps || abs(u) > eps) && (l < 200)
l = l + 1;
[u up] = eval_Lag(x1,n);
step = u/up;
x1 = x1 - step;
end
[ignored d1] = eval_Lag(x1,n);
% -------------------------------------------------------------------------
function [L Lp] = eval_Lag(x,n)
Lm2 = 0; Lm1 = exp(-x/2); Lpm2 = 0; Lpm1 = 0;
for k = 0:n-1
L = ((2*k+1-x).*Lm1-k*Lm2)/(k+1);
Lp = ((2*k+1-x).*Lpm1-Lm1-k*Lpm2)/(k+1);
Lm2 = Lm1; Lm1 = L; Lpm2 = Lpm1; Lpm1 = Lp;
end
% -------------------------------------------------------------------------
function x = rk2_Lag(t,tn,x,n)
m = 10; h = (tn-t)/m;
for j = 1:m
f1 = (n+.5-.25*x);
k1 = -h/(sqrt(f1/x)+.25*(1/x-.25/f1)*sin(2*t));
t = t+h; x = x+k1; f1 = (n+.5-.25*x);
k2 = -h/(sqrt(f1/x)+.25*(1/x-.25/f1)*sin(2*t));
x = x+.5*(k2-k1);
end