Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 梯形法则的问题_Fortran_Numerical_Calculus - Fatal编程技术网

Fortran 梯形法则的问题

Fortran 梯形法则的问题,fortran,numerical,calculus,Fortran,Numerical,Calculus,我在用fortran计算e^x内部和区间[b.a]的积分时遇到了一些麻烦 我想我在函数调用中做错了什么。谢谢你帮助我 program trapezium implicit none integer :: i, n, b, a real :: sumation, mean, deltax, f(i), integral ! The value of the integral using the trapezium rule can be found using ! i

我在用fortran计算e^x内部和区间[b.a]的积分时遇到了一些麻烦

我想我在函数调用中做错了什么。谢谢你帮助我

program trapezium
  implicit none

    integer :: i, n, b, a
    real :: sumation, mean, deltax, f(i), integral


 ! The value of the integral using the trapezium rule can be found using
 ! integral = (b - a)*((f(a) +f(b))/2 + sumation_1_n-1 )/n 

write(*,*) "type the limits b, a and the number of intervals"
     read *, b, a, n

    deltax = (b - a)/n
        mean = (f(a) + f(b))/2
sumation = 0

do i = 1, n-1  
    sumation = sumation + f(i)
end do


      integral = deltax*(mean + sumation) 
  write (*,*) "the value of the integral using the trapezoidal method is", integral

     end program 

function f(x)
  real :: f(x) 
  integer :: x

      f(x) = EXP(x)

end function

您的代码有几个问题:

  • f
    是一个函数,但同时定义数组
    f(i)
  • 定义固定大小的数组时,必须在编译时知道大小。所以
    real::f(i)
    仅对常数
    i
  • exp()
    需要一个
    real
    变量,而不是整数
  • 整数运算可能导致意外结果:
    1/2=0
    而不是
    0.5
那么(这并不是为了解决数学问题——请看我的评论):


请注意,模块的使用使编译器能够检查函数的参数。此外,不需要在主程序中定义函数的返回值

我不确定你代码中的数学是否正确。。。为什么您总是从
f(1)
开始,然后转到
f(n-1)
,即使用户可以指定完全不同的限制?你不应该按照
(a-b)/n
的步骤从
a
转到
b
?为什么在这里使用整数?i=1到n-1只是计算图像上出现的总和。计算deltax的限制a和b。[link][/link]现在我的程序可以编译了。我想我在声明上犯了一些可怕的错误。现在算法可以编译了,但正如你所看到的,我需要修正数学。非常感谢。
module functions
contains
  function f(x)
    implicit none
    real :: f
    integer,intent(in) :: x

    f = EXP(real(x))

  end function
end module

program trapezium
  use functions
  implicit none

  integer :: i, n, b, a
  real :: sumation, mean, deltax, integral


  ! The value of the integral using the trapezium rule can be found using
  ! integral = (b - a)*((f(a) +f(b))/2 + sumation_1_n-1 )/n 

  write(*,*) "type the limits b, a and the number of intervals"
  read *, b, a, n

  deltax = real(b - a)/real(n)
  mean = (f(a) + f(b))/2
  sumation = 0

  do i = 1, n-1  
    sumation = sumation + f(i)
  end do


  integral = deltax*(mean + sumation) 
  write (*,*) "the value of the integral using the trapezoidal method is", integral

end program