Starting from:

$29

COMPUTER SCIENCE ASSIGNMENT #5- INTERPOLATION


1
1. Consider the function f(x) = sin2
(x) in the interval [0, 2π]. You are given the following
4 points of this function:
xi f(xi)
0 0

3
0.75

3
0.75
2π 0
(a) (4 points) Calculate the cubic Lagrange interpolating polynomial as the sum
of the L0(x)f(x0), L1(x)f(x1), L2(x)f(x2), L3(x)f(x3) polynomials we defined in
class. The final answer should be in the form P(x) = ax3 +bx2 +cx+d, but with
a, b, c, d known. [Note: if any of the coefficients are 0 you do not need to include
the term in the final polynomial.]
DELIVERABLES: All your work in constructing the polynomial. This is to be
done by hand not MATLAB.
(b) (2 points) Plot f(x) = sin2
(x) in the interval [0, 2π] by creating vectors
x = [0:0.1:2*pi];
y =sin(x).^2;
On the same graph plot the interpolating polynomial P(x) from part (a).
DELIVERABLES: The commands and the resulting plot from MATLAB.
2. (8 points) Consider the piecewise cubic polynomial
S(x) =



S0(x), if 0 ≤ x ≤

3
S1(x), if 2π
3 ≤ x ≤

3
S2(x), if 4π
3 ≤ x ≤ 2π
where
S0(x) = a0 + b0(x − x0) + c0(x − x0)
2 + d0(x − x0)
3
S1(x) = a1 + b1(x − x1) + c1(x − x1)
2 + d1(x − x1)
3
S2(x) = a2 + b2(x − x2) + c2(x − x2)
2 + d2(x − x2)
3
Using the data points given in question 1., specify all twelve conditions that S(x) must
satisfy in order for it to be a clamped, cubic spline interpolant for f(x) = sin2
(x). Do
not simplify the equations, just move all terms with unknown coefficients to the left of
the equation and the known constants to the right. Also, do not evaluate the π’s, leave
them as π in your equations. Set up the augmented matrix that needs to be solved to
get the spline. DO NOT SOLVE THE SYSTEM.
DELIVERABLES: Show all your work ending with the 12 unsolved equations in the
augmented matrix form.
2
3. Cubic spline interpolating functions can be computed in MATLAB. Many types of
boundary conditions are possible, including the ’clamped’ boundary conditions and the
’second’ boundary conditions. We will consider only the clamped boundary conditions.
For the cubic spline with clamped boundary conditions, the data to be interpolated
should be stored in vectors, say X (the xi
’s) and Y (the f(xi)’s), where Y has 2 more
entries than X, the first and last entries of Y are the two clamped boundary conditions
(f
0
(x0) and f
0
(xn)), respectively. If S(x) denotes the cubic spline interpolant, and z is
a given number, then the value of S(z) can be computed by entering
spline(X, Y, z)
Note that z can also be a vector of values at which you want to evaluate the spline (as
in part (b) below).
If you want to actually determine the coefficients of the spline, you first must determine
the pp (piecewise polynomial) form of the spline by entering
pp = spline(X, Y)
Some information (which you can ignore) about the pp form of the spline is given.
Then enter
[b, c] = unmkpp( pp )
The values returned are:
b – a vector of the knots (or nodes) of the spline,
c – an array, the i-th row of which contains the coefficients of the i-th spline
Note: if the entries of X are denoted by {x0, x1, . . . , xn} and the entries in the first
row of c are c(1, 1), c(1, 2), c(1, 3), c(1, 4), then the first cubic polynomial of the spline
is
(∗) S0(x) = c(1, 1)(x − x0)
3 + c(1, 2)(x − x0)
2 + c(1, 3)(x − x0) + c(1, 4),
and similarly for the other cubic polynomials S1(x), . . . , Sn−1(x).
(a) (3 points) Use MATLAB to determine the coefficients of the 3 cubic polynomials of the cubic spline interpolant with clamped boundary conditions for the data
points given in questions 1. and 2.:
Use format short to display your output.
DELIVERABLES: The commands and the results from MATLAB plus the
final piecewise polynomial with coefficients.
3
(b) (3 points) In its simplest form, PLOT(X,Y,’-’) plots a vector of values Y versus
a vector of values X using a solid line. If ’-’ is replaced by ’:’ then the graph is
drawn with a dotted line. See help plot for other options. The graph is drawn
by connecting the points in X by a straight line, so if the points in X are quite
dense, then the graph will appear as a smooth continuous curve. For example,
the statements
X=linspace(0,pi,100);
Y=sin(X);
plot(X,Y,’-’)
will open a graphics window with the graph of sin x plotted on the interval [0, π]
using 100 equally spaced data points. The graph of cos x could be drawn with
a dotted line on the same graph as sin x on [0, π], by executing the following
statements:
X1=linspace(0,pi,100);
Y1=sin(X1);
Y2=cos(X1);
plot(X1,Y1,’-’,X1,Y2,’:’)
The functions sin x and cos x could be drawn on the same graph but on different
intervals by specifying a different set of X values for each.
Use the MATLAB function plot to draw a graph of the spline constructed in (a)
on the interval [0, 2π] along with the function f(x) = sin2
(x). So, f(x) and the 3
cubic polynomials should be drawn on one graph in the same graphics window.
The first cubic polynomial should be drawn with a dotted line on [0,

3
], the
second with a solid line on [ 2π
3
,

3
], and the third with a dotted line on [ 4π
3
, 2π].
As x0 = 0 in this problem, the cubic polynomial in (*) above could be plotted on
[0,

3
] with the following statements:
X1=linspace(0, 2*pi/3,50);
Y1=c(1,1)*(X1-0).^3+c(1,2)*(X1-0).^2+c(1,3)*(X1-0)+c(1,4);
plot(X1,Y1,’-’)
NOTE that ^ must be replaced by the MATLAB operator .^ for the cubed and
squared powers in this expression because the argument X1 is a vector (rather
than a scalar). This means that the exponentiation is done component wise to
the entries in the vector X1; for example, if X = [1 2 3 4], then X.^2 is equal
to [1 4 9 16], whereas X^2 is undefined.
DELIVERABLES: The statements you use to create the data to plot the original function and the spline function along with the plot itself.
4

More products