chapter 3 - GMU Math
CHAPTER 3
E
Interpolation
FFICIENT ways of representing data are fundamental to advancing the understanding of scientific
problems. This chapter introduces polynomial interpolation and spline interpolation as convenient tools for finding functions that pass through given data points.
3.1 Data and interpolating functions
mathematical function is said to interpolate a set of data points if it passes through those
points. Suppose you have collected a set of (x, y) data points, such as (0, 1), (2, 2), and
(3, 4). There is a parabola that passes through the three points, shown in Figure 3.1. This parabola
is called the degree 2 interpolating polynomial passing through these three points.
A
Definition 3.1 The function y = P (x) interpolates the data points (x1 , y1 ), . . . , (xn , yn ) if P (xi ) = yi )
for each 1 ≤ i ≤ n.
4
3
2
1
−1
1
2
3
4
−1
Figure 3.1: Interpolation by parabola. The points (0, 1), (2, 2), and (3, 4) are interpolated by the
function P (x) = 21 x2 − 21 x + 1.
Note that P is required to be a function, that is, each value x corresponds to a single y. This
puts a restriction on the set of data points {(xi , yi )} that can be interpolated - the xi ’s must be all
distinct, in order for a function to pass through them. There is no such restriction on the yi ’s.
Now suppose we are given a set of points (x1 , y1 ), . . . , (xn , yn ) with all distinct xi ’s. Does an
interpolating function always exist? Of course the answer is yes - one could simply “connect the
dots”, draw line segments between the points, and that would qualify as an interpolating function.
132
3.1
DATA
AND INTERPOLATING FUNCTIONS
0
S POTLIGHT ON: Complexity
Why polynomials? Polynomials are very often used for interpolation because of their straightforward
mathematical properties. There is simple theory about when an interpolating polynomial of a given degree exists for a given set of points. More important, in a real sense polynomials are the most fundamental
of functions for digital computers. Central processing units usually have fast methods in hardware for
adding and multiplying floating points numbers, which are the operations needed to evaluate a polynomial. Complicated functions can be approximated by interpolating polynomials in order to make them
computable with these two hardware operations.
More useful would be a single polynomial through the data points. In the remainder of this
section, we will focus on the problem of finding interpolating polynomials.
Interpolation is the reverse of evaluation. In polynomial evaluation (such as Horner’s method)
we are given a polynomial and asked to evaluate a y-value for a given x-value - that is, compute
points lying on the curve. Polynomial interpolation asks for the opposite process: Given these
points, compute a polynomial that can generate them.
Does such a polynomial always exist? Again, we must assume that the x-coordinates of the
points are distinct. It would be impossible, by definition of function, for a polynomial or any
function to go through two points with the same x coordinate and different y coordinates. With
that proviso, the answer is yes. No matter how many points are given, there is some polynomial
y = P (x) that runs through all the points. We will prove this and several other facts about the
existence of interpolating polynomials. Later, we will consider how to interpolate with other, nonpolynomial, functions, such as splines.
3.1.1 Lagrange interpolation
Assume we have n data points (x1 , y1 ), . . . , (xn , yn ) and would like to find an interpolating polynomial. With any problem, it is good to know before the computing starts how many solutions there
are. Let’s fix the degree d of the polynomial. How many degree d polynomials pass through the n
points? The answer depends on how the points are situated with respect to one another.
The case d = 1 is simple but illuminating. Either the points are collinear, in which case there is
a degree 1 interpolating polynomial, or they are not collinear, and none exists. Again, we ignore the
case of the points lying along a vertical line, because we will require in this chapter that all xi are
distinct.
For polynomials of degree greater than one, we will have to work a little harder, but by the
time we are done we will have a computational method for determining exactly what degrees of
polynomials go through a given set of points, as well as for computing the polynomials explicitly
when they exist.
If we are given n data points, there is an explicit formula for writing down a polynomial of
degree d = n − 1 which interpolates the points. This is called the Lagrange interpolating formula.
Chapter 3 I NTERPOLATION
133
For example, suppose we are given three points (x1 , y1 ), (x2 , y2 ), (x3 , y3 ). Then the polynomial
P2 (x) = y1
(x − x2 )(x − x3 )
(x − x1 )(x − x3 )
(x − x1 )(x − x2 )
+ y2
+ y3
(x1 − x2 )(x1 − x3 )
(x2 − x1 )(x2 − x3 )
(x3 − x1 )(x3 − x2 )
(3.1)
is the Lagrange interpolating polynomial for these points. First notice why the points each lie on the
polynomial curve. When x1 is substituted for x, the terms evaluate to y1 + 0 + 0 = y1 . The second
and third numerators are chosen to disappear when x1 is substituted, and the first denominator is
chosen just so to balance the first denominator so that y1 pops out. Similar things happen when x2
and x3 are substituted. When any other number is substituted for x, we have little control over the
result. But then, the job was only to interpolate at the three points - that’s the extent of our concern.
Second, notice that the polynomial (3.1) is of degree two in the variable x.
Example 3.1 Find an interpolating polynomial for the data points (0, 1), (2, 2) and (3, 4) in Figure 3.1.
Substituting into Lagrange’s formula (3.1) yields
(x − 2)(x − 3)
(x − 0)(x − 3)
(x − 0)(x − 2)
+2
+4
(0 − 2)(0 − 3)
(2 − 0)(2 − 3)
(3 − 0)(3 − 2)
1 2
1 2
1 2
(x − 5x + 6) + 2(− )(x − 3x) + 4 (x − 2x)
6
2
3
1 2 1
x − x + 1.
2
2
P2 (x) = 1
=
=
Finally, check that P2 (0) = 1, P2 (2) = 2, and P2 (3) = 4.
In general, suppose we are presented with n points (x1 , y1 ), . . . , (xn , yn ). For each k between 1
and n, define the term
Lk (x) =
(x − x1 ) · · · (x − xk−1 )(x − xk+1 ) · · · (x − xn )
.
(xk − x1 ) · · · (xk − xk−1 )(xk − xk+1 ) · · · (xk − xn )
The interesting property of Lk is that Lk (xk ) = 1 while Lk (xj ) = 0, where xj is any of the other
data points. Then define the degree n − 1 polynomial
Pn−1 (x) = y1 L1 (x) + . . . + yn Ln (x)
This is a straightforward generalization of the polynomial in (3.1), and works the same way. Substituting xk for x yields
P (xk ) = y1 L1 (xk ) + . . . + yn Ln (xk ) = 0 + . . . + 0 + yk Lk (xk ) + 0 + . . . + 0 = yk ,
so it works as designed.
We have constructed a polynomial of degree at most n − 1 that passes through any set of n
points with distinct xi ’s. Next it is natural to ask how many of these there are. Assume for the
sake of discussion that there are two, say P (x) and Q(x), that have degree at most n − 1 and that
both interpolate all n points. That is, we are assuming P (x1 ) = Q(x1 ) = y1 , P (x2 ) = Q(x2 ) =
y2 , . . . , P (xn ) = Q(xn ) = yn . Now define the new polynomial H(x) = P (x) − Q(x). Clearly
134
3.1
DATA
AND INTERPOLATING FUNCTIONS
the degree of H is also at most n − 1, and note that 0 = H(x1 ) = H(x2 ) = · · · = H(xn ), that is
H has n distinct zeros. According to the Fundamental Theorem of Algebra, a degree d polynomial
can have at most d zeros, unless it is the identically zero polynomial. Therefore H is the identically
zero polynomial, and P (x) ≡ Q(x). We conclude that there is a unique P (x) of degree ≤ n − 1
interpolating the n points (xi , yi ). These facts are summarized in the following theorem.
Theorem 3.2 (Main Theorem of Polynomial Interpolation) Let (x1 , y1 ), . . . , (xn , yn ) be n points in the
plane with distinct xi . Then there exists one and only one polynomial P of degree n − 1 or less that
satisfies P (xi ) = yi for i = 1, . . . , n.
Example 3.2 Find the polynomial of degree 3 or less that interpolates the points (0, 2), (1, 1), (2, 0), and
(3, −1).
The Lagrange form is
(x − 1)(x − 2)(x − 3)
(x − 0)(x − 2)(x − 3)
+1
(0 − 1)(0 − 2)(0 − 3)
(1 − 0)(1 − 2)(1 − 3)
(x − 0)(x − 1)(x − 3)
(x − 0)(x − 1)(x − 2)
+ 0
−1
(2 − 0)(2 − 1)(2 − 3)
(3 − 0)(3 − 1)(3 − 2)
1 3
1
1
= − (x − 6x2 + 11x − 6) + (x3 − 5x2 + 6x) − (x3 − 3x2 + 2x)
3
2
6
= −x + 2
P2 (x) = 2
Theorem 3.2 says that there exists exactly one interpolating polynomial of degree 3 or less - it may
or may not be exactly degree 3. In this case, the data points are collinear, so the interpolating
polynomial has degree 1. The theorem implies that there are no interpolating polynomials of degree
two or three. It may be already intuitively obvious to you that no parabola can pass through these
four collinear points, but here is the reason. Theorem 3.2 also forbids any four collinear points to
lie on a cubic polynomial.
3.1.2 Newton’s divided differences
The Lagrange interpolation method, as described in the previous section, never fails. It is a constructive way to write the unique polynomial promised by Theorem 3.2. It is also intuitive; one
glance explains why it works. However, it is seldom used for calculation due to the existence of
alternative methods that result in more manageable and less computationally complex forms.
Newton divided differences give a particularly simple way to write the interpolating polynomial.
Given n data points, the result will be a degree at most n−1 polynomial, just as Lagrange form does.
Theorem 3.2 says that it can be none other than the same as the lagrange interpolating polynomial,
written in a disguised form.
The idea of divided differences is fairly simple, but some notation needs to be mastered first.
View the data points as given by some function f , and list the data points in a table:
x1
x2
..
.
f (x1 )
f (x2 )
..
.
xn
f (xn )
Chapter 3 I NTERPOLATION
135
Now define the divided differences, which are the real numbers
f [xk ] = f (xk )
f [xk+1 ] − f [xk ]
f [xk xk+1 ] =
xk+1 − xk
f [xk+1 xk+2 ] − f [xk xk+1 ]
f [xk xk+1 xk+2 ] =
xk+2 − xk
f [xk+1 xk+2 xk+3 ] − f [xk xk+1 xk+2 ]
f [xk xk+1 xk+2 xk+3 ] =
xk+3 − xk
(3.2)
and so on. These numbers are the coefficients of the interpolating polynomial for the data points,
which is given by the Newton’s divided difference formula
P (x) = f [x1 ] + f [x1 x2 ](x − x1 )
+ f [x1 x2 x3 ](x − x1 )(x − x2 )
+ f [x1 x2 x3 x4 ](x − x1 )(x − x2 )(x − x3 )
+ ...
+ f [x1 · · · xn ](x − x1 ) · · · (x − xn−1 ).
(3.3)
A proof of this fact is given in Section 3.2.2. Notice that the divided difference formula gives the
interpolating polynomial as a nested polynomial in the sense of Horner’s method. It is automatically
ready to be evaluated in an efficient way.
Finally, the recursive definition of the Newton’s divided differences allows arrangement into a
convenient table. For three points the table has form
x1
f [x1 ]
f [x1 x2 ]
x2
f [x2 ]
f [x1 x2 x3 ]
f [x2 x3 ]
x3
f [x3 ]
and the coefficients of the polynomial (3.3) can be read from the top edge of the triangle.
Example 3.3 Use Newton’s divided difference to find the interpolating polynomial passing through the
points (0, 1), (2, 2), (3, 4).
Applying the definitions of divided differences leads to the table
0
1
1
2
2
2
3
4
2
1
2
136
3.1
DATA
AND INTERPOLATING FUNCTIONS
This table is computed as follows. After writing down the x and y coordinates down in separate
columns, calculate the next columns, left to right, as divided differences as in (3.2). For example,
2−1
2−0
=
1
2
2 − 12
1
=
3−0
2
4−2
3−2
= 2
After completing the divided difference triangle, the coefficients of the polynomial 1, 1/2, 1/2 can
be read from the top edge of the table. The interpolating polynomial can be written as
1
1
P (x) = 1 + (x − 0) + (x − 0)(x − 2),
2
2
or in nested form
P (x) = 1 + (x − 0)
1
1
+ (x − 2) ·
2
2
.
The base points for the nested form (see Horner’s Method in Chapter 0) are r1 = 0 and r2 = 2.
Alternatively, we could do more algebra and write the interpolating polynomial as
1
1
1
1
P (x) = 1 + x + x(x − 2) = x2 − x + 1,
2
2
2
2
matching the version Lagrange interpolation gave above.
Using the Newton divided difference approach, new data points that arrive after computing the
original interpolating polynomial can be easily added.
Example 3.4 Add the fourth data point (1, 0) to the list in Example 3.3.
We can keep the calculations that were already done and just add a new bottom row to the
triangle:
0
1
1
2
2
2
1
2
2
3
4
1
0
− 12
0
2
The result is one new term to add to the original polynomial P2 (x). Reading from the top edge of
the triangle, we see that the new degree 3 interpolating polynomial is
1
1
1
P3 (x) = 1 + (x − 0) + (x − 0)(x − 2) − (x − 0)(x − 2)(x − 3).
2
2
2
Note that P3 (x) = P2 (x) − 12 (x − 0)(x − 2)(x − 3), so the previous polynomial can be reused as
part of the new one.
Chapter 3 I NTERPOLATION
137
It is interesting to compare the extra work necessary to add a new point using the Lagrange
formulation versus the divided difference formulation. The Lagrange polynomial must be restarted
from the beginning when a new point is added; none of the previous calculation can be used. On
the other hand, in divided difference form, we keep the earlier work and add one new term to the
polynomial. Therefore the divided difference approach has a “real-time updating” property that the
Lagrange form lacks.
Example 3.5 Use Newton’s divided differences to find the interpolating polynomial passing through
(0, 2), (1, 1), (2, 0), (3, −1).
The Newton divided difference triangle is
0
2
1
1
2
0
−1
0
−1
0
0
−1
3
−1
Reading off the coefficients, we find that the interpolating polynomials of degree 3 or less is
P (x) = 2 + (−1)(x − 0) = 2 − x,
agreeing with Example 3.2 but with much less work.
3.1.3 How many degree d polynomials pass through n points?
Theorem 3.2, the Main Theorem of polynomial interpolation, answers this question if 0 ≤ d ≤ n−1.
Given n = 3 points (0, 1), (2, 2), (3, 4), there is one interpolating polynomial of degree 2 or less.
Example 3.1 shows that it is degree two, so there are no degree zero or one interpolating polynomials
through the three data points.
How many degree 3 polynomials interpolate the above three points? One way to construct one
such polynomial is clear from the previous discussion - add a fourth point. Extending the Newton
divided difference triangle gives a new top coefficient. In Example 3.4 the point (1, 0) was added.
The resulting polynomial
1
P3 (x) = P2 (x) + − (x − 0)(x − 2)(x − 3)
2
(3.4)
passes through the three points in question, in addition to the new point (1, 0). So there is at least
one degree 3 polynomial passing through our three original points (0, 1), (2, 2), (3, 4).
Of course, there are many different ways we could have chosen the fourth point. For example,
if we keep the same x4 = 1 and simply change y4 from 0, we must get a different degree 3 interpolating polynomial, since a function can only go through one y value at x4 . Now we know there
138
3.1
DATA
AND INTERPOLATING FUNCTIONS
are infinitely many polynomials that interpolate the three points (x1 , y1 ), (x2 , y2 ), (x3 , y3 ), since for
any fixed x4 there are infinitely many ways y4 can be chosen, each giving a different polynomial.
This line of thinking shows that given n data points (xi , yi ) with distinct xi , there are infinitely
many degree n polynomials passing through them.
A second look at (3.4) suggests a more direct way to produce interpolating polynomials of
degree 3 through 3 points. Instead of adding a fourth point to generate a new degree 3 coefficient,
why not cut out the middleman and just pencil in a new coefficient? Does the result interpolate the
original 3 points? Yes, because P2 (x) does, and the new term evaluates to zero at x1 , x2 and x3 .
So there is really no need to construct the extra Newton divided differences for this purpose. This
technique will also easily construct (infinitely many) polynomials of degree ≥ n for n given data
points, as illustrated in the next example.
Example 3.6 How many degree 0, 1, 2, 3, 4 and 5 polynomials pass through the points
(−1, −5), (0, −1), (2, 1) and (3, 11)?
The Newton’s divided difference triangle is
−1
−5
0
−1
2
1
4
−1
1
1
3
10
3
11
so there are no interpolating polynomials of degree 0, 1 or 2, and the single degree 3 is
P3 (x) = −5 + 4(x + 1) − (x + 1)x + (x + 1)x(x − 2).
There are infinitely many degree 4 interpolating polynomials
P4 (x) = P3 (x) + c1 (x + 1)x(x − 2)(x − 3)
for arbitrary c1 6= 0, and infinitely many degree 5 interpolating polynomials
P5 (x) = P3 (x) + c2 (x + 1)x2 (x − 2)(x − 3)
for arbitrary c2 6= 0.
3.1.4 Code for interpolation
The Matlab program for computing the coefficients follows:
%Program 3.1 Newton Divided Difference Interpolation Method
%Computes coefficients of interpolating polynomial
%Input: x and y are vectors containing the x and y coordinates
%
of the n data points
Chapter 3 I NTERPOLATION
139
%Output: coefficients c of interpolating polynomial in nested form
%Use with horner.m to evaluate interpolating polynomial
function c=newtdd(x,y,n)
for j=1:n
v(j,1)=y(j);
% Fill in y column of Newton triangle
end
for i=2:n
% For column i,
for j=1:n+1-i
% fill in column from top to bottom
v(j,i)=(v(j+1,i-1)-v(j,i-1))/(x(j+i-1)-x(j));
end
end
for i=1:n
c(i)=v(1,i);
% Read along top of triangle
end
% for output coefficients
This program can be applied to the data points of Example 3.3 to return the coefficients 1, 1/2, 1/2
found above. These coefficients can be used in the Horner’s method program to evaluate the interpolating polynomial at various x-values.
For example, the Matlab code segment
x0=[0 2 3];
y0=[1 2 4];
c=newtdd(x0,y0,3);
x=0:.01:4;
y=horner(2,c,x,x0);
plot(x0,y0,’o’,x,y)
will result in the plot shown in Figure 3.1.
Just for fun, now that we have Matlab code for finding the coefficients of the interpolating polynomial (newtdd.m) and for evaluating the polynomial (horner.m), we can put them together
to build a polynomial interpolation machine. We’ll use Matlab’s graphics capability to plot the interpolation polynomial as it is being created. Matlab’s mouse input command ginput is used to
facilitate data entry.
%Program 3.2. clickinterp.m Polynomial Interpolation Program
%Left click in Matlab figure window to locate data point.
%
Continue, to add more points.
%
Right-click to terminate program.
xl=-3;xr=3;yb=-3;yt=3;
plot([xl xr],[0 0],’k’,[0 0],[yb yt],’k’);grid on;
xlist=[];ylist=[];
button=1;k=0;
% initialize counter k
while(button ˜= 3) % if right click, terminate
[xnew,ynew,button] = ginput(1); % get one mouse click
if button == 1
% current click is a left click
k=k+1;
% k counts clicks
xlist(k)=xnew; ylist(k)=ynew; % add new point to the list
c=newtdd(xlist,ylist,k); % get interpolation coeffs
x=xl:.01:xr;
% define x coordinates of curve
y=horner(k-1,c,x,xlist); % get y coordinates of curve
plot(xlist,ylist,’o’,x,y,[xl xr],[0,0],’k’,[0 0],[yb yt],’k’);
140
3.1
DATA
AND INTERPOLATING FUNCTIONS
3
2
1
0
−1
−2
−3
−3
−2
−1
0
1
2
Figure 3.2: Interpolation Program 3.2 using mouse input.
clickinterp.m with 4 input data points.
3
Screenshot of Matlab code
axis([xl xr yb yt]);grid on;
end
end
3.1.5 Representing functions by approximating polynomials.
A major use of polynomial interpolation is to replace evaluation of a complicated function by evaluation of a polynomial, which of course involves only elementary computer operations like addition,
subtraction and multiplication. Think of this as a form of compression: something complex is replaced with something simpler, and computable, with perhaps some loss in accuracy that we will
have to analyze. We begin with an example from trigonometry.
Example 3.7 Interpolate the function sin x at 4 equally-spaced points on [0, π/2].
Let’s compress the sine function on the interval [0, π/2]. Take four data points at equally spaced
points and form the divided difference triangle. We list the values to 4 correct places:
0
0.0000
0.9549
π/6
0.5000
−0.2443
0.6691
2π/6
0.8660
3π/6
1.0000
−0.1139
−0.4232
0.2559
Chapter 3 I NTERPOLATION
141
0
S POTLIGHT ON: Compression
This is our first encounter with the concept of compression in numerical analysis. At first, interpolation
may not seem like compression. After all, we take n points as input and deliver n coefficients (of the
interpolating polynomial) as output. What has been compressed?
Think of the data points as coming from somewhere, say as representatives chosen from the
multitude of points on a curve y = f (x). The degree n − 1 polynomial, characterized by n coefficients,
is a “compressed version” of f (x), and may in some cases be used as a fairly simple representative of
f (x) for computational purposes.
For example, what happens when the sin button is pushed on a calculator? The calculator has
hardware to add and multiply, but how does it compute the sin of a number? Somehow the operation
must reduce to the evaluation of a polynomial, which requires exactly those operations. By choosing data
points lying on the sine curve, an interpolating polynomial can be calculated and stored in the calculator
as a compressed version of the sine function.
This type of compression is “lossy compression”, meaning there will be error involved, since
the sine function is not actually a polynomial. How much error is made when a function f (x) is replaced
by an interpolating polynomial is the subject of the next section.
The degree 3 interpolating polynomial is therefore
P3 (x) = 0 + 0.9549x − 0.2443x(x − π/6) − 0.1139x(x − π/6)(x − π/3)
= 0 + x(0.9549 + (x − π/6)(−0.2443 + (x − π/3)(−0.1139)))
(3.5)
This polynomial is graphed together with the sine function in Figure 3.3. At this level of resolution,
P3 (x) and sin x are virtually indistinguishable on the interval [0, π/2]. We have compressed the
infinite amount of information held by the sine curve into a few stored coefficients and the ability to
perform the 3 adds and 3 multiplies in (3.5).
How close are we to designing the sin button on a calculator? Certainly we need to handle
inputs from the entire real line. But due to the symmetries of the sine function, we have done the
hard part. The interval [0, π/2] is a so-called fundamental domain for sin, and an input from any
other interval can be referred back to it. Given an input x from [π/2, π], say, we can compute sin x
as sin(π − x), since sin is symmetric about x = π/2. Given an input x from [π, 2π], sin x =
− sin(2π − x) due to antisymmetry about x = π. Finally, because sin repeats its behavior on the
interval [0, 2π] across the entire real line, we can calculate for any input by first reducing modulo
2π. This leads to a straightforward design for the sin button:
%Program 3.3 Building a sin calculator button, attempt #1
%Approximates sin curve with degree 3 polynomial
%
(Caution: do not use to build bridges,
142
3.1
DATA
AND INTERPOLATING FUNCTIONS
%
at least until we have discussed accuracy.)
%Input: x
%Output: approximation for sin(x)
function y=sin1(x)
%First calculate the interpolating polynomial and store coefficients
b=pi*(0:3)/6;yb=sin(b);
% b holds base points
c=newtdd(b,yb,4);
%For each input x, move x to the fundamental domain and evaluate
%
the interpolating polynomial
s=1;
% Correct the sign of sin
x1=mod(x,2*pi);
if x1>pi
x1 = 2*pi-x1;
s = -1;
end
if x1 > pi/2
x1 = pi-x1;
end
y = s*horner(3,c,x1,b);
1
−1
1
−1
Figure 3.3: Degree 3 interpolation of sin x. The interpolation polynomial is plotted along with
y = sin x. Equally-spaced interpolation nodes are at 0, π/6, 2π/6 and 3π/6. The approximation is
very close between 0 and π/2.
Most of the work in Program 3.3 is to place x into the fundamental domain. Then we evaluate
the degree 3 polynomial by Horner’s method. Here is some typical output from Program 3.3.
x
1
2
3
4
14
1000
sin x
0.8415
0.9093
0.1411
−0.7568
0.9906
0.8269
P3 (x)
0.8411
0.9102
0.1428
−0.7557
0.9928
0.8263
error
0.0004
0.0009
0.0017
0.0011
0.0022
0.0006
Chapter 3 I NTERPOLATION
143
Not bad for the first try. The error is usually under 1%. In order to get enough correct digits to fill
the calculator readout we’ll need to know a little more about interpolation error, the topic of the next
section.
Exercises 3.1
3.1.1. Use Lagrange interpolation to find a polynomial that passes through the points.
(a) (0, 1), (2, 3), (3, 0)
(b) (−1, 0), (2, 1), (3, 1), (5, 2)
(c) (0, −2), (2, 1), (4, 4)
3.1.2. Repeat the previous problem, but use Newton’s divided differences.
3.1.3. Count the number of multiplications and additions necessary to evaluate the interpolating polynomial through n
data points in (a) Lagrange form (b) Newton divided difference form (using nested multiplication to evaluate).
3.1.4. Let P1 , P2 , P3 , and P4 be four different points lying on a parabola y = ax2 + bx + c. How many cubic (degree
3) polynomials pass through those four points? Explain your answer.
3.1.5.
(a) How many degree n = 2 polynomials pass through the four points
(−1, 3), (1, 1), (2, 3), (3, 7)? Write one down if possible.
(b) How many degree n = 3 polynomials pass through the four points? Write one down if possible.
(c) How many degree n = 6 polynomials pass through the four points? Write one down if possible.
3.1.6.
(a) Find a polynomial P (x) of degree 3 or less whose graph passes through the points (0, 0), (1, 1), (2, 2), (3, 7).
(b) Find 2 other polynomials (of any degree) which pass through these 4 points.
(c) Decide whether there exists a polynomial P (x) of degree 3 or less whose graph passes through the points
(0, 0), (1, 1), (2, 2), (3, 7), and (4, 2). Be sure to justify your answer.
3.1.7.
(a) Find a polynomial P (x) of degree 3 or less whose graph passes through the four data points
(−2, 8), (0, 4), (1, 2), (3, −2).
(b) Are there any other polynomials of degree 4 or less which pass through the 4 points in part (a)? If so, find
as many as you can.
3.1.8. Decide whether there is a polynomial P (x) of degree 3 or less that passes through the five data points (0, 2), (1, 0), (3, 2), (5, 4), (6, 2).
3.1.9. Write down a polynomial of degree exactly 5 that interpolates the four points (1, 1), (2, 3), (3, 3), (4, 4).
3.1.10. Find P (0), where P (x) is the degree 10 polynomial which is zero at x = 1, . . . , 10 and satisfies P (12) = 44.
3.1.11. Let P (x) be the degree 9 polynomial that takes the value 112 at x = 1, takes the value 2 at x = 10, and equals
zero for x = 2, . . . , 9. Calculate P (0).
3.1.12. Give an example of the following or explain convincingly why no such example exists.
(a) A degree 6 polynomial L(x) which is zero at x = 1, 2, 3, 4, 5, 6 and equal to 10 at x = 7.
(b) A degree 6 polynomial L(x) which is zero at x = 1, 2, 3, 4, 5, 6, equal to 10 at x = 7, and equal to 70 at
x = 8.
3.1.13. Can a parabola (degree 2 polynomial) intersects a cubic (degree 3 polynomial) in exactly 4 points? Explain.
3.1.14. A degree m and n polynomial can intersect in at most how many points?
3.1.15. Write down a polynomial y = P (x) of degree exactly 31 that passes through the 31 data points (−15, 225),
(−14, 196), (−13, 169), (−12, 144), (−11, 121), . . . , (15, 225).
Computer Problems 3.1
144
3.2
I NTERPOLATION
ERROR
3.1.1. Write a version of Program 3.2 that is a Matlab function, whose inputs x and y are equal length vectors of data
points, and whose output is a plot of the interpolating polynomial. In this way, the points can be entered more
accurately than by mouse input. Check your program by replicating Figure 3.4.
3.1.2. Write a Matlab function polyinterp.m that takes as input a set of (x, y) interpolating points, and another x0 ,
and outputs y0 , the value of the interpolating polynomial at x0 . The first line of the file should be function y0
= polyinterp(x,y,x0) where x and y are input vectors of data points. Your function may call newtdd
from Program 3.1, and horner from Chapter 0, and may be structured similarly to Program 3.2, but without the
graphics. Demonstrate that your function works.
3.1.3. Remodel the sin1 calculator button in Program 3.3 to build cos1, a cos button that follows the same principles.
First decide on the fundamental domain for cos.
3.1.4. (a) Use the addition formulas for sin and cos to prove that tan(π/2 − x) = tan x. (b) Show that [0, π/4] can be
used as a fundamental domain for tan x. (c) Design a tan button, following the principles of Program 3.3, using
degree 3 polynomial interpolation on this fundamental domain.
3.2 Interpolation error
accuracy of our sin calculator key depends on the approximation in Figure 3.3. How close
is it? We showed a table indicating that for a few examples, the first two digits are fairly
reliable, but after that the digits are not necessarily correct. This may be good enough for some
purposes but not all. In this section we investigate ways to measure this error and determine how to
make it smaller.
T
HE
3.2.1 Interpolation error formula
Assume we start with a function y = f (x) and take data points from it to build an interpolating
polynomial P (x), as we did with sin in Example 3.7. The interpolation error at x is f (x) − P (x),
the difference between the original function that provided the data points and the interpolating
polynomial, evaluated at x. The interpolation error is the vertical distance between the curves in
Figure 3.3. The next theorem gives a formula for the interpolation error that is usually impossible
to evaluate exactly, but often can at least lead to an error bound.
Theorem 3.3 Assume that P (x) is the (degree n − 1 or less) interpolating polynomial fitting the n points
(x1 , y1 ), . . . , (xn , yn ). The formula for interpolation error is
f (x) − P (x) =
(x − x1 )(x − x2 ) · · · (x − xn ) (n)
f (c),
n!
(3.6)
where c lies between the smallest and largest of the numbers x, x1 , . . . , xn .
See Section 3.2.2 for a proof of Theorem 3.3. We can use the theorem to assess the accuracy of
the sin key we built in Example 3.7. Equation (3.6) yields
sin x − P (x) =
(x − 0)(x − π6 )(x − π3 )(x − π2 ) ′′′′
f (c)
4!
where 0 < c < π/2. The limitation on the location of c doesn’t help much, since f ′′′′ (c) = sin c
varies from 0 to 1 in this range. But at worst, | sin c| is no more than 1, so we can be assured of an
upper bound on interpolation error:
| sin x − P (x)| ≤
|(x − 0)(x − π6 )(x − π3 )(x − π2 )|
|1|.
24
Chapter 3 I NTERPOLATION
145
At x = 1, the worst-case error is
| sin 1 − P (1)| ≤
|(1 − 0)(1 − π6 )(1 − π3 )(1 − π2 )|
|1| ≈ 0.0005348.
24
(3.7)
This is an upper bound for the error, since we used a ”worst case” bound for the fourth derivative.
Note that the actual error at x = 1 was .0004, which is within the error bound given by (3.7). We
can make some conclusions based on the form of the interpolation error formula. We expect smaller
errors when x is closer to the middle of the interval of xi ’s than near one of the ends, because there
will be more small terms in that case. For example, we compare the above error bound to the case
x = 0.2, which is near the left end of the range of data points. In this case the error formula is
| sin 0.2 − P (0.2)| ≤
|(.2 − 0)(.2 − π6 )(.2 − π3 )(.2 − π2 )|
|1| ≈ 0.00313,
24
about six times larger. Correspondingly, the actual error is larger, specifically
sin 0.2 − P (0.2) = 0.19867 − 0.20056 = 0.00189
5
−3
−2
−1
1
2
Figure 3.4: Interpolating polynomial for approximating f (x) = ex .
points −1, −0.5, 0, 0.5, 1
Equally spaced base
Example 3.8 Find an upper bound for the difference at x = 0.25 and x = 0.75 between f (x) = ex and
the polynomial that interpolates it at base points −1, −0.5, 0, 0.5, 1.
Note that we don’t need to construct the interpolating polynomial to find the bound. The interpolation error formula (3.6) gives
f (x) − P4 (x) =
(x + 1)(x + 12 )x(x − 12 )(x − 1) (5)
f (c),
5!
146
3.2
I NTERPOLATION
ERROR
where −1 < c < 1. The fifth derivative is f (5) (c) = ec . Since ex is increasing with x, its maximum
is at the right-hand end of the interval and so |f (5) | ≤ e1 on [−1, 1]. For −1 ≤ x ≤ 1, the error
formula boils down to
|ex − P4 (x)| ≤
(x + 1)(x + 12 )x(x − 12 )(x − 1)
e.
5!
At x = 0.25, the interpolation error has the upper bound
(1.25)(0.75)(0.25)(−0.25)(−0.75)
e
120
= .000995
|e0.25 − P4 (0.25)| ≤
At x = 0.75, the interpolation error is potentially larger:
(1.75)(1.25)(0.75)(0.25)(−0.25)
e
120
= .002323
|e0.75 − P4 (0.75)| ≤
Note again that the interpolation error will tend to be smaller close to the center of the interpolation
interval.
3.2.2 Proof of Newton form and error formula
In this section we explain the reasoning behind two important facts used earlier. First we establish the Newton divided difference form of the interpolating polynomial, and then we prove the
interpolation error formula.
Recall what we know so far. If x1 , . . . , xn are n distinct points on the real line, we know there
is exactly one (degree at most n − 1) interpolating polynomial Pn−1 (x) for these points. We also
know that the Lagrange interpolating formula gives such a polynomial.
The only thing we don’t know is whether the Newton divided difference formula also gives us
an interpolating polynomial. Once we prove that it does in Theorem 3.7, we will know it must agree
with the Lagrange version. We start by finding a formula for the degree n − 1 coefficient of Pn−1 ,
and then specialize to the Newton form.
Consider two other interpolating polynomials: Pn−2 , which interpolates x1 , . . . , xn−1 , and
Qn−2 , which interpolates x2 , . . . , xn . The following relation holds between them.
Lemma 3.4 (Pn−1 (x) − Pn−2 (x))(xn − x1 ) = (x − x1 )(Qn−2 (x) − Pn−2 (x))
Proof. Since the difference between the two sides is a polynomial of degree at most n − 1, it
suffices to prove the two sides are equal at any n distinct points. Then by the Fundamental Theorem
of Algebra, the difference must be the identically zero polynomial. The n points we choose are the
n interpolation points. At x = x1 , the left side is zero because both Pn−1 and Pn−2 interpolate the
point (x1 , y1 ). The right side is also zero. At x = x2 , . . . , xn−1 , both sides are zero because all
three polynomials, Pn−1 , Pn−2 , Qn−2 interpolate the points. For x = xn , both sides are identical
because Pn−1 (xn ) = Qn−2 (xn ).
2
Chapter 3 I NTERPOLATION
147
Definition 3.5 We denote the degree n − 1 coefficient of the unique degree ≤ n − 1 polynomial interpolating x1 , . . . , xn by f [x1 . . . xn ].
Using this definition, the degree n − 1 coefficient of the left-hand side in Lemma 3.4 is
f [x1 . . . xn ](xn − x1 )
and the degree n − 1 coefficient of the right-hand side is
f [x2 . . . xn ] − f [x1 . . . xn−1 ].
Since they must be equal, we have proved
Lemma 3.6 If f [x1 . . . xn ] denotes the degree n − 1 coefficient of the degree ≤ n − 1 polynomial
interpolating x1 , . . . , xn , then
f [x1 . . . xn ] =
f [x2 . . . xn ] − f [x1 . . . xn−1 ]
xn − x1
(3.8)
Now we know how to find the degree n − 1 coefficient. Using the recursive nature of (3.8), we can
evaluate it using a Newton divided difference triangle.
We therefore specialize our discussion to the Newton form:
Pn−1 (x) = a1 + a2 (x − x1 ) + a3 (x − x1 )(x − x2 ) + . . . + an (x − x1 ) · · · (x − xn−1 ).
(3.9)
First, is it always possible to write the interpolating polynomial in this form? We show that it is
possible, by solving for the coefficients ai in terms of the data points yi . For example, substituting
x1 into the Newton form (3.9) yields
a1 = Pn (x1 ) = y1 .
(3.10)
a1 + a2 (x2 − x1 ) = Pn−1 (x2 ) = y2 ,
(3.11)
Substituting x2 yields
so we may also solve for a2 , because a1 is known from (3.10) and the coefficient of a2 is nonzero.
Substituting x3 , we see the same thing:
a1 + a2 (x3 − x1 ) + a3 (x3 − x1 )(x3 − x2 ) = Pn−1 (x3 ) = y3 ,
(3.12)
and in general
a1 +a2 (xm −x1 )+a3 (xm −x1 )(xm −x2 )+. . .+am (xm −x1 ) · · · (xm −xm−1 ) = Pn−1 (xm ) = ym .
(3.13)
The important fact is that the previous ai are known, and that the coefficient of am in the equation
is nonzero because the points xi are distinct.
Now that we know the Newton form always exists, we show how to compute the am . Of course,
we already know the degree n − 1 coefficient from Lemma 3.6, an = f [x1 . . . xn ]. For a polynomial
written in Newton form, denote by (Pn−1 )m−1 the degree ≤ m−1 polynomial comprised of the first
m terms of the Newton form of Pn−1 . Note that (Pn−1 )m−1 interpolates (x1 , y1 ), . . . , (xm , ym )
because Pn−1 does and because the higher terms are all zero at those points. By uniqueness of
interpolating polynomial, (Pn−1 )m−1 is the degree ≤ m−1 interpolating polynomial of x1 , . . . , xm ,
and so by Lemma 3.6, am = f [x1 . . . xm ]. We have shown
148
3.2
I NTERPOLATION
ERROR
Theorem 3.7 Given n points (x1 , y1 ), . . . , (xn , yn ) such that the xi are distinct, the polynomial Pn−1 (x)
written in Newton form (3.9) has coefficients given by
am = f [x1 . . . xm ]
for 1 ≤ m ≤ n. In other words, the Newton divided difference formula gives the unique degree ≤ n − 1
interpolating polynomial throught the n points.
Next we prove the Interpolation Error Theorem 3.3. Consider adding one more point x to the
set of interpolation points. The new interpolation polynomial would be
Pn (t) = Pn−1 (t) + f [x1 . . . xn x](t − x1 ) · · · (t − xn ).
Evaluated at the extra point x, Pn (x) = f (x), so
f (x) = Pn−1 (x) + f [x1 . . . xn x](x − x1 ) · · · (x − xn ).
(3.14)
This formula is true for all x. Now define
h(t) = f (t) − Pn−1 (t) − f [x1 . . . xn x](t − x1 ) · · · (t − xn ).
Note that h(x) = 0 by (3.14) and 0 = h(x1 ) = · · · = h(xn ) because Pn−1 interpolates f at these
points. Between each neighboring pair of the n + 1 points x, x1 , . . . , xn , there must be a new point
where h′ = 0, by Rolle’s Theorem (see Chapter 0). There are n of these points. Between each pair
of these, there must be a new point where h′′ = 0; there are n − 1 of these. Continuing in this way,
there must be one point c for which h(n) (c) = 0, where c lies between the smallest and largest of
x, x1 , . . . , xn . Note that
h(n) (t) = f (n) (t) − n!f [x1 . . . xn x]
because the nth derivative of the polynomial Pn−1 (t) is zero. Substituting c gives
f [x1 . . . xn x] =
which leads to
f (x) = Pn−1 (x) +
f (n) (c)
,
n!
f (n) (c)
(x − x1 ) · · · (x − xn )
n!
using (3.14).
3.2.3 Runge phenomenon
Polynomials can fit any set of data points, as Theorem 3.2 shows. However, there are some shapes
that polynomials prefer above others. You can achieve a better understanding of this point by playing
with Program 3.3. Try data points that cause the function to be zero at equally spaced points x =
−3, −2.5, −2, −1.5, . . . , 2.5, 3 except for x = 0, where we set a value of 1. The data points are flat
along the x-axis except for a triangular ”bump” at x = 0, as shown in Figure 3.5.
The polynomial that goes through points situated like this refuses to stay between 0 and 1
vertically, unlike the data points. This is an illustration of the so-called Runge phenomenon. It
is usually used to describe extreme “polynomial wiggle” associated with high-degree polynomial
interpolation at evenly-space points.
Chapter 3 I NTERPOLATION
149
3
2
1
−3
−2
−1
1
2
3
−1
−2
−3
Figure 3.5: Interpolation of triangular bump function. The interpolating polynomial wiggles
much more than the input data points.
Example 3.9 An example of large polynomial interpolation error.
An often-discussed example of this type is f (x) = 1/(1+12x2 ), which we call the Runge example.
This function has the same general shape as the triangular bump in Figure 3.5. Figure 3.6 shows
behavior that is characteristic of the Runge phenomenon - polynomial wiggle near the ends of the
interpolation interval.
As we have seen, examples with the Runge phenomenon characteristically have large error near
the outside of the interval of data points. The cure for this problem is intuitive: move some of the
interpolation points toward the outside of the interval, where the function producing the data can be
better fit. We will see how to accomplish this in the next section.
Exercises 3.2
3.2.1.
(a) Find the degree 2 interpolating polynomial through the points (0, 0), (π/2, 1), and (π, 0). Draw a picture.
You are approximating one hump of a sine curve with a parabola. Should you draw the parabola “inside”
or “outside” the sine hump?
(b) Use the result of (a) to approximate sin(π/4).
(c) Give an error bound for the approximation in part (b).
(d) Using a calculator or Matlab, compare the true error to your error bound.
3.2.2.
(a) Given the data points (1, 0), (2, ln 2), (4, ln 4), find the degree 2 interpolating polynomial.
(b) Use the result of (a) to approximate ln 3.
(c) Give an error bound for the approximation in part (b).
(d) Compare the true error to your error bound.
3.2.3. Assume that the polynomial P9 (x) interpolates the function f (x) = e2x at the 10 evenly-spaced points x =
0, 1/9, 2/9, 3/9, . . . , 8/9, 1. (a) Find an upper bound for the interpolation error |f (1/2) − P9 (1/2)|. (b) How
many decimal places can you guarantee to be correct if P9 (1/2) is used to approximate e?
150
3.3
C HEBYSHEV
2
2
1
1
−1
1
−1
1
−1
−1
−2
−2
(a)
INTERPOLATION
(b)
Figure 3.6: Runge example. Polynomial interpolation of the Runge function of Example 3.9 at
evenly-spaced base points causes extreme variation near the ends of the interval, similar to Figure
3.5. (a) 15 base points (b) 25 base points.
3.2.4. You plan to find an interpolating polynomial for f (x) = 1/(x + 5) using interpolation nodes x = 0, 2, 4, 6, 8, 10.
Find an upper bound (worst case) for the interpolation error at x = 1 and x = 5.
3.2.5. Assume a function f (x) has been approximated by the degree 5 interpolating polynomial P (x) using the data
points (xi , f (xi )) where x1 = .1, x2 = .2, x3 = .3, x4 = .4, x5 = .5, x6 = .6. Do you expect the interpolation
error |f (x) − P (x)| to be smaller for x = .35 or for x = .55? Explain why.
3.2.6. Assume that the polynomial P5 (x) interpolates a function f (x) at the six data points (xi , f (xi )) with x-coordinates
x0 = 0, x1 = .2, x2 = .4, x3 = .6, x4 = .8 and x5 = 1. Assume that the interpolation error at x = .3 is
|f (.3) − P5 (.3)| = .01. Estimate (as well as you can) the new interpolation error |f (.3) − P7 (.3)| that would
result if two additional interpolation points (x6 , y6 ) = (.1, f (.1)) and (x7 , y7 ) = (.5, f (.5)) are added. What
assumptions have you made to make this estimate?
Computer Problems 3.2
3.2.1. (a) Use the method of divided differences to find the degree 4 interpolating polynomial P4 (x) for the data
(0.6, 1.433329), (0.7, 1.632316), (0.8, 1.896481), (0.9, 2.247908) and (1.0, 2.718282). (b) Calculate P4 (0.82)
2
and P4 (0.98). (c) The above data comes from the function f (x) = ex . Use the interpolation error formula to
find upper bounds for the error at x = 0.82 and x = 0.98, and compare the bounds with the actual error. (d) Plot
the actual interpolation error P (x) − e( x2 ) on the intervals [.5, 1] and [0, 2].
3.2.2. Plot the interpolation error of the sin1 button from Program 3.3 on the interval [−2π, 2π].
3.3 Chebyshev interpolation
is common to choose the base points xi for interpolation to be evenly-spaced. In many cases,
the data to be interpolated are only available in that form, for example when the data consists of
instrument readings separated by a constant time interval. In other cases, for example the sin button,
we are free to choose the base points as we see fit. It turns out that the choice of base point spacing
can have a significant effect on the interpolation error. Chebyshev interpolation refers to a particular
optimal way of spacing.
I
T
Chapter 3 I NTERPOLATION
151
3.3.1 Chebyshev’s Theorem
The motivation for Chebyshev interpolation is to better control the maximum value of interpolation
error
(x − x1 )(x − x2 ) · · · (x − xn ) (n)
f (c)
n!
on the interpolation interval. Let’s fix the interval to be [−1, 1] for now.
The numerator
(x − x1 )(x − x2 ) · · · (x − xn )
(3.15)
of the interpolation error formula is itself a degree n polynomial in x, and has some maximum value
on [−1, 1]. Is it possible to find particular x1 , . . . , xn in [−1, 1] that cause the maximum value of
(3.15) to be as small as possible? This is the so-called “minimax” problem of interpolation.
For example, Figure 3.7(a) shows a plot of the degree 9 polynomial (3.15) when x1 , . . . , x9 are
evenly spaced. The tendency for this polynomial to be large near the ends of the interval [−1, 1]
is related to the Runge phenomenon of the previous section. Figure 3.7(b) shows the same polynomial (3.15), but where the points x1 , . . . , x9 have been chosen in a way that equalizes the size
of the polynomial throughout [−1, 1]. These points have been chosen according to Chevyshev’s
prescription.
0.02
0.02
0.01
0.01
−1
1
−1
1
−0.01
−0.01
−0.02
−0.02
(a)
(b)
Figure 3.7: Part of the interpolation error formula. Plots of (x − x1 ) · · · (x − x9 ) for (a) 9
evenly-spaced base points xi (b) 9 Chebyshev roots xi
These ideas are relevant to the Interpolation Error Theorem 3.3. There are three parts to the
error formula (3.6), the numerator, the denominator, and the derivative. Once the degree is fixed,
the only part we can optimize is the numerator
(x − x1 )(x − x2 ) · · · (x − xn ).
(3.16)
In Figure 3.7 we see that the maximum contribution of the term (3.16) depends on the x coordinates
of the interpolation points.
For evenly-spaced points, as in Figure 3.7(a), there is relatively large error near the ends.
When points are moved toward the ends, as in Figure 3.7(b), the maximum error on the interval is decreased. In fact, this precise positioning, where the base points xi are chosen to be
152
3.3
C HEBYSHEV
INTERPOLATION
cos π/18, cos 3π/18, . . . , cos 17π/18, makes the maximum absolute value of (3.16) equal to 1/256,
the minimum possible for 9 points on the interval [−1, 1]. Chebyshev gave this optimal answer to
the problem, which is summarized in the following theorem.
Theorem 3.8 The choice of real numbers −1 ≤ x1 , . . . , xn ≤ 1 that makes the value of
max |(x − x1 ) · · · (x − xn )|
−1≤x≤1
as small as possible is
xi = cos
(2i − 1)π
for i = 1, . . . , n
2n
and the minimum value is 1/2n−1 . In fact, the minimum is achieved by
(x − x1 ) · · · (x − xn ) =
1
2n−1
Tn (x),
where Tn (x) denotes the degree n Chebyshev polynomial.
The proof of this theorem is given below, after we establish a few properties of Chebyshev
polynomials. We conclude from the theorem that interpolation error can be minimized if the n interpolation base points in [−1, 1] are chosen to be the roots of the degree n Chebyshev interpolating
polynomial Tn (x). These roots are
odd π
xi = cos
(3.17)
2n
where “odd” stands for the odd numbers from 1 to 2n − 1. Then we are guaranteed that (3.15) is
less than 1/2n−1 for all x in [−1, 1].
Choosing the Chebyshev roots as the base points for interpolation distributes the interpolation
error as equally as possible across the interval [−1, 1]. We will call the interpolating polynomial
that uses the Chebyshev roots as base points the Chebyshev interpolating polynomial.
Example 3.10 Find a worst-case error bound for the difference on [−1, 1] between f (x) = ex and the
Chebyshev interpolating polynomial.
The interpolation error formula (3.6) gives
f (x) − P4 (x) =
(x − x1 )(x − x2 )(x − x3 )(x − x4 )(x − x5 ) (5)
f (c),
5!
where x1 , . . . , x5 are the Chebyshev zeros
x1 = cos
π
3π
9π
, x2 = cos , . . . , x5 = cos
10
10
10
and where −1 < c < 1. According to the Chebyshev Theorem 3.8, for −1 ≤ x ≤ 1,
|(x − x1 ) · · · (x − x5 )| ≤
1
.
24
Also |f (5) | ≤ e1 on [−1, 1]. Therefore the interpolation error, the difference between ex and P4 (x),
is
e
|ex − P4 (x)| ≤ 4 = 0.00141
2 5!
Chapter 3 I NTERPOLATION
153
for all x in the interval [−1, 1].
Compare this result with Example 3.8. The error bound for Chebyshev interpolation for the
entire interval is only slightly larger than the bound for a point near the center of the interval, when
evenly-spaced interpolation is used. Near the ends of the interval, the Chebyshev error is much
smaller.
Returning to the Runge Example 3.9, we can try to eliminate the Runge phenomenon by choosing the interpolation points according to Chebyshev’s idea. Figure 3.8 shows that the interpolation
error is made small throughout the interval [−1, 1].
2
2
1
1
−1
1
−1
−1
1
−1
(a)
(b)
Figure 3.8: Interpolation of Runge example with Chebyshev nodes. The Runge function
f (x) = 1/(1 + 12x2 ) is graphed along with its Chebyshev interpolation polynomial for (a) 15
points (b) 25 points. The error on [−1, 1] is negligible at this resolution. Polynomial wiggle has
mysteriously vanished, at least between −1 and 1.
3.3.2 Chebyshev polynomials.
Define the nth Chebyshev polynomial by Tn (x) = cos(n arccos x). Despite not looking like the
formula for a polynomial, it is a polynomial in the variable x for each n. For example, for n = 0
it gives the degree 0 polynomial 1, and for n = 1 we get T1 (x) = cos(arccos x) = x. For n = 2,
recall the cosine addition formula cos(a + b) = cos a cos b − sin a sin b. Set y = arccos x, so that
cos y = x. Then T2 (x) = cos 2y = cos2 y −sin2 y = 2 cos2 y −1 = 2x2 −1, a degree 2 polynomial.
In general, note that
Tn+1 (x) = cos(n + 1)y = cos(ny + y) = cos ny cos y − sin ny sin y
Tn−1 (x) = cos(n − 1)y = cos(ny − y) = cos ny cos y − sin ny sin(−y)
(3.18)
Because sin(−y) = sin y we can add the above equations to get
Tn+1 (x) + Tn−1 (x) = 2 cos ny cos y = 2xTn (x).
(3.19)
154
3.3
C HEBYSHEV
INTERPOLATION
The resulting relation
Tn+1 (x) = 2xTn (x) − Tn−1 (x)
(3.20)
is called the recursion relation for the Chebyshev polynomials. Several facts follow from (3.20):
1
−1
1
−1
Figure 3.9: Plot of the degree 1 through 5 Chebyshev polynomials. Note that Tn (1) = 1 and
the maximum absolute value taken on by Tn (x) inside [−1, 1] is 1.
Fact 1. The Tn ’s are polynomials. We showed this explicitly for T1 and T2 . Since T3 is a polynomial
combination of T1 and T2 , T3 is also a polynomial. The same argument goes for all Tn . The first
few Chebyshev polynomials are:
T0 (x) = 1
T1 (x) = x
T2 (x) = 2x2 − 1
T3 (x) = 4x3 − 3x
−1
1
(a)
−1
1
−1
(b)
1
(c)
Figure 3.10: Location of zeros of the Chebyshev polynomial. The roots are the x-coordinates of
evenly-spaced points around the circle. (a) degree 5 (b) degree 15 (c) degree 25.
Fact 2. deg(Tn ) = n, and the leading coefficient is 2n−1 . This is clear for n = 1 and 2, and the
recursion relation extends the fact to all n.
Fact 3. Tn (1) = 1 and Tn (−1) = (−1)n . Both are clear for n = 1 and 2. In general,
Tn+1 (1) = 2(1)Tn (1) − Tn−1 (1) = 2(1) − 1 = 1,
Chapter 3 I NTERPOLATION
155
and
Tn+1 (−1) = 2(−1)Tn (−1) − Tn−1 (−1)
= −2(−1)n − (−1)n−1
= (−1)n−1 (2 − 1) = (−1)n−1 = (−1)n+1 .
Fact 4. The maximum absolute value of Tn (x) for −1 ≤ x ≤ 1 is 1. This follows immediately
from the fact that Tn (x) = cos y for some y.
Fact 5. All zeros of Tn (x) are located between −1 and 1. In fact, the zeros are the solution of
0 = cos(n arccos x). Since cos y = 0 if and only if y = odd integer·(π/2), we find that
n arccos x = odd · π/2
odd · π
.
x = cos
2n
Fact 6. Tn (x) alternates between −1 and 1 a total of n + 1 times. One check that this happens at
cos 0, cos π/n, . . . , cos(n − 1)π/n, cos π.
It follows from Fact 2 above that the polynomial Tn (x)/2n−1 is monic (has leading coefficient
1). Since all of its roots are real, according to Fact 5, we can write Tn (x)/2n−1 in factored form as
(x − x1 ) · · · (x − xn ) where the xi are the Chebyshev nodes as described in Theorem 3.8.
Given these facts, we are prepared to prove Chebyshev’s theorem.
Proof of Theorem 3.8. Let Pn (x) be a monic polynomial with an even smaller absolute maximum
on [−1, 1], or in other words, |Pn (x)| < 1/2n−1 for −1 ≤ x ≤ 1. This assumption leads to a
contradiction. Since Tn (x) alternates between −1 and 1 a total of n + 1 times (Fact 6), at these n + 1
points, the difference Pn − Tn /2n−1 is alternately positive and negative. Therefore Pn − Tn /2n−1
must cross zero at least n times, that is, must have at least n roots. This contradicts the fact that
because Pn and Tn /2n−1 are monic, their difference is of degree ≤ n − 1.
2
3.3.3 Change of interval
So far our discussion of Chebyshev interpolation has been restricted to the interval [−1, 1], because
Theorem 3.8 is most easily stated for this interval. Next we will move the whole methodology to a
general interval [a, b].
The base points are moved so that they have the same relative positions in [a, b] that they had in
[−1, 1]. It is best to think of doing this in two steps: (1) stretch the points by the factor (b − a)/2
(the ratio of the two interval lengths), and (2) translate the points by (b + a)/2 to move the center
of mass from 0 to the midpoint of [a, b]. In other words, move from the original points
cos
odd π
2n
to
b−a
odd π b + a
cos
+
.
2
2n
2
156
3.3
C HEBYSHEV
INTERPOLATION
With the new Chebyshev base points x1 , . . . , xn in [a, b], the corresponding upper bound on the
numerator of the interpolation error formula is
b−a n
|(x − x1 ) · · · (x − xn )| ≤
2
(3.21)
2n−1
The next example illustrates the usage of Chebyshev interpolation in a general interval.
0.003
0.5
1
1.5
5e−011
0.5
1
1.5
−5e−011
−0.003
(a)
(b)
Figure 3.11: Interpolation error for approximating f (x) = sin x. (a) Interpolation error for
degree 3 interpolating polynomial with evenly-spaced base points (solid curve) and Chebyshev
base points (dotted curve). (b) Same as (a), but degree 9
Example 3.11 Find the 4 Chebyshev base points for interpolation on the interval [0, π/2], and find an
upper bound for the Chebyshev interpolation error for f (x) = sin x on the interval.
This is a second attempt, as we attempted to do this with evenly-spaced base points in Example 3.7.
The Chebyshev base points are
π
π
+0
odd π
2 −0
cos
+ 2
,
2
2(4)
2
or
x1 =
π π
π
π π
3π
π π
5π
π π
7π
+ cos , x2 = + cos , x3 = + cos , x4 = + cos
.
4
4
8
4
4
8
4
4
8
4
4
8
The worst-case interpolation error for 0 ≤ x ≤ π/2 is
| sin x − P3 (x)| =
|(x − x1 )(x − x2 )(x − x3 )(x − x4 )| ′′′′
|f (c)|
4!
π 4
−0
2
4!23
2
≤
1 ≈ 0.00198
The Chebyshev interpolating polynomial for this example is evaluated at several points in the
following table:
Chapter 3 I NTERPOLATION
157
0
S POTLIGHT ON: Compression
As shown in this section, Chebyshev interpolation is a good way to turn general functions into a small
number of floating point operations, for ease of computation. An upper bound for the error made is easily
available, is usually smaller than for evenly-spaced interpolation, and can be made as small as desired.
Although we have used the sine function to demonstrate this process for a general function, a
different approach is taken to construct the actual ”sine button” on most calculators and canned software.
Special properties of the sine function allow it to be approximated by a simple Taylor expansion slightly
altered to take rounding effects into account. Because sine is an odd function, the even numbered terms
in its Taylor series around zero are missing, making it especially efficient to calculate.
x
1
2
3
4
14
1000
sin x
0.8415
0.9093
0.1411
−0.7568
0.9906
0.8269
P3 (x)
0.8408
0.9097
0.1420
−0.7555
0.9917
0.8261
error
0.0007
0.0004
0.0009
0.0013
0.0011
0.0008
The interpolation errors are well below the worst-case estimate above. Figure 3.11 plots the interpolation error as a function of x on the interval [0, π/2], compared with the same for evenly-spaced
interpolation. The Chebyshev error (lower curve) is a bit smaller, and distributed more evenly
throughout the interpolation interval.
Example 3.12 Design a sin button that will give output correct to 10 decimal places.
Thanks to our work earlier on setting up a fundamental domain for sine, we can continue to concentrate on the interval [0, π/2]. Repeat the previous calculation, but leave n, the number of base points,
as an unknown to be determined. The maximum interpolation error for the polynomial Pn−1 (x) on
the interval [0, π/2] is
| sin x − Pn−1 (x)| =
|(x − x1 ) · · · (x − xn )| (n)
|f (c)|
n!
π
−0
2
≤
n
2
n!2n−1
1
This equation is not simple to solve for n, but a little trial and error finds that for n = 9, the error
bound is ≈ 0.1224 × 10−8 , and for n = 10 it is ≈ 0.4807 × 10−10 . The latter meets our criterion for
10 correct decimal places. Figure 3.11(b) compares the actual error of the Chebyshev interpolation
polynomial with the error of the evenly-spaced interpolation polynomial.
158
3.3
C HEBYSHEV
INTERPOLATION
The 10 Chebyshev base points on [0, π/2] are π/4 + (π/4) cos(oddπ/20). The button can be
designed by storing the 10 y-values for sine at the base points, and do a Horner method evaluation
for each button press.
The following Matlab code, sin2.m, carries this out. The code is a bit awkward as written we have to do 10 sin evaluations, at the 10 Chebyshev nodes, in order to set up the interpolating
polynomial to approximate sin at one point. Of course, in a real implementation, these numbers
would be computed once and stored.
%Program 3.4 Building a sin calculator button, attempt #2
%Approximates sin curve with degree 9 polynomial
%Input: x
%Output: approximation for sin(x), correct to 10 decimal places
function y=sin2(x)
%First calculate the interpolating polynomial and store coefficients
n=10; b=1:2:2*n-1; b=pi/4+(pi/4)*cos(b*pi/(2*n));
yb=sin(b);
% b holds Chebyshev base points
c=newtdd(b,yb,n);
%For each input x, move x to the fundamental domain and evaluate
%
the interpolating polynomial
s=1;
% Correct the sign of sin
x1=mod(x,2*pi);
if x1>pi
x1 = 2*pi-x1;
s = -1;
end
if x1 > pi/2
x1 = pi-x1;
end
y = s*horner(n-1,c,x1,b);
Exercises 3.3
3.3.1. Write down the 8 interpolation nodes (base points) needed to use Chebyshev minimax interpolation with a degree
7 interpolating polynomial on the interval [−10, 10].
3.3.2. List the interpolation nodes necessary to construct the degree 10 Chebyshev interpolation on the interval [4, 12].
3.3.3. Assume Chebyshev interpolation is used to find a fourth-degree interpolating polynomial Q4 (x) on the interval
[.6, 1.0] for the function f (x) = ex . Use the interpolation error formula to find a worst-case estimate for the error
|ex − Q4 (x)| which is valid for x throughout the interval [.6, 1.0]. How many digits after the decimal point will
be correct when using Q4 (x) to approximate ex ?
3.3.4. Suppose you are designing the ln key for a calculator whose display shows (a maximum of six) digits to the right
of the decimal point.
(a) Begin by designing a method for evaluating ln x for 1 ≤ x ≤ e. Using Chebyshev minimax interpolation,
how many numbers will need to be stored in memory, and how many additions and multiplications will be
required for each press of the ln key?
(b) Discuss possible approaches for extending the allowable range of inputs to all x. Goals are to minimize
storage and calculation time as in part (a).
3.3.5. Assume you are to use Chebyshev minimax interpolation to find a degree 3 interpolating polynomial Q3 (x) that
approximates the function f (x) = x−3 on the interval [3, 4].
Chapter 3 I NTERPOLATION
159
(a) Write down the (x, y) points that will serve as interpolation nodes for Q3 .
(b) Find a worst-case estimate for the error |x−3 − Q3 (x)| which is valid for all x in the interval [3, 4]. How
many digits after the decimal point will be correct when using Q3 (x) to approximate x−3 ?
3.3.6. The recursion relation for Chebyshev polynomials is Tk (x) = 2xTk−1 (x) − Tk−2 (x).
(a)
(b)
(c)
(d)
(e)
What is the degree of the polynomial Tk (x)?
Find a formula for the leading coefficient of Tk (x).
Find the number T999 (1).
Find the numbers T999 (−1), T1000 (−1), T999 (0), and T1000 (0).
Find the numbers T999 (−1/2) and T1000 (−1/2).
3.3.7. Find the maximum error on [0, 2] when the degree 3 Chebyshev interpolating polynomial is used to approximate
f (x) = sin πx.
3.3.8. Assume you want to use Chebyshev interpolation to approximate the function f (x) = x−1 to 6 correct decimal
places on the entire interval [1, 2] with a polynomial. Find the degree of such a polynomial. How many additions
and multiplications will be necessary to do the approximation for each input x? How will your answers change if
the interval [1, 10] is used instead?
Computer Problems 3.3
3.3.1. Rebuild Program 3.3 to implement the Chebyshev interpolating polynomial with 4 base points on the interval
[0, π/2]. (Only one line of code needs to be changed.) Then plot the polynomial and compare it with the sine
function on the interval [−2, 2].
3.3.2. Build a Matlab program to evaluate cos correct to 10 decimal places using Chebyshev interpolation. It should be
possible to achieve this goal for inputs between −104 and 104 . You may want to use some of the Matlab code
written in this chapter.
3.3.3. Repeat the problem 3.3.2 for ln x, for inputs x between 10−4 and 104 . Use [1, e] as fundamental domain. Use
Chebyshev interpolation. What is the degree of the interpolation polynomial that guarantees 10 correct digits?
Your Matlab program should begin by finding the integer k such that ek < x < ek+1 . Then xe−k lies in the
fundamental domain. Demonstrate the accuracy of your program by comparing to Matlab’s log command.
3.3.4. Let f (x) = e|x| . Compare evenly-spaced interpolation to Chebyshev interpolation by plotting degree n polynomials of both types on the interval [−1, 1], for n = 10 and 20. For evenly-spaced interpolation, the left and right
interpolation base points should be −1 and 1. By sampling at 0.01 stepsize, create the empirical interpolation
errors for each type, and plot a comparison. Can the Runge phenomenon be observed in this problem?
2
3.3.5. Repeat the previous problem for e−x .
3.4 Cubic splines
represent an alternative approach to data interpolation. In polynomial interpolation, a
single formula, given by a polynomial, is used to meet all data points. The idea of splines is to
use several formulas, each a low degree polynomial, to pass through the data points.
The simplest example of a spline is a linear spline, in which one ”connects the dots” with
straight line segments. Assume we are given a set of data points (x1 , y1 ), . . . , (xn , yn ) with x1 <
. . . < xn . A linear spline consists of the n − 1 line segments that are drawn between neighboring
pairs of points. Figure 3.12(a) shows a linear spline where between each neighboring pair of points
(xi , yi ), (xi+1 , yi+1 ), the linear function y = ai + bi x is drawn through the two points. The given
data points in Figure 3.12 are (1, 2), (2, 1), (4, 4), and (5, 3), and the linear spline is given by
S
PLINES
S1 (x) = 2 − (x − 1) on [1, 2]
3
S2 (x) = 1 + (x − 2) on [2, 4]
2
S3 (x) = 4 − (x − 4) on [4, 5]
(3.22)
160
3.4
4
4
3
3
2
2
1
1
1
2
3
(a)
4
5
1
2
3
4
C UBIC
SPLINES
5
(b)
Figure 3.12: Splines through four data points. (a) Linear spline through (1, 2), (2, 1), (4, 4)
and (5, 3) consists of three linear polynomials given by (3.22). (b) Cubic spline through the same
points, given by (3.23)
The linear spline successfully interpolates an arbitrary set of n data points. However, linear
splines lack smoothness. Cubic splines are meant to address this shortcoming of linear splines. A
cubic spline replaces linear functions between the data points by degree 3 (cubic) polynomials.
An example of a cubic spline which interpolates the same points (1, 2), (2, 1), (4, 4) and (5, 3)
is shown in Figure 3.12(b). The equations defining the spline are
13
5
(x − 1) + 0(x − 1)2 + (x − 1)3 on [1, 2]
8
8
1
15
5
2
S2 (x) = 1 + (x − 2) + (x − 2) − (x − 2)3 on [2, 4]
4
8
8
1
5
15
S3 (x) = 4 + (x − 4) − (x − 4)2 + (x − 4)3 on [4, 5].
4
8
8
S1 (x) = 2 −
(3.23)
Note in particular the smooth transition from one Si to the next at the base points or “knots” at
x = 2 and x = 4. This is achieved by arranging for the neighboring pieces Si and Si+1 of the spline
to have the same 0th, 1st, and 2nd derivatives when evaluated at the knots. Just how to do this is the
topic of the next section.
Given n points (x1 , y1 ), . . . , (xn , yn ), there is obviously one and only one linear spline through
the data points. This will not be true for cubic splines. We will find there are infinitely many through
any set of data points. Because of this, we will add extra conditions when it is necessary to nail down
a particular spline of interest.
3.4.1 Properties of splines
To be a little more precise about the properties of a cubic spline we make the following definition.
Assume that we are given the n data points (x1 , y1 ), . . . , (xn , yn ) to interpolate. A cubic spline
Chapter 3 I NTERPOLATION
161
S(x) through the data points (x1 , y1 ), . . . , (xn , yn ) is a set of cubic polynomials
S1 (x) = y1 + b1 (x − x1 ) + c1 (x − x1 )2 + d1 (x − x1 )3 on [x1 , x2 ]
2
(3.24)
3
S2 (x) = y2 + b2 (x − x2 ) + c2 (x − x2 ) + d2 (x − x2 ) on [x2 , x3 ]
..
.
Sn−1 (x) = yn−1 + bn−1 (x − xn−1 ) + cn−1 (x − xn−1 )2 + dn−1 (x − xn−1 )3 on [xn−1 , xn ]
with the following properties:
Property 1. Si (xi ) = yi and Si (xi+1 ) = yi+1 for i = 1, . . . , n − 1.
′ (x ) = S ′ (x ) for i = 2, . . . , n − 1.
Property 2. Si−1
i
i i
′′ (x ) = S ′′ (x ) for i = 2, . . . , n − 1.
Property 3. Si−1
i
i
i
Property 1 guarantees the spline S(x) interpolates the data points. Property 2 forces the slopes
of neighboring parts of the spline to agree where they meet, and Property 3 does the same for the
curvature, represented by the second derivative.
Example 3.13 Check that {S1 , S2 , S3 } in (3.23) satisfies all spline properties for the data points (1, 2),
(2, 1), (4, 4), (5, 3).
We will check all three properties.
Property 1. There are n = 4 data points. We must check
S1 (1) = 2
and
S1 (2) = 1
S2 (2) = 1
and
S2 (4) = 4
S3 (4) = 4
and
S3 (5) = 3
These follow easily from the defining equations (3.23).
Property 2. The first derivatives of the spline functions are
13 15
+ (x − 1)2
8
8
1 15
15
+ (x − 2) − (x − 2)2
4
4
8
1 15
15
− (x − 4) + (x − 4)2
4
4
8
S1′ (x) = −
S2′ (x) =
S3′ (x) =
We must check S1′ (2) = S2′ (2) and S2′ (4) = S3′ (4). The first is
−
and the second is
both of which check.
13 15
1
+
= ,
8
8
4
1 15
15
1
+ (4 − 2) − (4 − 2)2 = ,
4
4
8
4
162
3.4
C UBIC
SPLINES
Property 3. The second derivatives are
15
(x − 1)
4
15 15
S2′′ (x) =
− (x − 2)
4
4
15 15
′′
S3 (x) = − + (x − 4)
4
4
S1′′ (x) =
(3.25)
We must check S1′′ (2) = S2′′ (2) and S2′′ (4) = S3′′ (4), both of which are true. Therefore (3.23) is a
cubic spline.
Constructing a spline from a set of data points means finding the coefficients bi , ci , di that make
Properties 1 - 3 hold. Before we discuss how to determine the unknown coefficients bi , ci , di of the
spline, let us count the number of conditions imposed by the definition. The first half of Property
1 is already reflected in the form (3.24); it says that the constant term of the cubic Si must be yi .
The second half of Property 1 consists of n − 1 separate equations that must be satisfied by the
coefficients, which we consider as unknowns. Each of Properties 2 and 3 add n − 2 additional
equations, for a total of n − 1 + 2(n − 2) = 3n − 5 independent equations to be satisfied.
How many unknown coefficients are there? For each part Si of the spline, three coefficients
bi , ci , di are needed, for a total of 3(n − 1) = 3n − 3. Therefore solving for the coefficients is
a problem of solving 3n − 5 linear equations in 3n − 3 unknowns. Unless there are inconsistent
equations in the system (and there are not), the system of equations is underdetermined, and so has
infinitely many solutions. In other words, there are infinitely many cubic splines passing through
the arbitrary set of data points (x1 , y1 ), . . . , (xn , yn ).
Users of splines normally exploit this embarrassment of riches by adding two extra constraints
to the 3n − 5 equations, to arrive at a system of m equations in m unknowns, where m = 3n − 3.
Aside from allowing the user to constrain the spline to given specifications, narrowing the field to a
single solution simplifies computing and describing the result.
The simplest way of adding 2 more constraints is to require, in addition to the previous 3n − 5
constraints, that the spline S(x) has an inflection point at each end of the defining interval [x1 , xn ].
The constraints added to Properties 1 - 3 are
′′ (x ) = 0.
Property 4a. (Natural spline) S1′′ (x1 ) = 0 and Sn−1
n
A cubic spline that satifies these two additional conditions is called a natural cubic spline.
Note that (3.23) is a natural cubic spline, since it is easily verified from (3.25) that S1′′ (1) = 0 and
S3′′ (5) = 0.
There are several other ways to add two additional conditions. Usually, as in the natural spline
case, they determine extra properties of the left and right ends of the spline, so they are called end
conditions. We will take up this topic in the next section, but for now we concentrate on natural
cubic splines.
Now that we have the right number of equations, 3n − 3 equations in 3n − 3 unknowns, we can
write a Matlab function to solve them for the spline coefficients. First we write out the equations in
Chapter 3 I NTERPOLATION
163
the unknowns bi , ci , di . Part 2 of Property 1 implies the n − 1 equations:
y2 = S1 (x2 ) = y1 + b1 (x2 − x1 ) + c1 (x2 − x1 )2 + d1 (x2 − x1 )3
..
.
(3.26)
yn = Sn−1 (xn ) = yn−1 + bn−1 (xn − xn−1 ) + cn−1 (xn − xn−1 )2 + dn−1 (xn − xn−1 )3
Property 2 generates the n − 2 equations
0 = S1′ (x2 ) − S2′ (x2 ) = b1 + 2c1 (x2 − x1 ) + 3d1 (x2 − x1 )2 − b2
..
.
(3.27)
′
′
0 = Sn−2
(xn−1 ) − Sn−1
(xn−1 ) = bn−2 + 2cn−2 (xn−1 − xn−2 ) + 3dn−2 (xn−1 − xn−2 )2 − bn−1 ,
and Property 3 implies the n − 2 equations
0 = S1′′ (x2 ) − S2′′ (x2 ) = 2c1 + 6d1 (x2 − x1 ) − 2c2
..
.
(3.28)
′′
′′
0 = Sn−2
(xn−1 ) − Sn−1
(xn−1 ) = 2cn−2 + 6dn−2 (xn−1 − xn−2 ) − 2cn−1
Instead of solving the equations in this form, the system can be simplified drastically by decoupling the equations. With a little algebra, a much smaller system of equations in the ci can be solved
first, followed by explicit formulas for the bi and di in terms of the known ci .
′′ (x )/2 is introduced. In addition, we
It is conceptually simpler if an extra unknown cn = Sn−1
n
introduce the shorthand notation δi = xi+1 − xi and ∆i = yi+1 − yi . Then (3.28) can be solved for
the coefficients
ci+1 − ci
for i = 1, . . . , n − 1.
(3.29)
di =
3δi
Solving (3.26) for bi yields
bi =
=
=
∆i
− ci δi − di δi2
δi
∆i
δi
− ci δi − (ci+1 − ci )
δi
3
∆i δi
− (2ci + ci+1 )
δi
3
(3.30)
for i = 1, . . . , n − 1.
Substituting (3.29) and (3.30) into (3.27) results in the following n − 2 equations in c1 , . . . , cn :
∆2 ∆1
δ1 c1 + 2(δ1 + δ2 )c2 + δ2 c3 = 3
−
δ2
δ1
..
.
∆n−1 ∆n−2
δn−2 cn−2 + 2(δn−2 + δn−1 )cn−1 + δn−1 cn = 3
−
δn−1
δn−2
Two more equations are given by the natural spline conditions (Property 4a):
S1′′ (x1 ) = 0 → 2c1 = 0
′′
Sn−1
(xn ) = 0 → 2cn = 0
164
3.4
C UBIC
SPLINES
This gives a total of n equations in n unknowns ci , which can be written in the matrix form

 c 

