chapter 16 curve fitting: splines. spline interpolation zthere are cases where polynomial...
TRANSCRIPT
Chapter 16Chapter 16
Curve Fitting:Curve Fitting:SplinesSplines
Spline InterpolationSpline Interpolation
There are cases where polynomial interpolation is bad
Noisy dataSharp Corners (slope discontinuity)Humped or Flat data
Overshoot
Oscillations
ExampleExample: : ff((xx) = ) = sqrtsqrt((absabs((xx))))
Interpolation at 4, 3, 2, 1, 0, 1, 2, 3, 4
Cubic interpolation
5th-order
7th-order
Linear spline
Spline InterpolationSpline Interpolation
Spline InterpolationSpline 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
Spline InterpolationSpline Interpolation
Use “piecewise” polynomials instead of a single polynomial
Spline -- a thin, flexible metal or wooden lathBent the lath around pegs at the required pointsSpline curves -- curves of minimum strain energyPiecewise Linear InterpolationPiecewise Quadratic InterpolationPiecewise Cubic Interpolation (cubic splines)
Drafting SplineDrafting SplineContinuous function and derivatives
Zero curvatures at end points
SplinesSplines
There are n-1 intervals and n data points
si(x) is a piecewise low-order polynomial
Spline fits of a set of 4 points
Example: Ship LinesExample: Ship Lines
waterline
Bow
Stern
Original databaseOriginal database
Spline interpolation Spline interpolation for submerged hull for submerged hull (below waterline)(below waterline)
1n1n1n1n
iiii
2222
1111
xxbaxs
xxbaxs
xxbaxs
xxbaxs
b is the slope between points
i1i
i1i
i1i
i1ii
ii
xx
ff
xx
xfxfb
fa
Linear SplinesLinear Splines Connect each two points with straight line
functions connecting each pair of points
Linear SplinesLinear Splines
n1n1n322211
nn,33,22,11,
xxI xxI xxI interval
y(xy(xy(xy(x points data
,,,,,,:
),),),),:
x1 x2 x3 x4
si (x) = ai + bi (x xi)
Linear SplinesLinear Splines
n1-nn
1nn
1n1n
n1n
n
32323
22
32
3
21212
11
21
2
i
xxx , xf xx
xxxf
xx
xx
xxx ,xf xx
xxxf
xx
xx
xxx , xf xx
xxxf
xx
xx
xs
)()(
)()(
)()(
)(
Identical to Lagrange interpolating polynomials
n1n1n322211
nn,33,22,11,
xxI xxI xxI interval
y(xy(xy(xy(x points data
,,,,,,:
),),),),:
Connect each two points with straight line Functions connecting each pair of points are
slope i1i
i1ii xx
ffb
Linear splinesLinear splines
1 1 1 1 1 2
2 2 2 2 2 3
1
1 1 1 1 1
;
;
;
;
i i i i i i
n n n n n n
s x a b x x x x x
s x a b x x x x x
s x a b x x x x x
s x a b x x x x x
Linear splines are exactly the same as linear interpolation!
Example:
586
39
195
7
22
3
40
bxfx i
Linear SplinesLinear Splines
Problem with linear splines -- not smooth at data points (or knots)
First derivative (slope) is not continuousUse higher-order splines to get continuous
derivativesEquate derivatives at neighboring splinesContinuous functional values and derivatives
Quadratic splines - continuous first derivatives
May have discontinuous second and higher derivatives
Derive second order polynomial between each pair of points
For n points (i=1,…,n): (n-1) intervals & 3(n-1) unknown parameters (a’s, b’s, and c’s)
Need 3(n-1) equations
ii2
ii cxbxaxf
Quadratic SplinesQuadratic Splines
Quadratic SplinesQuadratic Splines
x1 x2 x3 x4 x5
s 1 (x)
s2 (x)s 4
(x)
si(x) = ai + bi (xxi) + ci(xxi)2 3(n-1)3(n-1) unknownsunknowns
Interval 1 Interval 2 Interval 3 Interval 4
f(x1)
f(x2) f(x3)f(x4)
f(x5)
s3 (x)
n1n1n322211
nn,33,22,11,
xxI xxI xxI interval
y(xy(xy(xy(x points data
,,,,,,:
),),),),:
Piecewise Quadratic SplinesPiecewise Quadratic Splines
244444454
233333343
222222232
211111121
xxcxxbaxs xx on
xxcxxbaxs xx on
xxcxxbaxs xx on
xxcxxbaxs xx on
)()()(:,
)()()(:,
)()()(:,
)()()(:,
1. The functional must pass through all the points xi (continuity condition)
2. The function values of adjacent polynomials must be equal at all nodes (identical to condition 1.) 2(n-2) + 2 = 2(n-1)
3. The 1st derivatives are continuous at interior nodes xi, (n-2) 4. Assume that the second derivatives is zero at the first points
2(n1) + (n2) + (1) = 3(n1) 3(n1) equations for 3(n1) unknowns
Example with n = 5
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 = xi+1 – xi)
Apply to every interval (4 intervals, 8 equations)
Piecewise Quadratic SplinesPiecewise Quadratic Splines
2iiiii
2i1iii1iii1i1ii
iiii
hchbfxxcxxbafxs
afxs
)()()(
)(
2444445
2333334
2222223
2111112
44
33
22
11
hchbff
hchbff
hchbff
hchbff
&
fa
fa
fa
fa
Continuous slope at interior knots x = xi (n2 equations)
Apply to interior knots x2 , x3 and x4 (3 equations)
Zero curvatures at x = x1 (1 equation)
Piecewise Quadratic SplinesPiecewise Quadratic Splines
5444
4333
3222
bhc2b
bhc2b
bhc2b
0c 0c2xs 1111 )(
1iiii
1i1i1i1ii1iii
1i1i1ii
bhc2b
xxc2bxxc2b
xsxs
)()(
)()(
Example: Quadratic SplineExample: Quadratic Spline
0
0
0
0
ff
f
ff
f
ff
f
ff
f
c
b
a
c
b
a
c
b
a
c
b
a
100
0102h10
0102h10
0102h10
hh0
001
hh0
001
hh0
001
hh0
001
45
4
34
3
23
2
12
1
4
4
4
3
3
3
2
2
2
1
1
1
3
2
1
244
233
222
211
function [A, b] = quadratic(x, f)
% exact solution f(x)=x^3-5x^2+3x+4
% x=[-1 0 2 5 6]; f=[-5 4 -2 19 58];
h1=x(2)-x(1); h2=x(3)-x(2); h3=x(4)-x(3); h4=x(5)-x(4);
f1=f(1); f2=f(2); f3=f(3); f4=f(4); f5=f(5);
A=[1 0 0 0 0 0 0 0 0 0 0 0
0 h1 h1^2 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 h2^2 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 h3^2 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 h4^2
0 1 2*h1 0 -1 0 0 0 0 0 0 0
0 0 0 0 1 2*h2 0 -1 0 0 0 0
0 0 0 0 0 0 0 1 2*h3 0 -1 0
0 0 1 0 0 0 0 0 0 0 0 0];
b=[f1; f2-f1; f2; f3-f2; f3; f4-f3; f4; f5-f4; 0; 0; 0; 0];
Quadratic Spline InterpolationQuadratic Spline Interpolation
>> x=[-1 0 2 5 6]; f=[-5 4 -2 19 58];>> [A,b]=quadratic(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; z1=x(1):dx:x(2); s1=p(1)+p(2)*(z1-x(1))+p(3)*(z1-x(1)).^2;z2=x(2):dx:x(3); s2=p(4)+p(5)*(z2-x(2))+p(6)*(z2-x(2)).^2;z3=x(3):dx:x(4); s3=p(7)+p(8)*(z3-x(3))+p(9)*(z3-x(3)).^2;z4=x(4):dx:x(5); s4=p(10)+p(11)*(z4-x(4))+p(12)*(z4-x(4)).^2;H=plot(z1,s1,'r',z2,s2,'b',z3,s3,'m',z4,s4,'g');xx=-1:0.1:6; fexact=xx.^3-5*xx.^2+3*xx+4;hold on; G=plot(xx,fexact,'c',x,f,'ro');set(H,'LineWidth',3); set(G,'LineWidth',3,'MarkerSize',8);H1=xlabel('x'); set(H1,'FontSize',15);H2=ylabel('f(x)'); set(H2,'FontSize',15);H3=title('f(x)=x^3-5x^2+3x+4'); set(H3,'FontSize',15);
Quadratic Spline InterpolationQuadratic Spline Interpolation
4
4
4
3
3
3
2
2
2
1
1
1
c
b
a
c
b
a
c
b
a
c
b
a
p
s1(x)
s2(x)
s3(x)
s4(x)Quadratic Spline
Exact function
Cubic splines avoid the straight line and the over-swing
3ii
2iiiiii xxdxxcxxbaf )()()(
Can develop method like we did for quadratic – 4(n–1) unknowns – 4(n–1) equations
interior knot equality end point fixed interior knot first derivative equality assume derivative value if needed
Cubic SplinesCubic Splines
Piecewise Cubic SplinesPiecewise Cubic Splines
x1 x2 x3 xn
s 1 (x
)
s2 (x)s n-1
(x)
44((nn11)) unknownsunknowns
Continuous Continuous slopes and slopes and curvaturescurvatures
xn-1
n1n1n322211
nn,33,22,11,
xxI xxI xxI interval
y(xy(xy(xy(x points data
,,,,,,:
),),),),:
3ii
2iiiiii xxdxxcxxbaf )()()(
Piecewise Cubic SplinesPiecewise Cubic Splines
x0 x1 x2 xn
ssi i (x)(x) - piecewise cubic polynomials - piecewise cubic polynomials
ssii’(x)’(x) - piecewise quadratic polynomials (slope) - piecewise quadratic polynomials (slope)
ssii”(x) ”(x) - piecewise linear polynomials (curvatures)- piecewise linear polynomials (curvatures)
xn-1
s 1” (x)
s2”(x)
s3 ”(x)
S n-1” (x
)
Reduce to (n-1) unknowns and (n-1) equations for si’’
n1n1n322211
nn,33,22,11,
xxI xxI xxI interval
y(xy(xy(xy(x points data
,,,,,,:
),),),),:
Cubic SplinesCubic Splines Piecewise cubic polynomial with continuous
derivatives up to order 2
1. The function must pass through all the data points
gives 2(n-1) equations
2 3
2 31 1 1 1 1 1
: ( ) ( ) ( ) ( )
: ( ) ( ) ( ) ( )
i i i i i i i i i i i i i i i
i i i i i i i i i i i i i i
x x s x f a b x x c x x d x x a
x x s x f a b x x c x x d x x
2 31
i i
i i i i i i i i
a f
f b h c h d h f
i = 1,2,…, n-1
Cubic SplinesCubic Splines2. 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
1i2iiiii
1i1i1ii
2iiiiii
bhd3hc2b
xsxs
xxd3xxc2bxs
)()(
)()()(
1iiii
1i1i1ii
iiii
chd3c
xsxs
xxd6c2xs
)()(
)()(
Cubic SplinesCubic 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)
1n1n1nn1n
111
hd6c20xs
c20xs
)(
)(
0hd3cc
0c
0xs
0xs
1n1n1nn
1
n1n
11
)(
)(
Cubic SplinesCubic Splines
3ii
2iiiiii xxdxxcxxbaxs )()()()(
Solve for (ai, bi, ci, di) – see textbook for details
Tridiagonal system with boundary conditions c1 = cn= 0
]),[],[(
)(
1iii1i
1i
1ii
i
i1i1iiii1i1i1i
xxfxxf3
h
ff3
h
ff3chchh2ch
1iii
i
i1ii
i
i1iiii cc2
3
h
h
ffb
h3
ccd fa
,,
Cubic SplinesCubic Splines
0
xxfxxf3
xxfxxf3
xxfxxf3
0
c
c
c
c
c
1
hhh2h
hhh2h
hhh2h
1
2n1n1nn
2334
1223
n
1n
3
2
1
1n1n2n2n
3322
2211
],[],[
],[],[
],[],[
)(
)(
)(
Tridiagonal matrix
Hand CalculationsHand Calculations
f(4)estimate 581924)x(f
6520x
0f(4) ,4x3x5x f(x) :solution Exact 23
391
1958
h
ffxxf
73
219
h
ffxxf
32
42
h
ffxxf
58f 19f 2f 4f
156xxh 325xxh 202xxh
3
3434
2
2323
1
1212
4321
343232121
],[
)(],[
],[
,,,
,,
Hand CalculationsHand Calculations
0
96
30
0
0
7393
373
0
c
c
c
c
1
183
3102
1
0
xxfxxf3
xxfxxf3
0
c
c
c
c
1
hhh2h
hhh2h
1
4
3
2
1
2334
1223
4
3
2
1
3322
2211
)(
],[],[
],[],[
)(
)(
can be further simplified since c1 = c4 = 0 (natural spline)
25352112
6760560
c
c
96
30
c
c
83
310
3
2
3
2
.
.
Cubic Spline InterpolationCubic Spline Interpolation
1iii
i
i1ii
i
i1iiii
3ii
2iiiiii
cc23
h
h
ffb
h3
ccd fa
xxdxxcxxbaxs
,,
)()()()(
08309863cc23
h
h
ffb
9014083cc23
h
h
ffb
5492962cc23
h
h
ffb
08450704h3
ccd
43661971h3
ccd
1126760h3
ccd
0c
25352112c
6760560c
0c
58fa
19fa
2fa
4fa
433
3
343
322
2
232
211
1
121
3
343
2
232
1
121
4
3
2
1
44
33
22
11
.)(
.)(
.)(
.
.
.
.
.
Cubic SplinesCubic Splines
1iii
i
i1ii
i
i1iiii
3ii
2iiiiii
cc23
h
h
ffb
h3
ccd fa
xxdxxcxxbaxs
,,
)()()()(
32
333
2333333
32
322
2222222
3
311
2111111
5x084507045x253521125x8309863019
xxdxxcxxbaxs
2x4366197212x67605602x90140832
xxdxxcxxbaxs
x1126760x54929624
xxdxxcxxbaxs
)(.)(.)(.
)()()()(
)(.)(.)(.
)()()()(
..
)()()()(
Piecewise cubic splines (cubic polynomials)
Cubic Spline InterpolationCubic 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 !!
014114s4f :ioninterpolatspline Cubic
0f(4) 4x3x5x f(x) :solution Exact
2
23
.)()(
,
Cubic Spline InterpolationCubic Spline Interpolation
>> xx=[-1 0 2 5 6]; f = [-5 4 -2 19 58];>> Cubic_spline(xx,f);
Resulting piecewise function:s1 =(-5.000000)+(11.081218)*(x-(-1.000000))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.000000)).^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)
3ii
2iiiiii xxdxxcxxbaxs )()()()(
Cubic Spline InterpolationCubic Spline Interpolation
Piecewise cubic Continuous slope Continuous curvature
zero curvatures at end knots
exact solution
06f )(
01f )(
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
End Conditions of SplinesEnd Conditions of Splines
Comparison of Runge’s function (dashed red line) with a 9-point not-a-knot spline fit generated with MATLAB (solid green line)
>> x = linspace(-1,1,9); y = 1./(1+25*x.^2);>> xx = linspace(-1,1,100); yy = spline(x,y,xx);>> yr=1./(1+25*xx.^2);>> H=plot(x,y,'o',xx,yy,xx,yr,'--');
Continuous third derivatives
at x2 and xn-1
Note that first derivatives of 1 and 4 are specified at the left and right boundaries, respectively.
>> yc = [1 y –4];% 1 and -4 are the 1st-order derivatives (or slopes)at 1st & last point, respectively.>> yyc = spline(x,yc,xx);>> >> H=plot(x,y,'o',xx,yyc,xx,yr,'--');
Clamped End Spline - Use 11 values including slopes at end pointsClamped End Spline - Use 11 values including slopes at end points
MATLAB FunctionsMATLAB Functions
One-dimensional interpolationsyy = 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 splineyi = interp1 (x, y,, xi, ‘pchip’) – cubic Hermite yi = interp1 (x, y,, xi, ‘cubic’) – cubic Hermiteyi = interp1 (x, y,, xi, ‘nearest’) – nearest neighbor
Piecewise polynomial interpolation on velocity time series for an automobile
MATLAB FunctionMATLAB Function: : interp1interp1
>> x=[1 2 4 7 9 10]
x =
1 2 4 7 9 10
>> y=[3 -2 5 4 9 2]
y =
3 -2 5 4 9 2
>> xi=1:0.1:10;
>> y1=interp1(x,y,xi,'linear');
>> y2=interp1(x,y,xi,'spline');
>> y3=interp1(x,y,xi,'cubic');
>> plot(x,y,'ro',xi,y1,xi,y2,xi,y3,'LineWidth',2,'MarkerSize',12)
>> print -djpeg spline00.jpg
Spline InterpolationsSpline Interpolations
Spline InterpolationsSpline Interpolations
Linear
Not-a-knot
Cubic
MATLAB’s FunctionsMATLAB’s Functions
Two-dimensional interpolationszi = interp2 (x, y, z, xi, yi)
))(),((),(
),(,),,(),,(
),(,),,(),,(
),(,),,(),,(
jyixfjiz
yxyxyx
yxyxyx
yxyxyx
mn2n1n
m22212
m12111
MATLAB FunctionMATLAB Function: : interp2interp2
>> [x,y,z]=peaks(100); [xi,yi]=meshgrid(-3:0.1:3,-3:0.1:3);>> zi = interp2(x,y,z,xi,yi); surf(xi,yi,zi)>> print -djpeg075 peaks1.jpg>> [x,y,z]=peaks(10); [xi,yi]=meshgrid(-3:0.1:3,-3:0.1:3);>> zi = interp2(x,y,z,xi,yi); surf(xi,yi,zi)>> print -djpeg075 peaks2.jpg
100 100 data base 10 10 data base
CVEN 302-501CVEN 302-501Homework No. 11Homework 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 periodDue on Wed. 11/05/08 at the beginning of the period