Professional Documents
Culture Documents
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,’*’)
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
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
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.
clc;
clear;
format long
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4
%Input
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Neville Method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.)