Let's use the insight we extracted when building piecewise linear
interpolants to construct piecewise cubic interpolants, or
cubic splines.
We're skipping to cubic interpolants because you'll develop
quadratic splines in HW5.
In this case, we require that the piecewise interpolant
$$ s(x)= s_i(x) = \color{var(--emphColor)}{a_i} + \color{var(--emphColor)}{b_i} (x-x_i) + \color{var(--emphColor)}{c_i} (x-x_i)^2 + \color{var(--emphColor)}{d_i}(x-x_i)^3$$
on each subinterval $[x_i,x_{i+1}]$, where
$\color{var(--emphColor)}{a_i}, \color{var(--emphColor)}{b_i}, \color{var(--emphColor)}{c_i}$, and $\color{var(--emphColor)}{d_i}$ are coefficients to be determined.
Cubic spline
$s(x)= s_i(x) = \color{var(--emphColor)}{a_i} + \color{var(--emphColor)}{b_i} (x-x_i) + \color{var(--emphColor)}{c_i} (x-x_i)^2 + \color{var(--emphColor)}{d_i}(x-x_i)^3$
As in the piecewise linear case, we require
-
(Interpolation) $s(x_i) = s_i(x_i) = f(x_i)$, for $i = 0, 1, \ldots, n$.
-
(Continuity) $s_i(x_\color{var(--emphColor)}{i+1}) = s_{i+1}(x_\color{var(--emphColor)}{i+1})$, for $i = 0, 1, \ldots, n-2$.
In this case there are $4n$ coefficients in total, so our best bet
is to specify $4n$ constraints.
Smoothness
The piecewise
linear interpolant can have sharp corners, making it
non-differeniable.
This is undesirable, so we require smoothness for the
cubic spline.
-
(Continuity of $s'$) $s_i'(x_\color{var(--emphColor)}{i+1}) = s_{i+1}'(x_\color{var(--emphColor)}{i+1})$, for $i = 0, 1, \ldots, n-2$.
-
(Continuity of $s''$) $s_i''(x_\color{var(--emphColor)}{i+1}) = s_{i+1}''(x_\color{var(--emphColor)}{i+1})$, for $i = 0, 1, \ldots, n-2$.
Insufficient requirements
The interpolation and coninuity constraints amount to
$$n+ 1 + 3(n-1) = 4n - 2$$
conditions, so we must specify two more conditions in order to
hope we have defined the cubic spline uniquely.
Before discussing these additional constraints, let's work with
what we've got!
Manipulation
We can manipulate the given constraints to obtain formulas for
$a_i$, formulas for $b_i$ and $d_i$ in terms of the $c_i$, and we can set up a
linear system to solve for the $c_i$.
Continuity of $s''$
Let $h_i = x_{i+1} - x_i$. Then continuity of $s''$ means
$$2c_ih_i +3 \cdot 2\color{var(--emphColor)}{d_i}h_i=s''_i(x_{i+1}) = s''_{i+1}(x_{i+1})=2 c_{i+1},$$
for $i=0,1, \ldots, n-2$.
We may solve for $\color{var(--emphColor)}{d_i}$ to obtain
$$\color{var(--emphColor)}{d_i} = \frac{c_{i+1} - c_i}{3h_i}.$$
Continuity of $s'$
Substituting $\color{var(--emphColor)}{d_i}$ into the constraints requiring continuity of $s'$,
we obtain
\begin{align}
b_{i+1} &= b_i + 2c_ih_i + (c_{i+1} - c_i) h_i \\
&= b_i+(c_{i+1} + c_i) h_i.
\end{align}
Continuity of $s$
Similarly, substituting $\color{var(--emphColor)}{d_i}$ into the
constraints requiring continuity of $s$, we obtain
\begin{align}
a_{i+1}&= a_i + \color{var(--emphColor)}{b_i} h_i + c_ih_i^2 + \frac{c_{i+1} - c_i}{3}h_i^2\\
&= a_i + \color{var(--emphColor)}{b_i} h_i + \frac{c_{i+1} +2c_i}{3}h_i^2.
\end{align}
We may solve the last equation for $\color{var(--emphColor)}{b_i}$
to conclude that
$$\color{var(--emphColor)}{b_i} = \frac{a_{i+1} - a_i}{h_i} -\frac{2c_i +c_{i+1}}{3}h_i.$$
Interpolation
The interpolation conditions imply
\begin{align}
f(x_i) &= s_i (x_i) \\
&= \color{var(--emphColor)}{a_i} + b_i(x_i-x_i) + c_i (x_i-x_i)^2+ d_i (x_i-x_i)^3\\
&= \color{var(--emphColor)}{a_i}
\end{align}
for $i=0, 1, \ldots, n$.
At this point, all the coefficients are either known or they are
given in terms of the $c_i$!
Return of the linear system
After
doing some more algebra
we find simple
linear relations
defining the $c_i$'s:
$$c_{i-1} + 4 c_i + c_{i+1} = \frac{3}{h^2} (a_{i+1} - 2a_i + a_{i-1}),$$
for $i = 1, \ldots, n-2$, assuming the nodes $x_i$ are
evenly spaced, so that $h_0 = h_1 = \cdots = h_{n-1} = h$.
Not-a-knot
We require continuity of $s'''$ at $x = x_1$, and at $x = x_{n-1}$.
Since $s'''_i(x) = 6d_i$, the
not-a-knot
boundary conditions require
$$d_0 = d_1, \quad \text{and} \quad d_{n-2} = d_{n-1}.$$
We may obtain equations for $c_i$ using $d_i = \frac{c_{i+1}-c_i}{3h_i}$.
We may then solve for $c_0$ and $c_n$ to preserve the
tridiagonal structure of the system.
Not-a-knot
When all is said and done, we obtain an $(n-1) \times (n-1)$
tridiagonal system defining the $c_i$:
\begin{align}
6c_1 \quad \quad \quad \quad &=\frac{3}{h^2}(a_2 -2a_1 +a_0)\\
c_{i-1} + 4 c_i +c_{i+1} &= \frac{3}{h^2} (a_{i+1} - 2a_i + a_{i-1}), \quad i=2, \ldots, n-2\\
6c_{n-1} &= \frac{3}{h^2} (a_n -2a_{n-1} +a_{n-2})
\end{align}
Clamped spline
Imagine placing a clamp at $x = a$ and at $x = b$. This would force
your piecewise polynomial interpolant to have the same initial
and final slope as $f$.
If you know $f'(a)$ and $f'(b)$, use them!
Clamped spline
In this case we set
$$s'(a) = f'(a), \quad s'(b)=f'(b).$$
Note that
$$f'(a) = s'_0(a) = b_0 \quad \text{and} \quad f'(b) = s'_{n-1}(b)= b_n,$$
so we can use
\begin{equation}
b_j = \frac{a_{j+1}-a_j}{h_j} - \frac{2c_j+c_{j+1}}{3}h_j
\label{eqn3star}
\end{equation}
with $j=0$ and $j = n-1$ to re-write the boundary
conditions in terms of the $c_j$.
I'll let you work out the rest of the algebra on this one!
Conclusion
To sum up, computing the coefficients defining a cubic spline is
a two-step process.
-
Use your chosen boundary conditions to solve
a tridiagonal
linear system describing the $c_i$.
Results
Evaluating your cubic spline is also a
two-step process.
-
Given $x \in [a, b]$, find $i$ such that
$x_i \leq x \leq x_{i+1}$.
-
Compute
$$s_i(x) = \color{var(--emphColor)}{a_i} + \color{var(--emphColor)}{b_i} (x-x_i) + \color{var(--emphColor)}{c_i} (x-x_i)^2 + \color{var(--emphColor)}{d_i}(x-x_i)^3.$$
Congratulations! You reached the end of this lecture.