47
數數數數 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Embed Size (px)

Citation preview

Page 1: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 1

Spline interpolation

Page 2: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Spline A flexible piece of wood, hard rubber, or

metal used in drawing curves Spline

(mathematics) - Wikipedia, the free encyclopedia

數值方法 2008, Applied Mathematics NDHU 2

Page 3: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Before computers were used, numerical calculations were done by hand. Functions such as the step function were used but polynomials were generally preferred. With the advent of computers, splines first replaced polynomials in interpolation, and then served in construction of smooth and flexible shapes in computer graphics.[5]數值方法 2008, Applied Mathematics NDHU 3

Page 4: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Control Polygon

數值方法 2008, Applied Mathematics NDHU 4

Page 6: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Spline approximation

Matlab Spline toolbox Input

paired data (x,y) x_in : a set of horizontal coordinates

Output: y_out, spline Interpolation at x_in

數值方法 2008, Applied Mathematics NDHU 6

Page 7: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 7

Example I: three input arguments

Input: x = 0:10; y = sin(x); x_in = 0:.25:10;Output: y_out = spline(x,y,x_in);Figure: plot(x,y,'o',x_in,y_out)

Page 8: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 8

Page 9: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Example II: Two input arguments

數值方法 2008, Applied Mathematics NDHU 9

x = -4:4; y = [0 .15 1.12 2.36 2.36 1.46 .49 .06 0];

cs = spline(x,y);

xx = linspace(-4,4,101); plot(x,y,'o');

