Title: Curve Fitting: Splines
1Chapter 16
2Spline Interpolation
- There are cases where polynomial interpolation is
bad - Noisy data
- Sharp Corners (slope discontinuity)
- Humped or Flat data
- Overshoot
- Oscillations
3Example f(x) sqrt(abs(x))
Interpolation at ?4, ?3, ?2, ?1, 0, 1, 2, 3, 4
4Spline Interpolation
Cubic interpolation
7th-order
5th-order
Linear spline
5Spline Interpolation
- Idea behind splines
- Use lower order polynomials to connect subsets
of data points - Make connections between adjacent splines smooth
- Lower order polynomials avoid oscillations and
overshoots
6Spline Interpolation
- Use piecewise polynomials instead of a single
polynomial - Spline -- a thin, flexible metal or wooden lath
- Bent the lath around pegs at the required points
- Spline curves -- curves of minimum strain energy
- Piecewise Linear Interpolation
- Piecewise Quadratic Interpolation
- Piecewise Cubic Interpolation (cubic splines)
7Drafting Spline
- Continuous function and derivatives
Zero curvatures at end points
8Splines
- There are n-1 intervals and n data points
- si(x) is a piecewise low-order polynomial
9Spline fits of a set of 4 points
10(No Transcript)
11Example Ship Lines
waterline
12Bow
Stern
13Original database
Spline interpolation for submerged hull (below
waterline)
14Linear Splines
- Connect each two points with straight line
functions connecting each pair of points
b is the slope between points
15Linear Splines
si (x) ai bi (x ? xi)
x1 x2
x3 x4
16Linear Splines
Identical to Lagrange interpolating polynomials
17Linear splines
- Connect each two points with straight line
- Functions connecting each pair of points are
- slope
18Linear splines are exactly the same as linear
interpolation! Example
19Linear Splines
- Problem with linear splines -- not smooth at data
points (or knots) - First derivative (slope) is not continuous
- Use higher-order splines to get continuous
derivatives - Equate derivatives at neighboring splines
- Continuous functional values and derivatives
20Quadratic Splines
Quadratic splines - continuous first
derivatives May have discontinuous second and
higher derivatives Derive second order polynomial
between each pair of points For n points
(i1,,n) (n-1) intervals 3(n-1) unknown
parameters (as, bs, and cs) Need 3(n-1)
equations
21Quadratic Splines
f(x5)
s2 (x)
f(x2)
s4 (x)
f(x3)
s1 (x)
f(x4)
s3 (x)
f(x1)
Interval 1 Interval 2 Interval 3
Interval 4
x1 x2
x3 x4 x5
3(n-1) unknowns
si(x) ai bi (x?xi) ci(x?xi)2
22Piecewise Quadratic Splines
Example with n 5
- The functional must pass through all the points
xi (continuity condition) - The function values of adjacent polynomials must
be equal at all nodes (identical to condition 1.)
2(n-2) 2 2(n-1) - The 1st derivatives are continuous at interior
nodes xi, (n-2) - Assume that the second derivatives is zero at the
first points
3(n?1) equations for 3(n?1) unknowns
2(n?1) (n?2) (1) 3(n?1)
23Piecewise Quadratic Splines
- Function must pass through all the points x xi
(2n ? 2 eqns) - This also ensure the same function values of
adjacent polynomials at the interior knots (hi
xi1 xi) - Apply to every interval (4 intervals, 8
equations)
24Piecewise Quadratic Splines
- Continuous slope at interior knots x xi (n?2
equations) - Apply to interior knots x2 , x3 and x4 (3
equations) - Zero curvatures at x x1 (1 equation)
25Example Quadratic Spline
26Quadratic Spline Interpolation
function A, b quadratic(x, f) exact
solution f(x)x3-5x23x4 x-1 0 2 5 6
f-5 4 -2 19 58 h1x(2)-x(1) h2x(3)-x(2)
h3x(4)-x(3) h4x(5)-x(4) f1f(1) f2f(2)
f3f(3) f4f(4) f5f(5) A1 0 0 0
0 0 0 0 0 0 0 0 0 h1
h12 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 h2 h22 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 h3 h32 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
h4 h42 0 1 2h1 0 -1 0 0 0
0 0 0 0 0 0 0 0 1
2h2 0 -1 0 0 0 0 0 0 0
0 0 0 0 1 2h3 0 -1 0 0
0 1 0 0 0 0 0 0 0 0
0 bf1 f2-f1 f2 f3-f2 f3 f4-f3 f4
f5-f4 0 0 0 0
27Quadratic Spline Interpolation
gtgt x-1 0 2 5 6 f-5 4 -2 19 58 gtgt
A,bquadratic(x,f) p A\b -5.0000
9.0000 0 4.0000 9.0000
-6.0000 -2.0000 -15.0000 7.3333
19.0000 29.0000 10.0000 dx 0.1
z1x(1)dxx(2) s1p(1)p(2)(z1-x(1))p(3)(z1-
x(1)).2 z2x(2)dxx(3) s2p(4)p(5)(z2-x(2))
p(6)(z2-x(2)).2 z3x(3)dxx(4)
s3p(7)p(8)(z3-x(3))p(9)(z3-x(3)).2 z4x(4)
dxx(5) s4p(10)p(11)(z4-x(4))p(12)(z4-x(4)).
2 Hplot(z1,s1,'r',z2,s2,'b',z3,s3,'m',z4,s4,'g'
) xx-10.16 fexactxx.3-5xx.23xx4 hold
on Gplot(xx,fexact,'c',x,f,'ro') set(H,'LineWid
th',3) set(G,'LineWidth',3,'MarkerSize',8) H1xl
abel('x') set(H1,'FontSize',15) H2ylabel('f(x)'
) set(H2,'FontSize',15) H3title('f(x)x3-5x2
3x4') set(H3,'FontSize',15)
28Quadratic Spline
s4(x)
Exact function
s2(x)
s1(x)
s3(x)
29Cubic Splines
Cubic splines avoid the straight line and the
over-swing
- Can develop method like we did for quadratic
4(n1) unknowns 4(n1) equations - interior knot equality
- end point fixed
- interior knot first derivative equality
- assume derivative value if needed
30Piecewise Cubic Splines
s2 (x)
sn-1 (x)
s1 (x)
Continuous slopes and curvatures
x1
x2
x3
xn
xn-1
4(n?1) unknowns
31Piecewise Cubic Splines
s2(x)
Sn-1(x)
s1(x)
s3(x)
x0
x1
x2
xn
xn-1
si (x) - piecewise cubic polynomials si(x) -
piecewise quadratic polynomials (slope) si(x) -
piecewise linear polynomials (curvatures)
Reduce to (n-1) unknowns and (n-1) equations for
si
32Cubic Splines
- Piecewise cubic polynomial with continuous
derivatives up to order 2 - The function must pass through all the data
points - gives 2(n-1) equations
i 1,2,, n-1
33Cubic Splines
- 2. First derivatives at the interior nodes must
be equal -
(n-2) equations - 3. Second derivatives at the interior nodes must
be equal -
(n-2) equations
34Cubic Splines
- 4. Two additional conditions are needed
(arbitrary) - The last two equations
- Total equations 2(n-1) (n-2) (n-2) 2
4(n-1)
35Cubic Splines
- Solve for (ai, bi, ci, di) see textbook for
details - Tridiagonal system with boundary conditions c1
cn 0
36Cubic Splines
Tridiagonal matrix
37Hand Calculations
38Hand Calculations
- can be further simplified since c1 c4 0
(natural spline)
39Cubic Spline Interpolation
40Cubic Splines
- Piecewise cubic splines (cubic polynomials)
41Cubic Spline Interpolation
- The exact solution is a cubic function
- Why cubic spline interpolation does not give the
exact solution for a cubic polynomial? - Because the conditions on the end knots are
different! - In general, f? (x0) ? 0 and f? (xn) ? 0 !!
42Cubic Spline Interpolation
43gtgt xx-1 0 2 5 6 f -5 4 -2 19 58 gtgt
Cubic_spline(xx,f) Resulting piecewise
function s1 (-5.000000)(11.081218)(x-(-1.0000
00)) s2 (0.000000)(x-(-1.000000)).2(-2.081218
)(x-(-1.000000)).3 Resulting piecewise
function s1 (4.000000)(4.837563)(x-(0.000000)
) s2 (-6.243655)(x-(0.000000)).2(1.162437)(x
-(0.000000)).3 Resulting piecewise function s1
(-2.000000)(-6.187817)(x-(2.000000)) s2
(0.730964)(x-(2.000000)).2(1.221658)(x-(2.00
0000)).3 Resulting piecewise function s1
(19.000000)(31.182741)(x-(5.000000)) s2
(11.725888)(x-(5.000000)).2(-3.908629)(x-(5.
000000)).3
(i 1)
(i 2)
(i 3)
(i 4)
44Cubic Spline Interpolation
Piecewise cubic Continuous slope Continuous
curvature zero curvatures at end knots
exact solution
45End Conditions of Splines
- Natural spline zero second derivative
(curvature) at end points - Clamped spline prescribed first derivative
(clamped) at end points - Not-a-Knot spline continuous third derivative at
x2 and xn-1
46gtgt x linspace(-1,1,9) y 1./(125x.2)gtgt
xx linspace(-1,1,100) yy spline(x,y,xx)gtgt
yr1./(125xx.2)gtgt Hplot(x,y,'o',xx,yy,xx,yr,
'--')
Continuous third derivatives at x2 and xn-1
- Comparison of Runges function (dashed red line)
with a 9-point not-a-knot spline fit generated
with MATLAB (solid green line)
47gtgt yc 1 y 4 1 and -4 are the 1st-order
derivatives (or slopes)at 1st last point,
respectively.gtgt yyc spline(x,yc,xx)gtgt gtgt
Hplot(x,y,'o',xx,yyc,xx,yr,'--')
Clamped End Spline - Use 11 values including
slopes at end points
- Note that first derivatives of 1 and ?4 are
specified at the left and right boundaries,
respectively.
48MATLAB Functions
- One-dimensional interpolations
- yy spline (x, y, xx)
- yi interp1 (x, y, xi, method)
- yi interp1 (x, y, xi, linear)
- yi interp1 (x, y, xi, spline) not-a-knot
spline - yi interp1 (x, y,, xi, pchip) cubic Hermite
- yi interp1 (x, y,, xi, cubic) cubic Hermite
- yi interp1 (x, y,, xi, nearest) nearest
neighbor
49MATLAB Function interp1
- Piecewise polynomial interpolation on velocity
time series for an automobile
50Spline Interpolations
gtgt x1 2 4 7 9 10 x 1 2 4 7
9 10 gtgt y3 -2 5 4 9 2 y 3 -2
5 4 9 2 gtgt xi10.110 gtgt
y1interp1(x,y,xi,'linear') gtgt
y2interp1(x,y,xi,'spline') gtgt
y3interp1(x,y,xi,'cubic') gtgt plot(x,y,'ro',xi,y1
,xi,y2,xi,y3,'LineWidth',2,'MarkerSize',12) gtgt
print -djpeg spline00.jpg
51Spline Interpolations
Linear Not-a-knot Cubic
52MATLABs Functions
- Two-dimensional interpolations
- zi interp2 (x, y, z, xi, yi)
53MATLAB Function interp2
gtgt x,y,zpeaks(100) xi,yimeshgrid(-30.13,-
30.13) gtgt zi interp2(x,y,z,xi,yi)
surf(xi,yi,zi) gtgt print -djpeg075 peaks1.jpg gtgt
x,y,zpeaks(10) xi,yimeshgrid(-30.13,-30.
13) gtgt zi interp2(x,y,z,xi,yi)
surf(xi,yi,zi) gtgt print -djpeg075 peaks2.jpg
10 ? 10 data base
100 ?100 data base
54CVEN 302-501Homework No. 11
- Chapter 16
- Problem 16.1(a) (30) Hand Calculation
- Problem 16.1(b) (20) MATLAB program not-a-knot
spline yy spline(x, y, xx) - Problem 16.1(c)(20) MATLAB program
- Chapter 19
- Problem 19.2 (20) Hand Calculation
- Problem 19.4 (20) Hand Calculation
- Due on Wed. 11/05/08 at the beginning of the
period