Ch4.2: Piecewise Interpolation

Higher Degree Polynomial Interpolation

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

title

Piecewise Interpolation

  • A piecewise interpolation uses lower order polynomials between successive portions of data.
  • Common choices are linear and cubic.
  • Typically referred to as splines.

title

Quadratic Spline Figure

  • Note the piecewise listing of quadratic formulas

title

Draftsman's Spline

title

  • Woodworking
  • Ship hull design
  • Fit flexible strip of wood through pegs

Clamped Spline

title

  • Spline is clamped at endpoints
  • Specific derivative (shape) condition at endpoints

Natural Spline

title

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

R Function 4.1: Linear Interpolation

  • Fit equation of line to two data points \( (x_1,y_1), ~(x_2,y_2) \)
  • For \( y = mx + b \), need \( m \) and \( b \)
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))  }

R Function 4.3: Linear Spline

  • 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 )) }

Example: 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

Data Plot Function

DataPlot <- 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 = "Data Plot",
    xlab="x",ylab="y",type="p",lwd=3,col="blue", 
    xlim = c(xplotmin,xplotmax), 
    ylim = c(yplotmin,yplotmax) ) }

Data Plot Linear Function

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 = "Data Plot",
    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: 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
DataPlotLinear(x,y)

plot of chunk unnamed-chunk-7

Example: R Interpolation Function

  • R has a piecewise interpolation function
x <- c(-2, -1, 0, 1)
y <- c(-1, -2, -1, 2)
f <- approxfun(x, y)
f(0.8)
[1] 1.4

Example: R Interpolation Function

  • Using R's piecewise interpolation function
x <- c(-2, -1, 0, 1)
y <- c(-1, -2, -1, 2)
f <- approxfun(x, y)
f(0.8)
[1] 1.4
DataPlotLinear(x,y)

plot of chunk unnamed-chunk-10

Example 1: R Cubic Spline Function

  • R has a cubic spline function
  • Returns interpolated y-values at specified x-values
x <- c(-2, -1, 0, 1)
y <- c(-1, -2, -1, 2)
spline.ex <- splinefun(x, y, method = "natural")
spline.ex(x)
[1] -1 -2 -1  2
spline.ex(0.8)
[1] 1.3232

Example 1: R Cubic Spline Function

  • Using R's cubic spline function
x <- c(-2, -1, 0, 1)
y <- c(-1, -2, -1, 2)
spline.ex <- splinefun(x, y, method = "natural")
spline.ex(0.8)
[1] 1.3232
  • Python plot

title

Example 1: Python

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

x = [-2, -1, 0, 1]
y = [-1, -2, -1, 2]
p = interp1d(x, y, kind = 'cubic')

n = len(x)
xnew = np.linspace(x[0], x[n-1], num=500, endpoint=True)
plt.plot(x, y, 'o', xnew, p(xnew), '-,r')
plt.legend(['Original Data','Cubic Spline p(x)'], loc = 'best')
plt.show()

Example 2: R Cubic Spline Function

  • Book example
x <- c(-5, -2, 0, 2, 5)
y <- c(-2, -1, 4, -1, -2)
spline.ex <- splinefun(x, y, method = "natural")
spline.ex(x)
[1] -2 -1  4 -1 -2
  • Python plot

title

Example 2: Comparison

  • Ch4.2 Cubic spline
  • Four polynomials
  • Each deg 3
  • MATLAB plot

title

  • Ch4.1 Polynomial
  • One polynomial
  • Deg 4 (5 data points)

title

Example 3: Wage Data, Page 1

  • Wage and other data for 3000 male workers in Mid-Atlantic region; see weblink.
#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, Page 2

  • Plot of data and cubic spline

plot of chunk unnamed-chunk-16

Ch4.2 Notes Appendix

  • Extra slides for background programs

Ch4.1 Higher Order Polynomial Interpolation

polyinterp <- function(x,y) {
  if(length(x) != length(y))
   stop("Length of x & y vectors must be same")
  n <- length(x) - 1
  vandermonde <- rep(1,length(x))
  for(i in 1:n) {
    xi <- x^i
    vandermonde <- cbind(vandermonde,xi) }
  beta <- solve(vandermonde,y)
  names(beta) <- NULL
  return(beta) }

Horner's Method (Ch1.3.2)

  • Nested polynomial evaluation

\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 +4x^3 +5x^4 \\ & = 1 + x(-2 + x(3 + x(4 + 5x)) \end{align*} \]

horner <- function(x, coefs) {
  y <- rep(0, length(x))
  for(i in length(coefs):1)
    y <- coefs[i] + x*y
  return (y)  }

Plot Function: Horner

HornerPlot <- function(x,y) {
  n = length(x)
  N <- 100; h <- (x[n] - x[1])/N
  t <- rep(0, N); f <- rep(0, N) 
  t[1] <- x[1]; f[1] <- y[1]; 
  p <- polyinterp(x, y)

  for(i in 1:N) { 
     t[i+1] <- t[i] + h 
     f[i+1] <- horner(t[i+1],p) }

  plot(t,f, 
    main = "Horner Interpolating Polynomial",
    xlab="x",ylab="y",type="l",lwd=2,col="red", 
    xlim = c(x[1],x[n]), ylim = c(min(f),max(f)) )
  lines(x,y,type = "p",col="blue",lwd=3)}

Example 2: Degree 4 Interpolation

(p <- HornerPlot(x, y))

plot of chunk unnamed-chunk-20

NULL