BISECTION3

!Program name: BISECTION3

!Function: x^3-9

!Initial x value is 1.0

!Tolerance 1E-06

!initial Interval dx=0.5

!The number of bisections is 100

!Root is 2.08008382

 

module finding_roots

implicit none

Real :: xl, xr

 

contains

   subroutine init()

    !Root between x=0 and x=3

    xl=0.

    xr=3.

    write (*,5) "iter", "x", "y(x)", "fl", "fl*y(x)"

5  format (a5X,a10X,a20X,a20X,a15X)

   end subroutine init

  

   function func(x) result(y)

    implicit none

    Real :: x

    Real :: y !dummy argument

    y=x**3-9

   end function func

  

   subroutine bisect(xl,xr,x,func,f)

    implicit none

    integer :: i

    real :: xl, xr, x, func, toly

    real :: fl, fr, f       !dummy arguments

 

    call init()

 

    fl=func(xl)

    fr=func(xr)

    toly=1.E-06

 

    do i=1,100

      x=0.5*(xl+xr)

      f=func(x)

      if (f*fl .lt. 0) then

       xr=x

       fr=f

      else

       xl=x

       fl=f

      end if

      Write (*,*) i, x, f, fl, f*fl

      if (abs(f) .lt. toly) exit

   end do

   end subroutine bisect

 

end module finding_roots

 

use finding_roots

 

call bisect(xl,xr,x,func,f)

 

print *, x, func(x)

stop

end 

Comentários

Postagens mais visitadas deste blog

RECURSIVE

HYDROGEN-RADIAL

RUNGE-KUTTA