Académique Documents
Professionnel Documents
Culture Documents
Interpolation
Given points
x0 , x1 , . . . , xn
and corresponding values
y0 , y 1 , . . . , y n
f (xi ) = yi , i = 0, . . . , n.
Interpolation of functions
f (x)
x0 , x1 , . . . , xn
f (x0 ), f (x1 ), . . . , f (xn )
p(xi ) = f (xi ), i = 0, . . . , n.
Linear interpolation
Given two sets of points (x0 , y0 ) and (x1 , y1 ) with x0 6= x1 , draw a line
through them, i.e., the graph of the linear polynomial
x − x1 x − x0
x0 x1 `(x) = y0 + y1
x0 − x1 x1 − x0
y0 y1
(x1 − x)y0 + (x − x0 )y1
`(x) = (5.1)
x1 − x0
We say that `(x) interpolates the value yi at the point xi , i = 0, 1, or
`(xi ) = yi , i = 0, 1
Example
Let the data points be (1, 1) and (4,2). The polynomial P1 (x) is given by
(4 − x) · 1 + (x − 1) · 2
P1 (x) = (5.2)
3
√
The graph y = P1 (x) and y = x, from which the data points were taken.
√
Figure: y = x and its linear interpolating polynomial (5.2)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.1 Linear interpolation
Example
Obtain an estimate of e0.826 using the function values
· ·
e0.82 = 2.270500, e0.83 = 2.293319
(x−x0 )(x−x2 )
L1 (x) = (x1 −x0 )(x1 −x2 ) (5.5)
(x−x0 )(x−x1 )
L2 (x) = (x2 −x0 )(x2 −x1 )
Example
Construct P2 (x) for the data points (0, −1), (1, −1), (2, 7). Then
With linear interpolation: obvious that there is only one straight line
passing through two given data points.
With three data points: only one quadratic interpolating polynomial
whose graph passes through the points.
Indeed: assume ∃Q2 (x), deg(Q2 ) ≤ 2 passing through
(xi , yi ), i = 0, 1, 2, then it is equal to P2 (x). The polynomial
Example
Calculate a quadratic interpolate to e0.826 from the function values
· · ·
e0.82 = 2.27050 e0.83 = 2.293319 e0.84 = 2.231637
With x0 = e0.82 , x1 = e0.83 , x2 = e0.84 , we have
·
P2 (0.826) = 2.2841639
·
to eight digits, while the true answer e0.826 = 2.2841638 and
·
P1 (0.826) = 2.2841914.
Lagrange’s Formula
f (x1 ) − f (x2 )
f [x0 , x1 ] = (5.8)
x1 − x2
Example
Let f (x) = cos(x), x0 = 0.2, x1 = 0.3.
Then
cos(0.3) − cos(0.2) ·
f [x0 , x1 ] = = −0.2473009 (5.9)
0.3 − 0.2
while
0 x0 + x1 ·
f = − sin(0.25) = −0.2474040
2
so f [x0 , x1 ] is a very good approximation of this derivative.
Theorem
Let n ≥ 1, f ∈ C n [α, β] and x0 , x1 , . . . , xn n + 1 distinct numbers in [α, β].
Then 1 (n)
f [x0 , x1 , . . . , xn ] = n! f (c) (5.13)
for some unknown point c between the maximum and the minimum of
x0 , . . . , xn .
Example
Let f (x) = cos(x), x0 = 0.2, x1 = 0.3, x2 = 0.4.
The f [x0 , x1 ] is given by (5.9), and
cos(0.4) − cos(0.3) ·
f [x1 , x2 ] = = −0.3427550
0.4 − 0.3
hence from (5.11)
· −0.3427550 − (−0.2473009)
f [x0 , x1 , x2 ] = = −0.4772705 (5.14)
0.4 − 0.2
For n = 2, (5.13) becomes
1 1 1 ·
f [x0 , x1 , x2 ] = f 00 (c) = − cos(c) ≈ − cos(0.3) = −0.4776682
2 2 2
which is nearly equal to the result in (5.14).
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.5 Properties of divided differences
The divided differences (5.12) have special properties that help simplify work
with them.
(1) Let (i0 , i1 , . . . , in ) be a permutation (rearrangement) of the integers
(0, 1, . . . , n). It can be shown that
(2) The definitions (5.8), (5.11) and (5.12) extend to the case where some or
all of the xi coincide, provided that f (x) is sufficiently differentiable.
f (x0 ) − f (x1 )
f [x0 , x0 ] = lim f [x0 , x1 ] = lim
x1 →x0 x1 →x0 x1 − x0
f [x0 , x0 ] = f 0 (x0 )
We can use the MATLAB function divdif using the function call
divdif y = divdif(x nodes, y values)
Note that MATLAB does not allow zero subscripts, hence x nodes and
y values have to be redefined as vectors containing n + 1 components:
x nodes = [x0 , x1 , . . . , xn ]
x nodes(i) = xi−1 , i = 1, . . . , n + 1
y values = [f (x0 ), f (x1 ), . . . , f (xn )]
y values(i) = f (xi−1 ), i = 1, . . . , n + 1
i xi cos(xi ) Di
0 0.0 1.000000 0.1000000E+1
1 0.1 0.980067 -0.9966711E-1
2 0.4 0.921061 -0.4884020E+0
3 0.6 0.825336 0.4900763E-1
4 0.8 0.696707 0.3812246E-1
5 1.0 0.540302 -0.3962047E-2
6 1.2 0.36358 -0.1134890E-2
Interpolation of f at x0 , x1 , . . . , xn
Idea: use Pn−1 (x) in the definition of Pn (x):
Example
Example
Let f (x) = cos(x). The previous table contains a set of nodes xi , the values
f (xi ) and the divided differences computed with divdif
Di = f [x0 , . . . , xi ] i ≥ 0
n
X
Pn (x) = f (xj )Lj (x)
j=0
Theorem
Let n ≥ 0, f ∈ C n+1 [a, b], x0 , x1 , . . . , xn distinct points in [a, b]. Then
Proof of Theorem
Fix t ∈ R. Consider
From Newton
Let x = t.
Examples
f (x) = xn , f [x0 , . . . , xn ] = n!
n! = 1 or
n
pn (x; f ) = 1 · x ⇒ f [x0 , . . . , xn ] = 1
f (x) ∈ Pn−1 , f [x0 , x1 , . . . , xn ] = 0
f (x) = xn+1 , f [x0 , . . . , xn ] = x0 + x1 + . . . + xn
Example
Take f (x) = ex , x ∈ [0, 1] and consider the error in linear interpolation
to f (x) using x0 , x1 satisfying 0 ≤ x0 ≤ x1 ≤ 1.
From (5.23)
(x − x0 )(x − x1 ) cx
ex − P1 (x) = e
2
for some cx between the max and min of x0 , x1 and x. Assuming
x0 < x < x1 , the error is negative and approximately a quadratic
polynomial
(x1 − x)(x − x0 ) cx
ex − P1 (x) = − e
2
Since x0 ≤ cx ≤ x1
(x1 − x)(x − x0 ) x0 (x1 − x)(x − x0 ) x1
e ≤ |ex − P1 (x)| = e
2 2
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
(x1 − x)(x − x0 ) h2
max = , h = x1 − x0
x0 ≤x≤x1 2 8
h2
|ex − P1 (x)| ≤ e, 0 ≤ x0 ≤ x ≤ x1 ≤ 1
8
independent of x, x0 , x1 .
·
Recall that we estimated e0.826 = 2.2841914 by e0.82 and e0.83 , i.e.,
h = 0.01.
h2 0.012
|ex − P1 (x)| ≤ e = |ex − P1 (x)| ≤ 2.72 = 0.0000340,
8 8
The actual error being −0.0000276, it satisfies this bound.
Example
Again let f (x) = ex on [0, 1], but consider the quadratic interpolation.
(x − x0 )(x − x1 )(x − x2 ) cx
ex − P2 (x) = e
6
for some cx between the min and max of x0 , x1 , x2 and x.
Assuming evenly spaced points, h = x1 − x0 = x2 − x1 , and
0 ≤ x0 < x < x2 ≤ 1, we have as before
x
(x − x0 )(x − x1 )(x − x2 ) 1
|e − P2 (x)| ≤
e
6
while
(x − x0 )(x − x1 )(x − x2 ) h3
max = √ (5.24)
x0 ≤x≤x2 6 9 3
hence
h3 e
|ex − P2 (x)| ≤ √ ≈ 0.174h3
9 3
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
For h = 0.01, 0 ≤ x ≤ 1
Let (x + h)x(x − h) x3 − xh
w2 (x) = =
6 6
a translation along x-axis of polynomial in (5.24). Then x = ± √h3 satisfies
3x2 − h2 h3
h
0 = w20 (x) =
and gives w2 ± √ = √
6 3 9 3
Figure: y = w2 (x)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2.2 Behaviour of the error
ψn (x) = (x − x0 ) · · · (x − xn )
Figure: y = Ψ6 (x)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2.2 Behaviour of the error
Example
1 10
Le f (x) = 1+x2 , x ∈ [−5, 5], n > 0 an even integer, h = n
xj = −5 + jh, j = 0, 1, 2, . . . , n
It can be shown that for many points x in [−5, 5], the sequence of {Pn (x)}
does not converge to f (x) as n → ∞.
1
Figure: The interpolation to 1+x2
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3 Interpolation using spline functions
x 0 1 2 2.5 3 3.5 4
y 2.5 0.5 0.5 1.5 1.5 1.125 0
The simplest method of interpolating data in a table: connecting the node
points by straight lines: the curve y = `(x) is not very smooth.
a = x1 < . . . < xn = b
S(xi ) = yi , i = 1, . . . , n
S(xi ) = yi : n constraints
S 0 (xi − 0) = S 0 (xi + 0)
continuity 2 = 1, . . . , n − 1
S 00 (xi − 0) = S 00 (xi + 0) i = 2, . . . , n − 1 3n − 6
S(xi − 0) = S(xi + 0) i = 2, . . . , n − 1
n + (3n − 6) = 4n − 6 constraints
S 00 = 0 at a = x1 , xn = b,
Formula (5.26) implies that S(x) is continuous over all [a, b] and
satisfies the interpolating conditions S(xi ) = yi .
Similarly, formula (5.25) for S 00 (x) implies that is continuous on [a, b].
To ensure the continuity of S 0 (x) over [a, b]: we require S 00 (x) on
[xj−1 , xj ] and [xj , xj+1 ] have to give the same value at their common
pointxj , j = 2, 3, . . . , n − 1, leading to the tridiagonal linear system
xj − xj−1 xj+1 − xj−1 xj+1 − xj
Mj−1 + Mj + Mj+1 (5.27)
6 3 6
yj+1 − yj yj − yj−1
= − , j = 2, 3, . . . , n − 1
xj+1 − xj xj − xj−1
M1 = Mn = 0 (5.28)
Example
Calculate the natural cubic spline interpolating the data
1 1 1
(1, 1), (2, ), (3, ), (4, )
2 3 4
Example
Calculate the natural cubic spline interpolating the data
x 0 1 2 2.5 3 3.5 4
y 2.5 0.5 0.5 1.5 1.5 1.125 0
n = 7, system (5.27) has 5 equations
where c depends on f 00 (a), f 00 (b) and maxa≤x≤b |f (4) (x)|. The reason
why Sn (x) doesn’t converge more rapidly (have an error bound with a
higher power of h) is that f 00 (a) 6= 0 6= f 00 (b), while by definition
Sn00 (a) = Sn00 (b) = 0. For functions f (x) with f 00 (a) = f 00 (b) = 0, the
RHS of (5.29) can be replaced with ch4 .
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.3 Other interpolation spline functions
S(xi ) = yi , i = 1, . . . , n
the representation formula (5.26) and the tridiagonal system (5.27) are
still valid.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.3 Other interpolation spline functions
and f (z1 ), f (z2 ) are known. Then use the formula for S(x) in (5.26)
and
This adds two new equations to the system (5.27), one for M1 and
M2 , and another equation for Mn−1 , Mn . This form is preferable to
the interpolating natural cubic spline, and is almost equally easy to
produce. This is the default form of spline interpolation that is
implemented in MATLAB. The form of spline formed in this way is
said to satisfy the not-a-knot interpolation boundary conditions.
which produces the cubic spline function S(x) whose graph passes
through the points {(ξi , ηi ) : i = 1, . . . , n} with
Example
Approximate the function f (x) = ex on the interval [a, b] = [0, 1]. For
n > 0, define h = 1/n and interpolating nodes
x1 = 0, x2 = h, x3 = 2h, . . . , xn+1 = nh = 1
Example
Two interpolating nodes are inserted, the midpoints of the subintervals [0, h]
and [1 − h, 1]:
1 1
x1 = 0, x2 = h, x3 = h, x4 = 2h, . . . , xn+1 = (n−1)h, xn+2 = 1− h, xn+3 = 1
2 2
Using spline results in a cubic spline function Sn,2 (x); with the not-a-knot
interpolation conditions conditions, the nodes x2 and xn+2 are the points z1
and z2 of (5.30). Generally, Sn,2 is a more accurate approximation than is
Sn,1 (x).
The cubic polynomials produced for Sn,2 (x) by spline for the intervals
[x1 , x2 ] and [x2 , x3 ] are the same, thus we can use the polynomial for [0, 21 h]
for the entire interval [0, h]. Similarly for [1 − h, h].
(1) (2)
n En Ratio En Ratio
5 1.01E-4 1.11E-5
10 6.92E-6 14.6 7.88E-7 14.1
20 4.56E-7 15.2 5.26E-8 15.0
40 2.92E-8 15.6 3.39E-9 15.5
Table: Cubic spline approximation to f (x) = ex
4. Interpolation Math 1070