この方法では、収束解を得るのに必要な反復計算回数
を、初期値
、
を用いて、
| (14) |
以下の外部関数rtbisは、関数
をfuncとして、
となる解を
二分法で解くプログラム例である
。
function rtbis(func,x1,x2,xacc)
implicit NONE
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
! betwee x1 and x2. The root, returned as 'rtbis', will be refined
! until its accuracy is +-'xacc'.
Integer j
Real dx,f,fmid,xmid
fmid=func(x2)
f=func(x1)
if(f*fmid.ge.0.) pause 'root must be bracketed in rtbis'
if(f.lt.0.)then !Orient the search so that f>0 lies at x+dx.
rtbis=x1
dx=x2-x1
else
rtbis=x2
dx=x1-x2
endif
do j=1,JMAX !Bisection loop.
dx=dx*.5
xmid=rtbis+dx
fmid=func(xmid)
if(fmid.le.0.)rtbis=xmid
if(abs(dx).lt.xacc .or. fmid.eq.0.) return
end do
pause 'too many bisections in rtbis'
end