c   7.Newton$BK!(B
c     $B?9@n(B $BLwBg(B , 22980138
c
      program ex7
c
      implicit none
      integer :: n
      real :: a,b,c,d,x0,xk1,epsilon
      x0=-5.0e0           ! $B=i4|CM(B

      a = 1.0e0           ! 4$B78?t(B
      b =-4.0e0
      c =-2.0e0
      d = 8.0e0
      epsilon = 1.0e-4    ! $B<}B+@:EY(B

      write(6,*)'Newton$BK!(B'
      write(6,*)'3$B<!J}Dx<0(B f(x)=ax**3+bx**2+cx+d $B$N2r(B'
      write(6,'(a3,f4.1)')'a=',a
      write(6,'(a3,f4.1)')'b=',b
      write(6,'(a3,f4.1)')'c=',c
      write(6,'(a3,f4.1)')'d=',d
      write(6,300) epsilon
      write(6,200)
      do while (x0 >=-5.0e0 .and. x0 <= 5.0e0 )
         call newton(a,b,c,d,x0,xk1,n,epsilon)
        write(6,100) x0,n,xk1      
        x0=x0+0.5e0
      enddo

      stop
  100 format(3x,f4.1,1x,i5,3x,e18.9)
  200 format(1x,'$B=i4|CM(B',1x,'$BH?I|2s?t(B',2x,'$B2r(B') 
  300 format(1x,'$B<}B+@:EY(B',1x,e18.9)
      end

c-----Newton$BK!$G(B3$B<!J}Dx<0(B f(x)=ax**3+bx**2+cx+d $B$N(B
c-----$B2r$HH?I|2s?t$r5a$a$k%5%V%k!<%A%s(B
      subroutine newton(a,b,c,d,x0,xk1,n,epsilon)
       implicit none
       integer :: n
       real :: f,df,fxk,dfxk,a,b,c,d,x0,xk,xk1,epsilon
      
       n=1
       xk=x0
       fxk = f(a,b,c,d,xk)
       dfxk=df(a,b,c,d,xk)
       xk1=xk-fxk/dfxk

        do while (abs(xk1-xk) >= epsilon)
          n=n+1
          xk=xk1
          fxk = f(a,b,c,d,xk)
          dfxk=df(a,b,c,d,xk)
          xk1=xk-fxk/dfxk          
        enddo

      return
      end

c-----$B4X?t(B f(x)=ax**3+bx**2+cx+d
      function f(a,b,c,d,xk)
      implicit none
      real :: f,xk,a,b,c,d
      
      f=a*xk**3+b*xk**2+c*xk+d

      return
      end
c-----$B4X?t(B df(x)/dx = 3ax**2+2bx+c
      function df(a,b,c,xk)
      implicit none
      real :: df,xk,a,b,c

      df=3.0e0*a*xk**2+2.0e0*b*xk+c

      return
      end
