You are on page 1of 15

Lecture Notes for Math-CSE 451:

Introduction to Numerical Computation


Wen Shen
2011

2
These notes are used by myself in the course. They are provided to students. These
notes are self-contained, and covers almost everything we go through in class. However,
if a student shall be interested in more detailed discussion, he/she should consult the
recommended textbook.
Text: Guide to Scientific Computing, 2nd edition, by Peter Turner. CRC Press, ISBN
0-8493-1242-6.

Chapter 2

Polynomial interpolation
2.1

Introduction

Problem description:
Given (n + 1) points, (xi , yi ), i = 0, 1, 2, , n, with distinct xi , probably sorted
x0 < x1 < x2 < < xn ,
find a polynomial of degree n, call it Pn (x),
Pn (x) = a0 + a1 x + a2 x2 + + an xn
such that it interpolates these points:
Pn (xi ) = yi ,

i = 0, 1, 2, , n

The goal is to determine the coefficients a0 , a1 , , an .


Note that the number of points is 1 larger than the degree of the polynomial.
Why should we do this? Here are some reasons:
Find the values between the points for discrete data set;
To approximate a (probably complicated) function by a polynomial;
Then, it is easier to do computations such as derivative, integration etc.
We start with a simple example.
Example 1. Given the table
xi 0 1 2/3
yi 1 0 0.5
3

CHAPTER 2. POLYNOMIAL INTERPOLATION

Interpolate the data set with a polynomial with degree 2.


Note that this data set satisfies
yi = cos(xi /2).
Answer. Let
P2 (x) = a0 + a1 x + a2 x2
We need to find the coefficients a0 , a1 , a2 . By the interpolating properties, we have
x = 0, y = 1 : P2 (0) = a0 = 1
x = 1, y = 0 : P2 (1) = a0 + a1 + a2 = 0
x = 2/3, y = 0.5 : P2 (2/3) = a0 + (2/3)a1 + (4/9)a2 = 0.5
Here we have 3 equations and 3 unknowns. Writing it in matrix-vector form


1 0 0
a0
1
1 1 1 a1 = 0
a2
0.5
1 23 94
Easy to solve in Matlab (see Homework 1)
a0 = 1,
Then

a1 = 1/4,

a2 = 3/4.

3
1
P2 (x) = 1 x x2 .
4
4

The general case with (n + 1) points:


Pn (xi ) = yi ,

i = 0, 1, 2, , n

We will have (n + 1) equations:


Pn (x0 ) = y0 : a0 + x0 a1 + x20 a2 + + xn0 an = y0
Pn (x1 ) = y1 : a0 + x1 a1 + x21 a2 + + xn1 an = y1
..
.
Pn (xn ) = yn : a0 + xn a1 + x2n a2 + + xnn an = yn

In matrix-vector form
1 x0 x20
1 x1 x2
1

.. ..
.. . .
. .
.
.
2
1 xn xn


a0
y0
xn0
n

x1 a1 y1

.. .. = ..
. . .

xnn

an

yn

2.2. LAGRANGE INTERPOLATION


or with compact notation
X~a = ~y
where
X
~a
~y

:
:
:

(n + 1) (n + 1) matrix, given, (van der Monde matrix)


unknown vector, with length (n + 1)
given vector, with length (n + 1)

Known: if xi s are distinct, then X is invertible, therefore ~a has a unique solution.


In Matlab, the command vander([x1 , x2 , , xn ]) gives this matrix.
Bad news: X has very large condition number, not effective to solve if n is large.
Other more efficient and elegant methods include
Lagrange polynomials
Newtons divided differences

2.2

Lagrange interpolation

Given points: x0 , x1 , , xn
Define the cardinal functions: l0 , l1 , , ln : P n , which are polynomials of degree n,
and satisfy the properties

1 , i=j
i = 0, 1, , n
li (xj ) = ij =
0 , i 6= j
In words: the cardinal function li (x) takes value 1 for x = xi , but for all other interpolating points xj with j 6= i, it takes the value 0. For x value between the points xi , it
does not set any restrictions.
The Lagrange form of the interpolation polynomial is
Pn (x) =

n
X

li (x) yi .

i=0

We check the interpolating property:


Pn (xj ) =