0
1
1
0
0
 3 ∆2 −
 
δ2


..
 
 δ1 2δ1 + 2δ2

.
δ
2

 


 

 0
δ2
2δ2 + 2δ3
δ3
 




 . 

..

 . 
=

.
 .  

.
.
.
.

.
.
.
.


.
.
.
.
 


 


 


  
δn−2 2δn−2 + 2δn−1 δn−1  
 
∆n−1
−
 3 δn−1
0
0
1
cn
0
∆1
δ1















∆n−2

δn−2
(3.31)
are found from (3.29) and
After finding c1 , . . . , cn from (3.31), b1 , . . . , bn−1 and d1 , . . . , dn−1
(3.30).
Note that (3.31) is always solvable for the ci . The coefficient matrix is strictly diagonally dominant, so by Theorem 2.9 of Chapter 2, there is a unique solution for the ci , and therefore also for
the bi and di . We have proved:
Theorem 3.9 Let n ≥ 2. For a set of data points (x1 , y1 ), . . . , (xn , yn ) with distinct xi , there is a unique
natural cubic spline fitting the points.
3.4.2 Calculation of the cubic spline.
We summarize the calculations above and implement them in Matlab code. Assume we are given n
data points (x1 , y1 ), . . . , (xn , yn ) with distinct xi . The coefficients of the spline functions (3.24)
S1 (x) = y1 + b1 (x − x1 ) + c1 (x − x1 )2 + d1 (x − x1 )3 on [x1 , x2 ]
S2 (x) = y2 + b2 (x − x2 ) + c2 (x − x2 )2 + d2 (x − x2 )3 on [x2 , x3 ]
..
.
Sn−1 (x) = yn−1 + bn−1 (x − xn−1 ) + cn−1 (x − xn−1 )2 + dn−1 (x − xn−1 )3 on [xn−1 , xn ]
are found as follows.
For i = 1, . . . , n − 1, set
ai = yi .
(3.32)
Set δi = xi+1 − xi and ∆i = yi+1 − yi for i = 1, . . . , n − 1 and solve the n × n linear system
(3.31) for c1 , . . . , cn . Then (3.29) and (3.30) give
di =
ci+1 − ci
3δi
(3.33)
and
∆i δi
− (2ci + ci+1 )
δi
3
for i = 1, . . . , n − 1. This gives the coefficients of the natural cubic spline.
bi =
Example 3.14 Find the natural cubic spline through (0, 3), (1, −2), and (2, 1).

