0

我必须使用neqnfIMSL 库中包含的子程序 ( ),它可以让我解决非线性系统。(链接到用户手册,neqnf 页面在这里main.f90,是:

program prova_sistema_in_un_modulo

    include "link_fnl_shared.h"
    use neqnf_int
    use modx

    implicit none

    call d_neqnf(FCN, x, xguess=x_guess, fnorm=f_norm)

end program prova_sistema_in_un_modulo

其中子程序 FCN 在外部模块中编码,modx.f90

module modx

implicit none

integer, parameter :: ikind = selected_real_kind(8,99)
integer :: n=3
real(kind=ikind) :: f_norm
real(kind=ikind), dimension(3) :: x, x_guess=(/ 4.0, 4.0, 4.0/)

contains

subroutine FCN(x,f,n)
    integer :: n                                  !dummy var
    real(kind=ikind), dimension(3) :: x, f        !dummy var

    f(1)=x(1)+A(x(1))+(x(2)+x(3))*(x(2)+x(3))-27.0            ! =0
    f(2)=B(x(1),x(2))+x(3)*x(3)-10.0                          ! =0
    f(3)=Z(x(2),x(3))                                         ! =0

end subroutine FCN

function A(x)
    real(kind=ikind) :: x    !dummy var
    real(kind=ikind) :: A    !function var

    A=exp(x-1.0)

end function A

function B(x,y)
    real(kind=ikind) :: x,y  !dummy var
    real(kind=ikind) :: B    !function var

    B=exp(y-2.0)/x

end function B

function C(x)
    real(kind=ikind) :: x    !dummy var
    real(kind=ikind) :: C    !function var

    C=sin(x-2.0)

end function C

function Z(x,y)
    real(kind=ikind) :: x,y  !dummy var
    real(kind=ikind) :: Z    !function var

    Z=y+C(x)+x*x-7.0

end function Z
end module modx

但我得到这三个错误:

Error   1    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. (12.2)   [FCN]
Error   2    error #7062: The characteristics of dummy argument 2 of the associated actual procedure differ from the characteristics of dummy argument 2 of the dummy procedure. (12.2)   [FCN]
Error   3    error #7063: The characteristics of dummy argument 3 of the associated actual procedure differ from the characteristics of dummy argument 3 of the dummy procedure. (12.2)   [FCN]

注意:如果我将所有代码都放在主程序中,一切都会好起来的!而如果我使用模块进行编码(正如我所做的那样,实际发布的代码)我会得到那个错误!谁能帮我?

4

1 回答 1

0

问题是您为虚拟参数x(3)f(3)自定义函数提供了固定尺寸,FCN而 IMSL 需要可变尺寸x(n)f(n)

subroutine FCN(x,f,n)
    integer :: n                                  !dummy var
!    real(kind=ikind), dimension(3) :: x, f       !<- wrong
    real(kind=ikind), dimension(n) :: x, f        !<- correct

    f(1)=x(1)+A(x(1))+(x(2)+x(3))*(x(2)+x(3))-27.0            ! =0
    f(2)=B(x(1),x(2))+x(3)*x(3)-10.0                          ! =0
    f(3)=Z(x(2),x(3))                                         ! =0

end subroutine FCN

重现这一点的一个工作示例是(从 借来的接口HYBRD1):

module test_int
contains
  subroutine final(FCN, x, f, n)
    interface
      SUBROUTINE FCN (X, F, N)
        INTEGER             N
        DOUBLE PRECISION    X(N), F(N)
      END SUBROUTINE
    end interface
    integer          :: n
    double precision :: x(n), f(n)
    call FCN(x,f,n)
  end subroutine
end module

module test_fct
contains
  subroutine FCN(X, F, N) 
    integer          :: n
    double precision :: x(n), f(n)
    print *,X ; print *,F ; print *,N
  end subroutine
end module

program prova
  use, intrinsic :: iso_fortran_env
  use test_int
  use test_fct
  implicit none
  integer,parameter :: n=2
  double precision  :: x(n), f(n)
  x = [ 1.d0, 2.d0 ]
  f = [ 3.d0, 4.d0 ]
  call final(FCN, x, f, n)
end program prova
于 2015-06-14T11:03:17.470 回答