n
X

li (xj ) yi = yj ,

j.

i=0

The cardinal functions li (x) can be written as




n
Y
x xj
li (x) =
xi xj
j=0,j6=i

x x0 x x1
x xi1 x xi+1
x xn

xi x0 xi x1
xi xi1 xi xi+1
xi xn

CHAPTER 2. POLYNOMIAL INTERPOLATION

One can easily check that li (xi ) = 1 and li (xk ) = 0 for i 6= k, i.e., li (xk ) = ik .
Example 2. Consider again (same as in Example 1)
xi 0 2/3 1
yi 1 0.5 0
Write the Lagrange polynomial.
Answer. We first compute the cardinal functions
l0 (x) =
l1 (x) =
l2 (x) =

x 2/3 x 1
3
2

= (x )(x 1)
0 2/3 0 1
2
3
x1
9
x0

= x(x 1)
2/3 0 2/3 1
2
2
x 0 x 2/3

= 3x(x )
1 0 1 2/3
3

so
P2 (x) = l0 (x)y0 + l1 (x)y1 + l2 (x)y2
3
2
9
=
(x )(x 1) x(x 1)(0.5) + 0
2
3
2
3 2 1
= x x+1
4
4
This is the same as in Example 1.
Pros and cons

of Lagrange polynomial:

Elegant formula, (+)


slow to compute, each li (x) is different, (-)
Not flexible: if one changes a points xj , or add on an additional point xn+1 , one
must re-compute all li s. (-)

2.3

Newtons divided differences

Given a data set


xi
yi

x0 x1
y0 y1

xn
yn

We will try to design an algorithm in a recursive form.

2.3. NEWTONS DIVIDED DIFFERENCES


n=0

P0 (x) = y0

n=1

P1 (x) = P0 (x) + a1 (x x0 )

Determine a1 : set in x = x1 , then P1 (x1 ) = P0 (x1 ) + a1 (x1 x0 )


y1 y0
so y1 = y0 + a1 (x1 x0 ), we get a1 =
x1 x0
n = 2 : P2 (x) = P1 (x) + a2 (x x0 )(x x1 )
set in x = x2 : then y2 = P1 (x2 ) + a2 (x2 x0 )(x2 x1 )
y2 P1 (x2 )
so a2 =
(x2 x0 )(x2 x1 )
General expression for an :
Assume that Pn1 (x) interpolates (xi , yi ) for i = 0, 1, , n 1. We will find Pn (x) that
interpolates (xi , yi ) for i = 0, 1, , n, in the form
Pn (x) = Pn1 (x) + an (x x0 )(x x1 ) (x xn1 )
where
an =

yn Pn1 (xn )
(xn x0 )(xn x1 ) (xn xn1 )

(2.1)

(2.2)

One can easily check that such polynomial does the interpolating job!
(Detail: For i = 0, 1, , n 1, we have Pn (xi ) = Pn1 (xi ) = yi , since the last term in (2.1)
is 0. Now, for i = n, we have Pn (xn ) = yn since an is chosen as (2.2) which guarantees
this interpolating property. )
This now give the Newtons form for interpolating polynomial:
Pn (x) = a0 + a1 (x x0 ) + a2 (x x0 )(x x1 ) +
+an (x x0 )(x x1 ) (x xn1 ),
where the coefficient an could be determined by (2.2).
There is a more elegant way of computing these coefficients, which we will study now.
The constants ai s are called divided difference, written as
a0 = f [x0 ],

a1 = f [x0 , x1 ]

ai = f [x0 , x1 , , xi ]

These divided differences could be computed recursively, as


f [x0 , x1 , , xk ] =

f [x1 , x2 , , xk ] f [x0 , x1 , , xk1 ]


xk x0

(2.3)

Proof for (2.3): (optional) The proof is done through induction. The formula is clearly
true for n = 0 and n = 1. Now, assume that it holds for n = k 1, i.e., one can use it

CHAPTER 2. POLYNOMIAL INTERPOLATION

to write interpolation polynomial of degree k 1, to interpolate k points, in Newtons


