±`·L¤Àµ{¨D¸Ñ¡G¶©¥¨¡X®w¶ðªk¡B°ª¶¥¤èªkªº¹Ï¹³

 

ºtºâªk¤½¦¡»P¨ä¹Ï¹³ªí¥Ü

Euler Mehod

¹ï©ó¤@­Ó·L¤pªº¨B´T (step) h ¦Ó¨¥¡A ­Y±q²Ä n ¨B­n±À¨ì²Ä n+1 ªº¤èªk¬O¨Ì¥H¤Uªº¤è¦¡¡A´N¬O«e­±¤w¸g´£¨ì¹Lªº¶ø¨Ì°Çªk¡G

±q¹Ï¹³¤W¨Ó¬Ý¡A¥¦¬O¥H²Ä n ÂI¬°¥ß¨¬ÂI¡A¦b¨ºùبD¥X¤Á½u¤è¦V¥Xµo¡]±×²v¨ç¼Æ¬OÃD¥Øµ¹ªº¡^¡A§Q¥Î¸Ó¤è¦V¨Ó¨«¤@¨B¡A¨ì¹F²Ä n+1 ÂI¡A¦p¦¹¤@¨B¤@¨B¦a¨«¤U¥h¡A¦p¤U¹Ï©Ò¥Ü¡G

 

2nd Order Runge-Kutta Method

«e­±§Úµ¹¤j®a¥Ü½d¹L¡A§Q¥Î¨â­Ó¥b¨B»P¤@­Ó¾ã¨Bªº²Õ¦X¡A¥i¥H³Ð³y¥Xºë½T«×¹F¨ì¤G¶¥¡]§Y»~®t±q¤T¶¥¶}©l¡^¡A¦ý«o¥u­n­¼¤W·L¤p¶q h ¤@¦¸ªº¤½¦¡ªº¤½¦¡¡A¨ä¤¤¥²¶·¥I¥Xªº¥N»ù¬O¡A¤ñ°_¤W­±ªº Euler ªk­n¦hºâ k2¡]½Ðª`·N¨ì¥¦¬O¨D­È¦b¥b¨Bªº¦a¤è¡^¡G

¤W­±ªº¤½¦¡±q¹Ï¹³¤W«Ü®e©ö¥i¥H¬Ý¥X¨äµ¦²¤¡A¦b°_ÂI³B©Ò¨D±oªº±×²v¡A¥ý¥u¨«¤@¥b¨ì¤¤¶¡ÂI¡A¦b¨ºùئA¨D¤@¦¸±×²v¡AµM«á°h¦^¨ì°_ÂI§Q¥Î³o­Ó¤¤ÂI±×²v¨«§¹¾ã¤@¨B¨ì¤U¤@¨B¡A¦p¦¹±o¨ì yn+1¡A´N¬O¹Ï¤W¼Ð¬° (3) ªº¦a¤è¡C³o´N¬O¤@­Ó¤G¶¥ªº step¡A¤U¤@­Ó step ·|¸¨¦b¼Ð¬° (5) ³B¡A¦p¦¹¦A¤@¦¸¤@¦¸¨«¤U¥h¡C ¾ã­Óµ¦²¤¬Ý°_¨Ó¡A¦³¶i¨â¨B¡B°h¤@¨B¡A¥H°h¬°¶iªº·Pı¡C

 

4th Order Runge-Kutta Method

³Ì±`¥Î¥B³q¥Îªº RK ¤èªk¬O¥|¶¥ªº¡A§Ú­Ì¥i¥H·Q¨£¥¦¬O¥Ñ§ó¦h¤£¦P¼Ë¦¡ªº¥b¨B²Õ¦X©Òºc¦¨¡A¨ä¤¤¨Ã¥]§t¤F­ì¦³ªº°_ÂI³B©w±×²v»P²×ÂI³B©w±×²v¡A½Ðª`·N³oùØ k1¡Bk2¡Bk3¡Bk4 ªº©w¸q»P¨ä¬Û¨Ì©Ê¡]§Y k4 ¥Î¨ì k3¡Bk3 ¥Î¨ì k2¡Bk2 ¥Î¨ì k1 ªºª¬ªp¡^

