COSMOS v7.655  COSMOSv7655
(AirShowerMC)
kbinChop.f
Go to the documentation of this file.
1 ! external ff
2 ! real*8 ff
3 ! real*8 x1, x2, x, eps, ans
4 ! integer icon
5 ! x1 = 0.
6 ! x2 = 1.8
7 ! x = 1.
8 ! eps= 1.d-11
9 ! call kbinChop(ff, x1, x2, x, eps, ans, icon)
10 ! write(*, *) icon, ans, ff(ans)
11 ! end
12 ! real*8 function ff(x)
13 ! real*8 x
14 ! ff = sin(x) - 0.5d0
15 ! end
16 !
17 ! Binary Chop for getting a solution of f(x) = 0.
18 !
19  subroutine kbinchop(f, x1, x2, x, eps, ans, icon)
20  implicit none
21 !
22  real*8 f ! input. function name. to be used as f(x)
23  ! f(x) = 0 is solved.
24  real*8 x1 ! input. lower bound of solution
25  real*8 x2 ! input. upper bound of solution
26  real*8 x ! input. initial guess of solution.
27  real*8 eps ! input. relative error of solution.
28  real*8 ans ! output. obtained solution
29  integer icon ! output. condition code. 0--> ok.
30 ! 1--> unconvergence after 45 iterations
31 ! 2--> x not in the range
32  real*8 xa, xb, fa, fb, xt, ft
33  integer n
34 !
35  if(x .lt. x1 .or. x .gt. x2) then
36  icon = 2
37  else
38  xa = x1
39  xb = x2
40  fa = f(xa)
41  fb = f(xb)
42  icon = 1
43  do n = 0, 45
44  if(fa * fb .gt. 0.) then
45  icon = 1
46  goto 100
47  else
48  xt = (xa + xb)/ 2
49  ft = f(xt)
50  if( ft * fa .gt. 0.) then
51  xa = xt
52  fa = ft
53  else
54  xb = xt
55  fb = ft
56  endif
57  if(abs(xt) .gt. 1.) then
58  if(abs( (xa-xb) / xt ) .lt. eps) then
59  icon = 0
60  goto 100
61  endif
62  else
63  if(abs(xa-xb) .lt. eps) then
64  icon = 0
65  goto 100
66  endif
67  endif
68  endif
69  enddo
70  100 continue
71  ans = xt
72  endif
73  end
74 
75 
76 
subroutine kbinchop(f, x1, x2, x, eps, ans, icon)
Definition: kbinChop.f:20