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

Interpolation and Extrapolation

 

¼Æ¾Ç°ÝÃD±´°Q

·í§Ú­Ì¦bªí²{¸ê®Æ®É¡A±`±`·|¦³»Ý­n¤ñ¹ê»Ú¶q´úÂI¤Wªº­È§ó²Ó±Kªº±¡ªp¡A©ÎªÌ¬O¦³»Ý­n¦b½d³ò¥~¹w´ú¨ä­È¡C

¤ñ¤è»¡¤Ñ®ð¹ÏªºÃ¸»s¡A¤£½×¬O®ðÀ£©Î¬O«B¶q¡A³£¤£¥i¯à°µ¨ì³B³B³£¦³´ú¶q¯¸¡A¤S¨Ò¦p§Ú­ÌÃö¤ß¤@¤Ñ¤§¤¤·Å«×ÀH®É¶¡ªºÅܤơA¦ý¬O¹ê»Ú¤W°O¿ý®ð·Åªº°Ê§@¥i¯à¥u¬O¨C¤p®É¤@¦¸¡A«h§Ú­Ì­n§@¤@­Ó³sÄòªº¹Ï®É¡A´N·|¥Î¨ì´¡­Èªk¡C

´¡­Èªkªº¤¤¤ßijÃD¬O¡G¦b§Ú­Ì¤v¨ã³Æ¤@²Õªí¦C¼Æ¡]tabulated value¡^ªº±¡ªp¤U¡A ¦p¦ó±o¥X¨S³Q©w¨ì¤§°Ï°ìªº­È¡C

¤°»ò¼Ëªº¨ç¼Æ¤~¯à³Q´¡­È¡A³o¬O¼Æ¾Ç¤W°Q½×ªº¶¡°ÝÃD¡A°Ñ¨£½Ò¤å p.99 ²Ä¥|¬q¡CµM¦Ó¡A§Ú­Ì·|­n¥Î¨ì´¡­Èªkªº³õ¦X©¹©¹³£¤£ª¾¹D´y­z¹ï¶H­I«áªº¨ç¼Æ¬O¤°»ò§Î¦¡¡]¦ý¬Û«H¨ä¦³³sÄòªº¥»½è¡^¡A¦]¦¹§Ú­Ì¤]¥u¯àºÉ¤O¨D¯u¹ê¡C

¨Ï¥Î´¡­Èªk©Ò«Ø¥ßªº¨ç¼Æ¡A¦bªí¦CÂI¤W¤@©w­n­«²{­ì¥»µ¹©wªºªí¦C­È¡A§_«h´N¤£¬O´¡­Èªk¦Ó¬O¨ç¼Æªñ¦ü©Î¦±½uÀÀ¦Xªº¶¡°ÝÃD¤F¡A¥¦­Ì¬O¤£¤@¼Ëªº¡C

´¡­Èªº§@ªk¡A«Üª½Æ[¦a¨ÓÁ¿¡A´N¬O¡A(1) ¥ý±qªí¦C­È¨ÓÀò±o¨ç¼Æ f(x)¡A¦A (2) ¥Î¨ç¼Æ f(x) ¨D¥X§Ú­Ì©Ò­nªº¥ô¦ó¯S©w x ¤§ f(x) ¨ç¼Æ­È¡CµM¦Ó¡A¤ñ¸ûºë±K¥B¨t²Î¤Æªº¼Æ­È¤èªk«o¤£¬O¥Î³o¨â­Ó¨BÆJ¨Ó¶i¦æ´¡­È¡A­ì¦]¬O«e­z¨â¶¥¬q¤èªk¹ï©ó´¡­Èªººë±K«×¨Ã¨S¦³±±¨î¡A®Ä²v¸û®t¡A¤]¤ñ¸û·|¦³¶i¦ì»~®t¡C¤@¯ë¦b°µ´¡­Èªk¡A¬O±q±ý´¡­ÈÂI x ªþªñªº´X­Óªí¦CÂI xi ¶}©l¡A«Ø¥ß´¡­È¨ç¼Æ f(x)¡A¨Ã¥B¤]¸ÕµÛºôù§ó¦hªí¦CÂI¨Ó´¡­È¡A¬ÝÀHµÛ¶µ¼ÆÅܦh»~®t·|¤£·|Åܤp¡A¦p¦¹§ä¥X³Ì¾A¦Xªº¨ç¼Æ f(x)¡C

