Bisection method in fortran with compilating error

122 views Asked by At

I wrote the following program that find roots of a given function using the bisection method. The program looks like to be correct. But, one error is produced during the compilation process.

MODULE RootSubs
    TYPE RootType
    REAL root,f_root,interval
    END TYPE RootType
    CONTAINS
    !1----------------------------------------------------
    SUBROUTINE Bisection(G,XL,XR,n_intervals,roots)
    !Set up intervals for finding roots with bisection method.
    IMPLICIT NONE
    REAL, INTENT(IN) :: XL,XR
    INTEGER, INTENT(IN) :: n_intervals
    TYPE (RootType), INTENT(OUT) :: roots (n_intervals)
    REAL dx,xa,xb
    INTEGER i
    !-------------------------------
    INTERFACE
        REAL FUNCTION G(x)
            REAL, INTENT(IN) :: x
        END FUNCTION G
    END INTERFACE
    !------------------------------
    dx=(XR-XL)/n_intervals
    DO i=1,n_intervals
        xa=XL+REAL(i-1)*dx
        xb=XL+REAL(i)*dx
        IF (G(xa)*G(xb) < 0.) THEN
            CALL Bisect(G,xa,xb,roots(i)%root,roots(i)%f_root, &
            roots(i)%interval)
        ELSE
            roots(i)%root=0.; roots(i)%f_root=0.; roots(i)%interval=0.
        END IF
    END DO
    END SUBROUTINE Bisection

    SUBROUTINE Bisect(G,xa,xb,x_mid,f_mid,final_interval)
    IMPLICIT NONE
    REAL, INTENT (INOUT) :: xa,xb
    REAL, INTENT(OUT) :: x_mid,f_mid,final_interval
    LOGICAL hit
    REAL, PARAMETER:: epsilon_x=1e-5,epsilon_f=1e-5
    !-------------------------------
    INTERFACE
        REAL FUNCTION G(x)
            REAL, INTENT(IN) :: x
        END FUNCTION G
    END INTERFACE

    !------------------------------
    x_mid=(xb+xa)/2.
    hit=.false.
    f_mid=G(x_mid)

        DO WHILE (((xb-xa) > epsilon_x).AND. &
            (ABS(f_mid) > epsilon_f).AND.(.NOT. hit))
            IF (f_mid == 0.) THEN
                hit=.true.
            ELSE IF (G(xa)*f_mid < 0) THEN
                xb=x_mid
            ELSE IF (G(xb)*f_mid < 0) THEN
                xa=x_mid
            ELSE
                PRINT *, 'Unexplained error!'
            END IF
            x_mid=(xb+xa)/2.
            f_mid=G(x_mid)
            final_interval=xb-xa
        END DO

    END SUBROUTINE Bisect
!--------------------------
END MODULE RootSubs
!==============================

MODULE FunctionDefinition
CONTAINS
!-----------------------
REAL FUNCTION G(x)
    IMPLICIT NONE
    REAL x
    G=5.d0*x**3-2.d0*x**2+3.d0
END FUNCTION G
!----------------------------------
END MODULE FunctionDefinition
!==================================

PROGRAM GetRoots
!MS-DOS file name ROOTS.F90

USE RootSubs, ONLY : RootType,Bisection
USE FunctionDefinition, ONLY : G
IMPLICIT NONE
INTEGER, PARAMETER :: n_intervals=10
TYPE (RootType) roots (n_intervals)
INTEGER i

CALL Bisection(G,-10.,0.,n_intervals,roots)

DO i=1,n_intervals
    PRINT *,i,roots(i)%root,roots(i)%f_root,roots(i)%interval
END DO

END PROGRAM

But when I compile the above program, the below error is produced

FIG12-12.F90(96): error #7061: The characteristics of dummy argument 1 of the associated actual procedure differ from the characteristics of dummy argument 1 of the dummy procedure.   [G]
CALL Bisection(G,-10.,0.,n_intervals,roots)
---------------^
compilation aborted for FIG12-12.F90 (code 1)

What can this be?

I don't know what I can solve this. I'm grateful if anyone can help.

0

There are 0 answers