form. We now show that it also holds for n = k, for any k. By induction, it holds for
all n.
k1
Let Pk1 (x) interpolates the points (xi , yi )i=0
, and let q(x) interpolates the points
k
(xi , yi )i=1 . Note that, comparing to Pk1 , the function q(x) does not interpolate (x0 , y0 ),
instead it interpolates an extra point of (xk , yk ). Both Pk1 and q(x) are polynomials
of degree k 1. By our assumption, formula (2.3) holds in Newtons form, i.e.,

Pk1 (x) = Pk2 (x) + f [x0 , , xk1 ](x x0 )(x x1 ) (x xk2 )


= f [x0 , , xk1 ]xk1 + (l.o.t.) (i.e. lower order terms)
q(x) = f [x1 , , xk ]x
We now set
Pk = q(x) +

k1

+ (l.o.t.)

(2.4)
(2.5)

x xk
(q(x) Pk1 (x)) .
xk x0

We claim that Pk (x) interpolates all the points (xi , yi )ki=0 .


To check this claim, we go through all the point xi with i = 0, 1, 2, , k, as
i = 1, 2, , k :
i=0:
i=k:

q(xi ) = Pk1 (xi ) = yi , Pk (xi ) = yi


x0 xk
(q(x0 ) y0 ) = y0 ,
Pk (x0 ) = q(x0 ) +
xk x0
Pk (xk ) = q(xk ) + 0 = yk .

By using (2.4)-(2.5), we can now write


Pk (x) = f [x1 , , xk ]xk1 + l.o.t.
i
x xk h
+
f [x0 , , xk1 ]xk1 + (l.o.t.)
xk x0
f [x1 , , xk ] f [x0 , , xk1 ] k
x + (l.o.t.)
=
xk x0
Comapring this to the Newtons form for Pk
Pk (x) = Pk1 (x) + f [x0 , , xk ](x x0 ) (x xk1 )
= f [x0 , , xk ]xk + (l.o.t.)
Since these are the same polynomial (Uniqueness of interpolating polynomials, coming
later), they must have matching coefficients for the leading term xk , i.e.,
f [x0 , , xk ] =

f [x1 , , xk ] f [x0 , , xk1 ]


,
xk x0

proving (2.3).
Computation of the divided differences: We compute the f s through the following
table:

2.3. NEWTONS DIVIDED DIFFERENCES


x0

f [x0 ] = y0

x1

f [x1 ] = y1

f [x0 , x1 ] =

f [x1 ]f [x0 ]
x1 x0

x2

f [x2 ] = y2

f [x1 , x2 ] =

f [x2 ]f [x1 ]
x2 x1

..
.

..
.

..
.

xn

f [xn ] = yn

f [xn1 , xn ] =

f [xn ]f [xn1 ]
xn xn1

f [x0 , x1 , x2 ] =
..
.

..

f [xn2 , xn1 , xn ] =

...

.
f [x0 , x1 , , xn ]

Example : Use Newtons divided difference to write the polynomial that interpolates
the data
xi 0 1 2/3 1/3
yi 1 0 1/2 0.866
Answer. Set up the triangular table for computation
0
1
2/3
1/3

1
0
0.5
0.8660

-1
-1.5
-1.0981

-0.75
-0.6029

0.4413

So we have
a0 = 1,

a1 = 1,

a2 = 0.75,

a3 = 0.4413,

x0 = 0,

x1 = 1,

x2 = 2/3,

x3 = 1/3,

therefore the interpolating polynomial in Newtons form is


P3 (x) = 1 + -1 x + -0.75 x(x 1) + 0.4413 x(x 1)(x 2/3).

Flexibility of Newtons form: easy to add additional points to interpolate.


For example, if you add one more point to interpolate, say (x4 , y4 ) = (0.5, 1), you could
keep all the work, and add one more line in the table, to get a4 . Try it by yourself!
Nested form:
Pn (x) = a0 + a1 (x x0 ) + +a2 (x x0 )(x x1 ) +
+an (x x0 )(x x1 ) (x xn1 )
= a0 + (x x0 ) (a1 + (x x1 )(a2 + (x x2 )(a3 + + an (x xn1 ))))
Effective to compute in a program: Given the data xi and ai for i = 0, 1, , n, one can
evaluate the Newtons polynomial p = Pn (x) by the following algorithm (pseudo-code):
p = an

