**R – Aaron Schlegel**, and kindly contributed to R-bloggers)

Polynomial interpolation is the method of determining a polynomial that fits a set of given points. There are several approaches to polynomial interpolation, of which one of the most well known is the Lagrangian method. This post will introduce the Lagrangian method of interpolating polynomials and how to perform the procedure in R.

###### Lagrangian Polynomial Interpolation

The Lagrangian method of polynomial interpolation uses Lagrangian polynomials to fit a polynomial to a given set of data points. The Lagrange interpolating polynomial is given by the following theorem:

For a set of data points with no duplicate and there exists a function which evaluates to these points, then there is a unique polynomial with degree also exists. The polynomial is given by:

Where each in is:

###### Lagrangian Polynomial Interpolation Example

Consider the following set of data points:

x | y |
---|---|

0 | 7 |

2 | 11 |

3 | 28 |

4 | 63 |

The graph of the points looks like:

x <- c(0, 2, 3, 4) y <- c(7, 11, 28, 63) dat <- data.frame(cbind(x, y)) ggplot(dat, aes(x=x, y=y)) + geom_point(size=5, col='blue')

We wish to find a polynomial that passes through these points. Note that much of the intermediate algebra will be skipped for the sake of brevity.

Start with :

is then:

Then finally, and :

These polynomials are then multiplied by they corresponding value and added together, resulting in the following polynomial:

Simplifying this equation yields:

###### Polynomial Interpolation in R

We can implement a function in R to perform Lagrangian interpolation. The rSymPy package, which provides an R interface for the SymPy CAS (computer algebra system), will be used as we are interested in the symbolic form of the resulting polynomial.

library(rSymPy)

Our implementation of the Lagrangian polynomial interpolation method, `lagrange.poly()`

, proceeds as follows:

lagrange.poly <- function(x, y) { l <- list() # List to store Lagrangian polynomials L_{1,2,3,4} k <- 1 for (i in x) { # Set the numerator and denominator of the Lagrangian polynomials to 1 and build them up num <- 1 denom <- 1 # Remove the current x value from the iterated list p <- x[! x %in% i] # For the remaining points, construct the Lagrangian polynomial by successively # appending each x value for (j in p) { num <- paste(num, "*", "(", 'x', " - ", as.character(j), ")", sep = ", collapse = ") denom <- paste(denom, "*", "(", as.character(i)," - ", as.character(j), ")", sep = ", collapse = ") } # Set each Lagrangian polynomial in rSymPy to simplify later. l[k] <- paste("(", num, ")", "/", "(", denom, ")", sep = ", collapse = ") k <- k + 1 } # Similar to before, we construct the final Lagrangian polynomial by successively building # up the equation by iterating through the polynomials L_{1,2,3,4} and the y values # corresponding to the x values. eq <- 0 for (i in 1:length(y)) { eq <- paste(eq, '+', as.character(y[i]), "*", l[[i]], sep = ", collapse = ") } # Define x variable for rSymPy to simplify x <- Var('x') # Simplify the result with rSymPy and return the polynomial return(sympy(paste("simplify(", eq, ")"))) }

With the and values used previously, perform Lagrangian polynomial interpolation to see if our function returns the same polynomial we found earlier.

lagrange.poly(x, y) ## [1] "7 - 2*x + x**3"

The polynomial resulting from our `lagrange.poly()`

function matches what we found previously.

Lagrangian polynomial interpolation can also be performed in R using the `poly.calc()`

function from the polynom package.

library(polynom)

Use the `poly.calc()`

function to interpolate the data points using the Lagrangian method.

poly.calc(x, y) ## 7 - 2*x + x^3

We can plot this polynomial against the original data points to confirm visually that the polynomial does indeed pass through the points.

f <- function(x) { return(x^3 - 2 * x + 7) } ggplot(dat, aes(x=x, y=y)) + geom_point(size=5, col='blue') + stat_function(fun = f, size=1.25, alpha=0.4)

###### References

Burden, R. L., & Faires, J. D. (2011). Numerical analysis (9th ed.). Boston, MA: Brooks/Cole, Cengage Learning.

Cheney, E. W., & Kincaid, D. (2013). Numerical mathematics and computing (6th ed.). Boston, MA: Brooks/Cole, Cengage Learning.

The post Lagrangian Polynomial Interpolation with R appeared first on Aaron Schlegel.

**leave a comment**for the author, please follow the link and comment on their blog:

**R – Aaron Schlegel**.

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...