-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylegendre.m
80 lines (60 loc) · 1.97 KB
/
mylegendre.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
% Legendre polynomial
%
% H = mylegendre(n,x) computes the values H of the Legendre polynomials of
% degrees 0,...,n at points given in x. The values of the degree k
% polynomial are in H(:,k+1).
%
% H = mylegendre(n,x,'N') computes the values of the normalized Legendre
% polynomial.
%
% See also: hermite.
%
% Stuart C. Hawkins - 19 February 2015
% Copyright 2019-2022 Stuart C. Hawkins
%
% This file is part of TMATROM3
%
% TMATROM3 is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% TMATROM3 is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with TMATROM3. If not, see <http://www.gnu.org/licenses/>.
function H=mylegendre(n,x,opts)
% set default for opts
if nargin<3
opts='';
end
% make sure x is a column vector
x=x(:);
% setup return array
m=length(x);
H=zeros(m,n+1);
% set values for degree 0 polynomials
% Note: degree k polynomial is stored in H(:,k+1)
H(:,1) = ones(m,1);
if n>0
% compute degree 1 values
H(:,2) = x;
for k=1:n-1
% use three term recurrence for higher degree polynomials
H(:,k+2) = ((2*k+1)/(k+1))*x.*H(:,k+1)-(k/(k+1))*H(:,k);
end
end
% normalise the polynomials if necessary
if strfind(opts,'N')
% Note: we assume probability measure on [-1,1] is 0.5 so normalise wrt
% to this measure
for k=0:n
H(:,k+1)=H(:,k+1)*sqrt((2*k+1));
% Note standard normalised version would be
% H(:,k+1)=H(:,k+1)*sqrt((2*k+1)/2);
% but we use probabilists scaling.
end
end