10

CHAPTER 2. POLYNOMIAL INTERPOLATION


for k = n 1, n 2, , 0
p = p(x xk ) + ak
end

Existence and Uniqueness theorem for polynomial interpolation:


Given (xi , yi )ni=0 , with xi s distinct. There exists one and only polynomial Pn (x) of
degree n such that
Pn (xi ) = yi ,
i = 0, 1, , n

Proof. : The existence of such a polynomial is obvious, since we can construct it using
one of our methods.
Regarding uniqueness: Assume we have two polynomials, call them p(x) and q(x), of
degree n, both interpolate the data, i.e.,
p(xi ) = yi ,

q(xi ) = yi ,

i = 0, 1, , n

Now, let g(x) = p(x) q(x), which will be a polynomial of degree n. Furthermore,
we have
g(xi ) = p(xi ) q(xi ) = yi yi = 0,
i = 0, 1, , n
So g(x) has n + 1 zeros. We must have g(x) 0, therefore p(x) q(x).

2.4

Errors in Polynomial Interpolation

Given a function f (x) on the interval a x b, and a set of distinct points xi [a, b],
i = 0, 1, , n. Let Pn (x) be a polynomial of degree n that interpolates f (x) at xi ,
i.e.,
Pn (xi ) = f (xi ),
i = 0, 1, , n
Define the error
e(x) = f (x) Pn (x),

x [a, b].

Theorem. There exists some value [a, b], such that


n

Y
1
e(x) =
f (n+1) () (x xi ),
(n + 1)!
i=0

Proof. If f Pn , then f (x) = Pn (x), trivial.

for all x [a, b].

11

2.4. ERRORS IN POLYNOMIAL INTERPOLATION


Now assume f
/ Pn . For x = xi , we have e(xi ) = f (xi ) Pn (xi ) = 0, OK.
Now fix an a such that a 6= xi for any i. We define
n
Y
(x xi )
W (x) =

Pn+1

i=0

and a constant
c=

f (a) Pn (a)
,
W (a)

and another function


(x) = f (x) Pn (x) cW (x).
Now we find all the zeros for this function :
(xi ) = f (xi ) Pn (xi ) cW (xi ) = 0,

i = 0, 1, , n

and
(a) = f (a) Pn (a) cW (a) = 0
So, has at least (n + 2) zeros.
Here goes our deduction:
(x) has at least
(x) has at least
(x) has at least
(n+1) (x) has at least

n+2
n+1
n
..
.

zeros.
zeros.
zeros.

zero.

Call it .

So we have
(n+1) () = f (n+1) () 0 cW (n+1) () = 0.
Use
W (n+1) = (n + 1)!
we get
f (n+1) () = cW (n+1) () =

f (a) Pn (a)
(n + 1)!.
W (a)

Change a into x, we get


n

Y
1
1
e(x) = f (x) Pn (x) =
f (n+1) ()W (x) =
f (n+1) () (x xi ).
(n + 1)!
(n + 1)!
i=0

Example n = 1, x0 = a, x1 = b, b > a.

12

CHAPTER 2. POLYNOMIAL INTERPOLATION

We have an upper bound for the error, for x [a, b],


|e(x)| =

1
1 (b a)2
1
f () |(x a)(x b)| f
= f (b a)2 .
2
2
4
8

Observation: Different distribution of nodes xi would give different errors.


Uniform nodes: equally distribute the space. Consider an interval [a, b], and we
distribute n + 1 nodes uniformly as
xi = a + ih,
One can show that

h=

n
Y

ba
,
n

|x xi |

i=0

i = 0, 1, , n.

1 n+1
h
n!
4

(Try to prove it!)


This gives the error estimate

where



Mn+1 n+1
1
(n+1) n+1
h
(x) h

|e(x)|
f
4(n + 1)
4(n + 1)




Mn+1 = max f (n+1) (x) .
x[a,b]

Example Consider interpolating f (x) = sin(x) with polynomial on the interval [1, 1]
with uniform nodes. Give an upper bound for error, and show how it is related with
total number of nodes with some numerical simulations.
Answer. We have
so the upper bound for error is



(n+1)
(x) n+1
f

n+1
|e(x)| = |f (x) Pn (x)|
4(n + 1)

 n+1
