This lab will consume three sessions. It covers material from Sections 3.1 and 3.8 of Atkinson on interpolation on evenly-spaced points. If you print this lab, you may prefer to use the pdf version.
This lab is concerned with interpolating data with polynomials and with trigonometric functions. There is a difference between interpolating and approximating. Interpolating means that the function passes through all the given points little regard to the points between them. Approximating means only that the function is close to all the given points. Although it might seem that interpolating is always better than approximating, this is not true.
Polynomials are commonly used to interpolate data. There are several methods for defining, constructing, and evaluating the polynomial of a given degree that passes through data values. One common way is to solve for the coefficients of the polynomial by setting up a linear system called Vandermonde's equation. A second way is to represent the polynomial as the sum of simple Lagrange polynomials. Other ways are available and are discussed in Atkinson. In addition, we will discuss interpolation using trigonometric polynomials.
We will examine these two ways of finding the interpolating polynomial and a way of finding the interpolating trigonometric polynomial. We will see that interpolation does not necessarily mean good approximation and that polynomial interpolation can break down with a fatal case of ``the wiggles.'' Trigonometric polynomial interpolation does better, but can also break down.
Written in the customary notation, it is easy to see that the
quantities
and
are essentially different. In
Matlab, without kerning and font differentiation, it can be
difficult to keep the various quantities straight. In this
lab, we will use the name xval to denote the values of
and the names xdata and ydata to denote the
data
and
. The variable names fval or
yval are used
for the value
to emphasize that it is the value of the
function approximating
.
At some point in this lab, you will need to determine if a quantity A is not equal to a quantity B. The Matlab syntax for this is
if A ~= BSometimes it is convenient to enclose the logical expression in parentheses, but this is not required.
If you have a (square) linear system of equations, of the form
A * x = bwhere A is an N by N matrix, and b and x are column vectors, then Matlab can solve the linear system either by using the expression:
x = inv(A)*bor, better, via the ``backslash'' command:
x = A \ bThe backslash command is strange looking. You might remember it by noting that the matrix A appears underneath the backslash. The difference between the two commands is merely that the backslash command is about three times faster because it solves the equation without constructing the inverse matrix. You will not notice a difference in this lab, but you might see one later if you use Matlab for more complicated projects.
The backslash command is actually more general than just multiplication by the inverse. It can find solutions when the matrix A is singular or when it is not a square matrix! We will discuss how it does these things later in the course, but for now, be very careful when you use the backslash operator because it can find ``solutions'' when you do not expect a solution.
Matlab has several commands for dealing with polynomials:
When there are more data values than the minimum, the polyfit function returns the coefficients of a polynomial that ``best fits'' the given values in the sense of least squares. This polynomial approximates, but does not necessarily interpolate, the data. In this lab, you will be writing m-files with functions similar to polyfit but that generate polynomials of the precise degree determined by the number of data points. (n=length(xdata)-1.
The vector of coefficients c of a polynomial in Matlab are, by convention, defined as
In this and some later labs, you will be writing m-files with functions analogous to polyfit and polyval, using several different methods. Rather than following the matlab naming convention, functions with the prefix coef_ will generate a vector of coefficients, as by polyfit, and functions with the prefix eval_ will evaluate a polynomial (or other function) at values of xval, as with polyval.
In the this lab and often in later labs we will be using Matlab functions to construct a known polynomial and using it to generate ``data'' values. Then we use our interpolation functions to recover the original, known, polynomial. This strategy is a powerful tool for illustrative and debugging purposes, but practical use of interpolation starts from arbitrary data, not contrived data.
In Lab 4 we discussed Newton's method, which can be derived by
determining a linear polynomial (degree 1) that passes through the
point
with derivative
. That is
and
. One way of looking at this is that we are
constructing an interpolating function, in this case a polynomial,
that explains all the data that we have. We may then want to examine
the graph of the polynomial, evaluate it at other points, determine
its integral or derivative, or do other things with it. This is a
reason that polynomial interpolation is important even when the
functions we are interested in are not polynomials.
Here's one way to see how to organize the computation of the polynomial that passes through a set of data. It's not the best way, but it will give us a good place to start.
Suppose we wanted to determine the linear polynomial
that passes through the data points
and
. We simply have to
solve a set of linear equations for
and
.
Compare that situation with the case where we want to determine the
quadratic polynomial
that passes through three sets of data values. Then we
have to solve the following set of linear equations for the
polynomial coefficients c:
This is an example of a third order Vandermonde Equation. It is characterized by the fact that for each row (sometimes column) of the coefficient matrix, the succesive entries are generated by a decreasing (sometimes increasing) set of powers of a set of variables.
You should be able to see that, for any collection of abscissae and ordinates, it is possible to define a linear system that should be satisfied by the (unknown) polynomial coefficients. If we can solve the system, and solve it accurately, then that is one way to determine the interpolating polynomial.
Now, let's see how to construct and solve the Vandermonde equation
using Matlab. This involves setting up the coefficient matrix A.
As before, we use the Matlab variables xdata and ydata
to represent the quantities
and
, and we will assume them
to have length n.
for j = 1:n
for k = 1:n
A(j,k) = xdata(j)^(n-k) ;
end
end
Then we have to set the right hand side to the ordinates ydata.
If we can get all of that set up, then actually solving the linear
system is easy. We just write:
c = A \ ydata';Recall that the backslash symbol means to solve the system with matrix A and right side ydata'. Notice that we are using the transpose of the row vector ydata in this equation. By default, Matlab constructs row vectors unless told to do otherwise.
function c = coef_vander ( xdata, ydata ) % c = coef_vander ( xdata, ydata ) % xdata= ??? % ydata= ??? % c= ??? % other comments % your name and the datethat accepts a pair of vectors xdata and ydata of arbitrary but equal length, and returns the coefficient vector
c of the polynomial
that passes through that data.
xdata= [ 0 1 2 ] ydata= [ 0 1 4 ]
xdata= [ -3 -2 -1 0 1 2 3] ydata= [-364 -63 -6 -1 0 21 182]
In the following exercise you will construct a polynomial using coef_vander to interpolate data points and then you will see what happens between the interpolation points.
xdata=[0 1 2 3 4 5]; ydata=[?? 0 0 0 0 0];Call these coefficients cVander.
xval=linspace(0,5,500); % test abscissae for plotting yvalTrue=polyval(cTrue,xval); % true ordinates yvalVander=polyval(cVander,xval); % our ordinates plot(xval,yvalTrue,'b'); % true curve in blue hold on plot(xval,yvalVander,'r'); % our curve in red hold off norm(yvalTrue-yvalVander)Please include a copy of this plot with your summary. (If it appears that there is only one curve, it is possible that the two curves are on top of one another.)
Suppose we fix the set of
distinct abscissæ
,
and think about the problem of constructing a polynomial that has
(not yet specified) values
at these points. Now suppose
I have a polynomial
that is zero at each
abscissa value, except that at
, where it is
1. Then the intermediate polynomial
would have the
value
at
, and be 0 at all the other
.
Doing the same for each abscissa and adding the intermediate
polynomials together results in the polynomial that interpolates
the data!
In fact, the Lagrange polynomials
are easily constructed
for any set of abscissae. Each Lagrange polynomial will be of degree
.
There will be
Lagrange
polynomials, one per abscissa, and the
polynomial
will have the special relationship with the abscissa
, namely, it will be 1 there, and 0 at the other abscissæ.
In terms of Lagrange polynomials, then, the interpolating
polynomial has the form:
Remark: The trick of finding a function that equals 1 at a distinguished point and zero at all other points in a set is very powerful. One of the places you will meet it again is when you study the construction of finite elements for solving partial differential equations.
k : 1 2 3 xdata= [ 0 1 2 ] ydata= [ 0 1 4 ](Actually, ydata is immaterial for construction of
lagrange.m that
computes the
Lagrange polynomials (3) for any k.
The signature should be
function pval = lagrange( k , xdata, xval ) % pval = lagrange( k , xdata, xval ) % comments % k= ??? % xdata= ??? % xval= ??? % pval= ??? % your name and the dateand the function should evaluate the k-th Lagrange polynomial for the abscissas xdata at the point xval. Hint, you can implement the general formula using code like the following.
pval = 1;
for j = 1 : n
if j ~= k
pval = pval .* ???
end
end
Note 1: You will need to decide what to use for n.
Note 2: If xval is a vector of values, then pval will be the vector of corresponding values, so that an elementwise multiplication (.*) is being performed.
lagrange ( 1, xdata, xdata ) lagrange ( 2, xdata, xdata ) lagrange ( 3, xdata, xdata )
function yval = eval_lag ( xdata, ydata, xval ) % yval = eval_lag ( xdata, ydata, xval ) % comments % your name and the dateThis function should take the data values xdata and ydata, and compute the value of the interpolating polynomial at xval according to (2), using the lagrange.m file for the Lagrange polynomials. Be sure to include comments to that effect.
k : 1 2 3
xdata= [ 0 1 2 ]
ydata= [ 0 1 4 ]
by evaluating it at xval=xdata. Of course, you should
get ydata back.
xdata= [ -3 -2 -1 0 1 2 3] ydata= [-364 -63 -6 -1 0 21 182]
Interpolating functions that are polynomials and using polynomials to do it is cheating a little bit. It is harder to use polynomials to interpolate functions that are not themselves polynomials. At the interpolation points, the function and its interpolant agree exactly, so we want to examine the behavior between the interpolation points.
function y=runge(x) % commentsUse componentwise (vector) division and exponentiation (./ and .^).
% construct n=5 data points n=5; xdata=linspace(-pi,pi,n); ydata=runge(xdata); % construct many test points xval=linspace(??,??,4001); % construct the true function, for reference yvalTrue=runge(??); % use Lagrange polynomial interpolation to evaluate % the interpolant at the test points yval=eval_lag(??,??,xval); % plot reference values in blue, interpolant in red plot(xval,yvalTrue,'b'); hold on plot(xval,yval,'r'); hold off % estimate the approximation error of the interpolant approximationError=max(abs(yvalTrue-yval))Please send me this plot.
n = 5 Approximation Error = ________ n = 11 Approximation Error = ________ n = 21 Approximation Error = ________You probably are surprised to see that the errors do not decrease.
Most people expect that an interpolating polynomial
gives a good approximation to the function
everywhere, no
matter what function we choose. If the approximation is not good, we
expect it to get better if we increase the number of data points.
These expectations hold only when the function does not exhibit
some ``essentially non-polynomial'' behavior. You will see why
the Runge function cannot be approximated well by polynomials
in the following exercise.
n = 5 Approximation Error = ________ n = 11 Approximation Error = ________ n = 21 Approximation Error = ________
n=5 c( 5)= _____ c( 3)= _____ c( 1)= _____ n=11 c(11)= _____ c( 9)= _____ c( 7)= _____ c( 5)= _____ n=21 c(21)= _____ c(19)= _____ c(17)= _____ c(15)= _____
n=5 max(abs(c(2:2: 5)))= _____ n=11 max(abs(c(2:2:11)))= _____ n=21 max(abs(c(2:2:21)))= _____
Atkinson discusses interpolation by trigonometric polynomials in
Section 3.8. I modify his expressions (3.8.11), (3.8.13),
and (3.8.15) here to satisfy the restriction
that subscripts in Matlab must be positive integers.
Here,
is chosen so that
is the number of interpolation
points.
Remark: The trigonometric coefficients
in
(5) play the same role as the polynomial coefficients
in (1).
In the following exercises, we are going to write functions coef_trig to be analogous to polyfit and coef_vander, and also eval_trigl to be analogous to polyval and eval_lag.
function a=coef_trig(f,n) % a=coef_trig(f,n) % f=??? % n=??? % a=??? % comments % your name and the dateThis function should evaluate the trigonometric coefficients
function fval=eval_trig(a,tval) % fval=eval_trig(a,tval) % a=??? % tval=??? % fval=??? % comments % your name and the dateThis function should evaluate Equation (6) at an arbitrary collection of points, tval, not just
Trigonometric polynomial interpolation does well even when the functions are not themselves trigonometric polynomials.
n = 2 Runge Approximation Error = ________ n = 4 Runge Approximation Error = ________ n = 5 Runge Approximation Error = ________You do not have to send me the plots.
The previous exercise shows that trigonometric polynomial interpolation does well for some functions. It does much less well when the function is not continuous. Consider, for example, the function
function y=sawtooth(x) % y=sawtooth(x) % x=??? % y=??? % comments % your name and the date y=(x+pi)-2*pi*(x>=0);Be sure to add the usual comments. This code works because of a trick. In Matlab the logical ``true'' is the same as the value 1, and ``false'' is zero. Hence the result of x>=0 is a vector of ones and zeros, with zeros where x<0 and ones where x>=0.
n = 5 Approximation Error = ________ n = 10 Approximation Error = ________ n = 100 Approximation Error = ________You can see that the approximation error is not shrinking, but if you look at the plots you will see that the error really is shrinking everywhere except near zero, where the oscillations get more rapid but do not get smaller. This behavior is typical and is called Gibbs's phenomenon. Please send me only the plot for n=100.
Back to MATH2070 page.