1 function P = legendre_p(n)
2 % Return the `n`th Legendre polynomial.
6 % * ``n`` - The index of the polynomial that we want.
10 % * ``P`` - A polynomial function of one argument.
13 % Can't do anything here. Return nothing.
16 % One of our base cases.
19 % The second base case.
22 % Not one of the base cases, so use the recursive formula.
23 prev = legendre_p(n-1);
24 prev_prev = legendre_p(n-2);
25 P = @(x) (1/n).*( (2*n - 1).*x.*prev(x) - (n-1).*prev_prev(x) );