The divided differences method is a numerical procedure for interpolating a polynomial given a set of points. Unlike Neville’s method, which is used to approximate the value of an interpolating polynomial at a given point, the divided differences method constructs the interpolating polynomial in Newton form.
Consider a table of values of and corresponding values of a function
at those points:
x | y |
---|---|
Also assume that is the
th Lagrangian polynomial that corresponds with the function
at these points. The polynomial
can be expressed using the divided differences of the function
with respect to the
-values.
Therefore the constants must be found to construct the polynomial. To find these constants, the divided differences are recursively generated until
iterations have been completed. We start with the zeroth divided difference of the function
with respect to
, which is the value of
at that point. Bracket notation is introduced to distinguish the divided differences.
Now begins the recursive generation of the divided differences. The first divided difference is then the function with respect to the values
and
.
The second divided difference follows:
This iteration continues until the th divided difference:
Thus the interpolating polynomial resulting from the divided differences method takes the form:
Consider the following set of points and the corresponding values of a function
at those points. This data set is the same from the previous post on Neville’s method. We will perform the divided differences method to find the interpolating polynomial of these points and approximate the polynomial at
.
x | f(x) |
---|---|
8.1 | 16.9446 |
8.3 | 17.56492 |
8.6 | 18.50515 |
8.7 | 18.82091 |
First, is found.
Then, is computed.
With and
, we find
.
is then found.
We can then compute :
Lastly, we calculate :
The interpolating polynomial, , is then constructed.
We can see the interpolated polynomial passes through the points with the following:
p3x <- function(x) { f <- 16.9446 + 3.1016 * (x - 8.1) + 0.065 * (x - 8.1) * (x - 8.3) - 0.010417 * (x - 8.1) * (x - 8.3) * (x - 8.6) return(f)}x <- c(8.1, 8.3, 8.6, 8.7)fx <- c(16.9446, 17.56492, 18.50515, 18.82091)dat <- data.frame(cbind(x, fx))ggplot(dat, aes(x=x, y=fx)) + geom_point(size=5, col='blue') + stat_function(fun = p3x, size=1.25, alpha=0.4)
Using the function above, we can also see the interpolated polynomial resulting from the divided differences method returns the same approximated value of the function ,
as Neville’s method.
p3x(8.4)## [1] 17.87709Divided Differences in R
The following is an implementation of the divided differences method of polynomial interpolation in R. The function utilizes the rSymPy library to build the interpolating polynomial and approximate the value of the function for a given value of
.
divided.differences <- function(x, y, x0) { require(rSymPy) n <- length(x) q <- matrix(data = 0, n, n) q[,1] <- y f <- as.character(round(q[1,1], 5)) fi <- '' for (i in 2:n) { for (j in i:n) { q[j,i] <- (q[j,i-1] - q[j-1,i-1]) / (x[j] - x[j-i+1]) } fi <- paste(fi, '*(x - ', x[i-1], ')', sep = '', collapse = '') f <- paste(f, ' + ', round(q[i,i], 5), fi, sep = '', collapse = '') } x <- Var('x') sympy(paste('e = ', f, collapse = '', sep = '')) approx <- sympy(paste('e.subs(x, ', as.character(x0), ')', sep = '', collapse = '')) return(list('Approximation from Interpolation'=as.numeric(approx), 'Interpolated Function'=f, 'Divided Differences Table'=q))}
Let’s use the function to interpolate a function given the values of and the corresponding values of a function
,
and approximate the function when
.
divided.differences(x, fx, 8.4)## Loading required package: rSymPy## Loading required package: rJython## Loading required package: rJava## Loading required package: rjson## $`Approximation from Interpolation`## [1] 17.87709## ## $`Interpolated Function`## [1] "16.9446 + 3.1016*(x - 8.1) + 0.065*(x - 8.1)*(x - 8.3) + -0.01042*(x - 8.1)*(x - 8.3)*(x - 8.6)"## ## $`Divided Differences Table`## [,1] [,2] [,3] [,4]## [1,] 16.94460 0.0000 0.00000 0.00000000## [2,] 17.56492 3.1016 0.00000 0.00000000## [3,] 18.50515 3.1341 0.06500 0.00000000## [4,] 18.82091 3.1576 0.05875 -0.01041667
The function returns the approximated value, the interpolated polynomial use to approximate the function and the divided differences table which contains the intermediate calculations as we saw earlier. I wasn’t able to find a function in R or a package that performs the divided differences method, so, unfortunately, I have nothing for which to compare our function. However, the results agree with our manual calculations so that should be satisfactory for this introductory purpose.
ReferencesBurden, R. L., & Faires, J. D. (2011). Numerical analysis (9th ed.). Boston, MA: Brooks/Cole, Cengage Learning.
The post Divided Differences Method of Polynomial Interpolation appeared first on Aaron Schlegel.
var vglnk = { key: '949efb41171ac6ec1bf7f206d57e90b8' }; (function(d, t) {var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;s.src = '//cdn.viglink.com/api/vglnk.js';var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script'));R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...