§Ú­Ì·|¤ñ¸û§Æ±æºtºâªk¦b±qªí¦C­È«Ø¥ß´¡­È¥Î¨ç¼Æ®É¡A¤]¯à´£¨Ñ»~®t¤ÀªR¥H¨Ñ§Ú­Ì©Îµ{¦¡¨Ó§PÂ_¡C²¦³º¥i¥Îªº´¡­È¨ç¼Æ f(x) ¨Ã«D°ß¤@¡A¦Ó§Y«K¬O¤v³]©w¤F±Ä¥Î¤@ºØ¤èªk¡A¦p¦h¶µ¦¡ªk¡A¤]·|¦³¸Ó¨Ï¥Î¦h¤Ö¶µ¤~³Ì«ê·íªº°ÝÃD¡C

«Ø¥ß´¡­È¨ç¼Æ©Ò»Ý¤§¾Fªñªí¦C­È­Ó¼Æ¡A§Ú­ÌºÙ¤§¬°´¡­Èªkªº order¡]¶¥¡^¡A¸û°ª¶¥¥¼¥²«OÃÒ±o¨ì¸û¦X²zªº´¡­È¡A³oÂI¦b¦h¶µ¦¡´¡­Èªk¤×¨ä¦p¦¹¡A­n¤p¤ßª`·N¡C¸Ô¨£½Ò¤å¤¤¤§¨Ò¹Ï

¤W¨â¹Ï¹ê½u³£¬O­ì²{¶H­I«áªº¯u¥¿­È¡Aµuµê½u¥Nªí§C¶¥¦h¶µ¦¡´¡­Èµ²ªG¡Aªøµê½u¥Nªí°ª¶¥¦h¶µ¦¡´¡­Èµ²ªG¡C©úÅã¥i¨£¡Acase (a) °ª¶¥ªÌ¸û·Ç½T¡A¦Ó case (b) «h¬O§C¶¥¸û·Ç½T¡C

 

 

½u©Ê´¡­Èªk¡]Linear Interpolation¡^

©Ò¦³ªº´¡­ÈªkùØ­±³Ì²³æªº²ö¹L©ó½u©Ê´¡­Èªk¡A¥ô¨â­Ó¬Û¾Fªºªí¦CÂI¤§¶¡¥²¥i¥H©Ô¤@±øª½½u§â¥¦­Ì³s°_¨Ó¡A¦p¦¹¦b¤§¶¡ªº x ­È´N³£¦³½u©Ê¨ç¼Æ y(x) ¥i¥H¹ïÀ³¨ì¡A§Q¥Îª½½u¤Wªº±×²v¥²¬°©T©w­Èªº¯S©Ê¡A¨ä¤½¦¡¬O¡]¥H (x1,y1)¡B(x2,y2) ¬°¨â­Ó¬Û¾Fªºªí¦CÂI¬°¨Ò¡^¡G

(y - y1) / (x - x1) = (y2 - y1) / (x2 -x1)

¸g¾ã²z«á±o

y = [ (y2 -y1) / (x2 - x1) ] (x - x1) + y1

ª`·Nµ¥¸¹ªº¥kÃä¥þ¬O x »P±`¼Æ¡A§Ú­Ì¦]¦¹¦³¤F y(x) ªº©ú½T¤½¦¡¥i¥Î¡C

§Ú­n¨D¤j®a¹ï©ó½u©Ê´¡­Èªk³oºØ¸û²³æªº´¡­Èªk¡AÀ³¸Ó­n¯à¦b¤£¬Ý°Ñ¦Ò¸ê®Æªº±¡ªp¤U°µ¥X¡A§Y¦Û¦æ§â¦¡¤l¼g¤U¨Ó¡A¨Ã¥B§âµ{¦¡¼g¥X¨Ó¡C

 

 

¦h¶µ¦¡´¡­Èªk

¤j®a³£ª¾¹D¨âÂI°ß¤@¨M©w¤@±øª½½u¡]¤£ÂàÅs¡^¡B¤TÂI°ß¤@¨M©w¤@±ø¤G¦¸¦±½u¡]·|Âà¤@¦¸Ås¡^¡B¥|ÂI°ß¤@¨M©w¤@±ø¤T¦¸¦±½u¡]·|Âà¨â¦¸Ås¡A¦³¤Ï¦±ÂI¡^¡Aµ¥µ¥¡C³o¨Ç¦±½u³£¬O¥H¦h¶µ¦¡ªº§Î¦¡¡]ÅܼƥX²{®É¡A¨Ç¬O¾ã¼Æ¦¸¤è¡^¡C

¤@­Ó n - 1 ¦¸¦±½uªº¦h¶µ¦¡Áö¦³¹³ y = a(n-1)x(n-1) + a(n-2)x(n-2) + .... + a1x +a0 ³o¼Ëªº³q¦¡¥i¥Hªí¥Ü¥X¡A¦ý¥²¶·¥N¤J n ­Óªí¦C­È¤~¯à©w¥X an-1 ¦Ü a0 ¨º n ­Ó«Y¼Æ¡A¤@¤U¤l¤£©ö¬Ý¥X¡C

¼Æ¾Ç¤W¦³¤@­Ó Lagrange ¦h¶µ¦¡¤½¦¡¡A¥¦¥i¥H¥Ñ n ¹ï (x,y) ­È°ß¤@¨M©w n-1 ¶¥¦h¶µ¦¡¡A¥B¤½¦¡«D±`¦n°O¡A ¦p½Ò¤å¤¤ªº¦¡ (3.1.1)

