Numerical Analysis: Part Four
Chapter 18: Splines and Piecewise
Interpolation
Prof. Sung Jin Yoo
School of Electrical and Electronics Engineering
Chung-Ang University
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation – Introduction to Splines
Introduction to Splines
• An alternative approach to using a single (n-1)th order polynomial to
interpolate between n points is to apply lower-order polynomials in a
piecewise fashion to subsets of data points.
• These connecting polynomials are called spline functions.
• Splines minimize oscillations and reduce round-off error due to their
lower-order nature.
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation – Introduction to Splines
Higher Order polynomial vs. Splines
• Splines eliminate oscillations by using small
subsets of points for each interval rather than
every point. This is especially useful when
there are jumps in the data:
a) 3rd order polynomial
b) 5th order polynomial
c) 7th order polynomial
d) Linear spline
• seven 1st order polynomials generated
by using pairs of points at a time
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation – Spline Development
Spline Development
• Spline function (si(x)) coefficients are calculated for each interval of a
data set.
• The number of data points (fi) used for each spline function depends on
the order of the spline function.
• Linear splines
si ( x) ai bi ( x xi )
fi 1 fi
where ai f i , bi .
xi 1 xi
f i 1 f i
si ( x) f i ( x xi )
xi 1 xi
Ex 18.1)
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation – Spline Development
Quadratic splines
Second-order splines find quadratic
equations between each pair of points
that
• Go through the points
• Match first derivatives at the
interior points
si ( x) ai bi ( x xi ) ci ( x xi ) 2 , i 1, , n 1
Ex 18.2)
Finding coefficients
1) x xi ai fi si ( x) fi bi ( x xi ) ci ( x xi ) 2
2) Equality of values of adjacent polynomials
si ( x) si 1 ( x) fi bi ( x xi ) ci ( x xi ) 2 fi 1 bi 1 ( x xi 1 ) ci 1 ( x xi 1 ) 2
x xi 1 fi bi hi ci hi2 fi 1 where hi xi 1 xi
3) Equality of the first derivatives at the interior nodes
si' ( x) si' 1 ( x) bi 2ci ( x xi ) bi 1 2ci 1 ( x xi 1 ) x xi 1 bi 2ci hi bi 1
4) Assume that the second derivative is zero at the first point. c1 0
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation – Spline Development
Cubic splines
Third-order splines find cubic equations
between each pair of points that
• Go through the points
• Match first and second derivatives
at the interior points
Note that the results of cubic spline
si ( x) ai bi ( x xi ) ci ( x xi ) di ( x xi ) , i 1, , n 1. interpolation are different from
2 3
Finding coefficients the results of an interpolating cubic.
1) x xi ai fi si ( x) fi bi ( x xi ) ci ( x xi ) 2 di ( x xi )3
2) Equality of values of adjacent polynomials : si ( x ) si 1 ( x ) at x xi 1
f i bi hi ci hi2 di hi3 f i 1 where hi xi 1 xi
3) Equality of the first derivatives at the interior nodes
si' ( x) bi 2ci ( x xi ) 3di ( x xi ) 2 si' xi 1 si' 1 xi 1 bi 2ci hi 3di hi2 bi 1
4) Equality of the second derivatives at the interior nodes
si'' ( x) 2ci 6di ( x xi ) si'' xi 1 si''1 xi 1 ci 3di hi ci 1
5) Assume that the second derivative is zero at the first point and last point.
c1 cn 0
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation – Spline Development
Cubic splines (cont.)
By some manipulations, ci 1 ci
di
3hi
fi 1 fi hi
bi (2ci ci 1 )
hi 3
hi 1ci 1 2(hi 1 hi )ci hi ci 1 3( f [ xi 1 , xi ] f [ xi , xi 1 ])
fi f j
where f [ xi , x j ]
xi x j
And, to compute the coefficient c’s,
1 c1 0
h 2(h1 h2 ) h2 c 3( f [ x , x ] f [ x , x ])
1 2 3 2 2 1
hn 2 2(hn 2 hn 1 ) hn 1 cn 1 3( f [ xn , xn 1 ] f [ xn 1 , xn 2 ])
1 cn 0
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation – Spline Development
Cubic splines (cont.)
Ex 18.3) Fit cubic splines to the date in Table 18.1. Utilize the results to
estimate the value at x=5.
Sol.)
So,
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation – Spline Development
End conditions
• There are several options for the final two equations:
– Natural end conditions - assume the second derivative at the end
knots are zero.
– Clamped end conditions - assume the first derivatives at the first and
last knots are known.
– “Not-a-knot” end conditions – to force continuity of the third derivative
at the second and next-to-last points (results in the first two intervals
having the same cubic spline function and the last two intervals
having the same cubic spline function)
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation
– Piecewise Interpolation in MATLAB
MATLAB Func.: spline
• MATLAB has several built-in functions to implement piecewise
interpolation. The first is spline:
yy=spline(x, y, xx)
where x and y : vectors containing the values that are to be interpolated,
yy : a vector containing results of the spline interpolation as
evaluated at the points in the vector xx.
• Spline command performs cubic spline interpolation.
• By default, spline uses the not-a-knot end condition.
• However, if y contains two more values than x has entries, then the first
and last value in y are used as the first derivatives at the end points (i.e.
clamped end condition)
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation
– Piecewise Interpolation in MATLAB
MATLAB Func.: spline (cont.)
Ex 18.4) Use MATLAB to fit equally spaced data sampled from this
function in the interval [-1,1]. 1
f ( x)
a) Employ a not-a-knot end condition 1 25 x 2
• Generate data:
x = linspace(-1, 1, 9);
y = 1./(1+25*x.^2);
• Calculate 100 model points and
determine not-a-knot interpolation
xx = linspace(-1, 1);
yy = spline(x, y, xx);
• Calculate actual function values
at model points and data points, the
9-point not-a-knot interpolation (solid),
and the actual function (dashed),
yr = 1./(1+25*xx.^2)
plot(x, y, ‘o’, xx, yy, ‘-’, xx, yr, ‘--’)
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation
– Piecewise Interpolation in MATLAB
MATLAB Func.: spline (cont.)
Ex 18.4)
b) Employ a clamped spline with end slopes of f1 1 and f n 1 4
' '
• Generate data with first derivative information:
x = linspace(-1, 1, 9);
y = 1./(1+25*x.^2);
yc = [1 y -4]
• Calculate 100 model points and
determine not-a-knot interpolation
xx = linspace(-1, 1);
yyc = spline(x, yc, xx);
• Calculate actual function values
at model points and data points, the
9-point clamped interpolation (solid),
and the actual function (dashed),
yr = 1./(1+25*xx.^2)
plot(x, y, ‘o’, xx, yyc, ‘-’, xx, yr, ‘--’)
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation
– Piecewise Interpolation in MATLAB
• MATLAB Func.: interp1
• While spline can only perform cubic splines, MATLAB’s interp1 function
can perform several different kinds of interpolation:
yi = interp1(x, y, xi, ‘method’)
– x & y contain the original data
– xi contains the points at which to interpolate
– ‘method’ is a string containing the desired method:
• ‘nearest’ - nearest neighbor interpolation
• ‘linear’ - connects the points with straight lines
• ‘spline’ - not-a-knot cubic spline interpolation
• ‘pchip’ or ‘cubic’ - piecewise cubic Hermite interpolation
pchip uses cubic polynomial, but it differs from cubic splines in
that the second derivatives are not necessarily continuous.
(i.e., Shape preserving, no overshoot the data points )
– If ‘method’ is omitted, the default is linear interpolation.
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation
– Piecewise Interpolation in MATLAB
• MATLAB Func.: interp1 (cont.)
Ex 18.5) Use MATLAB’s interp1 func. to fit these data with a) linear interp.
b) Nearest neighbor, c) cubic spline with not-a-knot end conditions, and
d) Piecewise cubic Hermite interp.
Sol.)
>> vn=interp1(t,v,tt, ‘nearnest’);
>> vs=interp1(t,v,tt, ‘spline’);
>> vh=interp1(t,v,tt, ‘pchip’);
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation
– Multidimensional Interpolation
Bilinear interpolation
• The interpolation methods for one-
dimensional problems can be
extended to multidimensional
interpolation.
• Example - bilinear interpolation
using Lagrange-form equations:
Hold the y value fixed.
x x x x
f xi , y1 i 2 f x1 , y1 i 1 f x2 , y1
x1 x2 x2 x1
xi x2 x x
f xi , y2 f x1 , y2 i 1 f x2 , y2
x1 x2 x2 x1
yi y2 y y
f xi , yi f xi , y1 i 1 f xi , y2
y1 y2 y2 y1
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation
– Multidimensional Interpolation
Bilinear interpolation (cont.)
xi x2 yi y2
f xi , yi f x1 , y1
x1 x2 y1 y2
xi x1 yi y2
f x2 , y1
x2 x1 y1 y2
xi x2 yi y1
f x1 , y2
x1 x2 y2 y1
xi x1 yi y1
f x2 , y2
x2 x1 y2 y1
Ex 18.6) Use bilinear interpolation to estimate the temperature at xi 5.25, yi 4.8
Sol.)
Numerical analysis Chapter 18
18. Splines and Piecewise Interpolation
– Multidimensional Interpolation in MATLAB
Multidimensional Interpolation in MATLAB
• MATLAB has built-in functions for two- and three-dimensional piecewise
interpolation:
zi = interp2(x, y, z, xi, yi, ‘method’)
vi = interp3(x, y, z, v, xi, yi, zi, ‘method’)
• ‘method’ is again a string containing the desired method: ‘nearest’,
‘linear’, ‘spline’, ‘pchip’, or ‘cubic’
• For 2-D interpolation, the inputs must either be vectors or same-size
matrices.
• For 3-D interpolation, the inputs must either be vectors or same-size 3-D
arrays.
The same evaluation as in Ex. 18.6
Numerical analysis Chapter 18