## OUTPUTS:
##
## * ``dd`` - The divided difference f[xs(1), xs(2),...]
- ##
+ ##
order = length(xs) - 1;
if (order < 0)
## Our base case.
dd = f(xs(1))
else
- ## Order >= 1, recurse.
-
- ## f[x0,...,x_n-1]
- f0 = divided_difference(f, xs(1:end-1));
- ## f[x1,...,x_n]
- f1 = divided_difference(f, xs(2:end));
-
- # http://mathworld.wolfram.com/DividedDifference.html
- dd = (f0 - f1)/(xs(1) - xs(end));
+ ## Order >= 1.
+ cs = divided_difference_coefficients(xs)
+ dd = dot(cs, f(xs));
end
end
--- /dev/null
+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