°Oªk°Ñ¦Ò¡G¼g¤U¨C¤@¶µ³£¦³«Y¼Æ yi¡A¤À¥À¥þ¬O (xi - xj)¡A¨ä¤¤ i ¡Ú j¡A¤À¤l«h¥þ³¡³£¬O (x - xj)¡A¤@¼Ë¬O i ¡Ú j ¡A³o¼Ëªº¶µ¦@¦³ N ­Ó¬Û¥[¡C§Ú­Ìªº¦¡¤l³Ì°ª¶µ¦¸¤@©w¬O xN-1¡A²Å¦X (x - xj) ³s­¼¡A¨Ã¥B·í x «ê¬°¬Y¤@­Ó tabulated point xi ®É¡Ayi ·|¦]¤À¤l¤À¥À¤@¼Ò¤@¼Ë©è®ø¦¨ 1 ¦Ó¯d¤U¨Ó¡A¦Ó¨ä¥L¨ã yj ªº¶µ«h¦]¨ä¤À¤l¥²©w¦³ (x - xi) ¦Ó¦¨¬°¹s¡A¦]¦¹ y = yi¡A²Å¦Xªí¦C­Èªº©w¸q¡C

¦³¤F¤W­± Lagrange polynomial ªº©w¸q¡A®³¨Ó¼g­Óµ{¦¡¡A¹ï¥ôµ¹ªº x ¨D´¡­È¡A¤]¨Ã«D¤£¥i¥H¡A¥u¬O³o¼Ë´N¤Ö¤F»~®t¤ÀªR¤Îºë±K«×±±¨îªº°Ê§@¡C

¯u¥¿¦³¥Îªººtºâªk¡A¬O Neville's ºtºâªk¡C¥¦©w¥X Pi ¬°­ì¥»ªí¦C­È yi¡A¦Ó Pi(i+1) «h¥Nªí¤@¯ëªí¦CÂI¬O xi »P xi+1 ©Òºc¦¨ªº¤@¶¥ Lagrange ¦h¶µ¦¡¡C

Neville's algorithm ¼F®`ªº¦a¤è¡A¬Oµo²{¥Ñ§C¶¥¦h¶µ¦¡¡A¥i¥H¸g¥Ñ²Õ¦X¦Ó¨t²Î¤Æ¦a±o¥X§ó°ª¶¥¦h¶µ¦¡¡]¨Ò¦p¦Û P12 ¤Î P23 ±o¥X P123¡^¡A®Ú¾Ú¤U¦CÃö«Y¦¡¡G

¥t¥~¡A¤£¦P¯Å¼Æ¶¡ªº¤j¤p®t²§¡A¤]¤è«K¦a©w¸q¤FC »P D ¦p¦ó±q P ±o¨ì¡G

¶i¤@¨B±À¾É¡AC »P D §ó¥i¥H±q«e¤@¯Åªº C »P D ±o¨ì

¦p¦¹¡A§Ú­Ì¦A¦^¥h¬Ý¤W­±¨º­Ó¾î¦Vª÷¦r¶ð¡A¤ñ¤è»¡·Q­n±o x ªº P1234¡A¥i¥Ñ §C¯Å¼Æªº P ¥Xµo¡A³z¹L C ©Î D ªº¨D±o¨Ãªþ¥[¨ì P ¤W¡A¨ÓÀò±o¸û°ª¯Å¼Æªº P¡]§Y¦b ¾î¦Vª÷¦r¶ð ¤¤¥Ñ¥ª¦Ó¥k¡^¶V°µ¶Vºë±K¡A ¨Ã¥B¥i¤@¸ô¤W°lÂܤ£¦P¶¥¼Æ¦h¶µ¦¡¤§¶¡ªº»~®t«×¡C

