]> gitweb.michael.orlitzky.com - octave.git/blob - legendre_p.m
Add the Octave c_inner_product function.
[octave.git] / legendre_p.m
1 function P = legendre_p(n)
2 ## Return the nth legendre polynomial.
3 ##
4 ## INPUTS:
5 ##
6 ## * ``n`` - The index of the polynomial that we want.
7 ##
8 ## OUTPUTS:
9 ##
10 ## * ``P`` - A polynomial function of one argument.
11 ##
12 if (n < 0)
13 ## Can't do anything here. Return nothing.
14 P = NA;
15 elseif (n == 0)
16 ## One of our base cases.
17 P = @(x) 1
18 elseif (n == 1)
19 ## The second base case.
20 P = @(x) x
21 else
22 ## Compute recursively.
23 prev = legendre_p(n-1)
24 prev_prev = legendre_p(n-2)
25 P = @(x) (1/n)*( (2*n - 1)*prev(x) - (n-1)*prev_prev(x) )
26 end
27 end