【发布时间】:2013-10-31 21:02:02
【问题描述】:
我在使用 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(1)开始并转到f(n-1)?你不应该以(a-b)/n的步骤从a转到b吗?为什么在这里使用整数? -
i = 1 到 n-1 只计算出现在图像上的总和。计算 deltax 的限制 a 和 b。 [链接]imgur.com/uQOEGcS [/链接]
标签: fortran numerical calculus