The legendre polynomials are recursively defined as follows
$L_{k+1}(x)=\frac{2k-1}{k}xL_k(x)-\frac{k-1}{k}L_{k-1}(x)$
I want to calculate the value of $L_k(x)$ for any given value $x$. I used the above recursion to come up with the following matlab code:
function [out] = ausWert(k,x)
if(k==0)
out =1;
else if(k==1)
out=x;
else
out =ausWert(k-1,x)*x*(2*k-1)/(k)-((k-1)/k)*ausWert(k-2,x);
end
end
end
Whats the best/fastest way to calculate $L_k(x)$ ? My recursive code is terrible slow...
Would appreciate any help
Firstly, I think you can compute the value of Legendre polynomials using
legendreP(n,x)in MatLab.Alternativey, according to Wikipedia, there is an explicit form:
$$L_n(x)=\frac{(x-1)^n}{2^n}\sum_{k=0}^n {n\choose k}^2y^k,$$ where $$y=\frac{x+1}{x-1},$$ and $${n\choose k}=\frac{n!}{k!(n-k)!},$$ is the Binomial coefficient; $m!=1\times2\times\cdots\times m$ is the factorial function.
You should be able to develop code easily from this.