Lec. 3 Interpolation
Lec. 3 Interpolation
3.1 Introduction
We are given the values of a function 𝒇(𝒙) at different points
𝒙𝟎 , 𝒙𝟏 , … . . 𝒙𝒏 , we want to find approximate values of the function 𝒇(𝒙) for
“new” x’s that lie between these points for which the function values are
given, and this process is called interpolation.
Continuing our discussion, we write these given values of a function (𝑓) in
the form
𝑓0 = 𝑓(𝑥0 ), 𝑓1 = 𝑓(𝑥1 ), … . . 𝑓𝑛 = 𝑓(𝑥𝑛 )
Or as ordered pairs
(𝑥0 , 𝑓0 ), (𝑥1 , 𝑓1 ), … . . (𝑥𝑛 , 𝑓𝑛 )
These function values come from a “mathematical” function, such as
a logarithm or a Bessel function. More frequently, they may be measured or
automatically recorded values of an “empirical” function, such as air
resistance of a car or an airplane at different speeds. Other examples of
functions that are “empirical” are the yield of a chemical process at different
temperatures or the size of the Iraq population as it appears from censuses
taken at 10-year intervals.
𝑦(𝑥1 )
𝑦1 (𝑥)
𝑦(𝑥0 )
𝑥0 𝑥 𝑥1 𝑥
𝑦(𝑥1 ) − 𝑦(𝑥0 )
𝑦1 (𝑥) = 𝑦(𝑥0 ) + (𝑥 − 𝑥0 )
𝑥1 − 𝑥0
Example (3.1)
Estimate the natural logarithm of 2 using linear interpolation. First, perform
the computation by interpolating between 𝑙𝑛 (1) = 0 and ln(6) =
1.791759. Then, repeat the procedure, but use a smaller interval from
𝑙𝑛 (1) to 𝑙𝑛 (4) = 1.386294. Note that the true value of 𝑙𝑛 (2) is
0.6931472.
Solution
We use a linear interpolation for 𝑙𝑛(2) from 𝑥0 = 1 to 𝑥1 = 6 to give
𝑦(𝑥1 ) − 𝑦(𝑥0 )
𝑦1 (𝑥) = 𝑦(𝑥0 ) + (𝑥 − 𝑥0 )
𝑥1 − 𝑥0
1.791759 − 0
𝑦1 (2) = 0 + (2 − 1) = 0.3583519
6−1
This means the percentage error is 48.3%. Using a smaller interval from
𝑥0 = 1 to 𝑥1 = 4
1.386294 − 0
𝑦1 (2) = 0 + (2 − 1) = 0.4620981
4−1
Thus, using the shorter interval reduces the percent relative error to 33.3%.
3.3.2 Quadratic Interpolation
The error in Example (3.1) resulted from our approximating a curve with a
straight line is large. Consequently, a strategy for improving the estimate is
to introduce some curvature into the line connecting the points. If three data
points are available, this can be accomplished with a second-order
polynomial (also called a quadratic polynomial or a parabola). A
particularly convenient form for this purpose is
𝑦2 (𝑥) = 𝑏0 + 𝑏1 (𝑥 − 𝑥0 ) + 𝑏2 (𝑥 − 𝑥0 )(𝑥 − 𝑥1 )
Or
𝑦2 (𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2
Where
𝑎0 = 𝑏0 − 𝑏1 𝑥0 − 𝑏2 𝑥0 𝑥1
𝑎1 = 𝑏1 − 𝑏2 𝑥0 − 𝑏2 𝑥1
𝑎2 = 𝑏2
A simple procedure can be used to determine the values of the coefficients
𝑏0 = 𝑓(𝑥0 ) = 𝑦(𝑥0 )
𝑦(𝑥1 ) − 𝑦(𝑥0 )
𝑏1 =
𝑥1 − 𝑥0
𝑏1 = 𝑓(𝑥1 , 𝑥0 )
𝑏2 = 𝑓(𝑥2 , 𝑥1 , 𝑥0 )
𝑏𝑛 = 𝑓(𝑥𝑛 , 𝑥𝑛−1 , … , 𝑥1 , 𝑥0 )
𝑓(𝑥𝑖 ) − 𝑓(𝑥𝑗 )
𝑓[𝑥𝑖 , 𝑥𝑗 ] =
𝑥𝑖 − 𝑥𝑗
The second finite divided difference, which represents the difference of
two first divided differences, is expressed generally as
𝑓[𝑥𝑖 − 𝑥𝑗 ] − 𝑓[𝑥𝑗 − 𝑥𝑘 ]
𝑓[𝑥𝑖 , 𝑥𝑗 , 𝑥𝑘 ] =
𝑥𝑖 − 𝑥𝑘
Example (3.2)
Fit a second-order polynomial to the three points used in Example (3.1)
Solution
𝑥0 = 1 𝑦0 = 0
𝑥1 = 4 𝑦1 = 1.386294
𝑥2 = 6 𝑦2 = 1.791759
𝑏0 = 0
𝑦(𝑥1 ) − 𝑦(𝑥0 )
𝑏1 =
𝑥1 − 𝑥0
1.386294 − 0
𝑏1 = = 0.4620981
4−1
𝑦2 (𝑥) = 𝑏0 + 𝑏1 (𝑥 − 𝑥0 ) + 𝑏2 (𝑥 − 𝑥0 )(𝑥 − 𝑥1 )
𝑦2 (2) = 0 + 0.4620981(2 − 1) − 0.0518731(2 − 1)(2 − 4)
𝑦2 (2) = 0.5658444
Example (3.3)
Example (3.4)
𝑓𝑛 (𝑥) = ∑ 𝐿𝑖 (𝑥)𝑓(𝑥𝑖 )
𝑖=0
𝑛 𝑥 − 𝑥𝑖
𝐿𝑖 (𝑥) = ∏𝑗=0
𝑥𝑗 − 𝑥𝑖
𝑗≠𝑖
Where Π designates the “product of.” For example, the linear version (n = 1)
is
𝑥 − 𝑥1 𝑥 − 𝑥0
𝑓1 (𝑥) = 𝑓(𝑥0 ) + 𝑓(𝑥1 )
𝑥0 − 𝑥1 𝑥1 − 𝑥0
(𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) (𝑥 − 𝑥0 )(𝑥 − 𝑥2 )
𝑓2 (𝑥) = 𝑓(𝑥0 ) + 𝑓(𝑥1 )
(𝑥0 − 𝑥1 )(𝑥0 − 𝑥2 ) (𝑥1 − 𝑥0 )(𝑥1 − 𝑥2 )
(𝑥 − 𝑥0 )(𝑥 − 𝑥1 )
+ 𝑓(𝑥2 )
(𝑥2 − 𝑥0 )(𝑥2 − 𝑥1 )
Example (3-5)
Use Lagrange interpolating of the first order and second order to evaluate
𝑓(𝑥) at 𝑥 = 2 due to the following data.
𝑥0 = 1 𝑓(𝑥0 ) = 0
𝑥1 = 4 𝑓(𝑥1 ) = 1.3863
𝑥2 = 6 𝑓(𝑥2 ) = 1.7917
Solution:
The first order Lagrange interpolating is
𝑥 − 𝑥1 𝑥 − 𝑥0
𝑓1 (𝑥) = 𝑓(𝑥0 ) + 𝑓(𝑥1 )
𝑥0 − 𝑥1 𝑥1 − 𝑥0
At 𝑥 = 2
2−4 2−1
𝑓1 (2) = ∗0+ ∗ 1.3863
1−4 4−1
∴ 𝑓1 (2) = 0.4621
and held in place by weights. The mathematical idea of this method divided
into three types
1- Linear Spline
The simplest connection between two points is a straight line. The first-order
splines for a group of ordered data points can be defined as a set of linear
functions
𝑓(𝑥) = 𝑓(𝑥0 ) + 𝑚0 (𝑥 − 𝑥0 ) 𝑥0 ≤ 𝑥 ≤ 𝑥1
𝑓(𝑥) = 𝑓(𝑥1 ) + 𝑚1 (𝑥 − 𝑥1 ) 𝑥1 ≤ 𝑥 ≤ 𝑥2
.
.
.
𝑓(𝑥) = 𝑓(𝑥𝑛−1 ) + 𝑚𝑛−1 (𝑥 − 𝑥𝑛−1 ) 𝑥𝑛−1 ≤ 𝑥 ≤ 𝑥𝑛
Where mi is the slope of the straight line connecting the points:
𝑓(𝑥𝑖+1 ) − 𝑓(𝑥𝑖 )
𝑚𝑖 =
𝑥𝑖+1 − 𝑥𝑖
These equations can be used to evaluate the function at any point between x0
and xn by first locating the interval within which the point lies. Then the
appropriate equation is used to determine the function value within the
interval. The method is obviously identical to linear interpolation.
Example (3.6)
Fit the data in Table below with first-order splines. Evaluate the function at
𝑥=5
𝑋 𝐹(𝑥)
3.0 2.5
4.5 1.0
7.0 2.5
9.0 0.5
Solution
The data can be used to determine the slopes between points. For example,
for the interval 𝑥 = 4.5 to 𝑥 = 7 the slope can be computed
2.5 − 1
𝑚= = 0.6
7 − 4.5
The slopes for the other intervals can be computed, and the resulting first-
order splines are plotted in figure (3.3). The value at x = 5 is 1.3
Visual inspection of figure (3.3a) indicates that the primary disadvantage of
first-order splines is that they are not smooth. In essence, the slope changes
abruptly. In formal terms, the first derivative of the function is discontinuous
at these points. This deficiency is overcome by using higher-order
polynomial splines that ensure smoothness by equating derivatives at these
points, as discussed in the next section.
Figure (3.3) Spline fits of a set of four points. (a) Linear spline, (b)
quadratic spline, and (c) cubic spline, with a cubic interpolating
polynomial also plotted
2- Quadratic spline
To ensure that the nth derivatives are continuous at the points, a spline of at
least 𝑛 + 1 order must be used. Third-order polynomials or cubic splines
that ensure continuous first and second derivatives are most frequently used
in practice.
Because the derivation of cubic splines is somewhat involved, we have
chosen to include them in a subsequent section. We have decided to first
illustrate the concept of spline interpolation using second-order polynomials.
These “quadratic splines” have continuous first derivatives at the points.
Although quadratic splines do not ensure equal second derivatives at the
points, they serve nicely to demonstrate the general procedure for
developing higher-order splines.
The objective in quadratic splines is to derive a second-order polynomial for
each interval between data points. The polynomial for each interval can be
represented generally as
𝑓𝑖 (𝑥) = 𝑎𝑖 𝑥 2 + 𝑏𝑖 𝑥 + 𝑐𝑖
Figure (3.4) has been included to help clarify the notation. Notice that there
are (𝑛) intervals and (𝑛 + 1) data points. The example shown in the figure
is for 𝑛 = 3.
For n + 1 data points (i = 0, 1, 2, . . . , n), there are n intervals and,
consequently, 3n unknown constants (the a’s, b’s, and c’s) to evaluate.
Therefore, 3n equations or conditions are required to evaluate the
unknowns. These are:
2
𝑎𝑖 𝑥𝑖−1 + 𝑏𝑖 𝑥𝑖−1 + 𝑐𝑖 = 𝑓(𝑥𝑖−1 )
2- The first and last functions must pass through the end points. This
adds two additional equations:
𝑎1 𝑥02 + 𝑏1 𝑥0 + 𝑐1 = 𝑓(𝑥0 )
𝑎𝑛 𝑥𝑛2 + 𝑏𝑛 𝑥𝑛 + 𝑐𝑛 = 𝑓(𝑥𝑛 )
3- The first derivatives at the interior knots must be equal
𝑓 ′ (𝑥) = 2𝑎𝑥 + 𝑏
Therefore, the condition can be represented generally as
4- Assume that the second derivative is zero at the first point. Because
the second derivative is 2ai this condition can be expressed
mathematically as
𝑎1 = 0
The visual interpretation of this condition is that the first two points will be
connected by a straight line.
Example (3.7)
Fit quadratic splines to the same data used in Example (3.6). Use the results
to estimate the value at x = 5.
Solution
For the present problem, we have four data points and 𝑛 = 3 intervals.
Therefore, 3(3) = 9 unknowns must be determined. And 2(3) − 2 = 4
conditions
20.25𝑎1 + 4.5𝑏1 + 𝑐1 = 1.0
20.25𝑎2 + 4.5𝑏2 + 𝑐2 = 1.0
49𝑎2 + 7𝑏2 + 𝑐2 = 2.5
49𝑎3 + 7𝑏3 + 𝑐3 = 2.5
Passing the first and last functions through the initial and final values adds 2
more
9𝑎1 + 3𝑏1 + 𝑐1 = 2.5
81𝑎3 + 9𝑏3 + 𝑐3 = 0.5
Continuity of derivatives creates an additional 3 − 1 = 2
9𝑎1 + 𝑏1 = 9𝑎2 + 𝑏2
14𝑎2 + 𝑏2 = 14𝑎3 + 𝑏3
Finally, a1= 0. Because we know the value of a1 exactly, the problem
reduces to solving eight simultaneous equations. These conditions can be
expressed in matrix form as
These equations can be solved by using any method for solving a system of
simultaneous linear equations in chapter one
𝑎1 = 0 𝑏1 = −1 𝑐1 = 5.5
𝑎2 = 0.64 𝑏2 = −6.76 𝑐2 = 18.46
𝑎3 = −1.6 𝑏3 = 24.6 𝑐3 = −91.3
Which can be substituted into the original quadratic equations to develop the
following relationships for each interval:
𝑓1 (𝑥) = −𝑥 + 5.5 3.0 ≤ 𝑥 ≤ 4.5
𝑓2 (𝑥) = 0.64𝑥 2 − 6.7𝑥 + 18.46 4.5 ≤ 𝑥 ≤ 7.0
𝑓3 (𝑥) = −1.6𝑥 2 + 24.6𝑥 − 91.3 7.0 ≤ 𝑥 ≤ 9.0
When we use 𝑓2 , the prediction for 𝑥 = 5 is, therefore,
3- Cubic spline
The objective in cubic splines is to derive a third-order polynomial for each
interval between knots, as in
𝑓𝑖 (𝑥) = 𝑎𝑖 𝑥 3 + 𝑏𝑖 𝑥 2 + 𝑐𝑖 𝑥 + 𝑑𝑖
Thus, for n + 1 data points (i = 0, 1, 2, . . . , n), there are n intervals and,
consequently, 4n unknown constants to evaluate. Just as for quadratic
splines, 4n conditions are required to evaluate the unknowns. These are:
A. The function values must be equal at the interior knots (2n − 2
conditions).
B. The first and last functions must pass through the end points (2
conditions).
C. The first derivatives at the interior knots must be equal (n − 1
conditions).
D. The second derivatives at the interior knots must be equal (n − 1
conditions).
E. The second derivatives at the end knots are zero (2 conditions).
The visual interpretation of condition 5 is that the function becomes a
straight line at the end knots. Specification of such an end condition leads to
what is termed a “natural” spline. It is given this name because the drafting
spline naturally behaves in this fashion figure (3.5). If the value of the
second derivative at the end knots is nonzero (that is, there is some
curvature), this information can be used alternatively to supply the two final
conditions.
derivative at the first knot 𝑓𝑖′′ (𝑥𝑖−1 ) with the second derivative at the second
knot 𝑓𝑖′′ (𝑥).
Next, the equation above can be integrated twice to yield an expression for
fi(x). However, this expression will contain two unknown constants of
integration. These constants can be evaluated by invoking the function-
equality conditions 𝑓(𝑥) must equal 𝑓(𝑥𝑖−1 ) at 𝑥𝑖−1 and f (x) must equal f
(𝑥𝑖 ) at 𝑥𝑖 . By performing these evaluations, the following cubic equation
results:
𝑓𝑖′′ (𝑥𝑖−1 ) 𝑓𝑖′′ (𝑥𝑖 ) 𝑓(𝑥𝑖−1 )
𝑓𝑖 (𝑥) = (𝑥𝑖 − 𝑥)3 + (𝑥 − 𝑥𝑖−1 )3 + [ −
6(𝑥𝑖 −𝑥𝑖−1 ) 6(𝑥𝑖 −𝑥𝑖−1 ) 𝑥𝑖 −𝑥𝑖−1
This equation contains only two unknowns, the second derivatives at the end
of each interval. These unknowns can be evaluated using the following
equation:
𝑓 ′′ (𝑥𝑖−1 )(𝑥𝑖 − 𝑥𝑖−1 ) + 2𝑓 ′′ (𝑥𝑖 )(𝑥𝑖+1 − 𝑥𝑖−1 ) + 𝑓 ′′ (𝑥𝑖+1 )(𝑥𝑖+1 − 𝑥𝑖 ) =
6 6
[𝑓(𝑥𝑖+1 ) − 𝑓(𝑥𝑖 )] + [𝑓(𝑥𝑖−1 ) − 𝑓(𝑥𝑖 )] (D-3)
𝑥𝑖+1 −𝑥𝑖 𝑥𝑖 −𝑥𝑖−1
Example (3.8)
Fit cubic splines to the same data used in examples (3.6). Utilize the results
to estimate the value at x = 5.
Solution
The first step is to employ equation (D-3) to generate the set of simultaneous
equations that will be utilized to determine the second derivatives at the
knots. For example, for the first interior knot, the following data is used:
𝑥0 = 3 𝑓(𝑥0 ) = 2.5
𝑥1 = 4.5 𝑓(𝑥1 ) = 1
𝑥2 = 7 𝑓(𝑥2 ) = 2.5
These values can be substituted into equation (E-3) to yield
𝑓 ′′ (4.5) = 1.67909
𝑓 ′′ (7) = −1.53308
These values can then be substituted into equation (D-3), along with values
for the x’s and the f (x)’s, to yield
1.67909 2.5
𝑓1 (𝑥) = (𝑥 − 3)3 + (4.5 − 𝑥)
6(4.5 − 3) 4.5 − 3
1 1.67909(4.5 − 3)
+[ − ] (𝑥 − 3)
5.4 − 3 6
Or
𝑓1 (𝑥) = 0.186566(𝑥 − 3)3 + 1.666667(4.5 − 𝑥) + 0.246894(𝑥 − 3)
This equation is the cubic spline for the first interval. Similar substitutions
can be made to develop the equations for the second and third intervals:
𝑓2 (𝑥) = 0.111939(7 − 𝑥)3 − 0.102205(𝑥 − 4.5)3 − 0.299621(7 − 𝑥)
+ 1.638783(𝑥 − 4.5)
And
𝑓3 (𝑥) = −.127757(9 − 𝑥)3 + 1.761027(9 − 𝑥) + 0.25(𝑥 − 7)
The three equations can then be employed to compute values within each
interval. For example, the value at x = 5, which falls within the second
interval, is calculated as
𝑓2 (𝑥) = 0.111939(7 − 5)3 − 0.102205(5 − 4.5)3 − 0.299621(7 − 5)
+ 1.638783(5 − 4.5) = 1.102886
The results of examples (3.6) through (3.8) are summarized in figure (3.3).
Notice the progressive improvement of the fit as we move from linear to
quadratic to cubic splines. We have also superimposed a cubic interpolating
polynomial on figure (3.3c). Although the cubic spline consists of a series of
third-order curves, the resulting fit differs from that obtained using the third-
order polynomial. This is due to the fact that the natural spline requires zero
second derivatives at the end knots, whereas the cubic polynomial has no
such constraint.
H.W (4)
Develop quadratic splines for the data in the table below and predict f (3.4)
and f (2.2).
𝑥 𝐹(𝑥)
1.6 2
2 8
2.5 14
3.2 15
4 8
H.W (5)
Develop cubic splines for the data given below and (a) predict 𝑓 (4) and
𝑓 (2.5) and (b) verify that 𝑓2 (3) and 𝑓3 (3) = 19.
𝑥 𝐹(𝑥)
1 3
2 6
3 19
5 99
7 291
8 444