Interpolation Techniques
Background and implementation in C++
Pedagogical notes
2016
Quasar Chunawala
Introduction
Say that, in the study of some phenomenon, there is an established functional
relationship between the quantities y and x; but the function f(x) is unknown.
The experiment has established the values of the function y0, y1, . . . , yN for cer-
tain values of the argument x0, x1, . . . , xN in the interval [x0, xN ]. We don't
have an analytic expression for f(x). The problem is then to nd a function (as
simple as possible from the computational stand-point; for example a polyno-
mial) which will represent the unknown function y = f(x).
In more abstract fashion, the problem may be formulated as follows: given on
the interval [x0, xN ], the values of an unknown function y = f(x) at N + 1
distinct points x0, x1, . . . , xN , such that,
y0 = f(x0), y1 = f(x1), . . . , yN = f(xN )
It is required to nd a polynomial P(x) of degree≤ n that approximately
expresses the function f(x). Further, the task is to estimate f(x) for some
target value of x.
Interpolating polynomial given one data point and
its higher order derivatives
Say that, we are required to t a polynomial P(x), at a point x = x0, where
the value of the function and the value of its rst n derivatives at that point
f(x0), f (x), f(2)
(x), . . . , f(n)
(x) are given. Then, the Taylor's series expansion
of the function in terms of a polynomial of degree n over the interval [x0, x] is
the interpolating polynomial. This is intuitive, because the Taylor's series
polynomial and its higher order derivatives have the same values as the function
and its derivatives.
The Taylor's series expansion of a function f(x) over the interval [x0, x], such
that Pn(x0) = f(x0), Pn(x0) = f (x0), Pn (x0) = f (x0), . . . , P
(n)
n (x0) = f(n)
(x0)
is given as
Pn(x) = f(x0) + (x−x0)
1! f (x0) + (x−x0)2
2! f (x0) + . . . + (x−x0)n
n! f(n)
(x) + Rn(x)
Rn(x) is called the remainder. For those values of x, for which the Rn(x) is small,
the polynomial Pn(x) yields an approximate representation of the function f(x).
On interpolating any value using the polynomial Pn(x), it is necessary to know,
the degree of accuracy of the estimate, or the error. The remainder term can
be expressed in the form
Rn(x) = (x−x0)(n+1)
(n+1)! f(n+1)
(ξ), where x0  ξ  x
1
Error term
The error in an interpolation is of prime importance. For example, we may be
given N = 1000 data points. To construct an interpolating polynomial, we may
use only M = 4 points. What will be the error in that approximation. Further,
is there an upper bound on the error - what is the largest possible error in that
interpolation. The answers to these questions will be known through the error
term.
We have,
Rn(x) =
1
(n + 1)!
· |(x − x0)|n+1
· |fn+1
(ξ)|
≤
1
(n + 1)!
· |(x − x0)|n+1
· Mn+1
where Mn+1 is the maximum value of fn+1
(ξ) over an interval [x0, x].
Assume that an estimate of Mn+1is available. Suppose, we are asked to con-
struct a 5-term Taylor's series. Say, we desire have an error no more than an
acceptable tolerance, that is|Rn(x)|  . To fulll this condition, we must have,
|Rn(x)| = 1
(n+1)! · |(x − x0)|n+1
· Mn+1 

Conclusions:
1. If the tolerance and the number of terms in the Taylor's series n are given
to us, then we can easily nd out the distance h from the point x0, so that the
accuracy is retained.
2
2. If the distance h and the tolerance are given, we can solve for n, the number
of terms required in the Taylor's series, so that the accuracy is retained.

Example. Obtain the polynomial approximation to f(x) =
√
1 + x over the
interval [0, 1] by means of Taylor's series, about the point x0 = 0.
(i) Estimate the error of the approximate equation
√
1 + x ≈ 1+ 1
2 x− 1
8 x2
when
x = 0.2.
(ii) Find the number of terms required in the expansion to obtain results correct
to 5 × 10−6
for 0  x  1
2 .

Solution.

i f(i)
(x) f(i)
(0)
0 (1 + x)1/2
1
1 1
2 (1 + x)−1/2 1
2
2 − 1
22 (1 + x)−3/2
− 1
22
3 1·3
23 (1 + x)−5/2 1·3
23
n (−1)n−1
· 1·3·...·(2n−3)
2n (1 + x)−(2n−1)/2
(−1)n 1·3·...·(2n−3)
2n
n + 1 (−1)n
· 1·3·...·(2n−1)
2n+1 (1 + x)−(2n+1)/2

Therefore, our polynomial approximation is,
P(x) = 1 +
1
2
x −
1
2!
1
22
x2
+
1
3!
1 · 3
23
x3
+ . . . +
(−1)n−1
xn
n!
·
1 · 3 · . . . · (2n − 3)
2n
+ Rn(x)
= 1 +
x
2
−
x2
8
+
x3
16
+ . . . +
(−1)n−1
xn
n!
·
1 · 3 · . . . · (2n − 3)
2n
+ Rn(x)

(i) The maximum of f(n+1)
(x) in the interval [0, 1
2 ] is as follows.
f(n+1)
(x) = 1·3·...·(2n−1)
2n+1 · 1
(1+x)(2n+1)/2
This will be maximum when (1 + x) is minimum, or x is minimum. Thus,
f(n+1)
(x) will be maximum at x = 0.
Mn+1 = 1·3·...·(2n−1)
2n+1 = (2n)!
2nn! · 1
2n+1 = 1
22n+1 · (2n)!
n!
Rn(x) ≤ 1
(n+1)! · |x|n
· Mn+1 = 1
(n+1)! · |x|n+1
· 1
22n+1 · (2n)!
n!
At x = 0.2, n = 2,
3
Rn(0.2) ≤ 1
(3)! · | 2
10 |3
· 1
25 · (4)!
2! = 5 × 10−4

(ii) We must have,
= 1
(n+1)! · |x|n+1
· 1
22n+1 · (2n)!
n!
Rn(0.5) = 1
(n+1)! · |1
2 |n+1
· 1
22n+1 · (2n)!
n! = (2n)!
n!(n+1)! · 1
23n+2
We must have, Rn(0.5) ≤ 5 × 10−6
.
Solving for n, we get n = 10.
Interpolating polynomial for a table of data points
Suppose, we are given N + 1 data points.
x x0 x1 . . . xi . . . xn
f(x) f(x0) f(x1) . . . f(xi) . . . f(xn)

