Help with Legendre Plot Matlab

104 Views Asked by At

I've written a code to change a Chebyshev into a Legendre Polynomial, however it will not plot the graph after and I'm not sure why the graph will not plot? The code i have is: function LegendrePoly(n)

x=[-1:0.01:1]';
M=zeros(length(x),n);
M(:,1)=ones(length(x),1);
M(:,2)=x;
for k=3:n
    M(:,k)=(2*k-1)*x.*M(:,k-1)-(k-1).*M(:,k-2)/k;  
end
plot(x,M,'LineWidth',2);
grid;

Any help would be greatly appreciated.

1

There are 1 best solutions below

4
On

The recurrence relationship for Legendre polynomials is:

$$kP_{k}(x)=(2k-1)xP_{k}(x)-(k-1)P_{k-2}(x)$$

Thus, you have two corrections to bring:

  • coefficient $(k-1)$ instead of $(k-2)$ for the last term, and

  • division by $k$ that, at present, is only on the last term: it should be on all the right hand side.