print

4
subroutine bisect1 (left,right,middle,function) ! ! first bisection subroutine ! implicit none external function double precision tol parameter (tol=5.d-2) double precision x double precision function double precision left,right,middle double precision fleft,fright,fmiddle double precision error integer kread, kwrite data kread/5/, kwrite/6/ ! ! initialize variables ! fleft = function(left) fright = function(right) 100 continue middle = (left+right)*0.5d0 fmiddle = function(middle) ! determine which half contains the root if (fleft*fmiddle .le. 0) then ! root is located in the left subinterval right=middle fright=fmiddle else ! root is located in the right subinterval left=middle fleft=fmiddle endif ! check for relative error error = abs ( (right-left)/middle ) if (error .gt. tol ) go to 100 return end double precision function f1ofx (x) ! ! function cos x - x

Upload: tomas-rojas

Post on 19-Jul-2016

2 views

Category:

Documents


0 download

DESCRIPTION

print

TRANSCRIPT

Page 1: Print

subroutine bisect1 (left,right,middle,function)!! first bisection subroutine! implicit none

external function

double precision tol parameter (tol=5.d-2)

double precision x double precision function double precision left,right,middle double precision fleft,fright,fmiddle double precision error

integer kread, kwrite data kread/5/, kwrite/6/!! initialize variables! fleft = function(left) fright = function(right)

100 continue

middle = (left+right)*0.5d0 fmiddle = function(middle)

! determine which half contains the root

if (fleft*fmiddle .le. 0) then

! root is located in the left subinterval

right=middle fright=fmiddle

else

! root is located in the right subinterval

left=middle fleft=fmiddle

endif

! check for relative error

error = abs ( (right-left)/middle ) if (error .gt. tol ) go to 100

return end double precision function f1ofx (x)!! function cos x - x

Page 2: Print

! implicit none

double precision x double precision f1ofx

f1ofx = cos (x) - x return end program findroot1!! find root of function f1ofx! implicit none

external f1ofx

double precision x double precision f1ofx double precision xinitial,xfinal,root double precision delta double precision tolnr

double precision xx(50) double precision fxx(50) integer ie, iemax

integer kread, kwrite data kread/5/, kwrite/6/ data iemax/30/! xinitial = 0.d0 xfinal = 1.57d0 delta = 0.1d0 tolnr=1.d-10

write (kwrite,*) write (kwrite,*) 'Search Root via Bisection Method: ' write (kwrite,*)

write (kwrite,*) 'left bound :',xinitial,' right bound :',xfinal

! call the bisection routine

call bisect1 (xinitial,xfinal,root,f1ofx)

write (kwrite,*) 'root found at :',root

write (kwrite,*) write (kwrite,*) 'Search Root via Newton-Raphson Secant Method : ' write (kwrite,*) ! do newton search! starting from xinitial

xx(0) = xinitial fxx(0) = f1ofx(xx(0)) xx(1) = xx(0) + delta

Page 3: Print

fxx(1) = f1ofx(xx(1))

ie=0

20 ie=ie+1 if (ie.gt.iemax) stop

write (kwrite,*) ' f(',xx(ie),') = ',fxx(ie),ie call newtonr (fxx(ie-1),fxx(ie),xx(ie-1),xx(ie),xx(ie+1),delta) fxx(ie+1) = f1ofx(xx(ie+1))

if (abs(fxx(ie+1)).lt.tolnr) then

write(6,*) ' the root of f(x) is :',fxx(ie),' at x = ',xx(ie)

else go to 20 endif

stop end subroutine newtonr (f1,f2,e1,e2,e3,delta)! ========================================!! this subroutine searches for the zero of a function f(e).! first, search where f changes sign, then! search for the zero with the Newton-Raphson method.!! implicit none

double precision f1,f2,e1,e2,e3,delta double precision v1,v2 integer iv save iv

data iv/0/!! do linear search until f changes sign! if (iv.eq.0) then v1=sign(1.d0,f1) v2=sign(1.d0,f2) if (v1.eq.v2) then e3=e2+delta return! start newton seach else e3=e2-f2*(e2-e1)/(f2-f1) iv=1 endif ! newton search else if (iv.eq.1) then

Page 4: Print

e3=e2-f2*(e2-e1)/(f2-f1) endif return end