]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Clean up the divided_difference function.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 14 Sep 2012 23:44:43 +0000 (19:44 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 14 Sep 2012 23:44:43 +0000 (19:44 -0400)
divided_difference.m

index 7b8020e95c3b145fe637502802a09113126b329e..2b8f8df0d5e75ca126b618acb8e81257235e79c2 100644 (file)
@@ -3,16 +3,29 @@ function dd = divided_difference(f, xs)
   ## is assumed to be a vector containing at least one element. If it
   ## contains n elements, the (n-1)st divided difference will be
   ## calculated.
   ## is assumed to be a vector containing at least one element. If it
   ## contains n elements, the (n-1)st divided difference will be
   ## calculated.
-  order = length(xs);
+  ##
+  ## INPUTS:
+  ##
+  ##   * ``f`` - The function whose divided differences we want.
+  ##
+  ##   * ``xs`` - A vector containing x-coordinates. The length of `xs`
+  ##   determines the order of the divided difference.
+  ##
+  ##
+  ## OUTPUTS:
+  ##
+  ##   * ``dd`` - The divided difference f[xs(1), xs(2),...]
+  ##
+  order = length(xs) - 1;
   
   
-  if (order < 1)
+  if (order < 0)
     ## Can't do anything here. Return nothing.
     dd = NA;
     ## Can't do anything here. Return nothing.
     dd = NA;
-  elseif (order == 1)
+  elseif (order == 0)
     ## Our base case.
     dd = f(xs(1))
   else
     ## Our base case.
     dd = f(xs(1))
   else
-    ## Order > 1, recurse.
+    ## Order >= 1, recurse.
 
     ## f[x0,...,x_n-1]
     f0 = divided_difference(f, xs(1:end-1));
 
     ## f[x0,...,x_n-1]
     f0 = divided_difference(f, xs(1:end-1));
@@ -20,6 +33,6 @@ function dd = divided_difference(f, xs)
     f1 = divided_difference(f, xs(2:end));
 
     # http://mathworld.wolfram.com/DividedDifference.html
     f1 = divided_difference(f, xs(2:end));
 
     # http://mathworld.wolfram.com/DividedDifference.html
-    dd = (f0 - f1)/(xs(1) - xs(end))
+    dd = (f0 - f1)/(xs(1) - xs(end));
   end
 end
   end
 end