´¡­Èªk¡]¤º´¡»P¥~´¡¡^

Interpolation and Extrapolation

 

¤T¦¸ªO±ø½u (Cubic Spline) ¤T¦¸¶³§Î½u

¦³¨Ç´¡­ÈÀ³¥Îªº³õ¦X¡A§Ú­Ì·|«Ü­«µø¾ã­Ó´¡­È¦±½u³q¹Lªí¦CÂI®Éªº¶ê·Æ©Ê¡A¬Æ¦Ü¡A°ò©ó³Q´y­z¤§²{¶Hªº¥»½è¡A§Ú­Ì·|§Æ±æ³s¨ä¾É¼Æ¡]§Y±×²v¡^¨ç¼Æ¤]¬O³B³B¶ê·Æ¡C

¨Ò¦p¡A¦pªG´y­zªº²{¶H¬O­y¸ñ¡]¤]´N¬O¦ì¸m¹ï®É¶¡ªº§@¹Ï¡^¡A¨ä¾É¼Æ¡]·íµM´N¬O³t«×¨ç¼Æ¡^¦b¤£¦Pªº®É¨è¤W¡A¤´À³¬O¥­·Æªº¡C³t«×ÀH®É¶¡ªºÅܤƦpªG¦³¦y¾Uªº§éÂI¡A¥Nªí³t«×¦bÀþ¶¡§ïÅÜ¡A¥¼¥²²Å¦X§Ú­Ì©Ò­n´y­z¤§²{¶HÅé¨t¤ºÀ³¦³ªº±ø¥ó¡C¦b³oºØª¬ªp¤§¤U§Ú­Ì·|§Æ±æ³t«×¤´µM¶ê·Æ¡A¦Ü©ó¥¦ªº¤U¤@¶¥¾É¼Æ¥[³t«×¬O³sÄòªº´N¥i¥H¤F¡]¥i¥H¦³§é½u¡^¡C

¦pªG§Ú­Ì¥u¨Ï¥Î¤F½u©Ê´¡­È¡A«h¹Ï½u¹³¤U­±ªº f(x) ¡A¨ä¤@¶¥¾É¼Æ«h¬O f'(x)¡A±q³o­Ó¨Ò¤l§Ú­Ìª`·N¨ì¡A´¡­È¨ç¼Æªº¤@¶¥¾É¼Æ¦bªí¦CÂI¤W·|¨S¦³©w¸q

°w¹ï¦³¨Ç´¡­ÈªºÀ³¥Î¡A§Ú­Ì·|§Æ±æ¥¦¸ó¹Lªí¦CÂI®É«D±`¶ê·Æ¡]¨âÂI¤§¶¡ªº³¡¤À¥»¨Ó´N¨S¦³°ÝÃD¡^¡A¤@¶¥¾É¼Æ¤]¬O¡A¦Ü©ó¤G¶¥¾É¼Æ«h¦b¸óªí¦CÂI®É¬O½u©Ê´¡­È¡]¦p¦¹¦Ü¤Ö«OÃÒ³sÄò¡^´N¥i¥H¤F¡C¦b³oºØ³õ¦X¤U¡A¤T¦¸ªO±ø½u (Cubic Spline) ¥i¥Hº¡¨¬§Ú­Ìªº»Ý¨D¡C

ºû°ò¦Ê¬ì¤WÃö©ó spline ªº±ø¥Ø¡Ghttp://en.wikipedia.org/wiki/Spline_(mathematics)

Cubic Spline ªº¯S©Ê¡A(1) §Î¦¡¤W¬O½u©Ê´¡­Èªº©µ¦ù»P¶i¤Æ¡B(2) ³Ì°ª¦¸¼Æ¬°¤T¦¸¡A¦±½u¤£·|¹L«×ÂàÅs¡B(3) ³B³B¶ê·Æ¡A¸ó¹Lªí¦CÂI®É¤]¤@¼Ë¡C

­n«ç»òÅý¤G¶¥¾É¼Æº¡¨¬½u©Ê´¡­È¡H§Ú­Ì­«ÀY¨ì§À³£¥u¦³ªí¦CÂI»Pªí¦C­È¹ï (xi, yi) §r¡C ¨SÃö«Y¡A§Ú­Ì¥ý¼g¤U§t y'' ªºªí¦C­È

x1 y1   y''1
x2 y2   y''2
x3 y3   y''3
: :   :
: :   :
xN yN   y''N

§Ú­Ì¹w´Á´¡­È¨ç¼Æªº¤G¶¥¾É¼Æ¨ç¼Æ y''(x) ¤D¥Ñ¦Uªí¦C­Èªº¤G¶¥¾É¼Æ­È©Ò½u©Ê¤º´¡ºc¦¨¡G

y''(x) = y''i (x-xi+1) / (xi-xi+1) + y''i+1 (x - xi) / (xi+1 - xi)

²{¦b­«­nªº¬O¡A«ç»ò¼Ëªº´¡­È¨ç¼Æ y(x) ¯àº¡¨¬­ìªí¦CÂI¥N¤J±oªí¦C­È¤§±ø¥ó¡A¨Ã¥B¥¦¦b¹ï x ·L¤À¨â¦¸¤§«á·|¥¿½T±o¥X¤W­±ªº¤G¶¥¾É¼Æ¨ç¼Æ¡H

§Ú­Ì±q±À¼s½u©Ê´¡­È¥Xµo¡A

y = Ayj + Byj+1

¨ä¤¤ A ¡Ý ( x - xj+1 ) / ( xj - xj+1 ) ¡AB ¡Ý 1 - A = ( x - xj ) / ( xj+1 - xj )¡A´N¬O Lagrange polynomnial ªº§Î¦¡¡C

¦Ó§Ú­Ì²{¦b§Æ±æ¥[¤W¤@¨Ç°ª¶¥¶µ¡AÅý¨ç¼Æªº½u±øÅܱo§ó¶ê·Æ¡A¦ý³o¼Ë°µªº¦P®É¡A¤S§Æ±æ«O¯d­ì¥»¤v¸g¦b½u©Ê´¡­È¦¡¤¤º¡¨¬¤§ªí¦CÂI¥N¤J·|±oªí¦C­Èªº¯S©Ê¡A§Ú­Ì¦]¦¹­n¨D«á­±¥[¤JªºªF¦è¦Ü¤Ö¦bªí¦CÂI³B¨ä­È­n¬°¹s¡C

¦]¦¹¡A©Ò´¡­Èªº¶µ

y = Ayj + Byj+1 + Cy''j + Dy''j+1

³oùØ A »P B ©w¸q¦P¤W¡A¦Ó¦h¥X¤§³¡¤Àªº¨â­Ó·s¶µ¤¤¡A

ùØ­±¥Î¤F A3 - A = A(A+1)(A-1) ³o¼Ëªº§Î¦¡ªº¶µ¡Aªº½T¥i¥H«OÃÒ C ¦b x = xi ¤Î x = xj ³£·|µ¥©ó¹s¡AD ªº§@ªk¤]¬Û¦P¡C

³o¼Ë©w¸q¤Uªº y(x)¡A¹ï x ·L¤À¤@¦¸·|±o¡]«Ý·|«á­±ÁÙ·|¥Î¨ì¡^¡G

¦A·L¤À¤@¦¸¡AªG¯u±o¨ì¨ä¤G¶¥¾É¼Æ¨ç¼Æ«ê§e½u©Ê´¡­Èªº§Î¦¡¡A¦p¤U¡G

¦n¤F¡A§Ú­Ì³z¹L¤W¦C A¡BB¡BC¡BD ªº©w¸q¡A¦³¤F¥i¥H¥Îªº´¡­È¤½¦¡¡A¦ý¬O³o­Ó¤½¦¡ÁÙ¤£¯à¥Î¡A²z¥Ñ¬O¤½¦¡¤¤ªº y''i §Ú­Ì¨Ã¤£ª¾¹D¡C²{¦b¡A­n¥Î¥H¤Uªºµ¦²¤§â¥¦­Ì©w¥X¨Ó¡G

§Ú­Ì¬Ý¤W¤W¤@­Ó¦¡¤l¡A§Y cubic spline ¨ç¼Æªº¤@¶¥¾É¼Æ¡A¥¦ªº¨Ï¥Î½d³ò¬OÂI xj »PÂI xj+1 ¤§¶¡¡Cxj+1 ¬O³o­Ó°Ï¶¡ªº¥kºÝÂI¡A¦ÛµM¦³¦b¨ä´y­zªº½d³ò¤§¤º¡AµM¦Ó xj+1 ¦P®É¤]¬O xj+1 ¨ì xj+2 ªº¥t¤@±ø¦±½u¬qªº¥ªºÝÂI¡A¦³¥¦¦Û¤w¤£¤@¼Ëªº±×²v¨ç¼Æ¤½¦¡¡A¬°¤F½T«O¤@¶¥¾É¼Æªº¶ê·Æ«×¡A§Ú­Ì¥i¥H­n¨D¦b xj+1 ³o­ÓÂI¤W¨âÃ䪺±×²v¨ç¼Æ­È­n¤@¼Ë¡C ³o­Ó±ø¥ó¥Î¤U¥h©Ò¼g¥X¨Óªº¤@±ø·sªºµ¥¦¡¡A§â©Ò¦³¥¼ª¾ªº y''i ³q³q²¾¨ìµ¥¸¹¥ªÃä¡A«h¾ã²z±o¥H¤Uªº§Î¦¡¡Cª`·N©Ò¥¼ª¾¼Æ§Î¦¨¤@­Ó½u©Ê¥N¼Æ¤èµ{¦¡¡G

§Ú­Ì¦]¦¹¥i¥H³z¹L¼Æ­È¤èªk¨D¸Ñ¡C¤@¥¹¸Ñ¥X¦U y''i ¡A«h cubic spline ´¡­È¤½¦¡ªº«Ø¥ß´N½T©w¨Ã§¹¦¨¤F¡C

³Ì«á¨â­Ó§Þ³N¤Wªº¤p²Ó¸`¡G

¡]¤@¡^"¸ó¹Lªí¦CÂI¤§±×²v­n³sÄò" ªº±ø¥ó¥u¦³ N-2 ­Ó¡]x1 ¤Î xN ¨âÂI¨S¦³¥Î¤W³o­Ó±ø¥ó¡^¡A§Ú­Ì»Ý­n¥t¥~¿W¥ß¤§¨â±ø¥¼ª¾¼Æ y''i ¤§¶¡ªº½u©ÊÃö«Y¦¡¨Ó§@¬°±ø¥ó¡A¤~¯à¸Ñ¥X¥¼ª¾¼Æ¡C³o¨Ã¤£Ãø¡A¨Ï¥ÎªÌ¥i¥H¦Û¦æ«ü©w y''1 »P y''N ªº­È¡A­Y¨â­Ó³£¿ï¬°¹s´N¬O©Ò¿×ªº Nantural Cubic Spline¡C©ÎªÌ¡A§Ú­Ì¤]¥i¥H«ü©w¨âÃäºÝÂI x1 ¤Î xN ¤Wªº±×²v¬°¬Y¯S©w­È y'1 ¤Î y'N¡A³o¬O§Q¥Î¤W¤W¤W¦¡±×²v»P y''j ªº¶¡Ãö«Y¡]§Y½Ò¥» (3.3.5) ¦¡¡^¡C

¡]¤G¡^¤W­±ªº½u©Ê¥N¼Æ¤èµ{¦¡¨Æ¹ê¤W¬O¤@­Ó²¤Æªº¯S®í§Î¦¡¡A¼g¦¨¯x°}¦¡·|¬Ý¨ì¥u¦³¹ï¨¤½u»P¨äªñ¾Fªº¤W¤U¨â±ø¤¸¯À¤£¬O¹s¡A¨ä¥L³£¬O¡C³oºØ¯x°}¥s¤T¹ï¨¤¯x°} (tri-diagonal matrix)¡A¦³±M¥Îªº§Ö³tºtºâªk¥i¥H¸Ñ¥X¡A¨äµ{¦¡¬Û·í²³æ¡A¤w¥]§t¦b spline ªº°Æµ{¦¡¤¤¡C

 

°Æµ{¦¡ªº¨Ï¥Î

¥»¸`ªº°Æµ{¦¡­n spline ¤Î splint °t¦X¨Ï¥Î¡C

SUBROUTINE spline(x,y,n,yp1,ypn,y2)
INTEGER n,NMAX
REAL yp1,ypn,x(n),y(n),y2(n)
PARAMETER (NMAX=500)

Given arrays x(1:n) and y(1:n) containing a tabulated function, i.e., yi = f(xi), with x1 < x2 < .. . < xN, and given values yp1 and ypn for the first derivative of the interpolating function at points 1 and n, respectively, this routine returns an array y2(1:n) of length n which contains the second derivatives of the interpolating function at the tabulated points xi. If yp1 and/or ypn are equal to 1 ¡Ñ 1030 or larger, the routine is signaled to set the corresponding boundary condition for a natural spline, with zero second derivative on that boundary.
Parameter: NMAX is the largest anticipated value of n.

INTEGER i,k
REAL p,qn,sig,un,u(NMAX)

spline ¥u»Ý­n³Q©I¥s¤@¦¸¡A¥¦·|§â¤½¦¡©Ò»Ýªº¤G¶¥¾É¼Æ¸Ñ¥X¨Ó¡A­n´¡­È®É´N­n©I¥s splint ¡]¥Nªí spline interpolation¡^¡A¶Ç¤Jªí¦CÂI¡Bªí¦C­È¡B¤G¶¥¾É¼Æ¡A¥H¤Î´¡­ÈÂI x¡Aµ²ªG­È¥Ñ y ¶Ç¥X¡C µ¹©w x¡A­n§ä´M¨ä¦bªí¦CÂI¨º¤@­Ó°Ï¶¡ªº¾÷¨î¤w¤º«Ø¦b splint °Æµ{¦¡¤¤¡A¦ý¥Îªº¬O bisection ¤G¤Àªk¡C

SUBROUTINE splint(xa,ya,y2a,n,x,y)
INTEGER n
REAL x,y,xa(n),y2a(n),ya(n)

Given the arrays xa(1:n) and ya(1:n) of length n, which tabulate a function (with the xai¡¦s in order), and given the array y2a(1:n), which is the output from spline above, and given a value of x, this routine returns a cubic-spline interpolated value y.
INTEGER k,khi,klo
REAL a,b,h

 

¸É¥R¸ê®Æ

An Interactive Introduction to Splines

ºû°ò¦Ê¬ì¡Ghttp://en.wikipedia.org/wiki/Spline_(mathematics)¡]Spline ³o­Ó¦rªº¥Ñ¨Ó¡^

Before computers were used, numerical calculations were done by hand. Although piecewise-defined functions like the signum function or step function were used, polynomials were generally preferred because they were easier to work with. Through the advent of computers splines have gained importance. They were first used as a replacement for polynomials in interpolation, then as a tool to construct smooth and flexible shapes in computer graphics.

It is commonly accepted that the first mathematical reference to splines is the 1946 paper by Schoenberg, which is probably the first place that the word "spline" is used in connection with smooth, piecewise polynomial approximation. However, the ideas have their roots in the aircraft and ship-building industries. In the foreword to (Bartels et al., 1987), Robin Forrest describes "lofting," a technique used in the British aircraft industry during World War II to construct templates for airplanes by passing thin wooden strips (called "splines") through points laid out on the floor of a large design loft, a technique borrowed from ship-hull design. For years the practice of ship design had employed models to design in the small. The successful design was then plotted on graph paper and the key points of the plot were re-plotted on larger graph paper to full size. The thin wooden strips provided an interpolation of the key points into smooth curves. The strips would be held in place at discrete points (called "ducks" by Forrest; Schoenberg used "dogs" or "rats") and between these points would assume shapes of minimum strain energy. According to Forrest, one possible impetus for a mathematical model for this process was the potential loss of the critical design components for an entire aircraft should the loft be hit by an enemy bomb. This gave rise to "conic lofting," which used conic sections to model the position of the curve between the ducks. Conic lofting was replaced by what we would call splines in the early 1960's based on work by J. C. Ferguson at Boeing and (somewhat later) by M.A. Sabin at British Aircraft Corporation.

The word "spline" was originally an East Anglian dialect word.

The use of splines for modeling automobile bodies seems to have several independent beginnings. Credit is claimed on behalf of de Casteljau at Citroen, Pierre Bezier at Renault, and Birkhoff, Garabedian, and de Boor at General Motors (see Birkhoff and de Boor, 1965), all for work occurring in the very early 1960s or late 1950s. At least one of de Casteljau's papers was published, but not widely, in 1959. De Boor's work at GM resulted in a number of papers being published in the early 60's, including some of the fundamental work on B-splines.

Work was also being done at Pratt & Whitney Aircraft, where two of the authors of (Ahlberg et al., 1967) ¡X the first book-length treatment of splines ¡X were employed, and the David Taylor Model Basin, by Feodor Theilheimer. The work at GM is detailed nicely in (Birkhoff, 1990) and (Young, 1997). Davis (1997) summarizes some of this material.

 

½d¨Ò¥Dµ{¦¡

program spline_main

implicit none
integer max_tabu, ngrids_max, n_tabu, i, choice
integer more_points, ngrids
parameter (max_tabu = 100, ngrids_max = 2000)
real xa(max_tabu), ya(max_tabu), y2a(max_tabu), x, y, delta_x
real x_plot(ngrids_max), y_plot(ngrids_max), yp1, ypn
real x_min, x_max
character*40 filename1

c Ask filename and the number of tabulated points in the file.

write(*,*) 'Type in the file name of tabulated values (x,y)'
read (*,*) filename1
write(*,*) 'How many points are there in the tabulated set ?'
read (*,*) n_tabu

¡@ ¡@ if (n_tabu.gt.max_tabu) stop 'Too many tabulated values,
¡@¡@ & please recompile the main program with a larger max_tabu.'

c Open the file and read in the tabulated values, and write to screen
c for double check.

open (unit = 20, file = filename1)

do i = 1, n_tabu
¡@read (20,*) xa(i), ya(i)
¡@write(*,*) xa(i), ya(i)
end do

close (20)

c Ask slopes at two ends. (If > 10E30 then natural spline is set.)

write(*,*) 'Please type in slopes at bothend (> 10E30 -> y2a=0).'
read (*,*) yp1, ypn

c Call subroutine spline to solve for y2a array.

call spline(xa, ya, n_tabu, yp1, ypn, y2a)

c One can choose to type in x one by one or to generte a continuous set
c for graph plotting.

write(*,*) 'What do you want to do ?'
write(*,*)'0:input x, find y ; 1:generate a set of points (x,y)'
read (*,*) choice

c This part get x one by one.

if (choice.eq.0) then

1002 ¡@write(*,*) 'Type in your x:'

¡@read(*,*) x
¡@call splint(xa, ya, y2a, n_tabu, x, y)
¡@write(*,*) 'Interpolated value is ', y
¡@write(*,*) 'One more point (yes=1;no=0) ?'
¡@read (*,*) more_points
¡@if (more_points.eq.1) goto 1002

endif

c This part generate a set of x.
c The resulting (x,y) is written out into a file named by user.

if (choice.eq.1) then

¡@write(*,*) 'Type your range x_initial and x-final:'
¡@read (*,*) x_min, x_max
¡@write(*,*) 'How many points do you want to plot ?'
¡@read (*,*) ngrids
¡@if (ngrids.le.1) stop 'Need at least two grids.'

¡@delta_x = ( x_max - x_min ) / (ngrids - 1)
¡@do i=1, ngrids
¡@¡@x_plot(i) = x_min + (i-1)*delta_x
¡@¡@call splint(xa,ya,y2a,n_tabu,x_plot(i),y_plot(i))
¡@end do

¡@write(*,*) 'Plrease give a name to your output file:'
¡@read (*,*) filename1
¡@open (unit=30, file=filename1)
¡@do i=1,ngrids
¡@¡@write(30,*) x_plot(i), y_plot(i)
¡@end do
¡@close(30)

end if

end