Lesson 5
Lesson 5
Numerical Methods in
Engineering
Part II
Lesson 5: “Interpolation”
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Outline
§ What is interpolation? What is it used for?
§ Lagrange interpolation
§ Newton’s divided differences
§ Approximating functions
§ Interpolation error
§ Hermite interpolation
§ Chebyshev interpolation
§ Cubic splines
2
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
3
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
4.182
4.181
4.179
4
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
5
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
(x1,y1)
(x2,y2)
The function P(x) interpolates the data points (xi,yi) if P(xi)=yi for each 1≤ i ≤ n
6
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Lagrange interpolation
§ Let us consider 𝑛 + 1 data points in the form 𝑥5 , 𝑦5 , with 𝑖 = 0, … , 𝑛, we
can find a polynomial of degree 𝑛 that interpolates<
these points as:
𝑃< 𝑥 = 𝑦= 𝐿= 𝑥 + ⋯ + 𝑦< 𝐿< 𝑥 = @ 𝑦5 𝐿5 𝑥
5A=
and the following Lagrange basis polynomials: <
𝑥 − 𝑥= ⋯ 𝑥 − 𝑥BCD 𝑥 − 𝑥BED ⋯ 𝑥 − 𝑥< 𝑥 − 𝑥G
𝐿B 𝑥 = =F
𝑥B − 𝑥= ⋯ 𝑥B − 𝑥BCD 𝑥B − 𝑥BED ⋯ 𝑥B − 𝑥< 𝑥B − 𝑥G
GHB
§ Note that 𝐿B 𝑥B = 1 and 𝐿B 𝑥G = 0, where 𝑥G is any of the other data
points.
§ The main theorem of polynomial interpolation reads that there exists one
and only one polynomial 𝑃 of degree 𝑛 or less that satisfies 𝑃 𝑥5 = 𝑦5 for
𝑖 = 0, … , 𝑛
7
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Lagrange interpolation
§ E.g., find the polynomial of degree 3 or less that interpolates the
points: 0,2 , 1,1 , 2,0 , 3, −1 :
8
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Lagrange interpolation
§ E.g., find the polynomial of degree 3 or less that interpolates the
points: 0,2 , 1,1 , 2,0 , 3, −1 :
§ 𝑃O 𝑥 = 𝑦= 𝐿= 𝑥 + 𝑦D 𝐿D 𝑥 + 𝑦N 𝐿N 𝑥 + 𝑦O 𝐿O 𝑥
D D
§ 𝑃O 𝑥 = 2 · − 𝑥 O − 6𝑥 N + 11𝑥 − 6 +1· 𝑥 O − 5𝑥 N + 6𝑥 +0·
P N
D D
− 𝑥O − 4𝑥 N + 3𝑥 −1· 𝑥 O − 3𝑥 N + 2𝑥
P P
§ 𝑃O 𝑥 = −𝑥 + 2
9
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Lagrange interpolation
Data points Lagrange basis Lagrange interpolation
10
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
𝑥= , 𝑓 𝑥= , 𝑥D , 𝑓 𝑥D , … , 𝑥< , 𝑓 𝑥<
𝑃< 𝑥
= 𝑓 𝑥= + 𝑓 𝑥= 𝑥D 𝑥 − 𝑥= + 𝑓 𝑥= 𝑥D 𝑥N 𝑥 − 𝑥= 𝑥 − 𝑥D
+ 𝑓 𝑥= … 𝑥< 𝑥 − 𝑥= 𝑥 − 𝑥D ⋯ 𝑥 − 𝑥<CD
11
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
12
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
𝑓 𝑥B = 𝑓 𝑥B
𝑓 𝑥BED − 𝑓 𝑥B
𝑓 𝑥B 𝑥BED =
𝑥BED − 𝑥B
13
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
for 𝑗 = 1, … , 𝑛
𝑓 𝑥G = 𝑦G
end
for 𝑖 = 2, … , 𝑛
for 𝑗 = 1, … , 𝑛 + 1 − 𝑖
𝑓 𝑥G ⋯ 𝑥GE5CD = 𝑓 𝑥GED ⋯ 𝑥GE5CD − 𝑓 𝑥G ⋯ 𝑥GE5CN / 𝑥GE5CD − 𝑥G
end
end
14
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
0 1
1/2
2 2 1/2 1 1
𝑃N 𝑥 = 1 + 𝑥 − 0 + 𝑥 − 0 𝑥 − 2
2 2 2
3 4 𝑃< 𝑥 = 𝑓 𝑥= + 𝑓 𝑥= 𝑥D 𝑥 − 𝑥= + 𝑓 𝑥= 𝑥D 𝑥N 𝑥 − 𝑥= 𝑥 − 𝑥D
15
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Approximating functions
§ Polynomial interpolation can be used to replace the evaluation
of a complicated function by an approximated polynomial (the
counterpart is, of course, a loss in accuracy).
Z NZ OZ
§ E.g. Interpolate the function 𝑓 𝑥 = sin 𝑥 at 𝑥 = 0, , ,
P P P
0 0
𝜋 0.9549
6 0.5 −0.2443
0.6990 −0.1139
2𝜋
0.8660 −0.4232
6
0.2559
3𝜋
1.0000
6
16
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Approximating functions
Z NZ OZ
§ E.g. Interpolate the function 𝑓 𝑥 = sin 𝑥 at 𝑥 = 0, , ,
P P P
sin 𝑥
𝑃O 𝑥
17
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Approximating functions
Z NZ OZ
§ E.g. Interpolate the function 𝑓 𝑥 = sin 𝑥 at 𝑥 = 0, , ,
P P P
§ The resulting polynomial is:
𝜋 𝜋 𝜋
𝑃O 𝑥 = 0 + 0.9549𝑥 − 0.2443𝑥 𝑥 − − 0.1139𝑥 𝑥 − 𝑥−
6 6 3
§ What happens when we evaluate 𝑓 𝑥 and 𝑃O 𝑥 at 𝑥 =
Z NZ OZ
0, , , ?
P P P
NZ
§ What happens when we evaluate 𝑓 𝑥 and 𝑃O 𝑥 at 𝑥 = ?
O
18
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Approximating functions
§ What happens when we evaluate 𝑓 𝑥 and 𝑃O 𝑥 at 𝑥 =
Z NZ OZ
0, , , ?
P P P
𝑥 𝑓 𝑥 𝑃O 𝑥 𝝐𝒂𝒃𝒔 = 𝑃O 𝑥 − 𝒇(𝒙)
0 0 0 0
𝜋 0.5 0.5 0
6
2𝜋 0.8660 0.8660 0
6
3𝜋 1.000 1.000 0
6
19
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Interpolation error
§ The accuracy of the interpolation depends on the
approximation chosen.
§ In the case of 𝑓 𝑥 = sin 𝑥 the third-order polynomial, 𝑃O 𝑥 ,
provides an acceptable approximation for the first two digits.
§ Thus, the error depends on the order of the approximation.
§ It is possible to provide an upper bound for the error and the
form of the interpolation error formula, provided we can
compute 𝑓 𝑥 − 𝑃< 𝑥 : <
𝑓 (<ED) 𝑐
𝑓 𝑥 − 𝑃< 𝑥 = F 𝑥 − 𝑥5 ·
𝑛+1 !
5A=
where 𝑐 lies between the smallest and largest of 𝑥5 , 𝑐 ∈ [𝑥= , 𝑥< ].
20
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Interpolation error
§ E.g., 𝑓 𝑥 = sin 𝑥, we can compute 𝑓 𝑥 − 𝑃O 𝑥 as:
𝜋 2𝜋 3𝜋 𝑓 iiii 𝑐
𝑓 𝑥 − 𝑃O 𝑥 = 𝑥−0 𝑥− 𝑥− 𝑥−
6 6 6 4!
§ We now that 𝑓 iiii 𝑥 = sin 𝑥 and max 𝑓 iiii 𝑐 = 1 (at 𝑐 = 0), thus:
𝜋 2𝜋 3𝜋 1
max 𝑓 𝑥 − 𝑃O 𝑥 = 𝑥 − 0 𝑥 − 𝑥− 𝑥−
6 6 6 4!
which is an upper bound:
𝜋 2𝜋 3𝜋 1
𝑓 𝑥 − 𝑃< 𝑥 ≤ 𝑥 − 0 𝑥 − 𝑥− 𝑥−
6 6 6 4!
21
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Interpolation error
§ This is, at 𝑥 = 1, the worst-case error is:
𝜋 2𝜋 3𝜋 1
sin 1 − 𝑃O 1 ≤ 1−0 1− 1− 1− ~0.0005
6 6 6 4!
22
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Hermite interpolation
§ As we have seen before, the interpolation polynomial fulfills the
values at the base points but may differ in between.
§ To improve the approximation, we may prescribe that their
derivatives, at the base points, also match.
§ Hermite interpolation constructs an interpolant based on
equations of the function values and their derivatives.
§ Thus, we need to know 𝑥5 , 𝑓 𝑥5 , 𝑓′ 𝑥5 , this is, 2𝑛 + 2 data
points.
§ For this reason, the polynomial is in the order of 2𝑛 + 1.
23
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Hermite interpolation
§ The Hermite polynomial reads then,
𝑃N<ED 𝑥
= 𝑓 𝑥= + 𝑓 𝑥= , 𝑥= 𝑥 − 𝑥= + 𝑓 𝑥= , 𝑥= , 𝑥D 𝑥 − 𝑥= N
+ 𝑓 𝑥= , 𝑥= , 𝑥D , 𝑥D 𝑥 − 𝑥= N 𝑥 − 𝑥D + ⋯
+ 𝑓 𝑥= , 𝑥= , 𝑥D , 𝑥D , … , 𝑥< , 𝑥< 𝑥 − 𝑥= N 𝑥 − 𝑥D N ⋯ 𝑥 − 𝑥<
24
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Hermite interpolation
§ E.g. Determine the Hermite interpolation polynomial for 𝑓 𝑥 =
ln 𝑥 at 𝑥= = 1 and 𝑥D = 2.
𝑓[𝑥] 𝑓[𝑥, 𝑥] 𝑓[𝑥, 𝑥, 𝑥] 𝑓[𝑥, 𝑥, 𝑥, 𝑥]
25
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Hermite interpolation
§ E.g. Determine the Hermite interpolation polynomial for 𝑓 𝑥 =
ln 𝑥 at 𝑥= = 1 and 𝑥D = 2.
§ Thus, the interpolation polynomial yields,
𝑃O 𝑥
= 𝑓 1 + 𝑓 1,1 𝑥 − 1 + 𝑓 1,1,2 𝑥 − 1 N
+ 𝑓 1,1,2,2 𝑥 − 1 N 𝑥 − 2
which is
N N
𝑃O 𝑥 = 𝑥 − 1 − 0.31 𝑥 − 1 + 0.12 𝑥 − 1 𝑥−2
26
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Chebyshev interpolation
§ The choice of the base points have a significant effect on the
interpolation error.
§ In order to minimize the interpolation error in the case of evenly-
spaced base points, we can use the Chebyshev interpolation.
§ The Chebyshev polynomial of degree 𝑛 reads as:
𝑇< 𝑥 = cos 𝑛 cos CD 𝑥
27
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Chebyshev interpolation
§ Also note that, if we set 𝑦 = cos CD 𝑥, we have:
28
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Chebyshev interpolation
§ The first Chebyshev polynomials are:
𝑇= 𝑥 = 1
𝑇D 𝑥 = 𝑥
𝑇N 𝑥 = 2𝑥 N − 1
𝑇O 𝑥 = 4𝑥 O − 3𝑥
29
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Cubic splines
§ So far, we have used polynomials of degree 𝑛 to interpolate 𝑛 +
1 points.
§ However, another perspective is to use several low-degree
polynomials to pass through the base points, in a total of 𝑛 − 1
segments.
y y
Cubic splines
§ Let us consider a cubic spline 𝑆 𝑥 through the data points
𝑥= , 𝑦= , 𝑥D , 𝑦D , … , 𝑥< , 𝑦< :
𝑆= 𝑥 = 𝑦= + 𝑏= 𝑥 − 𝑥= + 𝑐= 𝑥 − 𝑥= N + 𝑑= 𝑥 − 𝑥= O on 𝑥= , 𝑥D
𝑆D 𝑥 = 𝑦D + 𝑏D 𝑥 − 𝑥D + 𝑐D 𝑥 − 𝑥D N + 𝑑D 𝑥 − 𝑥D O on 𝑥D , 𝑥N
…
N O
𝑆<CD 𝑥 = 𝑦<CD + 𝑏<CD 𝑥 − 𝑥<CD + 𝑐<CD 𝑥 − 𝑥<CD + 𝑑<CD 𝑥 − 𝑥<CD on
𝑥<CD , 𝑥<
Cubic splines
§ Thus, constructing a cubic spline 𝑆 𝑥 from a set of data points
requires finding the coefficients 𝑏5 , 𝑐5 , and 𝑑5 in:
𝑆5 𝑥 = 𝑦5 + 𝑏5 𝑥 − 𝑥5 + 𝑐5 𝑥 − 𝑥5 N + 𝑑5 𝑥 − 𝑥5 O on 𝑥5 , 𝑥5ED
§ This makes a total of 𝟑𝒏 − 𝟑 unknowns, that must be solved by
enforcing the following restrictions:
§ 𝑆5 𝑥5 = 𝑦5 and 𝑆5 𝑥5ED = 𝑦5ED
§ 𝑆′5 𝑥5 = 𝑆′5ED 𝑥5
§ 𝑆′′5 𝑥5 = 𝑆′′5ED 𝑥5
§ In total, 𝟑𝒏 − 𝟓 constraints, thus the problem is underdetermined.
§ This is solved by adding two more restrictions: 𝑆′′= 𝑥= = 0 and
𝑆′′<CD 𝑥< = 0, becoming the so-called natural spline.
32
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Cubic splines
§ Instead of solving the equations by hand, with some algebra, we are able to write
the problem in the following matrix form:
0
1 0 0 𝑐= ∆D ∆=
𝑐D 3 −
𝛿= 2𝛿= + 2𝛿D 𝛿D ⋱ 𝛿D 𝛿=
0 𝛿D 2𝛿D + 2𝛿N 𝛿N
⋮ =
⋱ ⋱ ⋱ ⋱ ⋮
𝛿<CD 2𝛿<CD + 2𝛿< 𝛿< ∆<CD ∆<CN
𝑐 3 −
0 0 1 <CD 𝛿 <CD 𝛿<CN
0
with 𝛿5 = 𝑥5ED − 𝑥5 and ∆5 = 𝑦5ED − 𝑦5
33
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Cubic splines
Algorithm
for 𝑖 = 0, … , 𝑛
𝑎5 = 𝑦5
𝛿5 = 𝑥5ED − 𝑥5
∆5 = 𝑦5ED − 𝑦5
end
for 𝑖 = 0, … , 𝑛
}~•J C}~
𝑑5 =
O€~
∆~ €
𝑏5 = − ~ 2𝑐5 + 𝑐5ED
€~ O
end
34
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
𝑥 𝑥 𝑥 N 𝑥 O 𝑥 „
𝑦• = 5𝑡 0.2969 − 0.1260 − 0.3516 + 0.2843 − 0.1015
𝑐 𝑐 𝑐 𝑐 𝑐
where 𝑐 is the chord, 𝑥 is the position, 𝑦• is half the thickness and 𝑡 the maximum thickness.
35
Escuela Técnica Superior de Ingeniería Numerical Methods in
Universidad Loyola Andalucía Engineering
Lesson check
§ Interpolation: what is and what is it used for?
§ Lagrange polynomial basis
§ Newton’s divided difference
§ Approximation of functions and associated error
§ Hermite polynomial basis
§ Piecewise interpolation: cubic splines
37