Está en la página 1de 14

Lesson 17

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 + x. Construct the Lagrange polynomial of degree


at most 2 to interpolate the point f(0.45) using grid points at x
0
= 0, x
1
= 0.6 and
x
2
= 0.9.
Solution. First we calculate the f
i
:
f
0
= f(x
0
) = f(0) =

1 = 1 (17.29)
f
1
= f(x
1
) = f(0.6) =

1.6 1.265 (17.30)


f
2
= f(x
2
) = f(0.9) =

1.9 1.378 (17.31)


So the Lagrange polynomial is
P(x) = L
20
(x)f
0
+ L
21
(x)f
1
+ L
22
(x)f
2
(17.32)
= L
20
(x) + 1.27L
21
(x) + 1.38L
22
(x) (17.33)
2008, B.E.Shapiro
Last revised: November 16, 2011
Math 481A
California State University Northridge
114 LESSON 17. LAGRANGE INTERPOLATION
Where
L
20
(x) =
(x 0.6)(x 0.9)
(0 0.6)(0 0.9)
= 1.85(x 0.6)(x 0.9) (17.34)
L
21
(x) =
(x 0)(x 0.9)
(0.6 0)(0.6 0.9)
= 5.56x(x 0.9) (17.35)
L
22
(x) =
(x 0)(x 0.6)
(0.9 0)(0.9 0.6)
= 3.70x(x 0.6) (17.36)
Thus
P(x) = L
20
(x) + 1.27L
21
(x) + 1.38L
22
(x) (17.37)
= 1.85(x 0.6)(x 0.9) 1.27(5.56)x(x 0.9)
+ 1.38(3.70)x(x 0.6) (17.38)
= 1.85(x 0.6)(x 0.9) 7.03x(x 0.9) + 5.11x(x 0.6) (17.39)
= 0.999 + 0.486x 0.07x
2
(17.40)
Hence
P(0.45) 0.999 + (0.486)(0.45) 0.07)(0.45)
2
= 1.20 (17.41)
We summarize the algorithm for Lagrange Interpolation here.
1
Algorithm LagrangeInterpolatingFunctions
Input: x
0
, . . . , x
n
, x
For i = 0, 1, . . . , n,
Dene the set U
i
={x
0
, . . . , x
n
} {x
i
}
Dene numerator = 1, denominator = 1
For j = 0, . . . , n 1
numerator = numerator (x U
ij
)
denominator = denominator (x
i
U
ij
)
End For
L
ni
= numerator/denominator
End For
Return the list {L
n1
, . . . , L
nn
}
Algorithm LagrangeInterpolatingPolynomial
Input: x
0
, . . . , x
n
, f
0
, . . . , f
n
, x
Let L be the list LagrangeInterpolatingFunctions(x
0
, . . . , x
n
, x)
1
The notation A B, where A and B are sets, means the relative complement of the set B in
the set A, e.g., all of the elements of A that are not in B. For an ordered set U
i
, the notation U
ij
means the j
th
element of U
i
. An ordered set is also called a List.
Math 481A
California State University Northridge
2008, B.E.Shapiro
Last revised: November 16, 2011
LESSON 17. LAGRANGE INTERPOLATION 115
P = f
0
L
0
+ f
1
L
1
+ + f
n
L
n
Return P
We now illustrate how to calculate the Lagrange Interpolating Polynomials both an-
alytically and numerically in Mathematica. First we make a few observations. The
relative complement of the set B in A is given by Complement[A, B]., e.g,
In:=
U={x1, x2, x3, x4, x5}
Complement[U, {x4}]
Out:=
{x1, x2, x3, x5}
Next, we observe that if U is a list such as the one dened above, then Map[f, U]
returns the result of f[u], for every element u of U. Recall that f/@U is a shorthand
for Map[f, U],
In:=
f/@U
Out:=
{f[x1], f[x2], f[x3], f[x4], f[x5]}
Suppose that f[x] represents the function f(x) = x 3. We can calculate some
value, say f(u) in two dierent ways. The rst is the usual way,
In:=
f[x_]:=x-3;
f[u]
Out:=
u-3
2008, B.E.Shapiro
Last revised: November 16, 2011
Math 481A
California State University Northridge
116 LESSON 17. LAGRANGE INTERPOLATION
The second way is to use pure functions:
In:=
(#-3)&[u]
Out:=
u-3
Pure functions allow us to dene a function and use it in a single statement. In-
stead of saying f[x] we replace the f with the pure function (#-3)&. The symbol &
tells us where the function denition ends, and the symbol # is used in place of the
functions argument x. We can also combine pure functions with the Map function.
This is convenient because it lets us map an expression that we are only going to use
once; otherwise wed have to use an extra line of code to dene an unnecessary extra
variable to hold the function. Thus
In:=
f[x_]:= x-3; V=Map[f, U]
and
In:=
V=(#-3)&/@U
both return the identical output
Out:=
{-3 + x1, -3 + x2, -3 + x3, -3 + x4, -3 + x5}
Next, we observe that the generalization of multiplication in Mathematicais the Times
function. Times can take an arbitrary number of arguments and returns their prod-
uct. Thus
In:=
Times[-3+x1, -3+x2, -3+x3, -3+x4, -3+x5]
Math 481A
California State University Northridge
2008, B.E.Shapiro
Last revised: November 16, 2011
LESSON 17. LAGRANGE INTERPOLATION 117
Out:=
(-3 + x1) (-3 + x2) (-3 + x3) (-3 + x4) (-3 + x5)
To multiply out the elements of V we need to take all the elements of V and place them
as arguments to Times. We do this with the Apply command, which has a shorthand
of @@. The following are:
In:=
Apply[Times, V]
In:=
Times@@V
and both return the same thing (recall the denition of V, above):
Out:=
(-3 + x1) (-3 + x2) (-3 + x3) (-3 + x4) (-3 + x5)
Now suppose we want to combine these two functions. We want to subtract 3 from
every element of the list U, which we can do with Map, and then take the product of
the results with Apply and Times:
In:=
Times@@(#-3)&/@U
or In:=
Apply[Times, Map[(# - 3)&, U]]
both of which return
Out:=
(-3 + x1) (-3 + x2) (-3 + x3) (-3 + x4) (-3 + x5)
With this we can dene a function to calculate the Lagrange Interpolating Functions
in Mathematica.
2008, B.E.Shapiro
Last revised: November 16, 2011
Math 481A
California State University Northridge
118 LESSON 17. LAGRANGE INTERPOLATION
LagrangeInterpolatingFunctions[{xj__}, x_] :=
Module[ {i, n, xi, xjc, L, xgrid, num, den},
xgrid = {xj};
n = Length[xgrid];
L = {};
For[i = 1, i <= n, i++,
xi = xgrid[[i]];
xjc = Complement[xgrid, {xi}];
den = Times @@ ((xi - #) & /@ xjc);
num = Times @@ ((x - #) & /@ xjc);
L = Append[L, num/den];
];
Return[L];
]
We can now calculated a set of functions analytically. For example,
In:=
LagrangeInterpolatingFunctions[{x1, x2, x3}, x]
Out:=
_
(x x2)(x x3)
(x1 x2)(x1 x3)
,
(x x1)(x x3)
(x2 x1)(x2 x3)
,
(x x1)(x x2)
(x3 x1)(x3 x2)
_
Repeating our earlier example,
In:=
LagrangeInterpolatingFunctions[{0, 0.6, 0.9}, x]
Out:=
{1.85185 (-0.9 + x) (-0.6 + x), -5.55556 (-0.9 + x) x,
3.7037 (-0.6 + x) x}
We can also calculate at a point:
In:=
LagrangeInterpolatingFunctions[{0, 0.6, 0.9},0.45]
Math 481A
California State University Northridge
2008, B.E.Shapiro
Last revised: November 16, 2011
LESSON 17. LAGRANGE INTERPOLATION 119
Out:=
{0.125, 1.125, -0.25}
Next we observe that the dot product of two lists A and B of the same length is
calculated with the dot operator, which is a period:
In:=
{f1, f2, f3, f4}.{L1, L2, L3, L4}
Out:=
f1 L1 + f2 L2 + f3 L3 + f4 L4
So we can repeat our previous example as follows:
In:=
f[x_]:= Sqrt[1.0+x];
points = {0.0, 0.6, 0.9};
(f/@points).LagrangeInterpolatingFunctions[points, 0.45]
Out:=
1.20342
We can get the Lagrange Interpolating Polynomial with
In:=
(f /@ points).LagrangeInterpolatingFunctions[points, x] // Expand
Out:=
1.+ 0.483656 x - 0.0702286 x^2
Theorem 17.2 (Error Bounds for Lagrange Interpolation). Suppose that f(x)
is n + 1 times continuously dierentiable, and suppose that the points x
0
, . . . , x
n
are
distinct. Then for any x [a, b] there exists a number c [a, b] such that
f(x) = P(x) +
f
n+1
(c)(x x
0
)(x x
1
) (x x
n
)
(n + 1)!
(17.42)
2008, B.E.Shapiro
Last revised: November 16, 2011
Math 481A
California State University Northridge
120 LESSON 17. LAGRANGE INTERPOLATION
where
P(x) =
n

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

(c)| max |(x x


0
)(x x
1
)| (17.61)
on each interval. Since f(x) = log x we have f

(x) = 1/x and 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) = 0. At the endpoints g(x) = 0. So rst we dierentiate:


0 = 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

También podría gustarte