plot(xx,ppval(cs,xx),'-‘);

INPUT

OUTPUT

FIGURE

Polynomial output

Page 10: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 10

Page 11: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Example III

2 D splines

數值方法 2008, Applied Mathematics NDHU 11

Page 12: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 12

2-D spline

demo_2d_spline.m

Page 13: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 13

Polygon

figure;hold onpoints = [0 0; 1 0; 1 1; 0 2; -1 1; -1 0; 0 -1; 0 -2].';plot(points(1,:),points(2,:),'k'), axis([-2 2 -2.1 2.2]), grid offtitle('Control polygon')

Page 14: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 14

2D spline

>> plot(points(1,:),points(2,:),'ok')>> fnplt( cscvn(points), 'g',1.5 )

Page 15: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

CSCVN `Natural' or periodic interpolating cubic spline curve CS = CSCVN(POINTS) returns a parametric `natural' cubic spline that interpolates

to the given points POINTS(:,i) at parameter values t(i) , i=1,2,..., with t(i) chosen by Eugene Lee's centripetal

scheme, i.e., as accumulated square root of chord-length. When first and last point coincide and there are no double

points, then a parametric *periodic* cubic spline is constructed

instead. However, double points result in corners.數值方法 2008, Applied Mathematics NDHU 15

Page 16: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 16

For example, x1=[1 0 -1 0 1];x2=[0 1 0 -1 0]; plot( x1,x2, ' ok ');hold on curve = cscvn([x1;x2]) ; fnplt( curve )

-1 -0.5 0 0.5 1-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 17: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 17

shows a (circular) curve through the four vertices of the standard diamond (because of the periodic boundary conditions enforced), while

x1=[1 0 -1 -1 0 1];x2=[0 1 0 0 -1 0]; plot( x1,x2, ' ok ');hold on curve = cscvn([x1;x2]) ; fnplt( curve )

-1 -0.5 0 0.5 1-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

shows a corner at the double point as well as at the curve endpoint.

Page 18: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Spline

A spline is composed of a set of piecewise polynomials

Linear spline Quadratic spline Cubic spline

數值方法 2008, Applied Mathematics NDHU 18

Page 19: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Spline approximation

Linear spline First order piecewise polynomials

Quadratic spline Second order piecewise polynomials

Cubic spline Cubic piecewise polynomials

數值方法 2008, Applied Mathematics NDHU 19

Page 20: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 20

L=length(x_in)n=length(x)

for j=1:L

for k=1:n

if x(k) >= x_in(j)breakend

EXIT

function y_out=my_spline1(x,y,x_in)

i=k-1;a=(y(i+1)-y(i))/(x(i+1)-x(i));y_out(j)=y(i)+a*(x_in(j)-x(i));

Page 21: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Procedure: my_spline1

Function y_out=my_spline(x,y,x_in) L=length(x_in); for j=1:N

A. Find i that satisfies x(i) <= xx(i) <x(i+1) B. Set g to the slop of a line that connects

(x(i),y(i)) and (x(i+1),y(i+1)). C. y_out(j) = y(i)+g*(x_in(j)-x(i))

數值方法 2008, Applied Mathematics NDHU 21

Page 22: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Line interpolating

數值方法 2008, Applied Mathematics NDHU 22

(x(i),y(i))

(x(i+1),y(i+1))

(x_in(j),y_out(j)=?)

)()1(

)()1(*)]()(_[)()(_

ixix

iyiyixjinxiyjouty

Page 23: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Linear spline

數值方法 2008, Applied Mathematics NDHU 23

x = 0:10; y = sin(x);x_in = 0:.25:10;y_out = my_spline1(x,y,x_in);plot(x,y,'o',x_in,y_out)

my_spline1.m

Page 24: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 24

Page 25: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 25

Quadratic Spline

polynomial quadratic a is

if )()( 1

i

iii

f

where

xxxxfxS

Page 26: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 26

Quadratic spline

),( 11 ii yx

),( 11 ii yx

iiii cxbxaxf 2)(

112

11 )( iiii cxbxaxf

),( ii yx

Page 27: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 27

Quadratic splines

Quadratic spline interpolation Determine all fi

i all )(' forzxSAssume ii

Page 28: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 28

quadratic polynomial

iiiiii

iii yxxzxx

xx

zzxf

)()()(2

)( 2

1

1

iiii

iii zxx

xx

zzxf

)()(

)(1

1'

iii zxf )('

11' )( iii zxf

Page 29: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Smoothness criteria

The slopes of fi and fi+1 are identical at point Pi

數值方法 2008, Applied Mathematics NDHU 29

)()( ''1 iiii xfxf

),( 11 ii yx

),( ii yx

),( 11 ii yx

)(xfi

)(1 xfi

Page 30: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 30

Smoothness Criteria Checking Check iiiii zxfxf )()( ''

1

iiii

iii zxx

xx

zzxf

)()(

)(1

1'

111

1'1 )(

)()(

iiii

iii zxx

xx

zzxf

iiiiii

iiii zzxx

xx

zzxf

11

1

1'1 )(

)()(

Page 31: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Fitting criteria

Pi denotes point (xi yi)

fi passes points Pi and Pi+1

數值方法 2008, Applied Mathematics NDHU 31

),( 11 ii yx

),( ii yx

),( 11 ii yx

)(xfi

)(1 xfi

11)( iii yxf

iii yxf )(

Page 32: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 32

Fitting Criteria Checking - I Check iii yxf )(

iiiiii

iii yxxzxx

xx

zzxf

)()()(2

)( 2

1

1

i

iiiiiiii

iiii

y

yxxzxxxx

zzxf

)()()(2

)( 2

1

1

Page 33: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 33

Fitting Criteria Checking - II Check

iiiiii

iii yxxzxx

xx

zzxf

)()()(2

)( 2

1

1

111

12

11

11

)(2

)()()(2

)(

iiiiii

iiiiiiii

iiii

yyxxzz

yxxzxxxx

zzxf

11)( iii yxf

Page 34: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 34

Recurrence relations

111 )(2

iiiiii yyxxzz

1,...,0,21

11

niz

xx

yyz i

ii

iii

Page 35: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 35

)1( 1,...,0,21

11 eqniz

xx

yyz i

ii

iii

determined becan ,...,

0Set

1

0

nzz

z

Assume

determined are )( All xfi

Page 36: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 36

Equations for Implementation

1-n0,...,i

eq(2) )()()(2

)( 2

1

1

iiii

ii

iii yxxzxx

xx

zzxf

)1( 1,...,0,2

,...,0),,( : data Paired

1

11 eqniz

xx

yyz

niyx

iii

iii

ii

Page 37: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 37

Re-indexing

n1,...,i

eq(2) )()()(2

)( 2

1

1

iiii

ii

iii yxxzxx

xx

zzxf

)1( ,...,1,2

1,...,1),,( : data Paired

1

11 eqniz

xx

yyz

niyx

iii

iii

ii

Page 38: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 38

L=length(x_in)K=length(x)n=K-1;z(1)=0

for j=1:L

for k=1:K

if x(k) >= x_in(j)breakend

EXIT

function y_out=my_spline1(x,y,x_in)

i=k-1a=(z(i+1)-z(i))/(x(i+1)-x(i));b=z(i);c=y(i)y_out(j)=a*(x_in(j)-x(i))^2+b*(x_in(j)-x(i)) +c;

for i=1:n

a=(y(i+1)-y(i))/(x(i+1)-x(i))z(i+1)=2*a-z(i)

Page 39: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Procedure: my_spline2

Function y_out=my_spline2(x,y,x_in) n=length(x)-1;L=length(x_in); for i=1:n

Determine zi+1 by eq(1)

for j=1:L A. Find j that satisfies x(i) <= x_in(i) <x(i+1) B. substitute x_in(j) to eq(2) C. Set y_out(j) to the result of step B

數值方法 2008, Applied Mathematics NDHU 39

Page 40: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 40

Cubic spline

),( 11 ii yx

),( ii yx

),( 11 ii yx

iiiii dxcxbxaxf 23)(

112

13

11 )( iiiii dxcxbxaxf

Page 41: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Criteria of cubic spline

Fitting criteria First order smoothness criteria Second order smoothness criteria

數值方法 2008, Applied Mathematics NDHU 41

Page 42: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 42

Fitting criteria

),( 11 ii yx

),( ii yx

),( 11 ii yx

)(xfi

)(1 xfi

11)( iii yxf

iii yxf )(

Page 43: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Smoothness criteria I

The slopes of fi and fi+1 are identical at point Pi

數值方法 2008, Applied Mathematics NDHU 43

)()( ''1 iiii xfxf

),( 11 ii yx

),( ii yx

),( 11 ii yx

)(xfi

)(1 xfi

Page 44: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

數值方法 2008, Applied Mathematics NDHU 44

Smoothness criteria II

The curvatures of fi and fi+1 are identical at point Pi

)()( ''1 iiii xfxf

),( 11 ii yx

),( ii yx

),( 11 ii yx

)(xfi

)(1 xfi )()( ''''1 iiii xfxf

Pi

Page 45: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Exercise 6 due to 10/29

Problem 3 of Ex 5

數值方法 2008, Applied Mathematics NDHU 45

Page 46: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Problem 2

Draw a flow chart to illustrate linear spline interpolation

Implement the flow chart by matlab codes

Give examples to test your matlab codes

數值方法 2008, Applied Mathematics NDHU 46

Page 47: 數值方法 2008, Applied Mathematics NDHU 1 Spline interpolation

Problem 3

Draw a flow chart to illustrate quadratic spline interpolation

Implement the flow chart by matlab codes

Give examples to test your matlab codes

數值方法 2008, Applied Mathematics NDHU 47