Legendre Polynomial Procedure

Below is a procedure for generating Legendre polynomials. They are defined recursively by

L[n](x) = (2*n-1)*x*L[n-1](x)/n-(n-1)*L[n-2](x)/n

with the initial conditions L[0](x) = 1 and L[1](x) = x . The procedure uses if-then-else statements, which has the form

if condition 1 then

statement sequence 1

elif condition 2 then

statement sequence 2

....

elif condition n then

statement sequence n

else

default statement sequence

fi;

and the normal function, which reduces fractional expressions.

> L := proc(n,x)

> local result:

> if n<2 then

> result := x^n;

> else

> result := normal((2*n-1)/n*x * L(n-1,x) -

> (n-1)/n * L(n-2,x));

> fi;

> if not type(x,numeric) then

> L(n,x) := result;

> fi;

> RETURN(result);

> end:

Below we observe the 4th Legendre polynomial, then scale it to see that it agrees with the solution above.

> L(4,x); (8/3)*L(4,x);