In the rectangular plane,
• given N = 2 distinct points, we can always construct a straight line; a
polynomial P1(x) of order 1 that passes through these two points.
• given N = 3 distinct points, we can always construct a quadratic, a poly-
nomial P2(x) of order 2 that passes through these two points. In an ex-
treme case, if all the 3 points lie on a straight line, then it will degenerate
into a polynomial of order 1. Hence, through N = 3 points, a polynomial
P2(x) of order ≤ 2 can be constructed.
• given N = 4 distinct points, we can always construct a cubic, a polynomial
P3(x) of order ≤ 3 that passes through these three points.
• given N +1 distinct points, we can always construct a polynomial of order
≤ N that passes through these two points.
The existence of this fact can also be proved mathematically.
4
Theorem. Given N + 1 data points, there exists a unique interpolating poly-
nomial Pn(x) of order N, which ts these points. 
Proof.
Suppose the interpolating polynomial is of the form
Pn(x) = C0 + C1x + C2x2
+ . . . + Cnxn
This polynomial ts the N + 1 data points, (xi, f(xi)) : i = 0, 1, . . . , N. There-
fore, these points must satisfy the equation of the polynomial. We have,
f(x0) = C0 + C1x0 + C2x2
0 + C3x3
0 + . . . + Cixi
0 + . . . + CN xN
0
f(x1) = C0 + C1x1 + C2x2
1 + C3x3
1 + . . . + Cixi
1 + . . . + CN xN
1
...
f(xN ) = C0 + C1xN + C2x2
N + C3x3
N + . . . + Cixi
N + . . . + CN xN
N
This is a system of N + 1 equations in N + 1 unknowns C0, C1, . . . , CN . We
are to determine C0, C1, . . . , CN . If a unique solution for C0, C1, . . . , CN exists,
then the interpolating polynomial exists uniquely. A unique solution exists if
and only if, the determinant of the coecients of these variables must not be
equal to 0.
D =
1 x0 x2
0 . . . xr
0 . . . xN
0
1 x1 x2
1 . . . xr
1 . . . xN
1
...
1 xi x2
i . . . xr
i . . . xN
i
...
1 xN x2
N . . . xr
N . . . xN
N
This determinant is called the Vandermonde's determinant. If we subtract the
rst row from the second, the rst row from the third and so forth, we have
5
D =
1 x0 x2
0 . . . xr
0 . . . xN
0
0 (x1 − x0) (x2
1 − x2
0) . . . (xr
1 − xr
0) . . . (xN
1 − xN
0 )
0 (x2 − x0) (x2
2 − x2
0) . . . (xr
2 − xr
0) . . . (xN
2 − xN
0 )
...
0 (xi − x0) (x2
i − x2
0) . . . (xr
i − xr
0) . . . (xN
i − xN
0 )
...
0 (xN − x0) (x2
N − x2
0) . . . (xr
N − xr
0) . . . (xN
N − xN
0 )
=
1 x0 x2
0 . . . xr
0 . . . xN
0
0 (x1 − x0) (x2
1 − x2
0) . . . (xr
1 − xr
0) . . . (xN
1 − xN
0 )
0 (x2 − x0) (x2
2 − x2
0) . . . (xr
2 − xr
0) . . . (xN
2 − xN
0 )
...
0 (xi − x0) (x2
i − x2
0) . . . (xr
i − xr
0) . . . (xN
i − xN
0 )
...
0 (xN − x0) (x2
N − x2
0) . . . (xr
N − xr
0) . . . (xN
N − xN
0 )
= (x1 − x0)(x2 − x0) . . . (xi − x0) . . . (xN − x0)·
1 (x1 − x0) (x2
1 − x2
0) . . . (xr−1
1 − xr−1
0 ) . . . (xN−1
1 − xN−1
0 )
1 (x2 − x0) (x2
2 − x2
0) . . . (xr−1
2 − xr−1
0 ) . . . (xN−1
2 − xN−1
0 )
...
1 (xi − x0) (x2
i − x2
0) . . . (xr−1
i − xr−1
0 ) . . . (xN−1
i − xN−1
0 )
...
1 (xN − x0) (x2
N − x2
0) . . . (xr−1
N − xr−1
0 ) . . . (xN−1
N − xN−1
0 )
Continuing in this fashion, we get,
D = (x1 − x0)(x2 − x0) . . . (xi − x0) . . . (xN − x0)(x2 − x1)(x3 − x1) . . . (xi −
x1) . . . (xN − x1) . . . (xN − xN−1)
D is the product of all possible factors xi − xj. Hence, it can be expressed as :
D =
n
i, j = 0
i  j
(xi − xj)
Since, these are N +1 distinct points, xi = xj for all i, j. Thus, the value of the
Vandermonde's determinant is non-zero. Hence, a unique solution C0, C1, . . . , CN
exists.
We can further prove that the interpolating polynomial is also unique.
Let P∗
n(x) be another polynomial which ts the given data. This means, P∗
n(xi) =
f(xi) for all i.
Let us dene an auxiliary function,
Q(x) = Pn(x) − P∗
n(x)
Since, both Pn(x) and P∗
n(x) are polynomials of degree ≤ n, the auxiliary func-
tion Q(x) must be a polynomial of degree ≤ n.
6
Now,
Q(xi) = Pn(xi) − P∗
n(xi) = 0, for all i = 0, 1, . . . , n
Observe that Q(x) vanishes at n+1 points, and thus has n+1 roots. But, Q(x)
is a polynomial of degree ≤ n. This is possible if and only if, Q(x) is identically
equal to 0.
Q(x) ≡ 0
Pn(x) ≡ P∗
n(x)
Thus, the interpolating polynomial Pn(x) is unique.
Lagrange's interpolating polynomial
Given N + 1 data points, we are asked to nd the interpolating polynomial
Pn(x) that ts these points.
x x0 x1 . . . xi . . . xn
f(x) f(x0) f(x1) . . . f(xi) . . . f(xn)
Since, the polynomial satises all of these points, it must be a linear combination
of all f(xi)'s.
Let
Pn(x) = l0(x)f(x0)+l1(x)f(x1)+l2(x)f(x2)+. . .+li(x)f(xi)+. . .+ln(x)f(xn)
As noted in the previous section Pn(x) is a polynomial of degree n. f(x0), f(x1),
f(x2), . . ., f(xn) are all numbers. Therefore, the only possibility is that li(x)
for all i = 0, 1, 2, . . . , n should be polynomials of degree n. Since, Pn(x) ts all
the data points, we must have,
f(x0) = Pn(x0) = l0(x0)f(x0) + l1(x0)f(x1) + l2(x0)f(x2) + . . . + li(x0)f(xi) + . . . + ln(x0)f(xn)
f(x1) = Pn(x1) = l0(x1)f(x0) + l1(x1)f(x1) + l2(x1)f(x2) + . . . + li(x1)f(xi) + . . . + ln(x1)f(xn)
...
f(xj) = Pn(xj) = l0(xj)f(x0) + l1(xj)f(x1) + l2(xj)f(x2) + . . . + li(xj)f(xi) + . . . + ln(xj)f(xn)
...
f(xn) = Pn(xn) = l0(xn)f(x0) + l1(xn)f(x1) + l2(xn)f(x2) + . . . + li(xn)f(xi) + . . . + ln(xn)f(xn)
The above conditions are satised, if and only if, the polynomial functions l(x)
are such that,
li(xj) =
1, i = j
0, i = j
These polynomials are called Lagrange fundamental polynomials. Let us
dene li(x) therefore in the following way:
7
li(x) =
(x − x0)(x − x1)(x − x2) . . . (x − xi−1)(x − xi+1) . . . (x − xn)
(xi − x0)(xi − x1)(xi − x2) . . . (xi − xi−1)(xi − xi+1) . . . (xi − xn)
This satises the property that li(xj) = 0 for i = j, and li(xj) = 1 for i = j.
Also, note that li(x) is a polynomial of degree n.
We can express li(x) in the below form.
Let w(x) = (x − x0)(x − x1)(x − x2) . . . (x − xi−1)(x − xi)(x − xi+1) . . . (x − xn).
Then,
w (xi) = (xi − x0)(xi − x1)(xi − x2) . . . (xi − xi−1)(xi − xi+1) . . . (xi − xn)
Li(x) =
w(x)
(x − xi)w (x)
Linear Interpolation
In linear interpolation, we are interested to nd a straight-line passing through
the two points (x0, f(x0)) and (x1, f(x1)). Then,
l0(x) =
x − x1
(x0 − x1)
l1(x) =
x − x0
(x1 − x0)
P1(x) =
x − x1
(x0 − x1)
· f(x0) +
x − x0
(x0 − x1)
· f(x1) 
Example. Construct the linear polynomial which ts the data (1, 2) and (2, 5).
Predict the value at x = 1.5.
P1(x) =
x − 2
(1 − 2)
· 2 +
x − 1
(2 − 1)
· 5 = −2(x − 2) + 5(x − 1) = 3x − 1
P1(1.5) = 3(1.5) − 1 = 3.5
Quadratic Interpolation
In quadratic interpolation, we are interested to nd a quadratic curve passing
through the two points (x0, f(x0)), (x1, f(x1)) and (x2, f(x2)). Then,
l0(x) =
(x − x1)(x − x2)
(x0 − x1)(x0 − x2)
8
l1(x) =
(x − x0)(x − x2)
(x1 − x0)(x1 − x2)
l2(x) =
(x − x0)(x − x1)
(x2 − x0)(x2 − x1)
P2(x) = l0(x) · f(x0) + l1(x) · f(x1) + l2(x)f(x2) 
Example. Construct the quadratic polynomial which ts the data (1, 2), (2, 5),
(4, 17) . Predict the value at x = 3.
l0(x) =
(x − 2)(x − 4)
(1 − 2)(1 − 4)
=
1
3
(x2
− 6x + 8)
l1(x) =
(x − 1)(x − 4)
(2 − 1)(2 − 4)
= −
1
2
(x2
− 5x + 4)
l2(x) =
(x − 1)(x − 2)
(4 − 1)(4 − 2)
=
1
6
(x2
− 3x + 2)
P2(x) = 2
3 (x2
− 6x + 8) − 5
2 (x2
− 5x + 4) + 17
6 (x2
− 3x + 2) = x2
+ 1
P2(3) = 32
+ 1 = 10
Neville's method
Conceptually, the interpolation process has two stages: (1) Fit (once) an inter-
polating function to the data points provided. (2) Evaluate as many times as
you wish that interpolating function at a target point x. However, this two-
stage method is usually not the best way to proceed in practice. Typically, it
is computationally less ecient, than methods that construct and estimate of
f(x) directly from the N tabulated values, every time one is desired. Neville's
method is one such method.
For concreteness, we shall consider three distinct points (x0, f(x0)), (x1, f(x1))
and (x2, f(x2)). Also, suppose that, we would like to approximate the value of
the function at x = p.
From each of these three points, we can construct a constant, zero-order
polynomial to approximate f(p).
f(p) ≈ P0(p) = f(x0)
9
f(p) ≈ P1(p) = f(x1)
f(p) ≈ P2(p) = f(x2)
Of course, this isn't a very good approximation, so we turn to the rst order
Lagrange polynomials.
LetP01(x) be a linear interpolation of the points (x0, f(x0)) and (x1, f(x1)).
Thus, P01(x) is a linear interpolation of P0(x) and P1(x).
LetP12(x) be a linear interpolation of the points (x1, f(x1)) and (x2, f(x2)).
Thus, P12(x) is a linear interpolation of P1(x) and P2(x).
P01(x) = (x−x1)
(x0−x1) f(x0) + (x−x0)
(x1−x0) f(x1) = (x−x1)
(x0−x1) · P0(x) − (x−x0)
(x0−x1) · P1(x)
P12(x) = (x−x2)
(x1−x2) f(x1) + (x−x1)
(x2−x1) f(x2) = (x−x2)
(x1−x2) · P0(x) − (x−x0)
(x1−x2) · P2(x)
In general, we are applying linear interpolation toPi(x) and Pi+1(x). The result
are polynomials of one degree higher then either of the two used to construct
and that interpolates all of the points of the individual polynomials combined.
Further, let P012(x) be a linear interpolation of the points P01(x), P12(x).
P012(x) = (x−x2)
(x0−x2) · P01(x) − (x−x0)
(x0−x2) · P12(x)
This can be expressed in the form of a table, whose columns are evaluated from
left-to-right. The polynomials for N = 4 are shown below.
i xi m = 0 m = 1 m = 2 m = 3
0 x0 P0
P01
1 x1 P1 P012
P12 P0123
2 x2 P2 P123
P23
3 x3 P3
In general we have,
Pi...(i+m)(x) =
(x − x(i+m))Pi...(i+m−1) − (x − xi)P(i+1)...(i+m)
(xi − x(i+m))
An implementation of the Neville's algorithm in C++ is shown next.
10
Program listing. Neville's method 
/∗ Polynomial interpolation f i t t i n g a set of data points
xx [ 0 . . . n−1], yy [ 0 . . . n−1]. This r e s u l t s in a polynomial
approximation of order (n−1). ∗/
void poly_interp ( f l o a t ∗xx , f l o a t ∗yy , f l o a t x , i n t n , f l o a t ∗y) {
f l o a t ∗P = new f l o a t [ n ] ;
i n t m, i ;
f o r (m = 0; m  n ; m++) {
f o r ( i = 0; i  n − m; i++) {
i f (m  0) {
P[ i ] = (( x − xx [ i + m])∗ (P[ i ] ) −
(x − xx [ i ])∗ (P[ i + 1])) / (xx [ i ] − xx [ i + m] ) ;
}
e l s e {
P[ i ] = yy [ i ] ;
}
}
}
∗y = P[ 0 ] ;
}
Error of interpolation
Let us denote the error of interpolation as,
En(f; x) = f(x) − Pn(x)
We are also given the N + 1 data points.
x x0 x1 . . . xi . . . xn
f(x) f(x0) f(x1) . . . f(xi) . . . f(xn)
Since, the interpolating polynomial ts the above data points, there is no error
at the nodal points.
En(f; xi) = f(xi) − P(xi) = 0
Let us denote the rst point x0 = a and the last point xn = b. Let us choose
an arbitrary point x at which we are interpolating f(x). Therefore, x ∈ [a, b].
Let us dene an auxiliary function,
g(t) = [f(t) − P(t)] − [f(x) − P(x)] (t−x0)(t−x1)...(t−xn)
(x−x0)(x−x1)...(x−xn)
g(t) is a continuous function.
11
(i) At t = x,g(x) = 0.
(ii) At t = xi, g(xi) = f(xi) − P(xi) = 0
Thus, the function g(t) vanishes at N + 2 points, x0, x1, x2, . . . , xn. Also, g(t)
is dierentiable on each of the sub-intervals [x0, x1], [x1, x2], . . . , [xn−1, xn].
Applying Rolle's theorem, there must be atleast one point c1, c2, . . . , ci, . . . , cn
in each of these sub-intervals such that g (ci) = 0.
Now, if we apply Rolle's theorem to the function g (t) over the sub-intervals
[c1, c2], [c2, c3], . . . , [cn−1, cn], then there exist points di : i = 1, 2, . . . , n − 1 in
each of these sub-intervals where the second derivative g (t) = 0.
Continuing in this fashion and applying Rolle's theorem iteratively n + 1 times
(since there are N + 2 points), there must be atleast one point in the interval
ξ ∈ [x0, xn], such that g(n+1)
(ξ) = 0.
Let us now dierentiate g(t), n + 1 times. Note that P(t) is a polynomial of
degree n. Hence, its (n + 1)'th derivative is zero.
g(n+1)
(ξ) = f(n+1)
(ξ) − 0 − [f(x)−P (x)](n+1)!
(x−x0)(x−x1)...(x−xn)
But, g(n+1)
(ξ) = 0.
Therefore, En(f; x) = f(x) − P(x) = f(n+1)
(ξ) · w(x)
(n+1)!
The magnitude of f(x) − P(x), would be:
|f(x) − P(x)| = 1
(n+1)! · |w(x)| · |f(n+1)
(ξ)|
Since ξ is unknown to us, we don't know, what is the exact value of f(n+1)
(ξ)
is. But, we can establish an upper bound on the error. We take the maximum
possible value of f(n+1)
(x). Therefore,
|f(x) − P(x)| ≤ 1
(n+1)! · max |w(x)| · max |f(n+1)
(x)|
Error in linear interpolation
We can express E1(f; x) as,
E1(f; x) = f (ξ) · (x−x0)(x−x1)
2!
|E1(f; x)| ≤ 1
2 max |f (x)| · max |(x − x0)(x − x1)|
The maximum of the expression (x − x0)(x − x1) is found by setting the rst
derivative to zero.
h(x) = x2
− (x0 + x1)x + x0x1
h (x) = 2x − (x0 + x1)
The function h(x) has a maximum at x = x0+x1
2 . The maximum value of h(x)
is, h x0+x1
2 = x1
2 − x0
2
x0
2 − x1
2 = −(x1−x0)2
4 .
12
Let us further denote the distance between the two points x0and x1, that is
x1 − x0 = h. Then, max |(x − x0)(x − x1)| = h2
4 . Therefore,
|E1(f; x)| ≤ 1
2 max |f (x)| · h2
4 .
Let us denote max |f (x)| by M2. Then,
|E1(f; x)| ≤ h2
8 · M2
Error in quadratic interpolation
We can write,
|E2(f; x)| ≤ 1
3! · max |(x − x0)(x − x1)(x − x3)| · M3
In the special case, that the data is equispaced, then we can nd a simple
expression for maximum error. If x0, x1, x2 are equispaced, then we can call the
distance x − x1 = t, x − x0 = t − h and x − x2 = t + h.
Let us call represent the function y(x) as,
y(x) = (x − x0)(x − x1)(x − x3)
= (t − h)t(t + h)
= (t3
− th2
)
y (x) = 3t2
− h2
y(x) has a maximum at the point t = h√
3
. The maximum value is, − 2h3
3
√
3
. Thus,
|E2(f; x)| ≤ 1
6 · 2
3
√
3
h3
· M3 = 2h3
9
√
3
M3
13

