\[ \small{ \begin{aligned} f(x) & \cong f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \cdots + \frac{f^{(n)}(a)}{n!}(x-a)^n \\ & = \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!} (x-a)^k \\ & = P_n(x) \end{aligned} } \]
\[ \small{ f(x) \cong f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \cdots + \frac{f^{(n)}(a)}{n!}(x-a)^n} \]
What do you call a fake noodle?
An impasta.
Why can't you hear a Pterodactyl go to the bathroom?
Because the pee is silent.
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)) }
(p<-linterp(1,3,2,5))
[1] 1 2
\[ y = 1 + 2x \]
\[ \begin{aligned} f(x) & = 1 - 2x + 3x^2 +4x^3 +5x^4 \\ & = 1 + x(-2 + x(3 + x(4 + 5x)) \end{aligned} \]
horner <- function(x, coefs) {
y <- rep(0, length(x))
for(i in length(coefs):1)
y <- coefs[i] + x*y
return (y) }
(p<-linterp(1,3,2,5))
[1] 1 2
horner(10,p)
[1] 21
\[ y = 1 + 2x \]
Fit a polynomial through multiple data points
\[ \begin{aligned} p(x_1) &= y_1 \\ p(x_2) &= y_2 \\ & \vdots \\ p(x_n) &= y_n \end{aligned} \]
Our polynomial is
\[ p(x) = \beta_n x^n + \beta_{n-1} x^{n-1} + \cdots + \beta_1 x + \beta_0 \]
Then \( p(x_i) = y_i \) for each \( i \), which can be expressed as a matrix-vector equation:
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) }
The polyinterp
program computes the coefficients of the interpolating polynomial using the Vandermonde matrix.
\[ p(x) = \beta_0 + \beta_1 x + \cdots + \beta_{n-1} x^{n-1} + \beta_n x^n \]
xdata <- c(0, 0.3, 0.8, 1.1, 1.6, 2.3)
ydata <- c(0.6 ,0.67, 1.01, 1.35, 1.47, 1.25)
(p <- polyinterp(xdata, ydata))
[1] 0.6000000 0.5910395 -2.5174715 5.4280961 -3.5892186 0.7303129
horner(xdata,p)
[1] 0.60 0.67 1.01 1.35 1.47 1.25
ydata
[1] 0.60 0.67 1.01 1.35 1.47 1.25
The output of the polyinterp
program is the vector of coefficients on the interpolating polynomial \( p(x) \).
(p <- polyinterp(xdata, ydata))
[1] 0.6000000 0.5910395 -2.5174715 5.4280961 -3.5892186 0.7303129
p[1]; p[2]
[1] 0.6
[1] 0.5910395
The naive method of polynomial evaluation uses the coefficients on powers of \( x \) to directly evaluate polynomial:
naivepoly <- function(x) {p[1] + p[2]*x + p[3]*x^2 + p[4]*x^3 + p[5]*x^4 + p[6]*x^5}
naivepoly(xdata)
[1] 0.60 0.67 1.01 1.35 1.47 1.25
ydata
[1] 0.60 0.67 1.01 1.35 1.47 1.25
The generate a plot of the data with interpolating polynomial, the program code on next slide does the following:
polyinterp
to compute coefficients.\[ h = \frac{x_n-x_1}{N} \]
HornerPlot <- function(x,y) {
p <- polyinterp(x, y)
n = length(x)
h <- (x[n] - x[1])/500
t <- seq(x[1],x[n],h)
f <- horner(t,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)))
points(x,y,type = "p",col="blue",lwd=3)
legend("topleft",
legend = c("Data", "Polynomial"),
col=c("blue","red"),
lty=c(1,1) ) }
(p <- HornerPlot(xdata, ydata))
$rect
$rect$w
[1] 0.6564585
$rect$h
[1] 0.1165985
$rect$left
[1] -0.092
$rect$top
[1] 1.565608
$text
$text$x
[1] 0.1667501 0.1667501
$text$y
[1] 1.526742 1.487876
\[ \small{ \begin{aligned} f(x) & \cong f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \cdots + \frac{f^{(n)}(a)}{n!}(x-a)^n \\ R(x) & = \frac{(x-a)^{n+1}}{(n+1)!}f^{(n+1)}(c(x)), \end{aligned} } \]
where \( c(x) \) between \( a \) and \( x \).
Error formula is shown below, where \( c(x) \) between \( x_1 \) and \( x_n \).
\[ \small{ E(x) = \frac{(x-x_1)(x-x_2)\cdots (x-x_n)}{n!}f^{(n)}(c(x)) } \]
Vandermonde approach is convenient way of theoretically describing how to find interpolating polynomial, but computing with it is ill-conditioned; see weblink.
\[ p(x) = c_0 + c_1 x + \cdots + c_{N-1} x^{N-1} \]
[,1] [,2] [,3]
[1,] 1.00 2.00 3.00
[2,] 0.48 0.99 1.47
rrefmatrix(Ab)
[,1] [,2] [,3]
[1,] 1 0 1
[2,] 0 1 1
[,1] [,2] [,3]
[1,] 1.00 2.00 3.00
[2,] 0.49 0.99 1.47
rrefmatrix(Ab)
[,1] [,2] [,3]
[1,] 1 0 3
[2,] 0 1 0
Suppose we are given \( n \) data points
\[ \small{ (x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n) } \]
Define the Lagrange helping function \( L_k \) as follows:
\[ \small{ L_k = \frac{(x-x_0)\cdots (x-x_{k-1})(x-x_{k+1})\cdots (x-x_n)}{(x_k-x_0)\cdots (x_k-x_{k-1})(x_k-x_{k+1})\cdots (x_k-x_n)} } \]
Suppose we are given \( n \) data points
\[ \small{ (x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n) } \]
With the Lagrange helping functions
\[ \small{ L_k = \frac{(x-x_0)\cdots (x-x_{k-1})(x-x_{k+1})\cdots (x-x_n)}{(x_k-x_0)\cdots (x_k-x_{k-1})(x_k-x_{k+1})\cdots (x_k-x_n)}, } \]
the Lagrange interpolating polynomial is defined as
\[ \small{ P(x) = y_1 L_1(x) + y_2 L_2(x) + \ldots + y_n L_n(x) } \]
Suppose we are given the \( n = 3 \) data points below:
\[ \small{ (1,5), (2,6), (3,7) } \]
Lagrange helping functions:
\[ \small{ L_1 = \frac{(x-2)(x-3)}{(1-2)(1-3)}, \,\, L_2 = \frac{(x-1)(x-3)}{(2-1)(2-3)}, \,\, L_3 = \frac{(x-1)(x-2)}{(3-1)(3-2)} } \]
Lagrange interpolating polynomial:
\[ \small{ \begin{aligned} P(x) & = 5 \frac{(x-2)(x-3)}{(1-2)(1-3)} + 6 \frac{(x-1)(x-3)}{(2-1)(2-3)} + 7 \frac{(x-1)(x-2)}{(3-1)(3-2)} \\ & = x + 4 \,\, \mathrm{(after \, some \, algebra)} \end{aligned} } \]
Suppose we are given the \( n = 3 \) data points below:
\[ \small{ (2,16), (5,12), (7,25) } \]
Lagrange helping functions:
\( L_1= \)
\( L_2 = \)
\( L_3 = \)
Lagrange interpolating polynomial (do not simplify):
\( P(x)= \)
We are interested in estimating missing value at \( x = 0.9 \).
\( (a) \) Use \( x_3, x_4 \) to construct a degree one interpolation.
We are interested in estimating missing value at \( x = 0.9 \).
\( (b) \) Use \( x_2, x_3, x_4 \) to construct a degree two interpolation.
We are interested in estimating missing value at \( x = 0.9 \).
\( (c) \) Use \( x_1, x_2, x_3, x_4 \) to construct degree three interpolation.
library(pracma)
LagrangePlot <- function(x,y) {
n = length(x)
N <- 500
t <- linspace(x[1], x[n], N)
p <- lagrangeInterp(x, y, t)
bottom = min(0,min(p)); top = max(0,max(p))
plot(t,p,
main = "Lagrange Interpolating Polynomial",
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)}
x<-c(0.6,0.7,0.8,1.0)
y<-c(-0.18,0.014,0.22,0.66)
LagrangePlot(x,y)
x<-seq(1,10,1)
y<-sin(x)
LagrangePlot(x,y)
x<-seq(-5,5,1)
y<-exp(-x^2)
LagrangePlotEx5(x,y)