(3.34)
Chapter 3 I NTERPOLATION
165
The x-coordinates are x1 = 0, x2 = 1, x2 = 2. The y-coordinates are a1 = y1 = 3, a2 = y2 = 2,
and the differences are δ1 = δ2 = 1, ∆1 = −5, ∆2 = 3. The tridiagonal matrix equation (3.31) is


 

1 0 0
c1
0
 1 4 1   c2  =  24  .
0 0 1
c3
0
The solution is [c1 , c2 , c3 ] = [0, 6, 0]. Now (3.33) and (3.34) yield
d1 =
d2 =
b1 =
b2 =
c2 − c1
6
= =2
3δ1
3
c3 − c2
−6
= −2
=
3δ2
3
∆1 δ1
1
− (2c1 + c2 ) = −5 − (6) = −7
δ1
3
3
∆2 δ2
1
− (2c2 + c3 ) = 3 − (12) = −1
δ2
3
3
Therefore the cubic spline is
S1 (x) = 3 − 7x + 0x2 + 2x3 on [0, 1]
S2 (x) = −2 − 1(x − 1) + 6(x − 1)2 − 2(x − 1)3 on [1, 2].
Matlab code for this calculation follows. For different (not natural) endpoint conditions, discussed in the next section, the top and bottom rows of (3.31) are replaced by other appropriate
rows.
%Program 3.5 Calculation of spline coefficients
function coeff=splinecoeff(x,y)
%Calculates coefficients of cubic spline
%Input: x,y vectors of data points
%
plus two optional extra data v1, vn
%Output: matrix of coefficients b1,c1,d1;b2,c2,d2;...
n=length(x);v1=0;vn=0;
A=zeros(n,n);
% matrix A is nxn
r=zeros(n,1);
for i=1:n-1
% define the deltas
dx(i) = x(i+1)-x(i); dy(i)=y(i+1)-y(i);
end
for i=2:n-1
% load the A matrix
A(i,i-1:i+1)=[dx(i-1) 2*(dx(i-1)+dx(i)) dx(i)];
r(i)=3*(dy(i)/dx(i) - dy(i-1)/dx(i-1)); % right-hand side
end
% Set endpoint conditions
% Use only one of following 5 pairs:
A(1,1) = 1;
% natural spline conditions
A(n,n) = 1;
%A(1,1)=2;r(1)=v1;
% curvature-adj conditions
166
3.4
C UBIC
SPLINES
%A(n,n)=2;r(n)=vn;
%A(1,1:2)=[2*dx(1) dx(1)];r(1)=3*(dy(1)/dx(1)-v1); % clamped conditions
%A(n,n-1:n)=[dx(n-1) 2*dx(n-1)];r(n)=3*(vn-dy(n-1)/dx(n-1));
%A(1,1:2)=[1 -1];
% parabol-term conditions, for n>=3
%A(n,n-1:n)=[1 -1];
%A(1,1:3)=[dx(2) -(dx(1)+dx(2)) dx(1)]; % not-a-knot conditions, for n>=4
%A(n,n-2:n)=[dx(n-1) -(dx(n-2)+dx(n-1)) dx(n-2)];
coeff=zeros(n,3);
coeff(:,2)=A\r;
% solve for c coefficients
for i=1:n-1
% solve for b and d
coeff(i,3)=(coeff(i+1,2)-coeff(i,2))/(3*dx(i));
coeff(i,1)=dy(i)/dx(i)-dx(i)*(2*coeff(i,2)+coeff(i+1,2))/3;
end
coeff=coeff(1:n-1,1:3);
We have taken the liberty to list other choices for end conditions, although they are commented
out for now. The alternate conditions will be discussed in the next section. Another Matlab function,
titled splineplot.m, calls splinecoeff.m to get the coefficients, and then plots the cubic spline.
Program 3.6 Cubic spline plot
function splineplot(x,y)
%Plots spline from data points
%Input: x,y vectors of data points
%Output: none
n=length(x);
coeff=splinecoeff(x,y);
cla;hold on;
%clear figure window and turn hold on
for i=1:n-1
x0=linspace(x(i),x(i+1),100);
dx=x0-x(i);
y0=coeff(i,3)*dx; %evaluate using horner’s method
y0=(y0+coeff(i,2)).*dx;
y0=(y0+coeff(i,1)).*dx+y(i);
plot([x(i) x(i+1)],[y(i) y(i+1)],’o’,x0,y0)
end
hold off
Figure 3.13(a) shows a natural cubic spline generated by splinecoeff.m.
3.4.3 Endpoint conditions
The two extra conditions specified in Property 4a above are called the “endpoint conditions” for a
natural spline. Requiring that these be satisfied along with Properties 1 through 3 narrows the field
to exactly one cubic spline, according to Theorem 3.9. It turns out that there are many different
versions of Property 4, meaning many other pairs of endpoint conditions, for which an analogous
theorem holds. In this section we present a few of the more popular ones.
Property 4b. (Curvature-adjusted cubic spline) The first alternative to a natural cubic spline is a
′′ (x ) are set to arbitrary values, chosen
simple generalization. The values of S1′′ (x1 ) and Sn−1
n
by the user, instead of zero. This choice corresponds to setting the desired curvatures at
the left and right endpoints of the spline. In terms of (3.24) this translates to the two extra
Chapter 3 I NTERPOLATION
167
4
4
3
3
2
2
1
1
1
2
3
4
5
1
2
(a)
4
3
3
2
2
1
1
2
4
5
3
4
5
(b)
4
1
3
3
4
5
1
(c)
2
(d)
Figure 3.13: Cubic splines through 6 points. The plots are generated by splinecoeff.m
with input vectors x = [0 1 2 3 4 5] and y = [3 1 6 1 2 0]. (a) Natural cubic spline
(notice inflection points at ends) (b) Not-a-knot cubic spline (single cubic equation on [0, 2], and
on [3, 5]) (c) Parabolically terminated spline (d) Clamped cubic spline (clamped at slope 0 at both
ends)
conditions
2c1 = v1
2cn = vn ,
where v1 , vn denote the desired values. The equations turn into the two tableau rows
2 0 0 0 0 · · · · · · 0 0 | v1
0 0 0 0 0 · · · · · · 0 2 | vn
to replace the top and bottom rows of (3.31), which were added for the natural spline. Notice
that the new coefficient matrix is again strictly diagonally dominant, so that a generalized
form of Theorem 3.9 holds for curvature-adjusted splines. See Theorem 3.10 below.
In splinecoeff.m, the two lines
A(1,1)=2;r(1)=v1;
A(n,n)=2;r(n)=vn;
% curvature-adj conditions
168
3.4
C UBIC
SPLINES
must be substituted in place of the two existing lines for the natural spline.
The next alternative set of end conditions is
Property 4c. (Clamped cubic spline) Similar to the above, but it is the values of the first deriva′
tives S1′ (x1 ) and Sn−1
(xn ) that are set to user-specified values v1 and vn , respectively. Thus
the slope at the beginning and end of the spline are under the user’s control.
Using (3.29) and (3.30), the extra condition S1′ (x1 ) = v1 can be rewritten
∆1
2δ1 c1 + δ1 c2 = 3
− v1 ,
δ1
′
and Sn−1
(xn ) = vn can be written as
∆n−1
δn−1 cn−1 + 2δn−1 cn = 3 vn −
δn−1
.
The two corresponding tableau rows are
0
0
|
3(∆1 /δ1 − v1 )
2δ1 δ1 0 0 · · · · · · 0
0
0 0 0 · · · · · · 0 δn−1 2δn−1 | 3(vn − ∆n−1 /δn−1 )
Note the strict diagonal dominance holds also for the revised coefficient matrix in (3.31), so
Theorem 3.9 also holds with natural replaced with clamped. In splinecoeff.m, the two
lines
A(1,1:2)=[2*dx(1) dx(1)];r(1)=3*(dy(1)/dx(1)-v1); % clamped conditions
A(n,n-1:n)=[dx(n-1) 2*dx(n-1)];r(n)=3*(vn-dy(n-1)/dx(n-1));
must be substituted.
Property 4d. (Parabolically-terminated cubic spline) The first and last parts of the spline, S1
and Sn−1 , are forced to be at most degree 2, by specifying d1 = 0 = dn−1 . Equivalently,
according to (3.29), we can require c1 = c2 and cn−1 = cn . The equations form the two
tableau rows
1 −1 0 0 0 · · · · · · 0 0
0 | 0
0
0 0 0 0 · · · · · · 0 1 −1 | 0
to be used as the top and bottom rows of (3.31). Assume that the number n of data points
satisfies n ≥ 3 (see Exercise 3.4.16 for the case n = 2). In this case, upon replacing c1 by c2
and cn by cn−1 , the matrix equation reduces to a strictly diagonally dominant n − 2 × n − 2
matrix equation in c2 , . . . cn−1 . Therefore a version of Theorem 3.9 holds for parametricallyterminated splines, assuming n ≥ 3.
In splinecoeff.m, the two lines
A(1,1:2)=[1 -1];
A(n,n-1:n)=[1 -1];
must be substituted.
% parabol-term conditions
Chapter 3 I NTERPOLATION
169
Property 4e. (Not-a-knot cubic spline) The two added equations are d1 = d2 and dn−2 = dn−1 ,
′′′ (x
′′′
or equivalently S1′′′ (x2 ) = S2′′′ (x2 ) and Sn−2
n−1 ) = Sn−1 (xn−1 ). Since S1 and S2 are
degree 3 or less polynomials, requiring their third derivatives to agree at x1 , while their zero,
first, and second derivatives already agree there, causes S1 and S2 to be identical cubic polynomials (cubics are defined by 4 coefficients, and 4 conditions are specified). Therefore the
result of this condition is that x2 is not needed as a base point: the spline is given by the same
formula S1 = S2 on the entire interval [x1 , x3 ]. The same reasoning shows that Sn−2 = Sn−1 ,
and so not only x2 but also xn−1 is “no longer a knot”.
Note that d1 = d2 implies that (c2 − c1 )/δ1 = (c3 − c2 )/δ2 , or
δ2 c1 − (δ1 + δ2 )c2 + δ1 c3 = 0,
and similarly dn−2 = dn−1 implies
δn−1 cn−2 − (δn−2 + δn−1 )cn−1 + δn−2 cn = 0.
The two tableau rows are
δ2 −(δ1 + δ2 ) δ1 0 · · ·
0
0
0 0 ···
···
···
0
0
0
0
| 0
0 δn−1 −(δn−2 + δn−1 ) δn−2 | 0
In splinecoeff.m, the two lines
A(1,1:3)=[dx(2) -(dx(1)+dx(2)) dx(1)]; % not-a-knot conditions
A(n,n-2:n)=[dx(n-1) -(dx(n-2)+dx(n-1)) dx(n-2)];
are used. Figure 3.13(b) shows an example of a not-a-knot cubic spline, compared with the
natural spline through the same data points in part (a) of the figure.
As mentioned above, a theorem analogous to Theorem 3.9 exists for each of the above choices
of end conditions. That is:
Theorem 3.10 Assume n ≥ 2. For a set of data points (x1 , y1 ), . . . , (xn , yn ) and for any one of the end
conditions Properties 4a - 4c, there is a unique cubic spline satisfying the end conditions and fitting the
points. The same is true assuming n ≥ 3 for Property 4d and n ≥ 4 for Property 4e.
Matlab’s default spline command constructs a not-a-knot spline when given 4 or more points.
Let x and y be vectors containing the xi and yi data values, respectively. Then the y-coordinates of
the not-a-knot spline at another input x0 is calculated by the Matlab command
>> y0 = spline(x,y,x0);
If x0 is a vector of x-coordinates, then the output y0 will be a corresponding vector of ycoordinates, suitable for plotting, etc. Alternatively, if the vector input y has exactly two more
inputs than x, the clamped cubic spline is calculated, with v1 and vn equal to the first and last
entries of y.
Exercises 3.4
170
3.4
3.4.1. Decide whether
S(x) =
x3 + x − 1
−(x − 1)3 + 3(x − 1)2 + 3(x − 1) + 1
C UBIC
SPLINES
on [0, 1]
on [1, 2]
is a natural cubic spline.
3.4.2. Assume the two leftmost parts of a potential cubic spline are given by
S1 (x) = 1 + 2x + 3x2 + 4x3 on [0, 1]
S2 (x) = 10 + 20(x − 1) + 15(x − 1)2 + 4(x − 1)3 on [1, 2]
(a) Check the spline conditions. Decide whether this can or cannot be part of a spline.
(b) Regardless of your answer to (a), decide whether any of the following extra conditions are satisfied for this
example: natural, parabolically terminated, not-a-knot.
3.4.3. How many natural cubic splines on [0, 2] are there for the given data (0, 0), (1, 1), (2, 2)? Exhibit one such spline.
3.4.4. Find the parabolically-terminated cubic spline through the data points (0, 1), (1, 1), (2, 1), (3, 1), (4, 1). Is this
spline also not-a knot? natural?
3.4.5. Find S ′ (2) for the cubic spline
S1 (x) = 3 + b1 x + x3 on [0, 1]
S2 (x) = 1 + b2 (x − 1) + 3(x − 1)2 − 2(x − 1)3 on [1, 2]
3.4.6. Can the following be two parts of a cubic spline? Explain why or why not.
2x3 + x2 + 4x + 5 on [0, 1]
(x − 1)3 − 5(x − 1)2 + 13(x − 1) + 12 on [1, 2]
3.4.7. Find c in the following cubic splines. Also, which of the three end conditions, natural, parabolically terminated,
or not-a-knot, if any, are satisfied?
(a)
S(x) =
x + 43 x3
4 − 11
4
1
2 − 2 (x − 1) + c(x − 1)2 − 43 (x − 1)3
on [0, 1]
on [1, 2]
(b)
S(x) =
3 − 9x + 4x2
−2 − (x − 1) + c(x − 1)2
on [0, 1]
on [1, 2]
(c)

