Polynomial Interpolation The problem: we know a function's value at some points, $(x_0, y_0)$, $(x_1, y_1), \dots, (x_n, y_n)$. How can we approximate that function?
There are many functions that can go through a sequence of points, the problem is not unique in general.
However (p309)
There is a unique polynomial of degree $n$ going through the $n+1$ points. (We assume the $x_i$ are real.)
Uniqueness Well, suppose there are two such polynomials $p(x)$ and $q(x)$. Then we have $h(x) = p(x) - q(x)$. It has degree at most $n$, so there are no more than $n$ roots. But $h(x_i) = p(x_i) - q(x_i) = y_i - y_i = 0$. So there are $n+1$ roots, hence a contradiction.
Existence For existence, we can use induction.
For $n=0$ we just have a constant $p_0(x) = y_1$ and then $p_0(x_1) = y_1$ too.
$$n=1$$
. Then we have two points and we can make the line between them. The point slope form is helpful, it would look like:
$$~
p_1(x) = x_0 + \frac{y_1 - y_0}{x_1 - x_0} (x - x_0).
~$$
Assuming that the statement is true for $k$, can we show it is true for $k+1$.
First form $p_{k-1}$ with degree $\leq k-1$ from the first $k-1$ points. Then we write the following formally:
$$~
p_k = p_{k-1}(x) + c(x-x_0)(x-x_1)\cdots (x - x_{k-1})
~$$
For some $c$ we will show that this polynomial has degree $\leq k$ and $p_k(x_i) =y_i$ for all $i$ between $0$ and $k$.
The term added is an $k$th degree polynomial. So $p_k$ is a polynomial of degree $\leq k$.
The term added is $0$ at each of $x_0, \dots, x_{k-1}$, so for these values
$$~
p_k(x_i) = p_{k-1}(x_i) + 0 = y_i.
~$$
Finally, $p_k(x_n) = p_{k-1}(x_k) + c \prod_{i=0}^{k-1}(x_k - x_i)$
Setting this to $y_k$ gives:
$$~
y_k= p_{k-1}(x_k) + c \prod_{i=0}^{k-1}(x_k - x_i)
~$$
Or
$$~
c = \frac{y_k - p_{k-1}(x_k)}{\prod_{i=0}^{k-1}(x_k - x_i)}.
~$$
The divisor is clearly not $0$, so this defines $c$ and we have found $p_k$.
The form of the polynomial We have just seen that
$$~
p_k(x) = c_0 + c_1(x-x_0) + c_2(x-x_0)(x-x_1) + \cdots + c_k(x-x_0)\cdots(x-x_{k-1}) =
\sum_{i=0}^{k} c_i \prod_{j=0}^{i-1} (x - x_j).
~$$
where
$$~
c_k = \frac{y_k - p_{k-1}(x_k)}{\prod_{i=0}^{k-1}(x_k - x_i)}.
~$$
Alternate derivations Suppose we have a polynomial $p_k(x) = a_0 + a_1x + a_2x^2 + \cdots a_k x^k$ which satisfies $p_k(x_i) = y_i$. Then this leads to a system of equations:
$$~
\begin{array}{cc}
&a_0 \cdot 1 + a_1 x_0 + a_2 x_0^2 + \cdots + a_k x_0^k = y_0\\
&a_0 \cdot 1 + a_1 x_1 + a_2 x_1^2 + \cdots + a_k x_1^k = y_1\\
&\cdots\\
&a_0 \cdot 1 + a_1 x_k + a_2 x_k^2 + \cdots + a_k x_k^k = y_k
\end{array}
~$$
This is a linear system of $k+1$ equations in $k+1$ unknowns. If the coefficient matrix is nonsingular, it has solution $A^{-1}y$.
For example
using SymPy
k=2
xs = Sym[Sym("x"*string(i)) for i in 0:k]
ys = Sym[Sym("y"*string(i)) for i in 0:k]
as = Sym[Sym("a"*string(i)) for i in 0:k]
x = symbols("x")
p = sum([as[i+1] * x^i for i in 0:k])
eqs = Sym[subs(p, x, u) - v for (u,v) in zip(xs, ys)]
\begin{bmatrix}a_{0} + a_{1} x_{0} + a_{2} x_{0}^{2} - y_{0}\\a_{0} + a_{1} x_{1} + a_{2} x_{1}^{2} - y_{1}\\a_{0} + a_{1} x_{2} + a_{2} x_{2}^{2} - y_{2}\end{bmatrix}
We can solve these (they are linear after all):
us = solve(eqs, as)
\begin{equation*}\begin{cases}a_{0} & \text{=>} &\frac{x_{0} x_{1} y_{2} \left(x_{0} - x_{1}\right) - x_{0} x_{2} y_{1} \left(x_{0} - x_{2}\right) + x_{1} x_{2} y_{0} \left(x_{1} - x_{2}\right)}{x_{0}^{2} x_{1} - x_{0}^{2} x_{2} - x_{0} x_{1}^{2} + x_{0} x_{2}^{2} + x_{1}^{2} x_{2} - x_{1} x_{2}^{2}}\\a_{2} & \text{=>} &\frac{y_{0} \left(x_{1} - x_{2}\right) - y_{1} \left(x_{0} - x_{2}\right) + y_{2} \left(x_{0} - x_{1}\right)}{x_{0}^{2} x_{1} - x_{0}^{2} x_{2} - x_{0} x_{1}^{2} + x_{0} x_{2}^{2} + x_{1}^{2} x_{2} - x_{1} x_{2}^{2}}\\a_{1} & \text{=>} &\frac{- y_{0} \left(x_{1}^{2} - x_{2}^{2}\right) + y_{1} \left(x_{0}^{2} - x_{2}^{2}\right) - y_{2} \left(x_{0}^{2} - x_{1}^{2}\right)}{x_{0}^{2} x_{1} - x_{0}^{2} x_{2} - x_{0} x_{1}^{2} + x_{0} x_{2}^{2} + x_{1}^{2} x_{2} - x_{1} x_{2}^{2}}\\\end{cases}\end{equation*}
But it isn't pretty and doesn't get prettier with factoring
Sym[factor(v) for (k,v) in us]
\begin{bmatrix}\frac{1}{\left(x_{0} - x_{1}\right) \left(x_{0} - x_{2}\right) \left(x_{1} - x_{2}\right)} \left(x_{0}^{2} x_{1} y_{2} - x_{0}^{2} x_{2} y_{1} - x_{0} x_{1}^{2} y_{2} + x_{0} x_{2}^{2} y_{1} + x_{1}^{2} x_{2} y_{0} - x_{1} x_{2}^{2} y_{0}\right)\\- \frac{x_{0} y_{1} - x_{0} y_{2} - x_{1} y_{0} + x_{1} y_{2} + x_{2} y_{0} - x_{2} y_{1}}{\left(x_{0} - x_{1}\right) \left(x_{0} - x_{2}\right) \left(x_{1} - x_{2}\right)}\\\frac{x_{0}^{2} y_{1} - x_{0}^{2} y_{2} - x_{1}^{2} y_{0} + x_{1}^{2} y_{2} + x_{2}^{2} y_{0} - x_{2}^{2} y_{1}}{\left(x_{0} - x_{1}\right) \left(x_{0} - x_{2}\right) \left(x_{1} - x_{2}\right)}\end{bmatrix}
Divided differences This is an alternate way of viewing the complicated coefficients. Note the following:
For $k=0$ we had $p_0(x) = f(x_0)$
For $k=1$ we hade $p_1(x) = f(x_0) + (f(x_1) - f(x_0)) / (x_1 - x_0) (x - x_0)$.
Letting $f[x_0] = f(x_0)$ and f[x_0, x_1] = (f(x_1) - f(x_0)) / (x_1 - x_0) $.
Then these become
$$p_0(x) = f[x_0]$$
, $p_1(x) = f[x_0] + f[x_0,x_1](x-x_0)$.
In general we define the interpolating polynomials through
$$~
f(x) = \sum_{k=0}^n f[x_0, x_1, \dots, x_k] \prod_{j=1}^{k-1}(x-x_j).
~$$
Then
Thm (p330)
$$~
f[x_0, x_1, \dots, x_n] = \frac{f[x_1, x_2,\dots,x_n] - f[x_0, x_1, \dots, x_{n-1}]}{x_n-x_0}.
~$$
These are like derivatives. But not quite.
Thm (p333) If $a \leq x_i \leq b$, then there exists $\xi$ in $[a,b]$ with
$$~
f[x_0, x_1, \dots,x_n] = \frac{1}{n!}f^{(n)}(\xi).
~$$
Error Let $f$ be continuous of degreen $n+1$ over $[a,b]$ and $p$ be a polynomial interpolating $p$ at $n+1$ distinct points. Then for each $x$ there exists $\xi$ with
$$~
f(x) - p(x) = \frac{1}{(n+1)!} f^{(n+1)}(\xi) \prod_{i=0}^n (x - x_i).
~$$
Example For the function $f(x) = \cos(x)$ over the interval $[0, \pi/2]$ if $k=4+1$ points are chosen and a polynomial interpolates, then the difference is
$$~
\cos(x) - p(x) = \frac{1}{120} f^{(5)}(\xi) \prod_0^4(x-x_i)
~$$
This can be as large (in absolute value) as:
$$~
|\cos(x) - p(x)| \leq \frac{1}{120} \cdot 1 \cdot (\pi/2)^5 = 0.07969262624616703
~$$
This is a worst case. We can graph to see the difference.
function dd(f,xs)
if length(xs) == 1
f(xs[1])
else
(dd(f,xs[2:end]) - dd(f, xs[1:end-1])) / (maximum(xs) - minimum(xs))
end
end
dd (generic function with 1 method)
n = 4
f(x) = cos(x)
a,b = 0, pi/2
xs = a + sort(rand(n+1)) * b
## prod_j=0^(k-1)(x-x_j)
prodk(x, k, xs) = prod([ x - xs[j+1] for j in 0:(k-1)])
## sum _(k=0)^n f[x_0, \dots x_k] prod_j=0^(k-1)(x-x_j)
p4(x) = f(xs[1]) + sum( [ dd(f, xs[1:(k+1)]) * prodk(x, k, xs) for k in 1:n])
p4 (generic function with 1 method)
To visualize we try:
using Plots
backend(:gadfly)
plot([f, p4], 0, pi/2)
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
-2.0
-1.9
-1.8
-1.7
-1.6
-1.5
-1.4
-1.3
-1.2
-1.1
-1.0
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2.0
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3.0
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4.0
-2
0
2
4
-2.0
-1.8
-1.6
-1.4
-1.2
-1.0
-0.8
-0.6
-0.4
-0.2
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
1.6
1.8
2.0
2.2
2.4
2.6
2.8
3.0
3.2
3.4
3.6
3.8
4.0
y1
y2
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
-1.00
-0.95
-0.90
-0.85
-0.80
-0.75
-0.70
-0.65
-0.60
-0.55
-0.50
-0.45
-0.40
-0.35
-0.30
-0.25
-0.20
-0.15
-0.10
-0.05
0.00
0.05
0.10
0.15
0.20
0.25
0.30
0.35
0.40
0.45
0.50
0.55
0.60
0.65
0.70
0.75
0.80
0.85
0.90
0.95
1.00
1.05
1.10
1.15
1.20
1.25
1.30
1.35
1.40
1.45
1.50
1.55
1.60
1.65
1.70
1.75
1.80
1.85
1.90
1.95
2.00
-1
0
1
2
-1.0
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2.0
Not too illuminating, so we try this:
plot(x -> f(x) - p4(x), 0, pi/2)
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
-2.0
-1.9
-1.8
-1.7
-1.6
-1.5
-1.4
-1.3
-1.2
-1.1
-1.0
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2.0
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3.0
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4.0
-2
0
2
4
-2.0
-1.8
-1.6
-1.4
-1.2
-1.0
-0.8
-0.6
-0.4
-0.2
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
1.6
1.8
2.0
2.2
2.4
2.6
2.8
3.0
3.2
3.4
3.6
3.8
4.0
y1
-0.0016
-0.0014
-0.0012
-0.0010
-0.0008
-0.0006
-0.0004
-0.0002
0.0000
0.0002
0.0004
0.0006
0.0008
0.0010
0.0012
-0.00145
-0.00140
-0.00135
-0.00130
-0.00125
-0.00120
-0.00115
-0.00110
-0.00105
-0.00100
-0.00095
-0.00090
-0.00085
-0.00080
-0.00075
-0.00070
-0.00065
-0.00060
-0.00055
-0.00050
-0.00045
-0.00040
-0.00035
-0.00030
-0.00025
-0.00020
-0.00015
-0.00010
-0.00005
0.00000
0.00005
0.00010
0.00015
0.00020
0.00025
0.00030
0.00035
0.00040
0.00045
0.00050
0.00055
0.00060
0.00065
0.00070
0.00075
0.00080
0.00085
0.00090
0.00095
0.00100
-0.002
-0.001
0.000
0.001
-0.00145
-0.00140
-0.00135
-0.00130
-0.00125
-0.00120
-0.00115
-0.00110
-0.00105
-0.00100
-0.00095
-0.00090
-0.00085
-0.00080
-0.00075
-0.00070
-0.00065
-0.00060
-0.00055
-0.00050
-0.00045
-0.00040
-0.00035
-0.00030
-0.00025
-0.00020
-0.00015
-0.00010
-0.00005
0.00000
0.00005
0.00010
0.00015
0.00020
0.00025
0.00030
0.00035
0.00040
0.00045
0.00050
0.00055
0.00060
0.00065
0.00070
0.00075
0.00080
0.00085
0.00090
0.00095
0.00100
So the actual difference is not as large as is possible $0.079$.
The errors can grow! The bound on the error depends on the derivatives of the function. For some functions, these may get big, so it isn't the case that we definitely know that $\| f -p_n \|_\infty \rightarrow 0$.
p319: "The surprising state of affairs is that for most continuous functions the quantity will not converge to 0"
Example, (Runge 1901). Let $f(x) = 1/(x^2+1)$ and $[a,b] = [-5,5]$. We will use evenly space points and have a look:
f(x) = 1/(x^2 + 1)
a,b = -5, 5
function show_n(n)
xs = linspace(a, b, n + 1 + 2)[2:end-1]
ip(x) = f(xs[1]) + sum( [ dd(f, xs[1:(k+1)]) * prodk(x, k, xs) for k in 1:n])
Gadfly.plot([f,ip], a, b)
end
show_n(2)
x
-20
-15
-10
-5
0
5
10
15
20
-15.0
-14.5
-14.0
-13.5
-13.0
-12.5
-12.0
-11.5
-11.0
-10.5
-10.0
-9.5
-9.0
-8.5
-8.0
-7.5
-7.0
-6.5
-6.0
-5.5
-5.0
-4.5
-4.0
-3.5
-3.0
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
12.5
13.0
13.5
14.0
14.5
15.0
-20
-10
0
10
20
-15
-14
-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
f1
f2
Color
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
-7.0
-6.8
-6.6
-6.4
-6.2
-6.0
-5.8
-5.6
-5.4
-5.2
-5.0
-4.8
-4.6
-4.4
-4.2
-4.0
-3.8
-3.6
-3.4
-3.2
-3.0
-2.8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15
-10
-5
0
5
10
15
20
-15.0
-14.5
-14.0
-13.5
-13.0
-12.5
-12.0
-11.5
-11.0
-10.5
-10.0
-9.5
-9.0
-8.5
-8.0
-7.5
-7.0
-6.5
-6.0
-5.5
-5.0
-4.5
-4.0
-3.5
-3.0
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
12.5
13.0
13.5
14.0
14.5
15.0
-20
-10
0
10
20
-15
-14
-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
f1
f2
Color
-500
-450
-400
-350
-300
-250
-200
-150
-100
-50
0
50
100
150
200
250
300
350
-450
-440
-430
-420
-410
-400
-390
-380
-370
-360
-350
-340
-330
-320
-310
-300
-290
-280
-270
-260
-250
-240
-230
-220
-210
-200
-190
-180
-170
-160
-150
-140
-130
-120
-110
-100
-90
-80
-70
-60
-50
-40
-30
-20
-10
0
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
-500
0
500
-460
-440
-420
-400
-380
-360
-340
-320
-300
-280
-260
-240
-220
-200
-180
-160
-140
-120
-100
-80
-60
-40
-20
0
20
40
60
80
100
120
140
160
180
200
220
240
260
280
300
f(x)