You are on page 1of 7

HW2 MTH452/552

Homework will generally have three parts: a matlab portion, a


theory portion, and an experimental portion. The matlab portion
is intended to teach some matlab facilities that might not be fa-
miliar to you. you do not need to turn this portion in. The theory
is intended to be turned in as a writeup. For the experimental
section you only turn in the results, not the code.

1. MATLAB PORTION:

(a) 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 P (x) = 2x2 + 2x − 4 and Q(x) = x2 − 6 are
represented in MATLAB by:
P = [2 2 -4];
Q = [1 0 -6];
P (4.7) is evaluated, for example, using:
polyval(P,4.7)
You can plot Q(x) in the interval [−6, 6] using:
x = [-6:0.1:6];
plot(x,polyval(Q,x))
The polynomial S(x) = P (x) + Q(x) 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 T (x) = P (x) ∗ Q(x) 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

1
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.
(b) matlab has very extensive interpolation tools. Type help interp1
to see the basic facilities for 1D data sets. (You should also look for
contributed codes in the mathworks web site).
The plotting window has built-in data analysis tools. Type
x = linspace(0,2*pi,10);
y = sin(x);
plot(x,y,’*’)

On the banner of the plot window, go to tools and then to basic


fitting. Hit the linear toggle. Can you make sense out of the
linear fit? (hint: the edge conditions). Hit the quadratic toggle
and make sense of this case, especially when you compare it to the
cubic fit (hint: what is the lowest order polynomial that can support
inflection points?). You can have it display the equations for these
or any other polynomial fits. Try a fifth order and then a really high
order polynomial fit (what happens?). Remove some of the fits, and
then try the spline fit.
2. THEORY SECTION:

(a) Find the best approximation in the L2 -norm of ex on x ∈ [−1, 1]


using a polynomial of at most degree 5. Some hints and directions:
• You might want to do this problem using mathematica or maple.
• Set up the L2 error of ex and the polynomial.
• Take the derivative of L2 with respect to the coefficients in the
polynomial expansion.
• You obtain a set of 6 equations in 6 unknowns, once you set the
derivatives to zero.
• Solve for the coefficients.
• You can use any form of a polynomial, but using Lagrange in-
terpolating polynomials are extremely convenient.
• Compare this norm to the norm of the polynomial obtained by
computing the Taylor series of degree 5 about 0.
(b) Construct the Lagrange interpolating polynomials for the following
functions and find a bound for the absolute error on the interval
[x0 , xn ]:

2
i. f (x) = e2x cos 3x where x0 = 0, x1 = 0.3, x2 = 0.6, where
n = 2.
(c) In class we discussed piece-wise linear interpolation and introduced
the “hat functions” Bi (x) (i = 1, 2, 3, . . . , n). These were claimed to
form a basis for S10 (∆), where ∆ ≡ a = x1 < x2 < x3 . . . < xn−1 <
xn = b. Show that these indeed form a basis. These hat functions
are easy to use and generalize to 2 and 3 dimensions. They are the
simplest ’finite’ element functions used in Finite Element Methods
(covered in MTH453 briefly), and widely used in graphics and image
reconstructions.
(d) The interpolation using Vandermonde matrices (see MTH451 and
the optional problem at the end of this homework), indicate that
using high order polynomials numerically is not a good idea: the
condition number of the resulting linear system can be unacceptably
high to offer reliable solutions. But another thing that’s evident with
these ’global’ (as opposed to finite) interpolation strategies is that
the error tends to focus in certain parts of the finite domain. (The
exception to this is for L2 periodic functions, where a (spectral)
Fourier interpolation is actually unbeatable). Another spectral ele-
ment family that is extremely useful, particularly when the functions
are not periodic, is the Chebyshev polynomials. They will concen-
trate notes at the edges of the domain, rescaled to x ∈ [−1, 1].
Definte the Chebyshev polynomial as

cos nθ = Tn (cos θ), Tn ∈ Pn (x), n = 0, 1, 2, ...

Show that the Chebyshev polynomials, defined by Tn (x) := cos(nθ),


