Next: About this document ...
Assignment 6, Math 575A
Part I Matlab Section:
MATLAB has special functions to deal with polynomials.
Using these commands is usually recommended, since they
make the code easier to write and understand and are usually more efficient.
In this HW assignment you should try to use MATLAB polynomial
commands (and avoid for loops) as much as possible.
The polynomial
and
are represented in MATLAB by:
P = [2 2 -4];
Q = [1 0 -6];
is evaluated, for example, using:
polyval(P,4.7)
You can plot
in the interval
using:
x = [-6:0.1:6];
plot(x,polyval(Q,x))
The polynomial
is calculated using
S = P+Q;
(but addition or subtraction of polynomials with different degrees
takes somewhat more effort). Multiplication is easy and the degrees
do not have to be equal. The multiplication
is
represented by
T = conv(P,Q);
Additional useful commands are prod,
roots (finds the roots of a
polynomial) and poly (constructs a polynomial with specified roots).
For vectors, roots and poly are inverse
functions of each other, up to ordering, scaling, and
roundoff error. For more info look at the table
of the MATLAB Primer and use the online help or ask for
technical assistance.
In this assignment you are asked to hand in many plots.
You can save paper by combining several plots on one page
using the subplot command. Use the help feature to find out how
to use subplot
Part II Theory Part:
- Find the best approximation in the
-norm of
on
.
using polynomials of at most degree
. Compare this norm to the norm
of the polynomial
obtained by computing the Taylor series of degree
about
.
- Construct the Lagrange interpolating polynomials for the following
functions and find a bound for the absolute error on the interval
:
-
where
, where
.
-
, where
, where
.
- Find the polynomial of least degree that interpolates these sets
of data:
-
-
- In class we discussed piece-wise linear
interpolation and introduced the ``hat functions''
(
).
These were claimed to form a basis for
, where
. Show that
these indeed form a basis.
- Show that the Chebyshev polynomials, which are defined over
, have the following properties:
-
, where
.
-
, where
is the Kroneker delta function.
- They satisfy the ordinary differential equation
- Show that
and
, for
.
- Plot a unit circle, centered at
. Project the location of
equally spaced points on this circle to the horizontal axis
, and
convince yourself that these lie at locations
with
. These are called the Chebyshev
interpolation points.
Part III Experimental Part:
For the following exercises you can adapt the following code, which
implements ``Neville's Algorithm'' for the generation of the Lagrange
polynomials.
%Lagrange Polynomial Algorithm by
%Neville Interpolation at a vector of %points.
% coded by Rachel Labes, Oct 2002
clc;
clear;
format long
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Input
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n = input('Enter the number of points to interpolate at: ');
x = input('Enter the vector of points to interpolate at: ');
d = input('Enter the vector of function values of interpolation points: ');
t = input('Enter the value to approximate the function at: ');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Neville Method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Q = zeros(n,n); %Initializes an n x n matrix
Q(:,1) = d'; %Enters the vector of
% function values as the first column of Q.
Q;
for i = 2:n
for j = 2:i
Q(i,j) = ...
[(t - x(i - 1))*Q(i,j-1) - (t - x(i))*Q(i-1,j-1)]/(x(i) - x(i-1));
end
end
%Q(i,j) calculates the polynomial approximation for each matrix point.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Output%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Q %Displays the complete matrix Q.
P = input('Enter the point in the matrix to be outputed: ');
disp('The value of the polynomial is approximately: ')
disp(P) %Displays the function approximation for that Lagrange
%polynomial.
- For
and
defined in the introduction evaluate
by hand and compare with the result you get from MATLAB.
Plot
,
,
and the x-axis (e.g. using grid on)
on the same graph.
What do you observe about the roots of the three polynomials?
-
Use appropriate Lagrange interpolation polynomials of degree one,
two three and four to approximate each of the following:
if
,
,
,
,
.
if
,
,
,
,
- To approximate the function
, we will use the
points (1,1) (4,2) and (9,3).
- Write the formula for the
Lagrange Polynomial
that interpolates these three
points.
- Write a .m function file that implements
using
the MATLAB polynomial commands.
- To see how well the approximation is:
- Plot
and
in the interval
.
- Plot
in the interval
.
- Plot
in the interval
.
- Using the plots determine where the approximation is
better/worse.
- Why do you get these results?
Do they agree with the formula derived in class
for the error bound?
- Add the point (0,0) and repeat (a)-(c). Do you see any
improvement? Deterioration? Where? Why?
- In this exercise we will pretend that MATLAB stores the
values of the function
only for
. Use these values and the
appropriate Lagrange polynomials of degrees zero, one, two
three and four to approximate
. How does the absolute
error change?
- We would like to approximate the function
in the interval
using
equally spaced x-values.
- Write a program that evaluates
the corresponding Lagrange polynomials using
points.
- Plot on one page (using subplot)
four plots of the function versus the
interpolation polynomial using 3,7,11 and 15 points.
- What do you observe?
- For each case, plot also the absolute error
versus the theoretical error bound
. You may want to plot them initially on separate
graphs. Then, come up with a meaningful way to compare the
plots using just one graph.
- What do you observe?
- In this exercise you will analyze the problems that arise when
the interpolation polynomial is evaluated using the
Vandermonde matrix. We will find the interpolation polynomial
for the nice smooth function sin (x) and see that as
increases problems arise.
To do so, run the following program (available as Vandermun.m
on the class home page)
% Comparison of the interpolation polynomial Pn
% for sin(x) in the interval [1 2] with sin(x)
% Pn(x) = c_1 x^n + ..+ c_n x + c_{n+1}
%
for i = 0:3
Nx = 10*3^i;
dx = 1/Nx;
x = [1:dx:2]';
% Find the interpolation polynomial using
% the Vandermonde matrix
V = vander(x);
C = V\sin(x); % Warning: use \, not /
% Plot the results at grid values other than the
% ones used in the interpolation.
y = x+0.1*dx*rand(size(x));
subplot(2,2,i+1)
plot(y,polyval(C,y)-sin(y))
xlabel('x')
ylabel('Pn(x)-f(x)')
title(['Nx = ',num2str(Nx)])
end
figure(gcf);
Do not submit the plots or the program, but answer the following
questions:
- Using the error formula derived in class, show that
as
for
.
- Does this agree with your plots?
- In practice, is larger
good or bad?
- What is the source of the problem with large
?
(Hint: Run the program again but use the grid points
rather than
in the plot
command. Also note the MATLAB error messages.)
Next: About this document ...
Juan Restrepo
2007-07-13