¨ç¼Æ¨D®Ú¡G°ÝÃD­åªR¡B¦a´à¦¡·j´M¡G¥]³ò¡B¤G¤Àªk

 

°ÝÃD­åªR

ª`·N¨ç¼Æ»P¤èµ{¦¡©w¸q¤Wªº®t§O

¨ç¼Æ¡G¬O¤@ºØ¬M®g¡A¥u­n¤£¬O¤@¹ï¦h¡]¹³¨¬¦b¥u­n¤£¬O¤@¹ï¦h¡]¹³¬O¤@¹ï¤@¡B¦h¹ï¤@¬Ò¥i¡^

¤èµ{¦¡¡G²³æªºÁ¿´N¬O¦³µ¥¸¹ªº¦¡¤l¡C

¦pªG°ÝÃD¤¤¥u¦³¤@­Ó¦ÛÅܼơ]¤]´N¬O¤@ºûªº°ÝÃD¡^¡A¨º³o³q±`³Q¥s°µ¬O¨ç¼Æ¨D®Ú¡C

¤èµ{¦¡¨D¸Ñ»P¨ç¼Æ¨D®Úªº°ÝÃD¦³®É¦b§Î¦¡¤W«Ü¹³¡A¨Ò¦p¹³ f(x) = 0 ³oºØ¼Ë¤l¡C ­n¸Ñ f(x) = 0 ³o±ø¤èµ{¦¡¡A´N¬Û·í©ó¹ï¨ç¼Æ f(x) ¨D®Ú¡A¤£¹L©w¸q»P³¯­z¤è¦¡¤Wªº¤£¦P¡]¤èµ{¦¡¨D¸Ñ¡B¨ç¼Æ¨D®Ú¡^¤j®aÁÙ¬O­nª`·N¤@¤U¡C

¦pªG¬O¦h­ÓÅܼƻP¦h±ø¤èµ{¦¡¡A«h¥i¥Î¦V¶q§Î¦¡Â²¼g¬° f(x) = 0 ¡]³oùزÊÅé¦r¥Nªí¦V¶q²Å¸¹¡^¡A«h°ÝÃDªº½ÆÂø«×´N°ª»È¦h¤F¡C

 

Áô¨ç¼Æ²z½×

N ­Ó¿W¥ßªº¦ÛÅܼƦ³§Æ±æ¥i¥H¤@¦¸º¡¨¬ N ±ø¤èµ{¦¡¡Cª`·N¡A¥u¬O¦³§Æ±æ¡A¦]¬°«D½u©Ê¤èµ{²Õ¦³¥i¯à®Ú¥»µL¸Ñ¡C¡]½Ð½Æ²ß¦^·Q¤@¤U¤°»ò¬O½u©Ê¡A¤S½u©Ê¤èµ{²Õ¨ä¦³¸ÑµL¸Ñ¦p¦ó§P©w¡C¡^

N ­Ó¿W¥ßÅܼƩҺ¡¨¬ N ±ø¤èµ{¦¡ªº¸Ñ "¤@¯ë¦Ó¨¥" ·|¬O¤@­Ó­Ó¡]§Y¤@²Õ²Õ¡^¿W¥ßÂI¦Ó¨S¦³³s¦b¤@°_ªº¡]¤Ï¨Ò¡G³æ¤@±ø¤èµ{¦¡ f(x) = x12 + x22 = 0 ªº¸Ñ¦bªÅ¶¡¤¤§Î¦¨¤@­Ó¶ê¡^¡C°£«D­è¦n¹J¨ì¬Û·í¯S®íªºÂ²¨Öª¬ªp¡A¤~·|¦³¤@¾ã±ø³sÄò¸Ñ¥X²{¡C

f(x) = 0 »P f(x) = 0 ÁöµM§Î¦¡«Ü¹³¡A¦ýÃø«×¦³¤ÑÄ[¤§§O¡A¥D­n¬O®t¦b¡A·Q¨D¸Ñ«eªÌ¡A¥i¥H¥Î¨D¨ç¼Æ­È¤§¥¿­t¥ÎÀY§À¹Gªñªº¤èªk¡AµM¦Ó¤@¥¹¬O°ªºû«×¡A¸Ñ´N¨S¦³¿ìªk¥ú¾a¨D¨ç¼Æ­È´N¨Ó§@§PÂ_±o¨ì¤F¡C¦]¦¹¡A¨ç¼Æ¨D®Ú¡]©Î¤@¯ë¥N¼Æ¤èµ{¦¡¡A§Y¤£§t·L¿n¤Àªº¤èµ{¦¡¡^»Ý­n³z¹L¦³®Äªººtºâªk¨Ó§ä¥X¡A¦Ó¥B³£¬O»Ý­nÂǥѤÏÂЭ¡¥Nªº¤èªk¡]¥u¦³½u©Êªº°ÝÃD¤~¥i¯à¤£»Ý­n¤ÏÂЭ¡¥N¡^¡C

¤£¹L¡A¬°Â²³æ¤J¤â°_¨£¡A§Ú­Ì¥ý±q¤@ºûªº°ÝÃD¶}©lÁ¿¡C

 

¦a´à¦¡·j´M

¨ãÅéµ¹©w¤@­Ó¨ç¼Æ f(x)¡A·Q­nª¾¹D¨º¨Ç x ·|¨Ï±o f(x) = 0¡A¦b¦³¹q¸£¨ó§U§Ú­Ìªº±¡ªp¤§¤U¡A¤@­Ó«Üª½Æ[¤èªk¬O¡A­n¹q¸£²£¥Í«D±`¦h­Ó¥B±K¶°ªº x ­È¡A³v¤@¥N¤J¤wµ¹¨ç¼Æ§Î¦¡ªº f(x) ¤§¤¤¡A¬Ý¨º¤@¨Ç x' ­È·|¨Ï f(x') ­È·¥¬°±µªñ¹s¡A³o¨Ç x' ­È´N¬O®Úªºªñ¦üµª®×¡]¤j®a¤£­n§Ñ°O¡Aµ´¤j³¡¤À¼Æ­È¤èªk³£¥u¬O¨ó§U§Ú­ÌÀò±oªñ¦üªºµª®×¡^¡C

 

¨Ï¥Î¼Æ­È¤èªk¨D®Úªºµ¦²¤¤Îª`·N¨Æ¶µ

¾¨¥i¯à¹ï·Q¨D®Úªº¨ç¼Æ¦³°ò¥»ªº»{ÃÑ

§@ªÌ±j½Õ¹ï©ó¤@ºû°ÝÃD¡A¤@©w­n¾¨¥i¯à¥ý¹ï®Úªº¤À§G¦³¤@ÂI¤F¸Ñ¡C¦p¦¹¸U¤@¦³§ä¨ì¿ùªº®Ú¡B©Î¬O¦]¬°¥»¨Ó¦b¨ºùØ´N¨S¦³®Ú©Ò¥H¨S§ä¨ì¡A§í©Î¬Oªì©l²q´ú­ÈÂ÷¤Ó»·¦Ó¨S¦³§ä¨ì¡A¹J¨ì¤W­zª¬ªp¤~·|¦³§U©ó¶i¤@¨B³B²z»P§PÂ_¡C§@ªÌ¤Þ¥Î Hamming ªº®æ¨¥»¡¡G" The purpose of computing is insight, not numbers." ¡]Richard W. Hamming ¬O¹ï¹q¸£¬ì¾Ç»P³q°T°^Äm«Ü¦hªº¼Æ¾Ç®a¡A¤]¼g¤F¦h¥»¨ü¦nµûªº®Ñ¡A¨ä¤¤¤@¥»¬O¡GNumerical methods for scientists and engineers¡C¡^

ªì©l²q´ú«Ü­«­n¡A·|¼vÅT¨ì¬O§_§ä±o¨ì®Ú¡]¤×¨ä¬O¦b¦hºû«×ªº°ÝÃD¡^¡A¥H¤Î©Òªáªº®É¶¡¡C§@ªÌ´£¿ôŪªÌ¡A¦b³B²z¤@ºû°ÝÃD®É¡AÀ³¥ý§â¸Ñ¥]³ò°_¨Ó¡]bracket the roots) ¡A¤~¨Ï¥Î­¡¥N¤èªk¥h·j´M®Ú¡C

