Download - Chapter 15 Curve Fitting : Splines
Chapter 15 Chapter 15 Curve Fitting : SplinesCurve Fitting : Splines
Gab Byung ChaeGab Byung Chae
Oscillation in a higher order Oscillation in a higher order interpolationinterpolation
The alternative : splineThe alternative : spline
Notation used to derive splinesNotation used to derive splinesn-1 intervals in n data points
Splines in 1Splines in 1stst, 2, 2ndnd, 3, 3rdrd order orderLinear splineLinear spline
Quadratic splineQuadratic spline
Cubic splineCubic spline
15.2 Linear Splines15.2 Linear Splineslinear splines passing thrulinear splines passing thru
Example : (3,2.5),(4.5,1),(7,2.5),(9,.5) -> (5,?)Example : (3,2.5),(4.5,1),(7,2.5),(9,.5) -> (5,?)
nnnn xxxxfxfx 12111 ...),,(),....,,(
nnnnn
nnnn
iiiii
iiii
xxxxxxx
fffxs
xxxxxxx
fffxs
xxxxxxx
fffxs
xs
111
111
11
1
21112
1211
),()(
),()(
),()(
)(
15.3 Quadratic Splines15.3 Quadratic SplinesThe objective in quadratic splines is to derive The objective in quadratic splines is to derive a second-order polynomial for each interval a second-order polynomial for each interval between data points between data points
The polynomial :The polynomial :
ssii(x) =a(x) =ai i + b+ bii(x-x(x-xii)+ c)+ cii(x-x(x-xii))22
For n data points, there are n-1 intervals and, For n data points, there are n-1 intervals and, consequently, 3(n-1) unknown constants to consequently, 3(n-1) unknown constants to evaluate. evaluate. 3(n-1) equations or conditions 3(n-1) equations or conditions are required to evaluate the unknowns.are required to evaluate the unknowns.
nnnnnnnn
iiiiiiii
xxxxxcxxbaxs
xxxxxcxxbaxs
xxxxxcxxbaxs
xs
12
111111
12
212
111111
,)()()(
,)()()(
,)()()(
)(
12
12
1111 )()()(
)(
iiiiii
iiiiiiiiiii
iiiii
fhchbf
fxxcxxbffxs
fafxs
,1 iii xxh Let for all i=1,,..,n-1 1. Continuity condition : the function must pass through all the points.
2. The function values of adjacent polynomials must be equal at the knots.
for all i=1,,..,n-1 : (n-1)
equations
for all i=1,,..,n-1 : (n-1)
equations
--- (15.8)
--- (15.6)
00)("
2)(')('
111
1111
cxs
bhcbxsxs iiiiiiii (n-2) eq.s
1 equation
Example : (3,2.5),(4.5,1),(7,2.5),(9,.5) -> (5,?)Example : (3,2.5),(4.5,1),(7,2.5),(9,.5) -> (5,?)
,1 iii xxh Let for all i=1,,..,n-1 3. The first derivatives at the interior nodes must be equal.
4. Assume that the second derivative is zero at the first point.
--- (15.9)
Example 15.2 Example 15.2 Problem] Fit quadratic splines to the data Problem] Fit quadratic splines to the data in Table 15.1. Use the results to estimate in Table 15.1. Use the results to estimate the value at x=5.the value at x=5.
Solution] Equation (15.8) is written for i=1 Solution] Equation (15.8) is written for i=1 through 3 (with cthrough 3 (with c11=0) to give=0) to give
ff11+ b+ b11hh11 = f = f22
ff22+ b+ b22hh22 + c + c22hh2222 = f = f33
ff33+ b+ b33hh33 + c + c33hh3322 = f = f44
Equation (15.9) creates 2 conditions with Equation (15.9) creates 2 conditions with
cc11=0 :=0 :
bb11 = b = b22
bb22+ 2c+ 2c22hh22 = b = b33
ff1 1 = 2.5 h= 2.5 h1 1 = 4.5-3.0 = 1.5= 4.5-3.0 = 1.5
ff2 2 = 1.0 h= 1.0 h2 2 = 7.0-4.5 = 2.5= 7.0-4.5 = 2.5
ff3 3 = 2.5 h= 2.5 h3 3 = 9.0-7.0 = 2.0 = 9.0-7.0 = 2.0
ff4 4 = 0.5= 0.5
2.5 + 1.5 b1 = + 1.5 b1 = 1.01.0 1.0 + 2.5 b2 + 6.25 c2 1.0 + 2.5 b2 + 6.25 c2 = 2.5= 2.5 2.5 + 2.0 b3 + 4c3 2.5 + 2.0 b3 + 4c3 = 0.5= 0.5 b1 - b2 = b1 - b2 = 00 b2 - b3 + 5c2b2 - b3 + 5c2 = = 00
bb11 = -1 = -1
bb2 2 = -1 c= -1 c2 2 = 0.64= 0.64
bb33= 2.2 c= 2.2 c3 3 = -1.6= -1.6
ss11(x) = 2.5 –(x-3)(x) = 2.5 –(x-3)
ss22(x) = 1.0 –(x-4.5)+0.64(x-4.5)(x) = 1.0 –(x-4.5)+0.64(x-4.5)22
ss33(x) = 2.5 + 2.2(x-7.0)-1.6(5-7.0)(x) = 2.5 + 2.2(x-7.0)-1.6(5-7.0)22
x=5 x=5 이므로 이므로 ss22(5) = 1.0 –(5-4.5)+0.64(5-4.5)(5) = 1.0 –(5-4.5)+0.64(5-4.5)2 2 =0.66=0.66
15.4 Cubic Splines15.4 Cubic Splines
,1 iii xxh
06220)(")("
3)(")("
32)(')('
)(
)(
,)()()()(
,)()()()(
,)()()()(
)(
1111111
1111
12
111
132
11
13
112
111111
132
213
112
111111
nnnnn
iiiiiiii
iiiiiiiiii
iiiiiiiiiii
iiiii
nnnnnnnnnn
iiiiiiiiii
hdccxsxs
chdcxsxs
bhdhcbxsxs
fhdhchbffxs
fafxs
xxxxxdxxcxxbaxs
xxxxxdxxcxxbaxs
xxxxxdxxcxxbaxs
xs
a. (n-1) eq.s
b. (n-1) eq.s
c. (n-2) eq.s
e. 2 eq.sd. (n-2) eq.s
Solving Cubic SplinesSolving Cubic Splines
0
33
:
:
33
0
:
:
100000
)(2000
000
000
000)(2
000001
),(0262,02.
)&.(33)(2.
)&.()(32.
)2(3
)&.()2(.
3/)().(3
21
21
1
1
12
12
23
23
1
2
1
1122
2211
1111
1
111111
112
11
113
32
11
2
nn
nn
nn
nn
n
nnnnn
nnnn
i
ii
i
iiiiiiiii
iiiiiiiiii
iii
i
iii
iiih
iiiiiiiiii
iiiiiiii
xx
ff
xx
ff
xx
ff
xx
ff
c
c
c
c
hhhh
hhhh
chdcc
hgEqsh
ff
h
ffchchhch
fcEqsbcchbhdhcb
cch
h
ffb
fbEqsfcchbfhdhchbf
hccddEqchdc
i
fej
i
h
g
f.
Example 15.3 Example 15.3 Fit cubic splines to the data . Utilize the results to estimate the value at x =5
Solution : employ Eq.(15.27)Solution : employ Eq.(15.27)
ff1 1 = 2.5 h= 2.5 h1 1 = 4.5-3.0 = 1.5= 4.5-3.0 = 1.5
ff2 2 = 1.0 h= 1.0 h2 2 = 7.0-4.5 = 2.5= 7.0-4.5 = 2.5
ff3 3 = 2.5 h= 2.5 h3 3 = 9.0-7.0 = 2.0 = 9.0-7.0 = 2.0
ff4 4 = 0.5= 0.5
0
]),[],[(3
]),[],[(3
0
1000
)(20
0)(2
0001
2334
1223
4
3
2
1
3212
2211
xxfxxf
xxfxxf
c
c
c
c
hhhh
hhhh
So we have So we have
Using MATLAB the results areUsing MATLAB the results are
cc11 = 0 c = 0 c22 = 0.839543726 = 0.839543726
cc33 = -0.766539924 c = -0.766539924 c44 = 0 = 0
0
8.4
8.4
0
1000
295.20
05.285.1
0001
4
3
2
1
c
c
c
c
Using Eq. (15.21) and (15.18), Using Eq. (15.21) and (15.18),
bb11 = -1.419771863 d = -1.419771863 d11 = 0.186565272 = 0.186565272
bb22 = -0.160456274 d = -0.160456274 d22 = -0.214144487 = -0.214144487
bb33 = 0.022053232 d = 0.022053232 d33 = 0.127756654 = 0.127756654
102889734.1)5.45(214144487.0
)5.45(839543726.0)5.45(160456274.00.1)5(
)0.7(127756654.0
)0.7(766539924.0)0.7(022053232.05.2)(
)5.4(214144487.0
)5.4(839543726.0)5.4(160456274.00.1)(
)3(186565272.0)3(419771863.15.2)(
3
22
3
23
3
22
31
s
x
xxxs
x
xxxs
xxxs
15.4.2 End Conditions15.4.2 End ConditionsClamped End Condition : Specifying the Clamped End Condition : Specifying the first derivatives at the first and last nodes. first derivatives at the first and last nodes.
Not-a-Knot End Condition : Force Not-a-Knot End Condition : Force continuity of the third derivative at the continuity of the third derivative at the second and the next-to-last knots. – same second and the next-to-last knots. – same cubic functions will apply to each of the cubic functions will apply to each of the first and last two adjacent segments, first and last two adjacent segments,
15.5 Piecewise interpolation in 15.5 Piecewise interpolation in MatlabMatlab
15.5.1 MATLAB function : spline15.5.1 MATLAB function : spline
Syntax :Syntax :
yy = spline(x, y, xx)yy = spline(x, y, xx)
Example 15.4 Splines in Example 15.4 Splines in MATLABMATLAB
Use MATLAB to fit 9 equally spaced data points Use MATLAB to fit 9 equally spaced data points sampled from this function in the interval [-1, 1].sampled from this function in the interval [-1, 1].
a) a not-a-knot spline b) a clamped spline with a) a not-a-knot spline b) a clamped spline with end slopes of fend slopes of f11’=1 and f’’=1 and f’n-1n-1 = -4 = -4
2251
1)(
xxf
Solution :Solution :
a)a)
>> x = linspace(-1,1,9);>> x = linspace(-1,1,9);
>> y = 1./(1+25*x.^2);>> y = 1./(1+25*x.^2);
>> xx = linspace(-1,1);>> xx = linspace(-1,1);
>> yy = spline(x,y,xx); >> yy = spline(x,y,xx);
>> yr = 1./(1+25*xx.^2);>> yr = 1./(1+25*xx.^2);
>> plot(x,y,’o’,xx,yy,xx,yr,’—’)>> plot(x,y,’o’,xx,yy,xx,yr,’—’)
Cubic spline of the Runge Cubic spline of the Runge functionfunction
2251
1)(
xxf
b)b)
>> yc =[1 y -4];>> yc =[1 y -4];
>> yyc =spline(x,yx,xx);>> yyc =spline(x,yx,xx);
>> plot(x,y,’o’,xx,yyc,xx,yr,’—’)>> plot(x,y,’o’,xx,yyc,xx,yr,’—’)
15.5.2 MATLAB function : interp115.5.2 MATLAB function : interp1
Implement a number of different types of Implement a number of different types of piecewise one-dimensional interpolation. piecewise one-dimensional interpolation.
Syntax :Syntax :
yi = interp1(x,y,xi, ‘method’)yi = interp1(x,y,xi, ‘method’)
yi = a vector containing the results of the yi = a vector containing the results of the interpolation as evaluated at the points in interpolation as evaluated at the points in the vector xi , and ‘method’ = the desired the vector xi , and ‘method’ = the desired method. method.
methodmethodNearest – nearest neighbor interpolation. Nearest – nearest neighbor interpolation.
Linear – linear interpolationLinear – linear interpolation
Spline – piecewise cubic spline Spline – piecewise cubic spline interpolation(identical to the spline interpolation(identical to the spline function)function)
pchip and cubic – piecewise cubic Hermite pchip and cubic – piecewise cubic Hermite interpolation.interpolation.
The default is linear.The default is linear.
MATLAB’s methodsMATLAB’s methods
>> yy=spline(x,y,xx); >> yy=spline(x,y,xx); cubic spilnecubic spilne
>> yy=interp1(x,y,xx): >> yy=interp1(x,y,xx): (piecewise) linear spline (piecewise) linear spline
>> yy=interp1(x,y,xx,’spline’)>> yy=interp1(x,y,xx,’spline’): cubic spline: cubic spline
>> yy=interp1(x,y,xx,’nearest’>> yy=interp1(x,y,xx,’nearest’) : nearest ) : nearest neighborneighbor methodmethod
>> >> yy=interp1(x,y,xx,’pchip’)yy=interp1(x,y,xx,’pchip’): piecewise cubic : piecewise cubic Hermite interpolationHermite interpolation
>> zz=interp2(x,y,z,xx,yy)>> zz=interp2(x,y,z,xx,yy): 2 dimensional : 2 dimensional interpolationinterpolation
Example 15.5 Trade-offs Using Example 15.5 Trade-offs Using interpl interpl
Linear, nearest, cubic spline, piecewise Linear, nearest, cubic spline, piecewise cubic Hermite interpolationcubic Hermite interpolation
>> t = [ 0 20 40 56 68 80 84 96 104 110];>> t = [ 0 20 40 56 68 80 84 96 104 110];
>> v = [0 20 20 38 80 80 100 100 125 125];>> v = [0 20 20 38 80 80 100 100 125 125];
>> tt = linspace(0,110);>> tt = linspace(0,110);
>> vl=interp1(t,v,tt); %(piecewise) linear spline >> vl=interp1(t,v,tt); %(piecewise) linear spline
>> plot(t,v,’o’,tt,vl)>> plot(t,v,’o’,tt,vl)
>> vn=interp1(t,v,tt,’nearest’); % nearest>> vn=interp1(t,v,tt,’nearest’); % nearest
>> plot(t,v,’o’,tt,vn)>> plot(t,v,’o’,tt,vn)
>> vs=interp1(t,v,tt,’spline’); % cubic spline>> vs=interp1(t,v,tt,’spline’); % cubic spline
>> plot(t,v,’o’,tt,vs)>> plot(t,v,’o’,tt,vs)
>> vh=interp1(t,v,tt,’pchip’); % piecewise cubic >> vh=interp1(t,v,tt,’pchip’); % piecewise cubic Hermite interpolationHermite interpolation
>> plot(t,v,’o’,tt,vh)>> plot(t,v,’o’,tt,vh)