I'm trying to plot the legendre polynomials, defined as:
P0(x) = 1
P1(x) = x
Pn+1(x) = ((2n+1)/(n+1)) * x * Pn(x) - (n / (n+1)) * Pn-1(x)
I've done it the easy slow way, and I've done it the direct, a little more complicated way. Both result in a similar figure, however, not quite the same. The Amplitudes are different. Here is the code with the respect figure (Notice that I adjust the subscript n+1 of the definition to n):
xi = linspace(-1,1,500);
n = 10;
Method 1:
Pn = cell(n+1,1);
Pn{1} = @ (x) 1;
Pn{2} = @ (x) x;
for i=3:(n+1)
Pn{i} = @ (x) ((2*(i-1)+1)/(i)).*x.*Pn{i-1}(x) - ((i-1)/i) .* Pn{i-2}(x);
end
plot(xi,Pn{1}(xi),'--r',xi,Pn{2}(xi),'--g',xi,Pn{3}(xi),'--b',...
xi,Pn{4}(xi),'--m',xi,Pn{5}(xi),'--c',xi,Pn{6}(xi),'--k');
legend('P0','P1','P2','P3','P4','P5');
Figure 1:
Method 2:
%Notice here that the results of Pj get stored into YI(j+1)
YI = zeros(length(xi),6);
YI(:,1) = ones(size(xi))';
YI(:,2) = xi';
for i=3:6;
Pn1 = 1;
Pn2 = xi;
for j=2:(i-1)
Pn3 = ((2*(j-1)+1)/j) .* xi .* Pn2 - ((j-1) / j) .* Pn1;
Pn1 = Pn2;
Pn2 = Pn3;
end
YI(:,i) = Pn3';
end
figure('Name','direct method');
plot(xi,YI(:,1)','--r',xi, YI(:,2)', '--g', xi, YI(:,3)', '--b', ...
xi,YI(:,4)','--m', xi,YI(:,5)', '--c', xi,YI(:,6)', '--k');
Figure 2:
This is weird, to say the least. For method 1 I'm calculating all the polynomials up to P11, but only utilizing the first 6 to plot. Does anybody know what is going on?
Method#2 can be made much simpler:
X = linspace(-1,1,500); X = X(:);
N = 10;
Y = zeros(numel(X),N);
Y(:,1) = 1;
Y(:,2) = X;
for n=1:(N-1)
Y(:,n+2) = ((2*n+1) .* X .* Y(:,n+1) - n .* Y(:,n)) / (n+1);
end
figure, plot(X, Y(:,1:6))
legend(num2str((1:6)'-1,'P_%d(x)'))
xlabel('x'), ylabel('P_n(x)'), title('Legendre Polynomials')
This is equivalent to the plot shown on the Wikipedia page.
I had an off-by-one bug in the array indexing; MATLAB uses 1-based indexing, but the formula is defined in a 0-based manner. It is fixed now, sorry for the confusion ;)
To confirm P(n=2,x=0)
should be -1/2
:
>> interp1(X, Y(:,3), 0)
ans =
-0.5000