## Lecture 2.3:

Linear and cubic splines

Higher-order polynomial interpolation is rarely used for practical purposes
because of a polynomial wiggle, i.e. high swings of the interpolating polynomials
between the data points. Instead, most of computational algorithms use the idea of
splines, i.e. piecewise interpolation. The MATLAB functions
**interp1(x,y,xi,'linear')** and **interp1(x,y,xi,'spline')**
are also based on piecewise linear and cubic interpolation.

### Linear splines

The linear spline represents a set of line segments between the two adjacent
data points (*V*_{k},I_{k}) and
(*V*_{k+1},I_{k+1}). The equations for each line segment
can be immediately found in a simple form:

I_{k}(V) = I_{k} + ( I_{k+1} - I_{k})
( V - V_{k} ) / (V_{k+1} - V_{k}),

where *V = [V*_{k},V_{k+1}] and *k = 0,1,...,(n-1)*.
When the linear spline is applied to the voltage-current data for a zener diode,
the interpolating algorithm produces the following picture:

The resulting curve is clearly not smooth, with sharp corners at the data points.
However, the piecewise interpolation represents the "exact" curve
*I = I(V)* better that the Lagrange or Newton polynomial interpolation.
The linear spline is constructed for each interval *V =
[V*_{k},V_{k+1}] independently and it matches the data values
*(V*_{k},I_{k}) for *k=0,1,...,n*.

### Cubic splines

We can improve the appearance of the piecewise interpolation by matching
not only the data values but also the slopes and the concavities of each individual
interpolating segment at the interior data points. For *(n+1)* data values
(*V*_{k},I_{k}) we would have *n* conditions to match the
values *I(V*_{k}), *(n-1)* conditions to match the slopes
*I'(V*_{k}) and *(n-1)* conditions to match the concavities
*I''(V*_{k}), i.e. *3n - 2 * conditions (equations).
Cubic splines would be good candidates for such interpolation:

I_{k}(V) = I_{k} + a_{k} ( V - V_{k} )
+ b_{k} ( V - V_{k} )^{2} +
c_{k} ( V - V_{k} )^{3},

where *V = [V*_{k},V_{k+1}] and *k = 0,1,...,(n-1)*.
Indeed, the cubic splines have *3n* unknown coefficients (more than required).
Two more conditions should be added separately at the ends of the interpolating
interval, i.e. at the points *V = V*_{0} and *V = V*_{n}.
These **end-point** or **boundary** conditions are set due to additional
assumptions on appearance of the interpolating curve.
The simplest conditions follow from the assumption that the concavity
is zero at the ends. This is a definition of a **natural cubic spline**.

A suitable computational algorithm to define the coefficients of cubic splines
solves a WELL-posed linear system for concavities *I''(V*_{k}) = m_{k}
subject to the boundary conditions: *m*_{0} = m _{n} = 0
(for natural splines). Provided the solution for *m*_{k} is found
for *k = 1,2,...,(n-1)*, the coefficients of
the cubic splines are

a_{k} = ( I_{k+1} - I_{k} ) / (V_{k+1} - V_{k} )
- (2 m_{k} + m_{k+1}) ( V_{k+1} - V_{k} ) / 6

b_{k} = 0.5 m_{k},
c_{k} = 1/6 ( m_{k+1} - m_{k} ) / (V_{k+1}-V_{k})

Note that the cubic spline reduces to the linear spline if concavities are
neglected, i.e. all *m*_{k} are zeros.
When the cubic spline is applied to the voltage-current data for a zener diode,
the interpolating algorithm produces the following picture acceptable both
for further work and visualization:

The error of the cubic spline interpolation is *O(h*^{4}),
where *h* is the maximum spacing between the data points.