]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Add some octave code to compute (scaled and unscaled) legendre polynomials.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 14 Nov 2012 20:48:23 +0000 (15:48 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 14 Nov 2012 20:48:23 +0000 (15:48 -0500)
legendre_p.m [new file with mode: 0644]
legendre_p_tilde.m [new file with mode: 0644]

diff --git a/legendre_p.m b/legendre_p.m
new file mode 100644 (file)
index 0000000..6238929
--- /dev/null
@@ -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
diff --git a/legendre_p_tilde.m b/legendre_p_tilde.m
new file mode 100644 (file)
index 0000000..e5e6f63
--- /dev/null
@@ -0,0 +1,24 @@
+function P_tilde = legendre_p_tilde(n, a, b)
+  ## Return the nth Legendre polynomial scaled to the interval [a,b].
+  ##
+  ## INPUTS:
+  ##
+  ##   * ``n`` - The index of the polynomial that we want.
+  ##
+  ##   * ``a`` - The left endpoint of the interval.
+  ##
+  ##   * ``b`` - The right endpoint of the interval.
+  ##
+  ## OUTPUTS:
+  ##
+  ##   * ``P_tilde`` - A polynomial function of one argument.
+  ##
+  if (n < 0)
+    ## Can't do anything here. Return nothing.
+    P = NA;
+  else
+    ## Compute the Legendre polynomial over [-1,1] and mangle it.
+    P = legendre_p(n)
+    P_tilde = @(x) P( (2/(b-a))*x + 1 - (2*b)/(b-a) )
+  end
+end