2
.
n

Below is a table of errors from simulations with various n.


n
4
8
16

error bound
4.8 101
3.2 103
1.8 109

measured error
1.8 101
1.2 103
6.6 1010

Problem with uniform nodes: peak of errors near the boundaries. See plots.

13

2.4. ERRORS IN POLYNOMIAL INTERPOLATION


Chebychev nodes:

equally distributing the error.

Type I: including the end points.


For interval [1, 1]

x
i = cos( ni ),

For interval [a, b]

x
i = 12 (a + b) + 12 (b a) cos( ni ),

i = 0, 1, , n
i = 0, 1, , n

With this choice of nodes, one can show that


n
Y

|x x
k | = 2n

k=0

n
Y

|x xk |

k=0

where xk is any other choice of nodes.


This gives the error bound:
|e(x)|



1
(n+1) n
(x) 2 .
f
(n + 1)!

Example Consider the same example with uniform nodes, f (x) = sin x. With Chebyshev nodes, we have
1
n+1 2n .
|e(x)|
(n + 1)!
The corresponding table for errors:
n
4
8
16

error bound
1.6 101
3.2 104
1.2 1011

measured error
1.15 101
2.6 104
1.1 1011

The errors are much smaller!


Type II: Chebyshev nodes can be chosen strictly inside the interval [a, b]:
x
i =

1
2i + 1
1
(a + b) + (b a) cos(
),
2
2
2n + 2

i = 0, 1, , n

See slides for examples.

Theorem. If Pn (x) interpolates f (x) at xi [a, b], i = 0, 1, , n, then


n
Y
(x xi ),
f (x) Pn (x) = f [x0 , x1 , , xn , x]
i=0

x 6= xi .

14

CHAPTER 2. POLYNOMIAL INTERPOLATION

Proof. Let a 6= xi , let q(x) be a polynomial that interpolates f (x) at x0 , x1 , , xn , a.


Newtons form gives
n
Y
q(x) = Pn (x) + f [x0 , x1 , , xn , a] (x xi ).
i=0

Since q(a) = f (a), we get


f (a) = q(a) = Pn (a) + f [x0 , x1 , , xn , a]

n
Y
(a xi ).
i=0

Switching a to x, we prove the Theorem.


As a consequence, we have:
f [x0 , x1 , , xn ] =

1 (n)
f (),
n!

[a, b].

Proof. Let Pn1 (x) interpolate f (x) at x0 , , xn1 . The error formula gives
n

Y
1
f (xn ) Pn1 (xn ) = f (n) () (xn xi ),
n!

(a, b).

i=0

From above we know


f (xn ) Pn1 (xn ) = f [x0 , , xn ]

n
Y
(xn xi )
i=0

Comparing the rhs of these two equation, we get the result.


Observation: Newtons divided differences are related to derivatives.
n=1
n=2

:
:

f [x0 , x1 ] = f (), (x0 , x1 )


f [x0 , x1 , x2 ] = f (). Let x0 = x h, x1 = x, x2 = x + h, then

f [x0 , x1 , x2 ] =

2.5

1
1
[f (x + h) 2f (x) + f (x + h)] = f (),
2
2h
2

[x h, x + h].

Convergence for polynomial interpolation

Here we briefly discuss the convergence issue. The main question to ask is: As n +,
does the polynomial Pn (x) converge to the function f (x)?

2.5. CONVERGENCE FOR POLYNOMIAL INTERPOLATION

15

Convergence may be understood in different ways. Let e(x) = f (x) Pn (x) be the error
function. We list some examples of convergence:
uniform convergence :
L1 convergence :
L2 convergence :

lim

max |e(x)| = 0
Z b
|e(x)| dx = 0
lim
n+ a
Z b
|e(x)|2 dx = 0
lim
n+ axb

n+ a

For uniform nodes, it is known that for some functions f (x), the error grows unbounded
as n +. We have observed it in our simulation, and it is very bad news.
It is possible to have convergence: For each function f (x), there is a way of designing a
sequence of nodes {xi }ni=0 , such that |e(x)| 0 as n +.
The sequence is different for each function.
With the wrong sequence, there will be no convergence!
This is not practical!

You might also like