Fortran,如何制作几个副本的程序

时间:2016-03-19 15:53:19

标签: function fortran double integration procedures

我有一个函数(Simpson)来计算函数的积分(f(x,y = 5))。我想使用相同的函数(Simpson)来对函数f(x,y)进行双重积分:

F(x)= \ int dy f(x,y)    I = \ int dx F(x)

我可以在Fortran中调用两次相同的函数(Simpson)吗? 在我放下的例子中我该怎么做? 我认为我的问题的主要思想已经在这里提出,但我没有找到答案。 fortran, how to make several copies of a program using module or interface?

我没有兴趣制作Simpson:Simpson1 Simpson2函数的副本既不使用外部包。

最佳

这是我正在使用的代码的大纲:

MODULE INTEG
IMPLICIT NONE
CONTAINS
REAL FUNCTION SIMPSON(FUNC,A,B,TOL)
    REAL, INTENT(IN) :: A, B, TOL
    INTERFACE
      FUNCTION FUNC (X) RESULT(OUT)
            IMPLICIT NONE
            REAL, INTEN(IN) :: X
            REAL :: OUT
       END FUNCTION 
    END INTERFACE
........
END FUNCTION SIMPSON 
END MODULE INTEG

MODULE DOING
USE INTEG
REAL :: TEMP
IMPLICIT NONE
CONTAINS
REAL FUNCTION  FUNC (X,Y) 
    IMPLICIT NONE
    REAL, INTENT(IN) :: X, Y
    REAL :: OUT
    FUNC =  X*Y
END FUNCTION FUNC

FUNCTION F(Y) RESULT(OUT2)
    REAL, INTENT(IN) :: Y
    REAL :: A, B, TOL,OUT2
    TEMP = Y
    OUT2 = SIMPSON(I1, A, B, TOL)
    CONTAINS
    REAL FUNCTION I1(X) RESULT(OUT)
        REAL, INTENT(IN) :: X
        OUT = FUNC (X,TEMP) 
    END FUNCTION I1
END FUNCTION F

    REAL FUNCTION I2(A2, B2, TOL2)
        REAL, INTENT(IN) :: A2, B2, TOL2
        I2 = SIMPSON(F, A2, B2,TOL)
    END FUNCTION I2 
END MODULE DOING

PROGRAM EXAMPLE
USE DOING
IMIPLICIT NONE
REAL :: A2, B2, TOL2, OUT3
OUT3 =  I2(A2, B2, TOL2)
END PROGRAM EXAMPLE

1 个答案:

答案 0 :(得分:0)

亚历山大你是对的。我在某个方面犯了一个错误。以下代码正在运行

MODULE INTEG 
    IMPLICIT NONE
    CONTAINS
    REAL FUNCTION Simpson(FUN, A, B, N)
        IMPLICIT NONE
        INTERFACE
            FUNCTION FUN (X) RESULT(RESUL)
               IMPLICIT NONE
               REAL, INTENT(IN) :: X
               REAL :: RESUL 
            END FUNCTION FUN
        END INTERFACE
        INTEGER, INTENT(IN) :: N
        REAL, INTENT(IN) :: A, B

        INTEGER :: N1, N2, I
        REAL :: STEP, H, SUMA1, SUMA2

        H = (B-A)/REAL(N)

        N1 = N/2 - 1
        N2 = N/2

        SUMA1 = 0.0
        DO I = 1, N1
            STEP = A + 2*I*H
            SUMA1 = SUMA1 + FUN(STEP)
        END DO
        SUMA2 = 0.0
        DO I = 1, N2
            STEP = A + (2*I-1)*H
            SUMA2 = SUMA2 + FUN(STEP)
        END DO
       Simpson = H*( FUN(A) + 2*SUMA1 + 4*SUMA2 + FUN(B) )/3.0
   END FUNCTION Simpson   
END MODULE INTEG

MODULE DOING
    USE INTEG
    IMPLICIT NONE
    REAL :: TEMP
    CONTAINS
    REAL FUNCTION  FUNC (X,Y) 
        IMPLICIT NONE
        REAL, INTENT(IN) :: X, Y
        REAL :: OUT
        FUNC =  X*Y
    END FUNCTION FUNC

    FUNCTION F(Y) RESULT(OUT2)
      REAL, INTENT(IN) :: Y
      REAL :: A, B, TOL,OUT2
      TEMP = Y
      A = 1
      B = 3
      OUT2 = Simpson(I1, A, B, 100)
      CONTAINS
      REAL FUNCTION I1(X) RESULT(OUT)
         REAL, INTENT(IN) :: X
         OUT = FUNC (X,TEMP) 
      END FUNCTION I1
   END FUNCTION F

  REAL FUNCTION I2(A2, B2, NN)
     REAL, INTENT(IN) :: A2, B2
     INTEGER, INTENT(IN) :: NN
     I2 = Simpson(F, A2, B2,100)
  END FUNCTION I2 
END MODULE DOING

PROGRAM EXAMPLE
  USE DOING
  IMPLICIT NONE
  REAL :: A2, B2, OUT3
  A2 = 1
  B2 = 3
  OUT3 =  I2(A2, B2, 100)
  WRITE(*,*)OUT3
END PROGRAM EXAMPLE