Fortran 在另一个函数中调用函数(在子例程中定义)
我正在用fortran编写函数func(x,y)的二维积分代码,y的极限从y1(x)到y2(x),x的极限从x1=3到x2=5。 基本假设如下: 积分[func(x,y),{y=y1到y2},{x=x1到x2}]=积分[funcx(x),{x=x1到x2}]。其中funcx=积分[func,{y=y1(x)到y2(x)}] 请在代码中间找到我的问题。我写代码如下Fortran 在另一个函数中调用函数(在子例程中定义),fortran,Fortran,我正在用fortran编写函数func(x,y)的二维积分代码,y的极限从y1(x)到y2(x),x的极限从x1=3到x2=5。 基本假设如下: 积分[func(x,y),{y=y1到y2},{x=x1到x2}]=积分[funcx(x),{x=x1到x2}]。其中funcx=积分[func,{y=y1(x)到y2(x)}] 请在代码中间找到我的问题。我写代码如下 implicit real*8(a-h,o-z) external func external y1 external y2 x1
implicit real*8(a-h,o-z)
external func
external y1
external y2
x1 = 3.d0
x2 = 5.d0
call twodint(func,y1,y2,x1,x2,result)
print*, result
stop
end
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subroutine twodint(origfunc,y1,y2,x1,x2,result)
implicit real*8(a-h,o-z)
external funcx
call simpsonintegral(funcx,x1,x2,ss) ! my integral routine funcx = function, x1 and x2 = limits, ss = output
result = ss
return
end
function funcx(x)
implicit real*8(a-h,o-z)
external funcy
external y1
external y2
common/xvalues/xx
xx = x
call simpsonintegral(funcy,y1(x),y2(x),ss)
funcx = ss
return
end
function funcy(y)
implicit real*8(a-h,o-z)
common/xvalues/x
function func(x,y)
implicit real*8(a-h,o-z)
func = (x**2)*y*dsin(x*y) ! some function of x and y
return
end
function y1(x)
implicit real*8(a-h,o-z)
y1 = x ! some limit
return
end
function y2(x)
implicit real*8(a-h,o-z)
y2 = x*x ! some limit
return
end
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subroutine twodint(origfunc,y1,y2,x1,x2,result)
implicit real*8(a-h,o-z)
external funcx
call simpsonintegral(funcx,x1,x2,ss) ! my integral routine funcx = function, x1 and x2 = limits, ss = output
result = ss
return
end
function funcx(x)
implicit real*8(a-h,o-z)
external funcy
external y1
external y2
common/xvalues/xx
xx = x
call simpsonintegral(funcy,y1(x),y2(x),ss)
funcx = ss
return
end
function funcy(y)
implicit real*8(a-h,o-z)
common/xvalues/x
function func(x,y)
implicit real*8(a-h,o-z)
func = (x**2)*y*dsin(x*y) ! some function of x and y
return
end
function y1(x)
implicit real*8(a-h,o-z)
y1 = x ! some limit
return
end
function y2(x)
implicit real*8(a-h,o-z)
y2 = x*x ! some limit
return
end
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subroutine twodint(origfunc,y1,y2,x1,x2,result)
implicit real*8(a-h,o-z)
external funcx
call simpsonintegral(funcx,x1,x2,ss) ! my integral routine funcx = function, x1 and x2 = limits, ss = output
result = ss
return
end
function funcx(x)
implicit real*8(a-h,o-z)
external funcy
external y1
external y2
common/xvalues/xx
xx = x
call simpsonintegral(funcy,y1(x),y2(x),ss)
funcx = ss
return
end
function funcy(y)
implicit real*8(a-h,o-z)
common/xvalues/x
function func(x,y)
implicit real*8(a-h,o-z)
func = (x**2)*y*dsin(x*y) ! some function of x and y
return
end
function y1(x)
implicit real*8(a-h,o-z)
y1 = x ! some limit
return
end
function y2(x)
implicit real*8(a-h,o-z)
y2 = x*x ! some limit
return
end
!!我的问题是:如果我在这里写funcy=func(x,y),我的代码运行良好。但我想在这里写一些东西,比如funcy=origfunc(x,y),这样它就可以从名为twodint的子例程接收函数。但这是行不通的。请帮忙。。。
回来
结束
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subroutine twodint(origfunc,y1,y2,x1,x2,result)
implicit real*8(a-h,o-z)
external funcx
call simpsonintegral(funcx,x1,x2,ss) ! my integral routine funcx = function, x1 and x2 = limits, ss = output
result = ss
return
end
function funcx(x)
implicit real*8(a-h,o-z)
external funcy
external y1
external y2
common/xvalues/xx
xx = x
call simpsonintegral(funcy,y1(x),y2(x),ss)
funcx = ss
return
end
function funcy(y)
implicit real*8(a-h,o-z)
common/xvalues/x
function func(x,y)
implicit real*8(a-h,o-z)
func = (x**2)*y*dsin(x*y) ! some function of x and y
return
end
function y1(x)
implicit real*8(a-h,o-z)
y1 = x ! some limit
return
end
function y2(x)
implicit real*8(a-h,o-z)
y2 = x*x ! some limit
return
end
在我看来,
external
主要是因为遗留的原因而包含在现代Fortran中的。这不是将函数传递给子例程的最佳方式。而且,隐式类型声明是危险的。。。最好使用隐式无
并显式键入所有变量。。。那样你会犯很多错误。所以下面是如何将各种函数传递给子例程(如积分器)的示意图
module MyStuff
implicit none
abstract interface
function funcX (x)
real, intent (in) :: x
real :: funcX
end function funcX
end interface
contains
subroutine twodint(origfunc,y1,y2,x1,x2,result)
real :: y1, y2, x1, x2, result, ss
procedure (funcX) :: origfunc
call simpsonintegral (origfunc,x1,x2,ss) ! my integral routine funcx = function, x1 and x2 = limits, ss = output
result = ss
return
end subroutine twodint
real function funcA (x)
real, intent (in) :: x
funcA = x ** 2
end function funcA
real function funcB (x)
real, intent (in) :: x
funcB = x ** 2
end function funcB
end module MyStuff
program test1
use MyStuff
implicit none
real :: x1, x2, y1, y2, result
x1 = 3.d0
x2 = 5.d0
call twodint(funcA,y1,y2,x1,x2,result)
print*, result
call twodint(funcB,y1,y2,x1,x2,result)
print*, result
stop
end program test1
它怎么不起作用?它应该通过哪个子程序接收原始文件?请不要用大写字母书写@VladimirF我通过名为origfunc的子程序twodint传递了该函数,并希望它在我提出问题的地方被接收。如果需要进一步澄清,请告诉我。谁知道呢,您的程序充满了难看的
隐式和外部语句,很难理解。至少对我来说,我也有同样的感觉,摆脱了隐式的和外部的。请注意,在代码末尾有一些伪星,如果用双精度常量初始化,reals可能会更高。我编辑以删除伪字符。