2
3
 −2 − 23 x + 27 x − x
1
−1 + c(x − 1) + 2 (x − 1)2 − (x − 1)3
S(x) =

1 + 21 (x − 1) − 52 (x − 1)2 − (x − 2)3
3.4.8. Find k1 , k2 , k3 in the following cubic spline. Also, which of the three end
terminated, or not-a-knot, if any, are satisfied?

2
3
 4 + k1 x + 2x − 16 x
1 − 34 (x − 1) + k2 (x − 1)2 − 61 (x − 1)3
S(x) =

1 + k3 (x − 2) + (x − 2)2 − 61 (x − 2)3
on [0, 1]
on [1, 2]
on [2, 3]
conditions, natural, parabolically
on [0, 1]
on [1, 2]
on [2, 3]
3.4.9. How many not-a-knot cubic splines are there for the given data (1, 3), (3, 3), (4, 2), (5, 0)? Exhibit one such
spline.
3.4.10. True or false: Given n = 3 data points, the parabolically-terminated cubic spline through the points must also be
not-a-knot.
3.4.11.
(a) How many parabolically-terminated cubic splines on [0, 2] are there for the given data (0, 2), (1, 0), (2, 2)?
Exhibit one such spline.
(b) Same question for not-a-knot.
3.4.12. Can a cubic spline be both natural and parabolically-terminated? If so, what else can you say about such a spline?
Chapter 3 I NTERPOLATION
171
3.4.13. Assume the leftmost piece of a given natural cubic spline is the constant function S1 (x) = 1, on the interval
[−1, 0]. Find three different possibilities for the neighboring piece S2 (x) of the spline on [0, 1].
3.4.14. Does there exist a (simultaneously) natural, parabolically-terminated, not-a-knot cubic spline through each set of
data points (x1 , y1 ), . . . , (x100 , y100 ) with distinct xi ? If so, give a reason. If not, explain what conditions must
hold on the 100 points in order for such a spline to exist.
3.4.15. Assume a car travels along a straight road from one point to another, from a standing start at time t = 0 to a
standing stop at time t = 1. The distance along the road is sampled at certain times between 0 and 1. Which
cubic spline (in terms of end conditions) will be most appropriate for describing distance versus time?
3.4.16. (a) The case n = 2 for parabolically-terminated cubic splines is not covered by Theorem 3.10. Discuss existence
and uniqueness for the cubic spline in this case. (b) Similarly, the cases n = 2 and 3 are not covered for not-a-knot
cubic splines. Discuss existence and uniqueness.
Computer Problems 3.4
3.4.1. Find the equations and plot the natural cubic spline that interpolates the data points (a) (0, 3), (1, 5), (2, 4), (3, 1)
(b) (−1, 3), (0, 5), (3, 1), (4, 1), (5, 1).
3.4.2. Repeat the previous problem, but find the not-a-knot cubic spline.
3.4.3. Find a cubic spline S satisfying S(0) = 1, S(1) = 3, S(2) = 3, S(3) = 4, S(4) = 2, and with S ′′ (0) =
S ′′ (4) = 0.
3.5 Bezier splines
splines are a type of spline that allows the user to control the slopes at the knots. In return for the extra
freedom, the smoothness of the first and second derivatives across the knot, which are automatic features of the
cubic splines of the previous section, are no longer guaranteed. Bezier splines are appropriate for cases where corners
(discontinuous first derivatives) and abrupt curvature changes (discontinuous second derivatives) are sometimes needed.
Pierre Bezier (1910-1999) developed this idea during his work for the Renault automobile company. The same idea
was discovered independently and used by Paul de Casteljau, working for Citroen, a rival automobile company. Because
the Bezier spline was considered an industrial secret for both companies, the fact that both had developed the idea came
to light only after Bezier published his research. Today Bezier splines are a fundamental component of computer-aided
design and manufacturing. In addition, Bezier splines are nearly ubiquitous due to their role in modern font design, as we
will see later.
Each piece of a Bezier spline is determined by four points (x1 , y1 ), (x2 , y2 ), (x3 , y3 ), (x4 , y4 ). The first and last of
the points are in the spline curve, the middle two are control points, as shown in Figure 3.15. The curve leaves (x1 , y1 )
along the tangent direction (x2 − x1 , y2 − y1 ), and ends at (x4 , y4 ) along the tangent direction (x4 − x3 , y4 − y3 ). The
equations that accomplish this are expressed as a parametric curve (x(t), y(t)) for 0 ≤ t ≤ 1:
B
EZIER
x(t)
=
x1 + bx t + cx t2 + dx t3
y(t)
=
y1 + by t + cy t2 + dy t3
where
bx
=
3(x2 − x1 )
cx = 3(x3 − x2 ) − bx
by
=
3(y2 − y1 )
cy = 3(y3 − y2 ) − by
dx = x4 − x1 − bx − cx
dy = y4 − y1 − by − cy
One checks the facts of the previous paragraph from the equations. In fact,
and the analogous facts hold for y(t).
x(0)
=
x0
x′ (0)
=
3(x2 − x1 )
x(1)
=
x4
x′ (1)
=
3(x4 − x3 )
(3.35)
172
3.5
(x ,y )
3
B EZIER
SPLINES
(x ,y )
2 2
3 3
2
(x ,y )
4 4
1
(x ,y )
1 1
1
2
3
Figure 3.14: Bezier spline of Example 3.15. The points (x1 , y1 ) and (x4 , y4 ) are spline points,
while (x2 , y2 ) and (x3 , y3 ) are control points.
Example 3.15 Find the Bezier spline (x(t), y(t)) through the points (x, y) = (1, 1) and (2, 2) with control points (1, 3) and
(3, 3).
The four points are (x1 , y1 ) = (1, 1), (x3 , y2 ) = (1, 3), (x3 , y3 ) = (3, 3), (x4 , y4 ) = (2, 2). Formula (3.35) yields
bx = 0, cx = 6, dx = −5 and by = 6, cy = −6, dy = 1. The Bezier spline is
x(t)
=
1 + 6t2 − 5t3
y(t)
=
1 + 6t − 6t2 + t3
The spline is shown in Figure 3.14 along with the control points.
Bezier splines are building blocks that can be put side-by-side to fit aribitrary function values and slopes. They are
an improvement over cubic splines, in the sense that the slopes at the nodes can be specified as the user wants them.
However, this freedom comes at the expense of curvature smoothness. The second derivatives from the two different
directions generally disagree at the nodes. Which type of spline is preferred depends on the application.
Bezier splines are also simple to program and are often used in drawing software. A freehand curve in the plane can
be represented as a parametric curve (x(t), y(t)), which can be replaced by a Bezier spline. We have implemented the
equations in the following Matlab freehand drawing program. The user clicks the mouse twice to fix a point in the plane;
once to define the starting point (x0 , y0 ), and the second click, to mark the first control point, in the intended direction of
the path. After another pair of clicks, to define the second control point and endpoint, a Bezier spline is drawn between
the two points. Each subsequent triple of mouse clicks extends the curve further, using the previous spline point as the
starting point for the next piece. The Matlab command ginput is used to read the mouse location, and which button is
being activated.
%Program 3.7 Freehand Draw Program Using Bezier Splines
%Left click in Matlab figure window to locate point, and click three
%
more times to specify 2 control points and the next spline point.
%
Continue with groups of 3 points to add more to
%
the curve. Right-click to terminate program.
hold off
plot([-1 1],[0,0],’k’,[0 0],[-1 1],’k’);hold on
xlist=[];ylist=[];t=0:.02:1;
button=1;k=0;
Chapter 3 I NTERPOLATION
173
1
0.8
0.6
0.4
0.2
0
−0.2
−0.4
−0.6
−0.8
−1
−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Figure 3.15: Draw Program 3.7 made with Bezier splines. Screenshot of Matlab code draw.m
including direction vectors drawn at each control point.
while(button ˜= 3)
% if right click, terminate
[xnew,ynew,button] = ginput(1); % get one mouse click
if button == 1
% current click is a left click
k=k+1;
% k counts clicks
xlist(k)=xnew; ylist(k)=ynew; % add new point to the list
if k>1
% After first input point:
if mod(k,3) == 1
% Wait for three new input points,
for i=1:4
% gather the previous four points
x(i)=xlist(k-i+1);
y(i)=ylist(k-i+1);
end
% Plot spline points and control pts
plot([x(1) x(2)],[y(1) y(2)],’c:’,x(2),y(2),’cs’);
plot([x(3) x(4)],[y(3) y(4)],’c:’,x(3),y(3),’cs’);
plot(x(1),y(1),’bo’,x(4),y(4),’bo’);
bx=3*(x(2)-x(1)); by=3*(y(2)-y(1)); % spline equations ...
cx=3*(x(3)-x(2))-bx;cy=3*(y(3)-y(2))-by;
dx=x(4)-x(1)-bx-cx;dy=y(4)-y(1)-by-cy;
xp=x(1)+t.*(bx+t.*(cx+t*dx));
% Horner’s method
yp=y(1)+t.*(by+t.*(cy+t*dy));
plot(xp,yp)
% Plot spline curve and wait for more pts
end
end
end
end
◮
REALITY CHECK: C ONSTRUCTING
FONTS FROM
B EZIER
SPLINES
Although developed for the design of automobile shapes for aerodynamic studies, Bezier splines
◭
174
3.5
B EZIER
SPLINES
have been used for many applications. They found a natural use with the advent of computer typesetting in the 1980’s. Chuck Geschke and John Warnock were engineers for Xerox Corporation who
quit to form a company called Adobe. They released the Postscript language in 1984, where it came
to the attention of Steve Jobs at Apple Corporation, who was looking for a way to control their first
laser printer. Postscript is a vector-based page description language, which revolutionized printing
in the sense that it was device-independent. Formerly, fonts at different sizes had to be tuned to the
dot patterns of printers of various resolutions. Using Bezier splines, it was simple to adapt the same
mathematical curves to whatever resolution was required.
A bare-bones Postscript file follows:
%!PS
newpath
100 100 moveto
100 300 300 300 200 200 curveto
stroke
This is a complete Postscript file. The newpath command initiates the curve. Postscript uses a
stack and postfix commands, similar to HP handheld calculators. The moveto command sets the
current plot point to be the (x, y) point specified by the last two numbers on the stack, in this case,
the point (100, 100). The curveto command accepts three (x, y) points from the current stack,
and constructs the Bezier spline starting at the current plot point, using the next three (x, y) pairs as
the two control points and the endpoint, respectively. If the above file is sent to a Postscript printer,
the Bezier curve of Example 3.15 will be printed. We have multiplied the coordinates by 100 to
match the conventions of Postscript, which are 72 units to the inch. A sheet of letter-sized paper is
612 Postscript units wide and 792 high.
Postscript fonts are drawn on computer screens and printers using the Bezier curves of the
curveto command. The fill command, given at the end, will fill in the outline if the figure is
closed.
The upper case T character in the Times-Roman font is constructed of the following 16 Bezier
curves. Each line consists of the numbers x1 y1 x2 y2 x3 y3 x4 y4 that define one piece of the Bezier
spline.
237
237
143
143
435
435
339
339
393
552
576
570
6
0
24
183
620
120
19
0
0
19
109
620
620
492
492
662
662
492
492
620
237
237
143
143
435
353
339
339
507
552
576
570
6
0
48
183
620
35
19
0
0
23
108
620
620
492
492
662
662
492
602
620
237
226
143
435
435
339
339
393
529
576
570
6
0
24
71
237
120
24
0
0
19
36
620
620
602
492
662
662
492
492
620
620
237
143
143
435
435
339
339
393
552
576
570
6
0
24
183
237
120;
19;
0;
0;
19;
109;
620;
620;
492;
492;
662;
662;
492;
492;
620;
620;
The Times-Roman lower case f requires 21 Bezier curves:
Chapter 3 I NTERPOLATION
289
166
166
223
276
321
363
259
118
83
1
1
83
83
0
0
260
260
167
167
289
452
452
568
657
618
580
621
683
611
450
450
418
418
104
15
0
0
15
104
418
418
289
166
166
245
292
345
363
196
92
83
1
1
83
83
0
0
260
178
167
167
289
452
452
627
657
589
580
657
683
566
450
450
418
418
31
15
0
0
18
104
418
418
166
166
185
258
304
363
319
144
84
1
1
83
83
72
0
260
260
167
167
289
289
452
568
657
647
580
598
683
656
530
450
418
418
104
19
0
0
15
29
418
418
452
166
166
223
276
321
363
259
118
83
1
1
83
83
0
0
260
260
167
167
289
289
175
452;
568;
657;
618;
580;
621;
683;
611;
450;
450;
418;
418;
104;
15;
0;
0;
15;
104;
418;
418;
452;
Figure 3.16: Times-Roman 5 made with Bezier splines. Spline points are circles, control points
are squares.
The number 5 also requires 21 curves:
149
345
368
406
397
351
142
33
32
44
149
324
185
597
597
606
695
702
676
676
439
434
426
408
208
37
149
361
406
397
382
351
33
32
32
74
269
324
165
597
597
695
702
681
676
439
438
428
426
372
112
37
149
365
368
406
372
351
142
32
35
109
324
264
149
597
599
606
695
676
676
676
436
426
420
310
37
44
345
368
406
397
351
142
33
32
44
149
324
185
119
597;
606;
695;
702;
676;
676;
439;
434;
426;
408;
208;
37;
66;
176
3.5
119 66 86 90
42 99 14 99
0 62
0 24
121
0 205
0
333 78 378 123
399 256 399 327
333 422 288 468
112 512 112 512
65
0
46
282
399
381
232
149
99
87
0
27
180
372
491
597
42
0
121
333
399
333
112
149
B EZIER
SPLINES
99;
62;
0;
78;
256;
422;
512;
597;
1. Use the draw.m program to sketch the upper case initial of your first name.
2. Revise the draw program to accept an n × 8 matrix of numbers, each row representing a piece
of a Bezier spline. Use the program to draw the letter f in the Times-Roman font, using the above
Bezier data.
3. Write a Postscript file to draw the letter f. The program should begin with a moveto to
the first point, followed by 21 curveto commands. Test it by opening your file in a Ghostscript
viewer or by sending to a Postscript printer.
4. Although font information was a closely-guarded secret for many years, much of it is now
freely available on the web. Search for other fonts, and find the Bezier curve data to draw letters of
your choice in Postscript or using draw.m.
◮
◭
Exercises 3.5
3.5.1.
Computer Problems 3.5
3.5.1.
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement