\magnification=\magstep1
\centerline{Mathematics 371 -- Numerical Analysis}
\centerline{Lab Day 3 -- Using Cubic Spline Interpolation}
\centerline{\it October 19, 2001}
\bigskip
\noindent
{\it Goals and Background}
\bigskip
The goals of today's lab are to use the Maple {\tt Spline}
procedure for {\it free cubic splines} and another ``homegrown'' Maple
procedure for {\it clamped cubic splines}.  We will use
them to construct cubic spline interpolating functions.
Recall that on Wednesday we saw that given $x_0, \ldots, x_n$ and 
function values $f(x_0), \ldots, f(x_n)$ to be interpolated,  
the coefficients $a_j,b_j,c_j,d_j$
in the free cubic spline interpolating function 
$$S(x) = 
\cases{S_0(x) = a_0 + b_0(x-x_0) + c_0(x-x_0)^2 + d_0(x-x_0)^3,\ x_0\le x\le x_1&\cr
S_1(x) = a_1 + b_1(x-x_1) + c_1(x-x_1)^2 + d_1(x-x_1)^3,\ x_1\le x\le x_2&\cr
\qquad \vdots & \cr
S_{n-1}(x) = a_{n-1} + b_{n-1}(x-x_{n-1}) + c_{n-1}(x-x_{n-1})^2 + 
d_{n-1}(x-x_{n-1})^3,&\cr
\qquad \qquad \qquad x_{n-1}\le x\le x_n&\cr}$$
satisfy $a_j = f(x_j)$ for $0 \le j \le n - 1$ and
$$\eqalign{b_j &= {a_{j+1} - a_j\over h_j} - {h_j\over 3}(2c_j + c_{j+1})\cr
           d_j &= {c_{j+1} - c_j\over 3 h_j}\cr}$$
for all $j = 0,\ldots, n-1$.
The $c_j$ are found by solving the $(n-1)\times (n-1)$ 
system of linear equations:
$$\eqalign{
&\pmatrix{
2(h_0+h_1) & h_1 & 0 &\cdots &0&0\cr
h_1 & 2(h_1+h_2) & h_2  & \cdots &0 &0\cr
\vdots&  &  && \vdots & \vdots \cr
0 & \cdots & 0 & 0&h_{n-2} & 2(h_{n-2} + h_{n-1}) \cr}\cdot 
\pmatrix{c_1 \cr \vdots \cr c_{n-1} \cr}\cr
& = 
\pmatrix{ {3(a_2-a_1)\over h_1} - {3(a_1-a_0)\over h_0}\cr
\vdots\cr
{3(a_n-a_{n-1})\over h_{n-1}} - {3(a_{n-2}-a_{n-1})\over h_{n-1}} }\cr}
$$
where $h_j = x_{j+1} - x_j$ and $c_0 = c_n = 0$.  There
is a very similiar system of linear equations whose solutions
give the $c_j$ coefficients for clamped cubic splines.
\bigskip
\noindent
{\it A New Maple Command}
\bigskip
As we saw in the handout from Monday's class, Maple has a spline 
interpolation function that can be used to construct free cubic spline 
interpolating functions.  It is invoked by first entering the command:
\medskip
\centerline{\tt with(CurveFitting);}
\medskip
\noindent
Then the format of the spline command is similar to the 
{\tt PolynomialInterpolation} command
from the last problem set.  
\medskip
\centerline{\tt Spline(xlist,ylist,var);}
\medskip
\noindent
where {\tt xlist} is the list of $x$-coordinates of the interpolation points
(that is, the $x_j$),
and {\tt ylist} is the list of $y$-coordinates $f(x_j)$.  These are 
Maple lists so, they should be lists of floating point numbers, 
separated by commas in {\it square brackets}: [ , ].
The output from the spline command is the piecewise cubic definition of
the {\it natural cubic spline\/} function $S(x)$ -- in Maple terms it is an 
{\it expression, not a function\/}, though(!) You make it into a function, 
assign it to a symbolic name, 
plot it, or whatever you like.
\bigskip
\noindent
{\it Laboratory Questions}
\bigskip
\item{A)}  In class on Wednesday, we worked out an example where 
$x_0 = 0, x_1 = 1, x_2 = 3, x_3 = 4$ and $f(x_0) = a_0 = 5, f(x_1) = a_1 = 4, 
f(x_2) = a_2 = 0, f(x_3) = a_3 = 1$.  We found that the cubic spline was
given by 
$$\cases{
S_0(x) = 5 - {5\over 8}x - {3\over 8} x^3, & $0 \le x \le 1$\cr
&\cr
S_1(x) = 4 - {7\over 4}(x-1) - {9\over 8}(x-1)^2 + {1\over 2} (x-1)^3, & $1\le x \le 3$\cr
&\cr
S_2(x) = -{1\over 4}(x-3) + {15\over 8}(x-3)^2 - {5\over 8} (x-3)^3, & $3\le x \le 4$\cr}$$
Use the {\tt Spline} command to check this work, plot $S(x)$ and the points 
$$(0,5), (1,4), (3, 0), (4,1)$$
together.  See the Maple online help page for the {\tt plot} command for some
ideas about plotting sets of points 
(in particular, the option {\tt style = point}).  You will probably want
to make the combined plot of $S(x)$ and the points by using the {\tt display}
command from the {\tt plots} package:  Generate the two plots separately and
assign them to symbolic names (this is one case where you probably 
{\it don't} want to see the output, so use a colon at the end of the 
plot commands), then use {\tt display} combine them.
\bigskip
\item{B)}  Use Maple's {\tt Spline} command to do problems 3d and 4d in section 3.4
of Burden and Faires.  Also, plot the spline together with $f(x) = x\cos x - 2 x^2  + 3 x - 1$,
and discuss the goodness of the ``fit'' between the spline function and $f(x)$.
\bigskip
\item{C)}  Use Maple's {\tt Spline} command to do problem 27 in 
section 3.4 of Burden and Faires.  Note that this is the same collection
of data points as you used in problem 25 from section 3.1 on the last
problem set, but there you used interpolating polynomials.  Compare
and contrast your results using interpolating polynomials and 
interpolating splines.
\bigskip
\item{D)}  Maple does not have a built-in procedure or a library
package for computing {\it clamped\/} cubic
splines.  However, you can get the source code for one on the course homepage.
Do this and read the code into your Maple session.  The format for using the 
procedure is similar to the one for {\tt spline}, except you also need to 
supply the values for the derivative of the spline function at the endpoints 
({\tt df0, dfn} are $f'(x_0)$ and $f'(x_n)$ respectively):
\medskip
\centerline{\tt clampedspline(xlist,ylist,df0,dfn,var);}
\medskip
\item{}
Use this procedure to solve problem 28 in section 3.4 of Burden and Faires.
\bigskip
\noindent
{\it Assignment}
\bigskip
Individual lab write-ups due in class Friday, October 26.
\bye
\bye