over x ∈ [−1, 1], have the following properties:
i. Using the identity cos[(n + 1)θ] = 2 cos θ cos nθ − cos[(n − 1)θ],
show that

Tn+1 = 2xTn (x) − Tn−1 (x), n = 1, 2, 3, ...

with T0 (x) = 1 and T1 (x) = x.


ii. Show that

T2 (x) = 2x2 − 1, T3 (x) = 4x3 − 3x, T4 (x) = 8x4 − 8x2 + 1.

R 1over x ∈ [−1, 1] with


iii. They are orthogonal respect to the weight
√ 1 . That is, −1 Tn (x)Tm (x) 1−x2 dx =  π2 δn,m , where
√ 1
1−x2

3
δn,m is the Kroneker delta function, and  = 2 when m = n = 0
and 1 otherwise. (It’s ok if you just confirm this for the first
few polynomials).
iv. They satisfy the ordinary differential equation

(1 − x2 )y 00 − xy 0 + n2 y = 0

v. Show that

(1 − x2 )Tn0 (x) = n[Tn−1 (x) − xTn (x)]

and
2Tn (x)Tm (x) = Tn+m (x) + Tn−m (x)
for n ≥ m.
vi. There is a Mathematica CDF file that shows plots of the poly-
nomials on the Resource Page of the class. Plot a unit circle,
centered at 0. Project the location of equally spaced points on
this circle to the horizontal axis t, and convince yourself that
these lie at locations
 
(2k − 1)π
tk = − cos ,
2n
with k = 1, 2, . . . , n.. These are called the Chebyshev interpo-
lation points.

3. EXPERIMENTAL SECTION:
For the following exercises you can adapt the following code, which im-
plements “Neville’s Algorithm” for the generation of the Lagrange poly-
nomials.

%Lagrange Polynomial Algorithm by


%Neville Interpolation at a vector of %points.

clc;
clear;
format long
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

4
%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.

(a) For P and Q defined in the introduction evaluate T = P ∗ Q by


hand and compare with the result you get from MATLAB. Plot P ,
Q, T and the x-axis (e.g. using grid on) on the same graph. What
do you observe about the roots of the three polynomials?

5
(b) Use appropriate Lagrange interpolation polynomials of degree one,
two three and four to approximate each of the following:
i. f (8.4) if f (8) = 16.63553, f (8.1) = 17.61549, f (8.3) = 17.56492,
f (8.6) = 18.50515, f (8.7) = 18.82091.
ii. f (−1/3) if f (−1) = 0.10000000, f (−0.75) = −0.07181250,
f (−0.5) = −0.02475000, f (−0.25) = 0.33493750, f (0) =
1.10100000

(c) To approximate the function f (x) = x, we will use the points (1,1)
(4,2) and (9,3).
i. Write the formula for the Lagrange Polynomial P2 (x) that in-
terpolates these three points.
ii. Write a .m function file that implements P2 (x) using the MAT-
LAB polynomial commands.
iii. To see how well the approximation is:
• Plot f and P2 in the interval [0.01, 12].
• Plot f − P2 in the interval [0.01, 12].
• Plot abs((f − P2 )./f ) in the interval [0.01, 12].
iv. Using the plots determine where the approximation is better/worse.
v. Why do you get these results? Do they agree with the formula
derived in class for the error bound?
vi. Add the point (0,0) and repeat (a)–(c). Do you see any im-
provement? Deterioration? Where? Why?
(d) (Optional) In this exercise you will analyze the problems that arise
when the interpolation polynomial is evaluated using the Vander-
monde matrix. We will find the interpolation polynomial Pn for the
nice smooth function sin (x) and see that as n 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]’;

6
% 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:
i. Using the error formula derived in class, show that | sin(x) −
Pn (x)| → 0 as n → ∞ for x ∈ [0, 2].
ii. Does this agree with your plots?
iii. In practice, is larger n good or bad?
iv. What is the source of the problem with large n? (Hint: Run
the program again but use the grid points x rather than y in
the plot command. Also note the MATLAB error messages.)

You might also like