Fortran Coder

标题: DNEQNF 算法求解非线性方程组 [打印本页]

作者: muzili2008    时间: 2015-3-19 16:09
标题: DNEQNF 算法求解非线性方程组
大家好,我采用DNEQNF求解非线性方程组,代码如下:
[Fortran] 纯文本查看 复制代码
C module function_equation
C implicit none

C contains
subroutine FCN(xx,yy,nn)
implicit none
integer nn
double precision xx(nn),yy(nn)
   
yy(1) = xx(1)**2+xx(2)**2-5d0
yy(2) = (xx(1)+1d0)*xx(2)-(3d0*xx(1)+1d0)
return
end subroutine
C end module
program main_test
use imsl
C use function_equation
implicit none
external FCN
double precision , parameter :: errrel = 1d-7
integer , parameter :: n = 2
integer , parameter :: itmax = 200
double precision x_guess,ans(2),fnorm
x_guess = (1d0,1d0)
call dneqnf(FCN,errrel,n,itmax,x_guess,ans,fnorm)
print *, ans
end

如果希望将求解的方程封装在一个module里面,则出现错误。这样的问题应该怎么解决呢?
(上述代码去掉注释后 ,就将求解的函数封装在module里面了。)

作者: aliouying    时间: 2015-3-19 16:18
从你的代码来看,这个是CVF的工程,因为用到了IMSL,我帮不到你,但是从代码分析来讲,你这去掉注释解除后,代码应该没有问题。
作者: 楚香饭    时间: 2015-3-19 17:22
错误提示很重要,为什么每次都不给出?
作者: muzili2008    时间: 2015-3-20 09:56
楚香饭 发表于 2015-3-19 17:22
错误提示很重要,为什么每次都不给出?

你好 ,这个是提示信息。

D:\Fortran\Algorithm\nonliner_equation_03\main_test.for
D:\Fortran\Algorithm\nonliner_equation_03\main_test.for(22) : Error: The attributes of this name conflict with those made accessible by a USE statement.   [FCN]
external FCN
-----------------^
D:\Fortran\Algorithm\nonliner_equation_03\main_test.for(29) : Error: 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]
call dneqnf(FCN,errrel,n,itmax,x_guess,ans,fnorm)
--------------------^
D:\Fortran\Algorithm\nonliner_equation_03\main_test.for(29) : Error: 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]
call dneqnf(FCN,errrel,n,itmax,x_guess,ans,fnorm)
--------------------^
Error executing df.exe.

main_test.obj - 3 error(s), 0 warning(s)

非常感谢!!
作者: 楚香饭    时间: 2015-3-20 13:21
用 Module 封装,你得调用 F90 接口,F77接口不行
[Fortran] 纯文本查看 复制代码
Module function_equation
contains
  Subroutine fcn(xx, yy, nn)
    Implicit None
    Integer nn
    Double Precision xx(nn), yy(nn)
    yy(1) = xx(1)**2 + xx(2)**2 - 5D0
    yy(2) = (xx(1)+1D0)*xx(2) - (3D0*xx(1)+1D0)
  End Subroutine fcn
End Module function_equation

Program main_test
  Use neqnf_int
  Use umach_int
  Use function_equation
  Implicit None
  Double Precision, Parameter :: errrel = 1D-7
  Integer, Parameter :: n = 2
  Integer, Parameter :: itmax = 200
  Double Precision x_guess(2), ans(2), fnorm
  x_guess = (1D0, 1D0)
  Call neqnf(fcn, ans, xguess=x_guess, fnorm=fnorm)
  Print *, ans
End Program main_test



作者: kerb    时间: 2015-3-20 15:36
Example of using NEQNF subroutine
Example Statement
Consider a binary system of component 1 and 2, with component 1 being the more volatile one. For a given pressure P and a vapor composition y of component 1 the dew point calculation consist in determining the Temperature T and the liquid composition x. Assuming ideal phase equilibrium the Pressure and the liquid composition are given by:

The pressure should be in mmHg and temperature in oK.

----------------------------------------------------------------------------------------------------------------------------
Fortran Program Listing
[Fortran] 纯文本查看 复制代码
 INTEGER N,ITMAX 
REAL X(3), XG(3), FNORM
EXTERNAL FCN

N=2
ERRREL=1.0e-4
ITMAX=100
XG(1)=0.5
XG(2)=100.0

call NEQNF (FCN, ERRREL, N, ITMAX, XG, X, FNORM)

WRITE(6,*)
WRITE(6,*)'FNORM = ',FNORM
WRITE(6,*)'X1 = ',X(1)
WRITE(6,*)'X2 = ',X(2)

STOP
END


   SUBROUTINE FCN(X,F,N)
INTEGER N
REAL X(N),F(N)

P=3.0*750.6
Y=0.6
A1=19.6664
B1=-4361.55
A2=20.1735
B2=-5050.5

PS1=EXP(A1+B1/(X(2)+273))
PS2=EXP(A2+B2/(X(2)+273))

F(1)=P-X(1)*PS1-(1-X(1))*PS2
F(2)=Y*P-X(1)*PS1

RETURN
END

C Output
C FNORM = 5.483037E-04
C X1 = 2.966535E-01
C X2 = 114.950700
C Stop - Program terminated.
C Press any key to continue



Example-of-using-NEQNF-subroutine.pdf

24.49 KB, 下载次数: 22






欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2