¦³¤H¥i¯à·|°Ý¡A­Y¦ÛÅܼƪº½d³ò¬O ­tµL­­¨ì¥¿µL­­¡A«ç»ò¥i¯à¥]³ò±o§¹¡H³o¤]´N§ó¬ðÅã¤F¹ï°ÝÃD¥»½è¥ý¦æ¤F¸Ñªº­«­n¡A¥t¥~¡A¹ï©ó¯u¹ê¬ì¾Ç©Î¤uµ{À³¥Î¤Wªº°ÝÃD¡A¦ÛÅÜ¼Æ x ªº½d³ò©¹©¹¤w¸g±q°ÝÃDªº¥»½è¤¤´N¤w¸g¬O½T©w©Î­­©wªº¤F¡C

 

¹ï¤@­Ó¨ç¼Æªº¼Ë¤l¹w¥ý¤F¸Ñªº­«­n

§@¹Ï¹wÄýªk

½Ò¥»´£¤F¨Ñ¤F¤@ºØ¥u»Ý­n¤å¦r¿Ã¹õªº²×ºÝ¾÷´N¥i¥Hµe¥X¨ç¼Æ¦b©Òµ¹©w¤W¤U­­½d³òªº°Æµ{¦¡¡A¥s scrsho (screen show)¡C¥¦¨Ã¥B·|½Õ¸` y ¶bªº¤Ø«×¤Î¤W¤U­­¨Ó½T«O¥þ³¡ªº¨ç¼Æ¹Ï§Î¡]¦b¸Ó¦¸©Ò¿ï©wªº½d³ò¤º¡^³£¥i¥H¬Ý±o¨ì¡C

°Æµ{¦¡ scrsho

SUBROUTINE scrsho(fx)
INTEGER ISCR,JSCR
REAL fx
EXTERNAL fx
PARAMETER (ISCR=60,JSCR=21)
Number of horizontal and vertical positions in display.
For interactive CRT terminal use. Produce a crude graph of the function fx over the
prompted-for interval x1,x2. Query for another plot until the user signals satisfaction.

INTEGER i,j,jz
REAL dx,dyj,x,x1,x2,ybig,ysml,y(ISCR)
CHARACTER*1 scr(ISCR,JSCR),blank,zero,yy,xx,ff

ª`·N­nÂI¡G¨ç¼Æªº¨ãÅé©w¸q¬O­n¦b¤W¤@¼h¥ý©w¦n¡A¦³¨Ç compiler ¦³¤ä´©ª½±µ¥Î

fx(x) = x**2 + 3*x + 2.0

¼g¦b¥Dµ{¦¡ùسo¼Ëªº¤è¦¡¡C

¦³¨Ç compiler ¤£¤ä´©¤W­z¤è¦¡¡A«h½Ð§ï¥Î¼Ð·Ç§@ªk¡A§@ªk¬O¦Û¦æ©w¸q function¡A¥H scrsho_main.f ¬°¨Ò¡]­n½Ð¤j®a¯S§Oª`·Nªº¦a¤è¥H±m¦â¼Ð¥X¡^¡G

program scrsho_main
real fx
external fx
call scrsho(fx)
end

function fx(a)
real fx, a
fx = a**2
end

¦A¤U gfortran -o scrsho_main.x scrsho_main.f scrsho.f §Y¥i²£¥Í°õ¦æÀÉ scrsho_main.x

 

 

¥]³òªk»P¤G¤Àªk (Bracketing and Bisection)

 

«e­z¦a´à¦¡·j´Mªºª½Æ[µ¦²¤¦³¤@­Ó¯ÊÂI¡A´N¬O¦p¦ó¨ú "¨¬°÷¦h" ªº x ÂI¡A¤£¬O¤@¥y "¾¨¥i¯à¦h¨ú¤@¨Ç" ´N¦n¤F¡C«Ü­«­nªº¬O¡AÀ³¸Ó­n¦³¤@­Ó¦³®Ä²vªºµ¦²¤¡]ºtºâªk¡^¡A¤@ºØ«Ü®e©ö¥i¥H·Q¨ìªº¡A´N¬O§Q¥Î³sÄò¨ç¼Æ¥H¤Uªº¯S©Ê¡G

¦pªG f(x) ¬O¤@­Ó³sÄò¨ç¼Æ¡A¨Ã¥B¨â­ÓÁ{ªñªº x' »P x' + Dx ·|¨Ï±o f(x') »P f(x' + Dx) ²Å¸¹¬Û²§¡A«h x' ¨ì x' + Dx ¤§¶¡¥²¦³¤@¸Ñ x'' ¨Ï±o f(x'') = 0¡C³o¬O¨Ó¦Û¤¤¶¡­È©w²z¡]intermediate value theorem¡^¡C

 

¥]³òªk (bracketing)

¹ï©ó¤@ºûªº³sÄò¨ç¼Æ¡A§Ú­Ì¥i¥H§Q¥Î¤W­z¯S©Ê¼g¤@­Óµ{¦¡¨Ó§ä¥X®Ú¬O·|¦b¨º¤@¨Ç°Ï¶¡¡A¦ý­º¥ýÅý§Ú­Ì¬Ý¤@¤U´XºØ§ä®Ú®É·|¹J¨ìªºª¬ªp¡A³o¨Ç¨Ò¤l´£¿ô§Ú­Ì¦b³]­p¥H¥]³òªº¤èªk¨Ó®Úªº®É­Ô­n¯S§O¤p¤ß¡G

¨ä¤¤ (a) ¬O«Ü¼Ð·Çªº²z·Qª¬ªp¡B(b) ¨úªº½d³ò³£¨S¦³Åܸ¹ªº¡A¦ý°£¤F²Ä¤@¬q¬O¯uªº¨S¦³®Ú¤§¥~¡A²Ä¤G¬q¬O­«®Ú³]¨S¦³°»´ú¨ì¡A²Ä¤T¬q¬O¨â­Ó¾Fªñªº®Ú¨S¦³°»´ú¨ì¡F(c) ¦³¥i¯à¨ç¼Æ¤§¸Ñªº¤À§G«Ü¯S®í¡A¦b¬Y¨Ç°Ï°ì·|¦³«Ü±K¶°ªº®Ú¡F(d) §Y«K¬O¦³Åܸ¹¡A¤]¦³¥i¯à¨S¦³®Ú¡A¹³ f(x) = 1/(x-c) ³o¼Ëªº¨ç¼Æ¡C

§@ªÌ¦A¦¸´£¿ô¤j®a¡A¦pªG§A¹ï«Ý¨D®Úªº¨ç¼Æ¤@µL©Òª¾¡]©ÎªÌ»¡§¹¥þ¨S¦³¤@ÂI¹w§@ªºµû¦ô¡^¡A«h¨Ã¨S¦³¤°»ò«Ü½T©wªº¥]³òµ¦²¤¥i¥H¥Î¡C¤@­Ó¨Ò¤l¬O½Ò¥» (3.0.1) ¦¡

¥¦ªº¨â­Ó®Ú¬O

¤]´N¬O»¡¡A­Y¥Î¥]³òªk¡A¨º³o­Ó¨ç¼Æ·|Åܸ¹¦b·¥¤p·¥¤pªº½d³òùØ¡C

§@ªÌ¤]´£¨ì¡A¤£¦P©ó¥t¤@³¹±Mªù§ä¨ç¼Æ·¥¤pªº³¹¸`¤¤©Ò¥Îªº "¦V¤U§ä³Ì§CÂI" ªº¤èªk¡A³oºØ¤èªk¨Ã¤£¯à¦b¨D®Ú®É­Ô¥Î¡]¦]¬°¦³¤H¥i¯à·|»{¬°¡A§Ú­Ì¥i¥H§â§ä f(x) Åܸ¹³o¥ó¨Æ§ï¦¨§ä [f(x)]2 ¡A¤Ï¥¿¥¦³Ì¤p¤]´N¬O¹s¡A¦ý¬O¥u­n f(x) ¤¤¦³±×²v¬O¹sªº·¥­È¡A "¦V¤U§ä³Ì§CÂI" ªº¤èªk´NµLªk¦Ê¤À¤§¦Ê¥Í®Ä¡A¦]¬°¥¦¬O¾a±×²v«ü¤Þ¨ç¼Æ­È¬O­n¦V¤W©Î¦V¤U¡C µM¦Ó¡A¦Ü¤Ö¡A¦pªG§Ú­Ì¦³¤ß²z·Ç³Æ¡A«h³oºØ "¦V¤U§ä³Ì§CÂIª½¨ìÅܸ¹ " ¤]¬O¥i¥H§@¬°¶}©l§ä´M¤èªk¡A¹ï¥¿­tµL­­»·¨âÃä¨ç¼Æ­È¤£¦P¸¹ªº¨D®ÚÆZ¦³¥Îªº¡C

