17 Lagrange Interpolation Mathematica Program
17 Lagrange Interpolation Mathematica Program
17 Lagrange Interpolation Mathematica Program
Lagrange Interpolation
Suppose we know the values of some function f(x) at n + 1 distinct grid points
a = x
0
, x
1
, x
2
, ..., x
n
= b (17.1)
Denote the values of the function at each of these points as
f
k
= f(x
k
), k = 0, 1, 2, ..., n (17.2)
The problem of interpolation is to nd an approximate (numerical) value for f(x)
at any point x [a, b] that does not necessarily correspond to one of the grid points.
(x
1
,f
1
)
(x
2
,f
2
)
(x
4
,f
4
)
(x
5
,f
5
)
(x
3
,f
3
)
x
1
x
2
x
3
x
4
x
5
(x
1
,f
1
)
(x
2
,f
2
)
(x
4
,f
4
)
(x
5
,f
5
)
f(x)
x
1
x
2
x
3
x
4
x
5
(x
3
,f
3
)
Function known only at the grid points
The unknown function f(x) goes through
the grid points
109
110 LESSON 17. LAGRANGE INTERPOLATION
The simplest method is linear interpolation: draw line segments connecting each
pair of consecutive grid points (x
k
, f
k
) and (x
k+1
, f
k+1
). For x
k
x x
k+1
we have:
y = f
k
+ m(x x
k
) = f
k
+
f
k+1
f
k
x
k+1
x
k
(x x
k
) (17.3)
(x
1
,f
1
)
(x
2
,f
2
)
(x
4
,f
4
)
(x
5
,f
5
)
x
1
x
2
x
3
x
4
x
5
(x
3
,f
3
)
In general, unless the grid points are very close, linear interpolation does not give very
accurate result. A better approximation would be given by a polynomial. The key is
to nd the right polynomial, not just any polynomial that goes through the points.
As it turns out, it is possible to nd a polynomial that approximates the function to
any desired degree of accuracy. This result is called the Weirstrass Approxima-
tion Theorem. Furthermore, given any n + 1 points it is possible to nd a unique
polynomial of minimum degree that ts all the points. For example, any two points
can be t by a line; and three non-collinear points can be t by a unique parabola;
any four points that do not line on the same line or on the same parabola can be t
by a unique cubic; and so forth.
Let us suppose that we have dened n + 1 unique points
(x
0
, f
0
), (x
1
, f
1
), . . . , (x
n
, f
n
) (17.4)
where
x
0
< x
1
< < x
n
(17.5)
and that we want to nd the polynomial of lowest order
P(x) = a
0
+ a
1
x + a
2
x
2
+ + a
n
x
n
(17.6)
Math 481A
California State University Northridge
2008, B.E.Shapiro
Last revised: November 16, 2011
LESSON 17. LAGRANGE INTERPOLATION 111
to these points. We begin by substituting the points 17.4 into the polynomial to get
n + 1 equations in the n + 1 unknowns a
0
, a
1
, . . . , a
n
:
f
0
= a
0
+ a
1
x
0
+ a
2
x
2
0
+ + a
n
x
n
0
(17.7)
f
1
= a
0
+ a
1
x
1
+ a
2
x
2
1
+ + a
n
x
n
n
(17.8)
.
.
.
f
n
= a
0
+ a
1
x
n
+ a
2
x
2
n
+ + a
n
x
n
n
(17.9)
which we can write as the matrix system
_
_
_
_
_
1 x
0
x
2
0
x
n
0
1 x
1
x
2
1
x
n
1
.
.
.
.
.
.
1 x
n
x
2
n
x
n
n
_
_
_
_
_
_
_
_
_
_
a
0
a
1
.
.
.
a
n
_
_
_
_
_
=
_
_
_
_
_
f
0
f
1
.
.
.
f
n
_
_
_
_
_
(17.10)
This equation has a solution if the matrix of coecients is non-singular. But because
the points are distinct the lines of the matrix form a linearly independent set of vectors
(proof left as an exercise). Hence the matrix is non-singular. To nd the a
0
, a
1
, . . .
we could use Gaussian elimination or some other method as we have discussed. It
turns out that this is not necessary because the form of the matrix allows us to write
a much simpler iterative process for nding these coecients.
We will actually present two dierent methods for constructing the polynomial: the
Lagrange method (in this section) and the Newton method (in the next section). Be-
cause of uniqueness both polynomials will be identical; however, they are constructed
dierently. The Newton method is particularly useful when one needs to calculate
numbers by hand, as was done in the 19th century. The Lagrange method, which we
will discuss rst, is somewhat more intuitive. Before providing the general form, we
will illustrate the technique with linear (n=1) and quadratic (n=2) interpolation.
For n = 1 we start with two points (x
0
, f
0
), (x
1
, f
1
) that we want to t a line to. Of
course we have already done it, but this time we will construct the line in such a way
that the method can be easily extending to higher degree ts (with more points). We
dene the functions
L
0
(x) =
x x
1
x
0
x
1
(17.11)
L
1
(x) =
x x
0
x
1
x
0
(17.12)
and we observe that
L
0
(x
0
) = 1 L
0
(x
1
) = 0 (17.13)
L
1
(x
0
) = 0 L
1
(x
1
) = 1 (17.14)
2008, B.E.Shapiro
Last revised: November 16, 2011
Math 481A
California State University Northridge
112 LESSON 17. LAGRANGE INTERPOLATION
We write this more compactly as
L
i
(x
j
) =
ij
=
_
1, i = j,
0, i = j
(17.15)
known as the Kroeneker delta function (for the German mathematician Leopold
Kroeneker, 1823-1891). Next, we dene the function
P(x) =
1
k=0
L
i
(x)f
i
(17.16)
= L
0
(x)f
0
+ L
1
(x)f
1
(17.17)
=
x x
1
x
0
x
1
f
0
+
x x
0
x
1
x
0
f
1
(17.18)
We observe that P(x
i
) = f
i
and that P is linear in x. Hence it is the equation of
a line that goes through both points (x
i
, f
i
), i = 0, 1. A rearrangement of this gives
equation 17.3.
For n = 2 we have 3 points: (x
0
, f
0
), (x
1
, f
1
), and (x
2
, f
2
). Again, we have already
solved for the equation of a parabola through three points in the previous section,
but we will do it this time by extending the Lagrange technique. We dene the three
functions
L
0
(x) =
(x x
1
)(x x
2
)
(x
0
x
1
)(x
0
x
2
)
(17.19)
L
1
(x) =
(x x
0
)(x x
2
)
(x
1
x
0
)(x
1
x
2
)
(17.20)
L
2
(x) =
(x x
0
)(x x
1
)
(x
2
x
0
)(x
2
x
1
)
(17.21)
We observe that
L
0
(x
0
) = 1 L
0
(x
1
) = 0 L
0
(x
2
) = 0 (17.22)
L
1
(x
0
) = 0 L
1
(x
1
) = 1 L
1
(x
2
) = 0 (17.23)
L
2
(x
0
) = 0 L
2
(x
1
) = 0 L
2
(x
2
) = 1 (17.24)
or in general L
i
(x
j
) =
ij
, as before with the linear functions. Then we dene the
function
P(x) = L
0
(x)f
0
+ L
1
(x)f
1
+ L
2
(x)f
2
=
2
k=0
L
i
(x)f
i
(17.25)
We observe now that P(x) is quadratic, and that P(x
i
) = f
i
. Thus it goes through
all three points, and hence by uniqueness it is the only parabola that goes through
all three points.
Math 481A
California State University Northridge
2008, B.E.Shapiro
Last revised: November 16, 2011
LESSON 17. LAGRANGE INTERPOLATION 113
In the general case it becomes more convenient to add a second index indicating the
order of the polynomials to the L functions. Thus we rename our linear functions
from L
0
and L
1
to L
10
and L
11
, and our quadratic functions L
0
, L
1
, and L
2
become
L
20
, L
21
, and L
22
. The general denition is
L
nk
(x) =
n
j=0,j=k
x x
j
x
k
x
j
(17.26)
for k = 0, . . . , n. It is easily observed that (a) each of the L
nk
has degree n; and that
(b) that L
nk
(x
i
) =
ik
. Hence the polynomial
P(x) =
n
k=0
L
nk
(x)f
k
(17.27)
is also of degree at most k and that P(x
j
) = f
j
. Thus P(x) is our interpolating
polynomial, and we have derived the following result.
Theorem 17.1 (Lagrange Interpolating Polynomial). Suppose that we are given
the values of the function f(x) at n + 1 distinct points x
0
, . . . , x
n
, which we denote
by f
0
, . . . , f
n
. Then the n
th
Lagrange interpolating polynomial
P(x) =
n
k=0
L
nk
(x)f
k
=
n
k=0
f
k
n
j=0,j=k
x x
j
x
k
x
j
(17.28)
is a polynomial of degree at most n that matches f(x) at each of the x
i
.
Example 17.1. Let f(x) =
1 = 1 (17.29)
f
1
= f(x
1
) = f(0.6) =
k=0
f
k
L
nk
(x) =
n
k=0
f
k
n
j=0,j=k
x x
j
x
k
x
j
(17.43)
Proof. If x = x
k
and P(x
k
) = f
k
for some k, then the second term in equation 17.42
is zero, regardless of the value of c, and the result holds identically.
So suppose that x = x
k
for all k, and dene the function
g(t) = f(t) P(t) [f(x) P(x)]
n
i=0
t x
i
x x
i
(17.44)
Then
g(x
k
) = f(x
k
) P(x
k
) [f(x) P(x)]
n
i=0
x
k
x
i
x x
i
= 0 (17.45)
The second equality follows because (a) by construction, f(x
k
) = P(x
k
), so the rst
term is zero; and (b) for some i we have i = k and hence there is a factor of x
k
x
k
in the numerator of the second term, making it zero as well. Furthermore,
g(x) = f(x) P(x) [f(x) P(x)]
n
i=0
x x
i
x x
i
(17.46)
= f(x) P(x) [f(x) P(x)] (17.47)
= 0 (17.48)
Hence g(t) = 0 as the n + 2 numbers x, x
0
, . . . , x
n
. Since it is also continuously
dierentiable n+1 times, then by the generalized Rolles theorem, theorem 4.4, there
exists at least one number c (a, b) such that g
(n+1)
(c) = 0. Dierentiating g(t) a
total of n + 1 times,
g
(n+1)
(t) = f
(n+1)
(t) P
(n+1)
(t) [f(x) P(x)]
d
(n+1)
dt
(n+1)
n
i=0
t x
i
x x
i
(17.49)
hence at t = c we have
0 = g
(n+1)
(c) (17.50)
= f
(n+1)
(c) P
(n+1)
(c) [f(x) P(x)]
d
(n+1)
dt
(n+1)
n
i=0
t x
i
x x
i
t=c
(17.51)
= f
(n+1)
(c) P
(n+1)
(c)
[f(x) P(x)]
n
i=0
(x x
i
)
d
(n+1)
dt
(n+1)
n
i=0
(t x
i
)
t=c
(17.52)
Now since
P(t) = a
0
+ a
1
t + + a
n
t
n
(17.53)
Math 481A
California State University Northridge
2008, B.E.Shapiro
Last revised: November 16, 2011
LESSON 17. LAGRANGE INTERPOLATION 121
then P
(n+1)
(t) = 0 for all t, and hence P
(n+1)
(c) = 0, so that
0 = f
(n+1)
(c)
[f(x) P(x)]
n
i=0
(x x
i
)
d
(n+1)
dt
(n+1)
n
i=0
(t x
i
)
t=c
(17.54)
Furthermore,
d
(n+1)
dt
(n+1)
n
i=0
(t x
i
) =
d
(n+1)
dt
(n+1)
(t x
1
)(t x
2
)(t x
3
) (t x
n
) (17.55)
=
d
(n+1)
dt
(n+1)
_
t
n
+ (stu) t
n1
+ (more stu) t
n2
+
(17.56)
= (n + 1)! (17.57)
Substituting equation 17.57 into equation 17.58 gives
0 = f
(n+1)
(c)
[f(x) P(x)]
n
i=0
(x x
i
)
(n + 1)! (17.58)
Solving for f(x) gives us equation 17.42, completing the proof.
Example 17.2. Suppose you want to make a table of the natural logarithms over
the range 1 x 100. What step size is sucient to ensure that linear interpolation
between each successive pair of points will be accurate to within 10
5
?
Solution. From equation 17.42 we have
|f(x) P(x)| =
f
n+1
(c)(x x
0
)(x x
1
) (x x
n
)
(n + 1)!
(17.59)
For linear interpolation we use n = 1 (there are two points, x
0
and x
1
, so that
|f(x) P(x)| =
f
(2)
(c)(x x
0
)(x x
1
)
2!
(17.60)
1
2
max |f
(x) = 1/x
2
. The
maximum value of | 1/x
2
| on [1, 100] is 1, so that
|f(x) P(x)|
1
2
max |(x x
0
)(x x
1
)| (17.62)
To nd the maximum value of g(x) = (x x
0
)(x x
1
) = x
2
(x
0
+ x
1
)x + x
0
x
1
on [x
0
, x
1
] we observe that the maximum either occurs at an endpoint or at a point
where g
(x) = 2x (x
0
+ x
1
) (17.63)
2008, B.E.Shapiro
Last revised: November 16, 2011
Math 481A
California State University Northridge
122 LESSON 17. LAGRANGE INTERPOLATION
which gives a possible maximum at x = (x
0
+ x
1
)/2. The value of g at this point is
g
_
x
0
+ x
1
2
_
_
x
0
+ x
1
2
x
0
__
x
0
+ x
1
2
x
1
_
(17.64)
=
_
x
1
x
0
2
__
x
0
x
1
2
_
(17.65)
=
h
2
4
(17.66)
where h is the size between entries in the table (the number we are solving for).
Substituting equation 17.66 into example 17.62 gives
|f(x) P(x)|
h
2
8
(17.67)
Since we want to ensure that the error is no larger than 10
5
we set
h
2
8
< 10
5
(17.68)
or
h <
8 10
5
0.0089 (17.69)
so if choose any step size smaller than h 0.0089 we are guaranteed to have an error
of no larger than 10
5
.
Math 481A
California State University Northridge
2008, B.E.Shapiro
Last revised: November 16, 2011