Interpolation techniques - Background and implementation

  • 1.
    Interpolation Techniques Background andimplementation in C++ Pedagogical notes 2016 Quasar Chunawala
  • 2.
    Introduction Say that, inthe study of some phenomenon, there is an established functional relationship between the quantities y and x; but the function f(x) is unknown. The experiment has established the values of the function y0, y1, . . . , yN for cer- tain values of the argument x0, x1, . . . , xN in the interval [x0, xN ]. We don't have an analytic expression for f(x). The problem is then to nd a function (as simple as possible from the computational stand-point; for example a polyno- mial) which will represent the unknown function y = f(x). In more abstract fashion, the problem may be formulated as follows: given on the interval [x0, xN ], the values of an unknown function y = f(x) at N + 1 distinct points x0, x1, . . . , xN , such that, y0 = f(x0), y1 = f(x1), . . . , yN = f(xN ) It is required to nd a polynomial P(x) of degree≤ n that approximately expresses the function f(x). Further, the task is to estimate f(x) for some target value of x. Interpolating polynomial given one data point and its higher order derivatives Say that, we are required to t a polynomial P(x), at a point x = x0, where the value of the function and the value of its rst n derivatives at that point f(x0), f (x), f(2) (x), . . . , f(n) (x) are given. Then, the Taylor's series expansion of the function in terms of a polynomial of degree n over the interval [x0, x] is the interpolating polynomial. This is intuitive, because the Taylor's series polynomial and its higher order derivatives have the same values as the function and its derivatives. The Taylor's series expansion of a function f(x) over the interval [x0, x], such that Pn(x0) = f(x0), Pn(x0) = f (x0), Pn (x0) = f (x0), . . . , P (n) n (x0) = f(n) (x0) is given as Pn(x) = f(x0) + (x−x0) 1! f (x0) + (x−x0)2 2! f (x0) + . . . + (x−x0)n n! f(n) (x) + Rn(x) Rn(x) is called the remainder. For those values of x, for which the Rn(x) is small, the polynomial Pn(x) yields an approximate representation of the function f(x). On interpolating any value using the polynomial Pn(x), it is necessary to know, the degree of accuracy of the estimate, or the error. The remainder term can be expressed in the form Rn(x) = (x−x0)(n+1) (n+1)! f(n+1) (ξ), where x0 ξ x 1
  • 3.
    Error term The errorin an interpolation is of prime importance. For example, we may be given N = 1000 data points. To construct an interpolating polynomial, we may use only M = 4 points. What will be the error in that approximation. Further, is there an upper bound on the error - what is the largest possible error in that interpolation. The answers to these questions will be known through the error term. We have, Rn(x) = 1 (n + 1)! · |(x − x0)|n+1 · |fn+1 (ξ)| ≤ 1 (n + 1)! · |(x − x0)|n+1 · Mn+1 where Mn+1 is the maximum value of fn+1 (ξ) over an interval [x0, x]. Assume that an estimate of Mn+1is available. Suppose, we are asked to con- struct a 5-term Taylor's series. Say, we desire have an error no more than an acceptable tolerance, that is|Rn(x)| . To fulll this condition, we must have, |Rn(x)| = 1 (n+1)! · |(x − x0)|n+1 · Mn+1 Conclusions: 1. If the tolerance and the number of terms in the Taylor's series n are given to us, then we can easily nd out the distance h from the point x0, so that the accuracy is retained. 2
  • 4.
    2. If thedistance h and the tolerance are given, we can solve for n, the number of terms required in the Taylor's series, so that the accuracy is retained. Example. Obtain the polynomial approximation to f(x) = √ 1 + x over the interval [0, 1] by means of Taylor's series, about the point x0 = 0. (i) Estimate the error of the approximate equation √ 1 + x ≈ 1+ 1 2 x− 1 8 x2 when x = 0.2. (ii) Find the number of terms required in the expansion to obtain results correct to 5 × 10−6 for 0 x 1 2 . Solution. i f(i) (x) f(i) (0) 0 (1 + x)1/2 1 1 1 2 (1 + x)−1/2 1 2 2 − 1 22 (1 + x)−3/2 − 1 22 3 1·3 23 (1 + x)−5/2 1·3 23 n (−1)n−1 · 1·3·...·(2n−3) 2n (1 + x)−(2n−1)/2 (−1)n 1·3·...·(2n−3) 2n n + 1 (−1)n · 1·3·...·(2n−1) 2n+1 (1 + x)−(2n+1)/2 Therefore, our polynomial approximation is, P(x) = 1 + 1 2 x − 1 2! 1 22 x2 + 1 3! 1 · 3 23 x3 + . . . + (−1)n−1 xn n! · 1 · 3 · . . . · (2n − 3) 2n + Rn(x) = 1 + x 2 − x2 8 + x3 16 + . . . + (−1)n−1 xn n! · 1 · 3 · . . . · (2n − 3) 2n + Rn(x) (i) The maximum of f(n+1) (x) in the interval [0, 1 2 ] is as follows. f(n+1) (x) = 1·3·...·(2n−1) 2n+1 · 1 (1+x)(2n+1)/2 This will be maximum when (1 + x) is minimum, or x is minimum. Thus, f(n+1) (x) will be maximum at x = 0. Mn+1 = 1·3·...·(2n−1) 2n+1 = (2n)! 2nn! · 1 2n+1 = 1 22n+1 · (2n)! n! Rn(x) ≤ 1 (n+1)! · |x|n · Mn+1 = 1 (n+1)! · |x|n+1 · 1 22n+1 · (2n)! n! At x = 0.2, n = 2, 3
  • 5.
    Rn(0.2) ≤ 1 (3)!· | 2 10 |3 · 1 25 · (4)! 2! = 5 × 10−4 (ii) We must have, = 1 (n+1)! · |x|n+1 · 1 22n+1 · (2n)! n! Rn(0.5) = 1 (n+1)! · |1 2 |n+1 · 1 22n+1 · (2n)! n! = (2n)! n!(n+1)! · 1 23n+2 We must have, Rn(0.5) ≤ 5 × 10−6 . Solving for n, we get n = 10. Interpolating polynomial for a table of data points Suppose, we are given N + 1 data points. x x0 x1 . . . xi . . . xn f(x) f(x0) f(x1) . . . f(xi) . . . f(xn) In the rectangular plane, • given N = 2 distinct points, we can always construct a straight line; a polynomial P1(x) of order 1 that passes through these two points. • given N = 3 distinct points, we can always construct a quadratic, a poly- nomial P2(x) of order 2 that passes through these two points. In an ex- treme case, if all the 3 points lie on a straight line, then it will degenerate into a polynomial of order 1. Hence, through N = 3 points, a polynomial P2(x) of order ≤ 2 can be constructed. • given N = 4 distinct points, we can always construct a cubic, a polynomial P3(x) of order ≤ 3 that passes through these three points. • given N +1 distinct points, we can always construct a polynomial of order ≤ N that passes through these two points. The existence of this fact can also be proved mathematically. 4
  • 6.
    Theorem. Given N+ 1 data points, there exists a unique interpolating poly- nomial Pn(x) of order N, which ts these points. Proof. Suppose the interpolating polynomial is of the form Pn(x) = C0 + C1x + C2x2 + . . . + Cnxn This polynomial ts the N + 1 data points, (xi, f(xi)) : i = 0, 1, . . . , N. There- fore, these points must satisfy the equation of the polynomial. We have, f(x0) = C0 + C1x0 + C2x2 0 + C3x3 0 + . . . + Cixi 0 + . . . + CN xN 0 f(x1) = C0 + C1x1 + C2x2 1 + C3x3 1 + . . . + Cixi 1 + . . . + CN xN 1 ... f(xN ) = C0 + C1xN + C2x2 N + C3x3 N + . . . + Cixi N + . . . + CN xN N This is a system of N + 1 equations in N + 1 unknowns C0, C1, . . . , CN . We are to determine C0, C1, . . . , CN . If a unique solution for C0, C1, . . . , CN exists, then the interpolating polynomial exists uniquely. A unique solution exists if and only if, the determinant of the coecients of these variables must not be equal to 0. D = 1 x0 x2 0 . . . xr 0 . . . xN 0 1 x1 x2 1 . . . xr 1 . . . xN 1 ... 1 xi x2 i . . . xr i . . . xN i ... 1 xN x2 N . . . xr N . . . xN N This determinant is called the Vandermonde's determinant. If we subtract the rst row from the second, the rst row from the third and so forth, we have 5
  • 7.
    D = 1 x0x2 0 . . . xr 0 . . . xN 0 0 (x1 − x0) (x2 1 − x2 0) . . . (xr 1 − xr 0) . . . (xN 1 − xN 0 ) 0 (x2 − x0) (x2 2 − x2 0) . . . (xr 2 − xr 0) . . . (xN 2 − xN 0 ) ... 0 (xi − x0) (x2 i − x2 0) . . . (xr i − xr 0) . . . (xN i − xN 0 ) ... 0 (xN − x0) (x2 N − x2 0) . . . (xr N − xr 0) . . . (xN N − xN 0 ) = 1 x0 x2 0 . . . xr 0 . . . xN 0 0 (x1 − x0) (x2 1 − x2 0) . . . (xr 1 − xr 0) . . . (xN 1 − xN 0 ) 0 (x2 − x0) (x2 2 − x2 0) . . . (xr 2 − xr 0) . . . (xN 2 − xN 0 ) ... 0 (xi − x0) (x2 i − x2 0) . . . (xr i − xr 0) . . . (xN i − xN 0 ) ... 0 (xN − x0) (x2 N − x2 0) . . . (xr N − xr 0) . . . (xN N − xN 0 ) = (x1 − x0)(x2 − x0) . . . (xi − x0) . . . (xN − x0)· 1 (x1 − x0) (x2 1 − x2 0) . . . (xr−1 1 − xr−1 0 ) . . . (xN−1 1 − xN−1 0 ) 1 (x2 − x0) (x2 2 − x2 0) . . . (xr−1 2 − xr−1 0 ) . . . (xN−1 2 − xN−1 0 ) ... 1 (xi − x0) (x2 i − x2 0) . . . (xr−1 i − xr−1 0 ) . . . (xN−1 i − xN−1 0 ) ... 1 (xN − x0) (x2 N − x2 0) . . . (xr−1 N − xr−1 0 ) . . . (xN−1 N − xN−1 0 ) Continuing in this fashion, we get, D = (x1 − x0)(x2 − x0) . . . (xi − x0) . . . (xN − x0)(x2 − x1)(x3 − x1) . . . (xi − x1) . . . (xN − x1) . . . (xN − xN−1) D is the product of all possible factors xi − xj. Hence, it can be expressed as : D = n i, j = 0 i j (xi − xj) Since, these are N +1 distinct points, xi = xj for all i, j. Thus, the value of the Vandermonde's determinant is non-zero. Hence, a unique solution C0, C1, . . . , CN exists. We can further prove that the interpolating polynomial is also unique. Let P∗ n(x) be another polynomial which ts the given data. This means, P∗ n(xi) = f(xi) for all i. Let us dene an auxiliary function, Q(x) = Pn(x) − P∗ n(x) Since, both Pn(x) and P∗ n(x) are polynomials of degree ≤ n, the auxiliary func- tion Q(x) must be a polynomial of degree ≤ n. 6
  • 8.
    Now, Q(xi) = Pn(xi)− P∗ n(xi) = 0, for all i = 0, 1, . . . , n Observe that Q(x) vanishes at n+1 points, and thus has n+1 roots. But, Q(x) is a polynomial of degree ≤ n. This is possible if and only if, Q(x) is identically equal to 0. Q(x) ≡ 0 Pn(x) ≡ P∗ n(x) Thus, the interpolating polynomial Pn(x) is unique. Lagrange's interpolating polynomial Given N + 1 data points, we are asked to nd the interpolating polynomial Pn(x) that ts these points. x x0 x1 . . . xi . . . xn f(x) f(x0) f(x1) . . . f(xi) . . . f(xn) Since, the polynomial satises all of these points, it must be a linear combination of all f(xi)'s. Let Pn(x) = l0(x)f(x0)+l1(x)f(x1)+l2(x)f(x2)+. . .+li(x)f(xi)+. . .+ln(x)f(xn) As noted in the previous section Pn(x) is a polynomial of degree n. f(x0), f(x1), f(x2), . . ., f(xn) are all numbers. Therefore, the only possibility is that li(x) for all i = 0, 1, 2, . . . , n should be polynomials of degree n. Since, Pn(x) ts all the data points, we must have, f(x0) = Pn(x0) = l0(x0)f(x0) + l1(x0)f(x1) + l2(x0)f(x2) + . . . + li(x0)f(xi) + . . . + ln(x0)f(xn) f(x1) = Pn(x1) = l0(x1)f(x0) + l1(x1)f(x1) + l2(x1)f(x2) + . . . + li(x1)f(xi) + . . . + ln(x1)f(xn) ... f(xj) = Pn(xj) = l0(xj)f(x0) + l1(xj)f(x1) + l2(xj)f(x2) + . . . + li(xj)f(xi) + . . . + ln(xj)f(xn) ... f(xn) = Pn(xn) = l0(xn)f(x0) + l1(xn)f(x1) + l2(xn)f(x2) + . . . + li(xn)f(xi) + . . . + ln(xn)f(xn) The above conditions are satised, if and only if, the polynomial functions l(x) are such that, li(xj) = 1, i = j 0, i = j These polynomials are called Lagrange fundamental polynomials. Let us dene li(x) therefore in the following way: 7
  • 9.
    li(x) = (x −x0)(x − x1)(x − x2) . . . (x − xi−1)(x − xi+1) . . . (x − xn) (xi − x0)(xi − x1)(xi − x2) . . . (xi − xi−1)(xi − xi+1) . . . (xi − xn) This satises the property that li(xj) = 0 for i = j, and li(xj) = 1 for i = j. Also, note that li(x) is a polynomial of degree n. We can express li(x) in the below form. Let w(x) = (x − x0)(x − x1)(x − x2) . . . (x − xi−1)(x − xi)(x − xi+1) . . . (x − xn). Then, w (xi) = (xi − x0)(xi − x1)(xi − x2) . . . (xi − xi−1)(xi − xi+1) . . . (xi − xn) Li(x) = w(x) (x − xi)w (x) Linear Interpolation In linear interpolation, we are interested to nd a straight-line passing through the two points (x0, f(x0)) and (x1, f(x1)). Then, l0(x) = x − x1 (x0 − x1) l1(x) = x − x0 (x1 − x0) P1(x) = x − x1 (x0 − x1) · f(x0) + x − x0 (x0 − x1) · f(x1) Example. Construct the linear polynomial which ts the data (1, 2) and (2, 5). Predict the value at x = 1.5. P1(x) = x − 2 (1 − 2) · 2 + x − 1 (2 − 1) · 5 = −2(x − 2) + 5(x − 1) = 3x − 1 P1(1.5) = 3(1.5) − 1 = 3.5 Quadratic Interpolation In quadratic interpolation, we are interested to nd a quadratic curve passing through the two points (x0, f(x0)), (x1, f(x1)) and (x2, f(x2)). Then, l0(x) = (x − x1)(x − x2) (x0 − x1)(x0 − x2) 8
  • 10.
    l1(x) = (x −x0)(x − x2) (x1 − x0)(x1 − x2) l2(x) = (x − x0)(x − x1) (x2 − x0)(x2 − x1) P2(x) = l0(x) · f(x0) + l1(x) · f(x1) + l2(x)f(x2) Example. Construct the quadratic polynomial which ts the data (1, 2), (2, 5), (4, 17) . Predict the value at x = 3. l0(x) = (x − 2)(x − 4) (1 − 2)(1 − 4) = 1 3 (x2 − 6x + 8) l1(x) = (x − 1)(x − 4) (2 − 1)(2 − 4) = − 1 2 (x2 − 5x + 4) l2(x) = (x − 1)(x − 2) (4 − 1)(4 − 2) = 1 6 (x2 − 3x + 2) P2(x) = 2 3 (x2 − 6x + 8) − 5 2 (x2 − 5x + 4) + 17 6 (x2 − 3x + 2) = x2 + 1 P2(3) = 32 + 1 = 10 Neville's method Conceptually, the interpolation process has two stages: (1) Fit (once) an inter- polating function to the data points provided. (2) Evaluate as many times as you wish that interpolating function at a target point x. However, this two- stage method is usually not the best way to proceed in practice. Typically, it is computationally less ecient, than methods that construct and estimate of f(x) directly from the N tabulated values, every time one is desired. Neville's method is one such method. For concreteness, we shall consider three distinct points (x0, f(x0)), (x1, f(x1)) and (x2, f(x2)). Also, suppose that, we would like to approximate the value of the function at x = p. From each of these three points, we can construct a constant, zero-order polynomial to approximate f(p). f(p) ≈ P0(p) = f(x0) 9
  • 11.
    f(p) ≈ P1(p)= f(x1) f(p) ≈ P2(p) = f(x2) Of course, this isn't a very good approximation, so we turn to the rst order Lagrange polynomials. LetP01(x) be a linear interpolation of the points (x0, f(x0)) and (x1, f(x1)). Thus, P01(x) is a linear interpolation of P0(x) and P1(x). LetP12(x) be a linear interpolation of the points (x1, f(x1)) and (x2, f(x2)). Thus, P12(x) is a linear interpolation of P1(x) and P2(x). P01(x) = (x−x1) (x0−x1) f(x0) + (x−x0) (x1−x0) f(x1) = (x−x1) (x0−x1) · P0(x) − (x−x0) (x0−x1) · P1(x) P12(x) = (x−x2) (x1−x2) f(x1) + (x−x1) (x2−x1) f(x2) = (x−x2) (x1−x2) · P0(x) − (x−x0) (x1−x2) · P2(x) In general, we are applying linear interpolation toPi(x) and Pi+1(x). The result are polynomials of one degree higher then either of the two used to construct and that interpolates all of the points of the individual polynomials combined. Further, let P012(x) be a linear interpolation of the points P01(x), P12(x). P012(x) = (x−x2) (x0−x2) · P01(x) − (x−x0) (x0−x2) · P12(x) This can be expressed in the form of a table, whose columns are evaluated from left-to-right. The polynomials for N = 4 are shown below. i xi m = 0 m = 1 m = 2 m = 3 0 x0 P0 P01 1 x1 P1 P012 P12 P0123 2 x2 P2 P123 P23 3 x3 P3 In general we have, Pi...(i+m)(x) = (x − x(i+m))Pi...(i+m−1) − (x − xi)P(i+1)...(i+m) (xi − x(i+m)) An implementation of the Neville's algorithm in C++ is shown next. 10
  • 12.
    Program listing. Neville'smethod /∗ Polynomial interpolation f i t t i n g a set of data points xx [ 0 . . . n−1], yy [ 0 . . . n−1]. This r e s u l t s in a polynomial approximation of order (n−1). ∗/ void poly_interp ( f l o a t ∗xx , f l o a t ∗yy , f l o a t x , i n t n , f l o a t ∗y) { f l o a t ∗P = new f l o a t [ n ] ; i n t m, i ; f o r (m = 0; m n ; m++) { f o r ( i = 0; i n − m; i++) { i f (m 0) { P[ i ] = (( x − xx [ i + m])∗ (P[ i ] ) − (x − xx [ i ])∗ (P[ i + 1])) / (xx [ i ] − xx [ i + m] ) ; } e l s e { P[ i ] = yy [ i ] ; } } } ∗y = P[ 0 ] ; } Error of interpolation Let us denote the error of interpolation as, En(f; x) = f(x) − Pn(x) We are also given the N + 1 data points. x x0 x1 . . . xi . . . xn f(x) f(x0) f(x1) . . . f(xi) . . . f(xn) Since, the interpolating polynomial ts the above data points, there is no error at the nodal points. En(f; xi) = f(xi) − P(xi) = 0 Let us denote the rst point x0 = a and the last point xn = b. Let us choose an arbitrary point x at which we are interpolating f(x). Therefore, x ∈ [a, b]. Let us dene an auxiliary function, g(t) = [f(t) − P(t)] − [f(x) − P(x)] (t−x0)(t−x1)...(t−xn) (x−x0)(x−x1)...(x−xn) g(t) is a continuous function. 11
  • 13.
    (i) At t= x,g(x) = 0. (ii) At t = xi, g(xi) = f(xi) − P(xi) = 0 Thus, the function g(t) vanishes at N + 2 points, x0, x1, x2, . . . , xn. Also, g(t) is dierentiable on each of the sub-intervals [x0, x1], [x1, x2], . . . , [xn−1, xn]. Applying Rolle's theorem, there must be atleast one point c1, c2, . . . , ci, . . . , cn in each of these sub-intervals such that g (ci) = 0. Now, if we apply Rolle's theorem to the function g (t) over the sub-intervals [c1, c2], [c2, c3], . . . , [cn−1, cn], then there exist points di : i = 1, 2, . . . , n − 1 in each of these sub-intervals where the second derivative g (t) = 0. Continuing in this fashion and applying Rolle's theorem iteratively n + 1 times (since there are N + 2 points), there must be atleast one point in the interval ξ ∈ [x0, xn], such that g(n+1) (ξ) = 0. Let us now dierentiate g(t), n + 1 times. Note that P(t) is a polynomial of degree n. Hence, its (n + 1)'th derivative is zero. g(n+1) (ξ) = f(n+1) (ξ) − 0 − [f(x)−P (x)](n+1)! (x−x0)(x−x1)...(x−xn) But, g(n+1) (ξ) = 0. Therefore, En(f; x) = f(x) − P(x) = f(n+1) (ξ) · w(x) (n+1)! The magnitude of f(x) − P(x), would be: |f(x) − P(x)| = 1 (n+1)! · |w(x)| · |f(n+1) (ξ)| Since ξ is unknown to us, we don't know, what is the exact value of f(n+1) (ξ) is. But, we can establish an upper bound on the error. We take the maximum possible value of f(n+1) (x). Therefore, |f(x) − P(x)| ≤ 1 (n+1)! · max |w(x)| · max |f(n+1) (x)| Error in linear interpolation We can express E1(f; x) as, E1(f; x) = f (ξ) · (x−x0)(x−x1) 2! |E1(f; x)| ≤ 1 2 max |f (x)| · max |(x − x0)(x − x1)| The maximum of the expression (x − x0)(x − x1) is found by setting the rst derivative to zero. h(x) = x2 − (x0 + x1)x + x0x1 h (x) = 2x − (x0 + x1) The function h(x) has a maximum at x = x0+x1 2 . The maximum value of h(x) is, h x0+x1 2 = x1 2 − x0 2 x0 2 − x1 2 = −(x1−x0)2 4 . 12
  • 14.
    Let us furtherdenote the distance between the two points x0and x1, that is x1 − x0 = h. Then, max |(x − x0)(x − x1)| = h2 4 . Therefore, |E1(f; x)| ≤ 1 2 max |f (x)| · h2 4 . Let us denote max |f (x)| by M2. Then, |E1(f; x)| ≤ h2 8 · M2 Error in quadratic interpolation We can write, |E2(f; x)| ≤ 1 3! · max |(x − x0)(x − x1)(x − x3)| · M3 In the special case, that the data is equispaced, then we can nd a simple expression for maximum error. If x0, x1, x2 are equispaced, then we can call the distance x − x1 = t, x − x0 = t − h and x − x2 = t + h. Let us call represent the function y(x) as, y(x) = (x − x0)(x − x1)(x − x3) = (t − h)t(t + h) = (t3 − th2 ) y (x) = 3t2 − h2 y(x) has a maximum at the point t = h√ 3 . The maximum value is, − 2h3 3 √ 3 . Thus, |E2(f; x)| ≤ 1 6 · 2 3 √ 3 h3 · M3 = 2h3 9 √ 3 M3 13