¥]³òªº¤èªk¡A½Ò¥»´£¨Ñ¤F¨â­Ó°Æµ{¦¡¡A

°Æµ{¦¡ zbrac

SUBROUTINE zbrac(func,x1,x2,succes)
INTEGER NTRY
REAL x1,x2,func,FACTOR
EXTERNAL func
PARAMETER (FACTOR=1.6,NTRY=50)
Given a function func and an initial guessed range x1 to x2, the routine expands the range geometrically until a root is bracketed by the returned values x1 and x2 (in which case succes returns as .true.) or until the range becomes unacceptably large (in which case succes returns as .false.).
INTEGER j
REAL f1,f2
LOGICAL succes

µ¹ªì©lÃä¬É x1¡Bx2 ¡A¦¹°Æµ{¦¡¦Û°Ê¦V¥~ÂX±iÃä¬Éª½¨ì¥]³ò¨ì¥ª¥kÃä¤@¥¿¤@­t¡]§Y½T©w¦Ü¤Ö¦³¤@­Ó®Ú¡^¬°¤î¡A©Î¬OÂX±i¨ì«D±`¤jªº½d³ò¤´§ä¤£¨ì¤]·|©ñ±ó¡C °ò¥»¤W³o¬O¦V¥~ÂX¤j½d³ò§ä´M¡C

 

°Æµ{¦¡ zbrak

SUBROUTINE zbrak(fx,x1,x2,n,xb1,xb2,nb)
INTEGER n,nb
REAL x1,x2,xb1(nb),xb2(nb),fx
EXTERNAL fx

Given a function fx defined on the interval from x1-x2 subdivide the interval into n equally spaced segments, and search for zero crossings of the function. nb is input as the maximum number of roots sought, and is reset to the number of bracketing pairs xb1(1:nb), xb2(1:nb) that are found.
INTEGER i,nbb
REAL dx,fc,fp,x

¨Ï¥Î zbrak ªº¤èªk¬O¡A¨Ï¥ÎªÌµ¹©w½d³ò x1¡Bx2¡A¥H¤Î­n§¡²Å¤À³Î´Xµ¥¤À n¡A°Æµ{¦¡·|³v¤@Àˬd¨C­Ó n µ¥¤À°Ï¶¡¬O§_³Q¤@¥¿¤@­tªº¨ç¼Æ­È¥]³ò¡A¦³§ä¨ìªº°Ï¶¡¥H xb1¡Bxb2 ¿é¥X¡A¦@ nb ­Ó¡A¦ý nb ªì©l­È¥H¹w´Á­n§äªº®Ú¤§¼Æ¥Øªº¤W­­­È¶Ç¤J¡C ´£¿ô¡An ¥i¥H¥Î«Ü¤j¡C

 

 

¤G¤Àªk (bisection)

¤@¥¹ª¾¹D¤F¥]³ò¦í®Úªº½d³ò¡A´N¦³¦hºØºtºâªk¥i¥H¿ï¾Ü¡A¦U¦³¤£¦Pªº¦¬Àijt«×¡A¶V«OÀIªº´N¶VºC¡C¦b¦¹¤¶²Ð¤@­Ó³Ì«OÀIªº¤èªk¡A´N¬O¤G¤Àªk¡C

¤G¤Àªk§ä®Úªº°ò¥»µ¦²¤«Ü²³æ¡A´N¬O°w¹ï¨ç¼Æ¤w¦³Åܸ¹ªº½d³ò¡A¨ú¨ä¤¤ÂI¨D¨ç¼Æ­È¡A¦¹­È«D¥¿§Y­t¡A«h·sªº¸û¤p¡]¶È­ì¨Ó¤§¤@¥b¤j¤p¡^¤§½d³ò´N¥i¦¸³Q¨D±o¡A¦p¦¹¤ÏÂ൤U¥h¡A¥i¦b©Ò»Ýªº®e§Ô«×½d³ò¤º§ä¨ì©Ò­nªº®Ú¡C

¨ç¦¡ rtbis ¡]root bisection ¤§·N¡^

FUNCTION rtbis(func,x1,x2,xacc)
INTEGER JMAX
REAL rtbis,x1,x2,xacc,func
EXTERNAL func
PARAMETER (JMAX=40)
Maximum allowed number of bisections.
Using bisection, find the root of a function func known to lie between x1 and x2. The root, returned as rtbis, will be refined until its accuracy is ¡Óxacc.

INTEGER j
REAL dx,f,fmid,xmid

¦b¤w¸gª¾¹D x1¡Bx2 ¤§¶¡¦³¤@­Ó®Úªº±¡ªp¤U¡A¤ÏÂÐ¥H¤G¤ÀªkÁY¤p½d³ò¡Aª½¨ì¦b©Òµ¹ªº»~®t®e§Ô«×¤§¶¡¡C

½Ðª`·N rtbis ¬O·í¨ç¼Æ¡]¨ç¦¡¡^¥Î¡A¦Ó«D°Æµ{¦¡¡C

¼g¤@­Ó¥Dµ{¦¡¨Ó¨Ï¥Î rtbis ¨ç¦¡¤§½d¨Ò¡G

program rtbis_main

real func, rtbis, x1, x2, xacc
external func, rtbis

write (*,*) 'What is the range x1 and x2 and accuracy ?'
read (*,*) x1, x2, xacc

write (*,*) 'A root is at ', rtbis(func,x1,x2,xacc),
& ' with accuracy ', xacc

end

function func(x)
real func, x
func = x**3
end

 

 

 

¾ãÅ鵦²¤

§Ú­Ì±q zbrac¡Bzbrak¡Brtbis ªº¹B§@¤è¦¡¥i¥Hª¾¹D¡A¨D®Úªºªººtºâªkùدuªº¨S¦³«OÃÒ·|§ä¨ì®Ú¦Ó¥B¤£·|¿òº|¡]¨Ò¦p¨â­Ó®Ú«Ü¾aªñ¦]¦¹¨ç¼Æ¦bÃä¬É¨â°¼¨S¦³Åܸ¹¡A©Î¬O¥]¨ì¤F¦h©ó¤@­Óªº©_¼Æ®Ú¡^¡C ¦]¦¹¥ý¥Î srcsho ©Î pgplot »{ÃѹϧΨӥý¬ã§P®Ú³Ì¥i¯à¥X²{ªº¦ì¸m¡A¦Aµ¹½d³ò§Q¥Î zbrac ¤Î zbrak §ä´M¤Î½T»{·|¥]³ò¦í®Úªº½d³ò¡A³Ì«á¥Î rtbis §â®Úªº¦ì¸m©w¥X¨Ó¡C

½Ð¤j®a«ä¦Ò¡A¦p¦ó¼g¥X¤@­Ó¾ã¦Xªºµ{¦¡¡A¥H¹F¦¨³Ì¤jµ{«×ªº¨D®Ú¦Û°Ê¤Æ¡C