]> gitweb.michael.orlitzky.com - octave.git/blobdiff - divided_difference_coefficients.m
Add the divided_difference_coefficients() function and reimplement divided_difference...
[octave.git] / divided_difference_coefficients.m
diff --git a/divided_difference_coefficients.m b/divided_difference_coefficients.m
new file mode 100644 (file)
index 0000000..53d5799
--- /dev/null
@@ -0,0 +1,26 @@
+function coefficients = divided_difference_coefficients(xs)
+  ## Compute divided difference coefficients of `f` at points `xs`.
+  ##
+  ## INPUTS:
+  ##
+  ##   * ``xs`` - A vector containing x-coordinates.
+  ##
+  ## OUTPUTS:
+  ##
+  ##   * ``coefficients`` - The vector of coefficients such that
+  ##   dot(coefficients, f(xs)) == dd. Used to solve linear systems.
+  ##
+
+  coefficients = [];
+    
+  for xj = xs
+    this_coeff = 1;
+    for xi = xs
+      if (xi != xj)
+       ## Append (xj - xi) to the vector of coefficients.
+       this_coeff = this_coeff * (1 / (xj - xi));
+      end
+    end
+    coefficients(end+1) = this_coeff;
+  end
+end