Ch4.2: Piecewise Interpolation

Higher Degree Polynomial Interpolation

  • Suppose we had a data set of 102 data points.
  • A polynomial interpolation to this data would have degree 101.
  • Can have as much as 100 oscillations, typically of large magnitude.
  • These are found by taking the derivative (deg 100 polynomial) and setting equal to zero, and then solving for critical points.

title

Draftsman's Spline

title

  • Woodworking, ship hull design, etc.
  • Smooth fit determined by a natural balance of internal and external forces (tension, weights).
  • Applications in other areas help create naturally smooth curves.

Clamped Spline

title

  • Fit flexible strip of wood through pegs
  • Spline is clamped at endpoints
  • Specific derivative (shape) condition at endpoints

title

Natural Spline

title

  • Spline is not clamped at endpoints (free)
  • Linear shape at endpoints
  • Second derivative equal to zero at endpoints.

Two Jokes



What do you call a fake noodle?

An impasta.


I like telling Dad jokes.

Sometimes he laughs!

Splines: Piecewise Interpolation

Piecewise Interpolation

  • A piecewise interpolation uses low order polynomials (splines) between groupings of data.
  • Common choices are linear and cubic.
  • Note difference between single cubic interpolating polynomial versus three separate cubic splines (one for each pair of data).

title

Quadratic Spline Figure

  • Note the piecewise listing of quadratic formulas

title

Linear Interpolation with R

  • Program from our book.
  • Fit equation of line to two data points \( (x_1,y_1), \, (x_2,y_2) \).
  • For \( y = mx + b \), need \( m \) and \( b \).

\[ \small{ \begin{aligned} m & = \frac{y_2 - y_1}{x_2-x_1} \\ b & = y_2 - m x_2 \end{aligned} } \]

linterp <- function (x1, y1, x2, y2) {
  m <- (y2-y1)/(x2-x1)
  b <- y2 - m*x2
  ## Convert into a form suitable for horner
  return (c(b, m))  }

Linear Spline with R

  • Program from our book.
  • Returns coefficients for each linear spline.
pwiselinterp <- function (x, y) {
  n <- length(x) - 1
  y <- y[order(x)]
  x <- x[order(x)]
  mvec <- bvec <- c()
  for(i in 1:n) {
    p <- linterp(x[i], y[i], x[i + 1], y[i + 1])
    mvec <- c(mvec,p[2])
    bvec <- c(bvec,p[1]) }
  return(list(m = mvec , b = bvec )) }

Data Plot Linear Function

  • Graphs the linear spline with data
DataPlotLinear <- function(x,y) {
  n = length(x)
  xplotmin <- x[1]-1
  xplotmax <- x[n]+1
  yplotmin <- min(c(min(y),0))-1
  yplotmax <- max(y)+1

  plot(x,y, 
    main = "Linear Splines",
    xlab="x",ylab="y",type="p",lwd=3,col="blue", 
    xlim = c(xplotmin,xplotmax), 
    ylim = c(yplotmin,yplotmax) ) 
  lines(x,y,type = "l",col="red",lwd=2)}

Example 1: Linear Spline

x <- c(-2, -1, 0, 1)
y <- c(-1, -2, -1, 2)
pwiselinterp(x, y)
$m
[1] -1  1  3

$b
[1] -3 -1 -1
f <- approxfun(x, y)
f(0.8)
[1] 1.4
DataPlotLinear(x,y)

plot of chunk unnamed-chunk-6

Cubic Splines

  • Instead of trying to fit a piecewise linear model through a set of data points, we can try to fit a piecewise cubic polynomial model through the points.
  • For a smooth curve (no jaggedness), we require that the resulting graph is differentiable everywhere.

Cubic Spline Derivation

  • Suppose that we want to fit cubic splines through the data points (1,2), (2,1), (3,2).
  • Find two cubic polynomials (splines) that fit their respective two data points along with additional smoothness conditions.

\[ \begin{aligned} p_1(x) &= a_1 x^3 + b_1 x^2 + c_1 x + d_1 , \,\, 1 \leq x \leq 2 \\ p_2(x) &= a_2 x^3 + b_2 x^2 + c_2 x + d_2 , \,\, 2 \leq x \leq 3 \end{aligned} \]

  • Here, \( p_1(x) \) passes through (1,2) and (2,1).
  • Also, \( p_2(x) \) passes through (2,1) and (3,2).
  • To join smoothly at the shared point (2,1), they need to have the same slope and concavity there.

Cubic Spline Derivation: 8 Equations

Cubic Spline Derivation: Matrix Equation

Cubic Spline Plot using R

library(pracma)
CubicSplinePlot <- function(x,y) {
  n = length(x);  N <- 100 
  t <- linspace(x[1], x[n], N) 
  p <- cubicspline(x, y, t)
  bottom = min(0,min(p)); top = max(0,max(p))
plot(t,p, 
   main = "Cubic Splines",
   xlab="x",ylab="y",type="l",lwd=2,col="red", 
   xlim = c(x[1],x[n]), ylim = c(bottom, top) )
points(x,y,type = "p",col="blue",lwd=3)}

Example 2: Cubic Spline Function & Plot

x <- c(-2, -1, 0, 1)
y <- c(-1, -2, -1, 2)
p <- splinefun(x,y,"natural")
p(0.8)
[1] 1.3232
CubicSplinePlot(x,y)

plot of chunk unnamed-chunk-10

Example 3: Wage Data

  • This application turned up on an internet search.
  • Wage and other data for 3000 male workers in Mid-Atlantic region; see weblink.
  • Uses ISLR package.
#loading the Splines Packages
require(splines)
#ISLR contains the Dataset
require(ISLR)
attach(Wage) #attaching Wage dataset
#?Wage #for more details on the dataset
agelims<-range(age)
#Generating Test Data
age.grid<-seq(from=agelims[1], to = agelims[2])

Example 3: Wage Data

  • Additional commands
#3 cutpoints at ages 25 ,50 ,60
fit<-lm(wage ~ bs(age,knots = c(25,40,60)),data = Wage )
#summary(fit)
#Plotting the Regression Line to the scatterplot   
plot(age,wage,col="grey",xlab="Age",ylab="Wages")
points(age.grid,predict(fit,newdata = list(age=age.grid)),col="darkgreen",lwd=2,type="l")
#adding cutpoints
abline(v=c(25,40,60),lty=2,col="darkgreen")

Example 3: Wage Data

  • Output plot

plot of chunk unnamed-chunk-13

Cubic Spline Activity

For our next class period, you will need a computer that runs RStudio and a recent version of R.

  • This could be one of the classroom computers, or a laptop.
  • We will also need to install and use the pracma package.

For the project, we collect data and use R to fit a cubic spline.