¡]¨Æ¹ê¤W¡^³oùتº¹Ï¬O¦³¦L¿ùªº±¡ªp¡A§Ú»{¬° 2 »P 3 ªº¼Ðª`³BÀ³¸Ó¹ï½Õ¡A¤½¦¡¤¤ªº k1¡Bk2¡Bk3¡Bk4 ¥þ³¡³£¦³ h ¡A¦ý¤À§O¬O­¼¥H (a) ¦b°_ÂI³B 1 ¨Dªº±×²v¡B(b) ¥H°_ÂI±×²v¨«¥b¨B¨ì 2 ³B¨D±×²v¡B(c) ¤@¼Ë±q°_ÂI¶}©l¦ý¡A§ï±Ä«e­±ªº¥b¨B³B 2 ¤§±×²v¡A­«¨«¥b¨B¨ì 3 «á¨D±×²v¡A¥H¤Î³Ì«á (d) ±Ä¥Î¤W¤@­Ó©ó 3 ³B©ÒÀò±oªº±×²v¡A±q°_ÂI¥Xµo¨«¤@¾ã¨B¨ì 4 ³B¨D±×²v¡C

 

 

 

°Æµ{¦¡¤Î¨Ï¥Î¤Wªºª`·N¨Æ¶µ

°Æµ{¦¡ rk4

ª`·N¨Æ¶µ¤Î§Þ¥©¡G

rk4 ¶i¦æ¤@¨Bªº 4 ¶¥ R-K ­pºâ¡A ­È±oª`·Nªº¬O«Å§i¬° external ªº derivs¡A¥¦¬O¤@­Ó­n¥Ñ¨Ï¥ÎªÌ´£¨Ñªº°Æµ{¦¡¡AµM¦Ó¡A¬°¤°»ò¦b rk4 ªº¤Þ¼Æ¦CùØ­±¡]³Ì«á¤@¶µ¡^¤]¥X²{¦³ derivs ªº¦WºÙ©O¡H³o¦³¤@­Ó¦n³B¡A´N¬O¨Ï¥ÎªÌ´£¨Ñªº°Æµ{¦¡¥i¥H¥s°µ§Oªº¦WºÙ¡A¤ñ¤è»¡ derivs01¡A¦Ó rk4 ­n©I¥s¥¦®É¡A´N¼g¦¨ call rk4(....,derivs01) ³o¼Ë¡A¦p¦¹¦b rk4 ¤º³¡¦Û¤w¤´»{¬°¬O¦b¨Ï¥Î¼Ð·Ç¬°ºÙªº derivs¡A©M¤@¯ë°Æµ{¦¡¶ÇÅܼƪº±¡ªp¤@¼Ë¡C

 

°Æµ{¦¡ rkdumb

ª`·N¨Æ¶µ¤Î§Þ¥©¡G

rkdumb ¬O¤@­Ó©T©w¨B´Tªº©Ò¿× driver¡]ÅX°Ê¡^µ{¦¡¡A¨Ï¥Î¨C¦¸¥u¨«¤@¨Bªº³æ¯Âªº¤u¨ãµ{¦¡ ¡]¦b³oùجO rk4¡^¤@ª½¶i¦æ«Ü¦h¨B¡A§â¾ã­Ó¸Ñ¤@¨B¤@¨B°µ¥X¨Ó¡C¦b³o­Ó°Æµ{¦¡¤¤¡A§A­nµ¹¦ÛÅܼơ]¤W¤U­­¡^½d³ò x1¡Bx2 ¥H¤Î¦@¤À³Î¦h¤Ö¨B nstep¡A¦¹ driver µ{¦¡·|¥Hµ¥¨B´T¨D¸Ñ­ì¥¼ª¾¨ç¼Æ¡C"Dumb" ¥»¨­¬O "²Â"¡B"§b" ªº·N«ä¡A§@ªÌ³o¼Ë¥s¬O¦]¬°¤U¤@¸`·|¤¶²Ð¸ûÁo©úªº driver µ{¦¡¡A¥¦¬O¥i¥H°µ¨C­Ó³æ¤@¨B´T¤j¤pªº±±¨î¥H¹F¨ì³Ì¨Îªº­pºâ®ÄªG¡]«ü¦P¼Ëºë±K«×ªº­n¨D¤U¥H³Ì¤Ö®É¶¡§¹¦¨¡^¡C

¦b¼g¥Dµ{¦¡¨Ó©I¥s rkdumb ®É¡A­º¥ýª`·N vstart ¦b rkdumb ¤¤«Å§i¬°°}¦C vstart(n)¡A¦ý n ªº­È¦b rkdumb ¤¤«o¨S¦³³]©w¡]¥u¦³«Å§i¥¦¬O¾ã¼Æ¡^¡A©Ò©¯¥¦¦b¤Þ¼Æ¦C¤¤¬O¦³¶Ç¶i¨Ó¡A³o´Nªí¥Ü¦b½sĶªº®É­Ô¤´¥i¥H³z¹LÄ~©Ó¤W¼hµ{¦¡ªºÄݩʦӨ㦳©ú½Tªº­È¡]«Å§i°}¦C®É°}¦Cªº¤j¤p¥²¶·©ú½T³]©w¡A¥i°õ¦æÀɤ~¯àª¾¹D­n´À¨º­ÓÅܼƫO¯d¦h¤Ö°O¾ÐÅéªÅ¶¡¤j¤p¡^¡C¦]¦¹¡A¬°¤F°t¦X°Æµ{¦¡¤¤«Å§i¤F vstart(n) ¦Ó¥u¶Ç n ¶i¥h¡A¥Dµ{¦¡¦b«Å§i n ¬°¾ã¼Æ«á´NÁ٥Πparameter (n=?) ³o­Ó«ü¥O±N n ­È³]©w¬°¬Y¯S©w±`¼Æ¡C

rkdumb ·|§â¾ã­Ó x1 ¨ì x2 ¶¡ nstep ¨º»ò¦hÂIªº xn ­È¤Î yn ­È³£«O¯d¤U¨Ó¡A³o¾ã²Õ¼Æ­È­pºâµ²ªG´N¥i¥H¥Nªí y(x) ³o­Ó¸Ñ¡]³o­Ó¸Ñ¬O°ß¤@¦s¦bªº¡A¸Ô²ÓÃÒ©ú¥i°Ñ¦Ò¬ÛÃö¼Æ¾Ç®ÑÄy¡A¦p Keryszig ªº°ªµ¥¤uµ{¼Æ¾Ç:²Ä¤Eª©¡^¡C¥Dµ{¦¡ªº­«­n¥ô°È·íµM¬O±q¨ú±o³o¨Ç¸ê®Æ¡A¨Ã§â¥¦¿é¥X©Î«O¦s¡Crkdumb ¥Î common /path/ xx,y ªº»yªk§â xx(NSTPMAX) ¤Î y(NMAX,NSTPMAX) Àx¦s¦b path ³o­Ó¥i¦@¨Éªº¤½¦@°Ï¶ô¡]common block¡^¤¤¡A¥¦ªº¥\¯à¬OÅý¨ä¥L¤W¼h©Î¤U¼hªº°Æµ{¦¡³£¤£¥²¶Ç¤Þ¼Æ¤]¯à¥Î±o¨ì°Ï¶ôùØ­±ªº¸ê®Æ¡A¥u­n¤@¶}©l¤]§@¬Û¦Pªº common /path/ x1,y1 «Å§i§Y¥i¡Aª`·N°Ï¶ô¦W path ­n¤@¼Ë¡AÅܼƦWºÙ«h¥i¥H¤£¦P¡C

Ãö©ó common block ªº¶i¤@¨B¥Îªk»¡©ú¡A¥i¨£ °t¦X±Ð¬ì®Ñªº¨ä¥L«ü¥O¡A©Î°Ñ¨£¤â¥U¡G http://proton.phys.tku.edu.tw/pgi/doc/pgf77_ref/f77ref04.htm#Heading68

½d¨Òµ{¦¡¡G

program rkdumb_main
implicit none
integer nstep,NMAX,NSTPMX,n_var,i,j,pgopen
parameter (NMAX=50,NSTPMX=200,n_var=2)
real xx(NSTPMX),y(NMAX,NSTPMX),x1,x2,vstart(n_var),yy(NSTPMX)
real y_min(NMAX), y_max(NMAX)
common /path/xx,y
external my_derivs

write(*,*) 'Type in initial and final values of x :'
read(*,*) x1,x2

c  Be careful the max value of nstep is 199, not 200, see rkdumb.

  100 write(*,*) 'How many steps to integrate the solution ? (<= 199)'

read(*,*) nstep
if (nstep.gt.199) goto 100
if (nstep.lt.0) goto 100

write(*,*) 'Your problem is an order- ',n_var,' ODE.'
do i=1,n_var
  write(*,*) 'Give the initial value y',i,'(x0) :'
  read (*,*) vstart(i)
end do

call rkdumb(vstart,n_var,x1,x2,nstep,my_derivs)
write(*,*) 'RK routine has completed.'

c Find Max and min values of yi(x), but here we will use only y1(x).

do i=1,n_var
  y_max(i)= y(i,1)
  y_min(i)= y(i,1)
end do
do i=1,n_var
  do j=1,nstep
    if (y(i,j).gt.y_max(i)) y_max(i)=y(i,j)
    if (y(i,j).lt.y_min(i)) y_min(i)=y(i,j)
  end do
end do

c Copy y(1,1:nstep) to yy(1:nstep) for PGLINE plotting

do i=1,nstep
  yy(i) = y(1,i)
end do

c Start plotting

if (pgopen('/xwin').le.0) stop
call pgpap(6.0,0.75)
call pgenv (x1,x2,y_min(1),y_max(1),0,0)
call pgline(nstep,xx,yy)
call pgclos

end

 

subroutine my_derivs(x,y,dydx)
real x,y(2),dydx(2)
dydx(1) = y(2)
dydx(2) = -y(1)
end

ª`¸Ñ¡G¥»¨Ò½dªº­ìµ¹©w·L¤À¤èµ{ÃD¥Ø¡A¬O¨D¸Ñ y''(x) + y(x) = 0 ³o¼Ëªº¤@­Ó·L¤À¤èµ{¦¡¡C¥Ñ©ó³o¬O¤@­Ó¤G¶¥±`·L¤À¤èµ{¦¡¡A¦]¦¹¥²¶·Âà¤Æ¦¨¨â±ø¤@¶¥±`·L¤À¤èµ{¦¡¡A¨ä§@ªk¬O¡A­º¥ý¡A¨Ì¥»³æ¤¸¤§©w¸q©Ò«Ý¸Ñ¥¼ª¾¨ç¼Æ y ¬° y1¡A¨Ã©w¸q dy1/dx = y2¡A³o´N©Ò­n¤§¨â±ø¤@¶¥·L¤À¤èµ{¦¡ªº²Ä¤@±ø¡A¥t¥~¡A±N dy1/dx «ö­è¤~ªº©w¸q¥N¤J­ì·L¤À¤èµ{¦¡¡A±o dy2/dx = -y1¡A¦p¦¹±o¨ì©Ò­n¤§¨â±ø·L¤À¤èµ{¦¡ªº²Ä¤G±ø¡C¤@¨Ö¦C¥X¡A§e dy1/dx = y2 »P dy2/dx = -y1 Áp¥ß¡A¦]¦¹¦b¤W¨Ò my_derivs °Æµ{¦¡¦³ dydx(1) = y(2) »P dydx(2) = -y(1) ³o¨â¦æ«ü¥O¡Cª`·N¦b¤@¯ëªº±¡§Î¤U¡A³£·|¬O»P x ¦³Ãöªº¡A¤£¬O¹³¥»¨Ò¤¤¤§±`·L¤Àµ{¦¡¨º¼Ë«ê¦n»P x µLÃö¡C

 

¸É¥R¾\Ū¡G

¥iÅܤƪº¨B´T±±¨î¡]NR 16.2¡^