program  hasamiuchi
!  2008. 5. 14.  I. Tamagawa
implicit none

 real :: x1=0, x2=10, xc, xcp, dx ! x1 x2 are starting values for x
 real :: y1, y2, yc, ycp, dy
 real :: eps = 1.0e-6	! Error for x
 integer :: count	! Loop counter
 real :: f


! Check y1 y2
 y1 = f(x1) ; y2 = f(x2)
 if( y1*y2 > 0 ) then
   write(*,*) 'x1 x2 should be changed, because f(x1)=', y1, &
     'f(x2)=', y2, 'have same sign.'
   stop
 end if

write(*,*) 'count, dx,  dy,  xc,  yc'
    write(*,*) x1, y1, ':', x2, y2
! Loop 
 count = 0
 dx = eps * 10 ; xc = x1 ; yc = y1
 do while( dx > eps )
    xcp = xc
    xc = (x1*y2-x2*y1)/(y2-y1)	; count = count + 1
    write(*,*) 'x1 xc x2', x1, xc, x2
    ycp = yc
    yc = f(xc)
    if( yc == 0 ) then
     write(*,*) xc
     stop
    else if( yc * y1 < 0 ) then
     x2 = xc
     y2 = yc
    else if( yc * y2 < 0 ) then
     x1 = xc
     y1 = yc
    else
     write(*,*) 'Never happan!!'
    end if
    dx = abs(xc -xcp)
    dy = abs(yc -ycp)
    write(*,*) count,  ',', dx, ',',dy, ',',xc, ',',yc
 end do
   
end 

real function f(x)
 real :: x
 real :: m=3, a=2
 f = x**m -a
 return
end

