X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=legendre_p.m;fp=legendre_p.m;h=6238929eb2e4aea13ea1cc1548b82dcc59fdb301;hp=0000000000000000000000000000000000000000;hb=ee06b882dfb9a86788d7057cec8ca6d7680c5ca5;hpb=2fbcf94908d646ec62d2f15f7ec35288c6ac1cfe diff --git a/legendre_p.m b/legendre_p.m new file mode 100644 index 0000000..6238929 --- /dev/null +++ b/legendre_p.m @@ -0,0 +1,27 @@ +function P = legendre_p(n) + ## Return the nth legendre polynomial. + ## + ## INPUTS: + ## + ## * ``n`` - The index of the polynomial that we want. + ## + ## OUTPUTS: + ## + ## * ``P`` - A polynomial function of one argument. + ## + if (n < 0) + ## Can't do anything here. Return nothing. + P = NA; + elseif (n == 0) + ## One of our base cases. + P = @(x) 1 + elseif (n == 1) + ## The second base case. + P = @(x) x + else + ## Compute recursively. + prev = legendre_p(n-1) + prev_prev = legendre_p(n-2) + P = @(x) (1/n)*( (2*n - 1)*prev(x) - (n-1)*prev_prev(x) ) + end +end