½Ò¥»´£¨Ñªº°Æµ{¦¡¬O polint¡Aª`·N§A©TµM¥i¥H¦³¦h¤Ö­Óªí¦CÂI´N¦h¤Ö­Ó¥þ¥Î¡]¸û¦MÀI¡^¡A§A¤]¥i¥H¥u¿ï¨ä¤¤ m ­Ó¡]¤ñ¤è»¡ 4 ¡^¨Ó¥Î¡A¥u¿ï¨ä¤¤ 4 ­Óªº©I¥s°Æµ{¦¡¤èªk¡A¥H tabulated values ¬O 18 ²Õ xx »P yy ¬°¨Ò¡A­n¥Î¨ìÂI 15 ~ 18 ªº§@ªk¬O¡G

call polint( xx(15), yy(15), 4, x, y, dy )

¦Ó¦³§O©ó¥þ³¡ 18 ²Õ³£¨Ï¥Îªº

call polint( xx, yy, 18, x, y, dy )

´£¿ô¤j®a¡A¦b Fortran ªº»yªkùØ¡A call polint( xx, yy, 18, x, y, dy ) ´N¬O call polint( xx(1), yy(1), 18, x, y, dy ) ªº·N«ä¡A§Y©I¥s°Æµ{¦¡©Ò¶Çªº¤Þ¼Æ¬O°}¦C©ÎÅܼƪº°_©l¦ì¸m¡C

 

 

¦p¦ó·j´M¦³§Çªºªí

§Ú­Ì«e­±¤w¸g«Ø¥ß¤F±q¨â­ÓÂI°ß¤@¨M©w¤@±øª½½uªº½u©Ê´¡­Èªk¡A ¨º»ò¦b¤wª¾¤@¨t¦Cªí¦CÂIªº±¡ªp¤U¡A³Q­n¨D­n´¡­È¬Y x ÂI¤W¨D y¡A¦ÛµM§Ú­Ì¥²»Ý¨ú¥Î xi < x < xi+1 ªº¨º¨â­ÓÂI ( xi , yi ) ¤Î ( xi+1 , yi+1 ) ¨Ó°µ½u©Ê´¡­È¡C²³æªº»¡¡A²{¦bªº°ÝÃD¬O¡Aµ¹©w x¡A¦p¦ó§ä¨ì i ¡H

§Ú­Ì¥i¥H·Q¹³¡A­Y§âµ{¦¡¼g¦¨±q³Ì¤p©Î±q³Ì¤jªºªí¦CÂI¶}©l»P x ¤ñ¹ï¡A¸U¤@ x ­ÈÂ÷¨ººÝ«Ü»·´N·|¨S¦³®Ä²v ¡C½Ò¥»´£¨Ñ¤F¤G¤Àªk (bisection) ªº¤èªk¡A­º¥ý¥ý§PÂ_ x ¦³¨S¦³¤p©ó x1 ©Î ¤j©ó xN¡A­Y½T©w x ¦b¨ä¤¤«h®³¨ä¤¤¶¡ÂI xN/2 ¡]­Y N «D°¸¼Æ´N¥Î N+1¡^©Î¨Ó»P x ¤ñ¡A§PÂ_¥X x ¬O¦b x1 »P xN/2 ¤§¶¡©Î¬O xN/2 »P xN ¤§¶¡¡AµM«á­«Âе¦²¤¡A¨C¦¸³£¬O¨ú¥Î·s¤W¤U­­ªº¤¤¶¡ÂI¥h·j´M ¡C½Ò¥»´£¨Ñ locate °Æµ{¦¡µ¹ÅªªÌ§@¤G¤Àªk·j´M¡A¸Ô¨£¤§¡C ¥H¤U¹Ï¨Ò¡G

¦pªG§Ú­Ì­n°µ¤@¨t¦C¬Û¾F´¡­ÈÂIªº´¡­È¡A¤ñ¤è»¡­n°µ¹Ï²£¥Í¹ÏÂI¥Î¡A«h¨C­Ó·sÂI·|¾Fªñ©óÂÂÂI¡C­Y¨C¦¸³£¥Ñ³Ì¤j½d³òªº¤W¤U­­¶}©l¥Î¤G¤Àªk¡A«h·|ªá«Ü¦h­ÞªP¤u¡C¦³®Äªºµ¦²¤À³¬O¡A¨C¦¸§äªí¦CÂI®É¡A±q¤W¤@¦¸Àò±oªºªí¦CÂI¶}©l¡A¦p¦¹¦³³Ì¤jªº¾÷·|©R¤¤¡A­Y¤p©ó x¡A«h¥H¦¸¨â­¿¨B´TÅܤj¸õÅD¦V¥k´M§ä¡Aª½¨ì§ä¨ìªºÂI¤ñ x ¤j¡A¦A§ï¥Î bisection¡A³o¼Ë¸û¦³®Ä²v¡C ½Ò¥»´£¨Ñ hunt °Æµ{¦¡°µ¤W­z¤u§@¡A¸Ô¨£¤§¡C ¥H¤U¹Ï¨Ò¡G

³Ì«á¡AÁÙ¦³¤@­Ó°ÝÃD¡GÁöµM¥Î locate ©Î hunt ¥i¥H¥Ñ x ±o j¡A¨ä¤¤ j »P j+1 ªí¦CÂI·|§â x ¥]§¨¦í¡A¦ý¹³¬O¦h¶µ¦¡´¡­Èªk¡A­Y§Ú­Ì¤@¦¸­n¥Î¡]¤@¦@¬O N ­ÓÂI¤¤ªº¡^m ­ÓÂI¡]m ¤ñ¤è»¡¬O 4¡^¡A¯à¨Ï¥Î j-1¡Bj¡Bj+1¡Bj+2 ·íµM«Ü¦n¡A¦ý­Y j ¤Ó¾aªñ¨â°¼¡A¨Ò¦p j ¬O 1 ©Î j+1 ¬O N ªº¸Ü¡Aj ´N¤£¯à¿ï§@¬O¨º m ­ÓÂIªº¤¤¶¡ÂI¤F¡A³o¼Ë­n¦p¦ó³B²z¡H µª®×¡G¨Ï¥Î¤U¦C«ü¥O¡A°w¹ï j¡Bm¡BN ¥h§@¹Bºâ¡A«h·|§âÃäÃä½Õ¨ì­è­è¦n¡A¬°¤°»ò¡H¥i¦Û¤v·Q¤@·Q¡C

k = min( max(j-(m-1)/2,1) , N+1-m )

©I¥s°Æµ{¦¡®É¡A¹³³o¼Ë

call polint( xx(k), yy(k), m, x, y, dy )

 

 

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

(1) ª`·N polint¡Bhunt ©Î»P locate ªº°t¦X¡C

(2) ¨Ï¥Î polint ®É¡Aª`·N¸Ó°Æµ{¦¡¤º¤v¦Û³]¤W­­ NMAX = 10 ¬O³Ì¤j¥i¥Îªº n ­È

SUBROUTINE polint(xa,ya,n,x,y,dy)
INTEGER n,NMAX
REAL dy,x,y,xa(n),ya(n)
PARAMETER (NMAX=10) Largest anticipated value of n.

Given arrays xa and ya, each of length n, and given a value x, this routine returns a
value y, and an error estimate dy. If P(x) is the polynomial of degree N ? 1 such that
P(xai) = yai, i = 1, . . . , n, then the returned value y = P(x).

INTEGER i,m,ns
REAL den,dif,dift,ho,hp,w,c(NMAX),d(NMAX)

(3) ­n¨ã³Æ¨Ï¥Î´¡­Èªk¨Ã©I¥s pgplot °Æµ{¦¡§@¹Ïªº¯à¤O¡C

 

 

½d¨Ò°Æµ{¦¡¡]¤E¤Q¤C¾Ç¦~«×«á°l¥[¡^

¥H¤U´£¨Ñ¦p¦ó¨Ï¥Î¥»¸`¤§°Æµ{¦¡ªº½d¨Ò¥Dµ{¦¡¡G

­º¥ý²Ä¤@­Ó¨Ò¤l¡A³o­Ó½d¨Ò´£¨Ñ polint ´¡­Èªº¨âºØ¥i¯à¥Îªk©ó¦P¤@­Ó¥Dµ{¦¡¤¤¡A¤@­Ó¬O¨Ï¥ÎªÌµ¹¤@­Ó x¡Aµ{¦¡´Nµ¹¤@­Ó y¡F¥t¤@­Ó¤èªk¬O°Ý½d³ò¤ÎÂI¼Æ¡AµM«áµ{¦¡¦b¦¹½d³ò²£¥Íµ¥¶¡¶Zªº²Ó±K´¡­ÈÂI¡]¾A¦X§@¹Ï¥Î¡^¡C

program polint_main

c In subroutine polint, array declaration is used for xa(n) and ya(n),
c for this we need to reclare larger or equal size xa(n) and ya(n) in
c this main program. As for NMAX and c(NAMX) and d(NAMX) in that routine,
c they are taken care locally in that routine alreay with specific size
c (parameter) given when declaring array, therefore we don't need to deal
c with that.

implicit none
integer max_tabu, ngrids_max, n_tabu, m, i, j, k, choice
integer k1, k2, more_points, ngrids
parameter (max_tabu = 100, ngrids_max = 2000)
real xa(max_tabu), ya(max_tabu), x, y, dy, delta_x
real x_plot(ngrids_max), y_plot(ngrids_max),dy_plot(ngrids_max)
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 the order of polynomial to be usedfor interpolation, not higher
c than 10.


1000 continue

write(*,*) 'How many points do you want to form polynomial ?'
write(*,*) 'For eample, 2 points mean linear interpolation.'
read (*,*) m

if (m.gt.10) then
write(*,*) 'Waring : Neumarial Recipe default max is 10.'
write(*,*) 'Please decress your value or set a bigger NMAX'
write(*,*) 'in the subroutine polint.'
write(*,*) 'Your choice (0 : exit) or (1: input again).'
read (*,*) choice
if (choice.eq.0) stop 'Terminated by user.'
if (choice.eq.1) goto 1000
goto 1000
endif

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, using "locate" to find the right interval.

if (choice.eq.0) then

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

read(*,*) x
call locate (xa, n_tabu, x, j)
k = min( max(j-(m-1)/2,1) , n_tabu+1-m )
call polint(xa(k), ya(k), m, x, y, dy)
write(*,*) 'Interpolated value is ', y, ', error is ', dy
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, using "hunt" to find the right interval.
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)
j = 1
do i=1, ngrids
x = x_min + (i-1)*delta_x
call hunt(xa, n_tabu, x, j)
k = min( max(j-(m-1)/2,1) , n_tabu+1-m )
x_plot(i) = x
write(*,*) 'x is ',x_plot(i),' j is ',j,' k is',k
call polint(xa(k),ya(k),m,x_plot(i),y_plot(i),dy_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)

write(*,*) 'Info: Error estimate will be written into file
&error.dat'
open (unit=40, file='error.dat')
do i=1,ngrids
write(40,*) x_plot(i), dy_plot(i)
end do
close(40)

end if

end

 

¥t¥~¡A·í§A­n§Q¥Î´¡­Èªk¨Ó°t¦Xø¹Ï¡A¥H¤U¬O¤@­Ó½d¨Òµ{¦¡¡G

program polint_pgplot

integer ntab_max, nplot_max
parameter (ntab_max = 20, nplot_max = 200)
real x_tab(ntab_max), y_tab(ntab_max), x_min, x_max, delta_x
real x_plot(nplot_max), y_plot(nplot_max), dy_plot(nplot_max)
integer ntab, i, nplot, m, j, pgopen
character*40 filename1

write(*,*)'Please tell me the number of tabulated values:'
read(*,*) ntab

write(*,*) 'Which filename contains the tabulated value ?'
read(*,*) filename1

open(unit=10, file=filename1)
do i=1,ntab
read(10,*) x_tab(i), y_tab(i)
end do
close(10)

write(*,*) 'Type in min and max of x:'
read(*,*) x_min, x_max

write(*,*) 'How many points to draw ?'
read(*,*) nplot

write(*,*) 'How many point to form polynomial ?'
read(*,*) m

delta_x = (x_max - x_min) / (nplot - 1)

do i=1, nplot
x_plot(i) = x_min + (i-1) * delta_x
call hunt(x_tab,ntab,x_plot(i),j)
k = min( max(j-(m-1)/2,1) , ntab+1-m )
call polint(x_tab(k), y_tab(k), m, x_plot(i), y_plot(i),
& dy_plot(i))
end do

do i=1,nplot,10
write(*,*) 'x_plot and y_plot,', x_plot(i), y_plot(i)
end do

if ( pgopen('?') .le. 0 ) stop
call pgenv(2.0, 6.0, 0.0, 40.0, 0, 1)
call pgline(nplot, x_plot, y_plot)
call pgpt(ntab, x_tab, y_tab, 9)
call pgclos

end