0

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

pshmath0
  • 10,565
XPenguen
  • 2,301
  • 1
    For a fixed $x$, the sequence given by ${L_n(x)}_{n\geq 0}$ fulfills a Fibonacci-like recursion, hence the problem is substantially equivalent to finding an efficient algorithm for computing $F_n$. Have a look at here: http://math.stackexchange.com/a/2081266/44121 – Jack D'Aurizio Jan 26 '17 at 19:07
  • 1
    As an alternative, you may use the representation given by the generating function for Legendre polynomials. – Jack D'Aurizio Jan 26 '17 at 19:08

1 Answers1

1

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.

pshmath0
  • 10,565