梯形法则的问题 [英] Problems with the Trapezoidal Rule

查看:188
本文介绍了梯形法则的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我觉得我在做错了什么函数调用。

 程序trapezium 
隐式无

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


!使用梯形法则的积分值可以使用
! (*,*)键入限制b,a(b),(b),和间隔数
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


积分= deltax *(平均值+ sumation)
写入(*,*)使用梯形方法的积分值为,积分

结束程序

函数f(x)
real :: f(x)
整数:: x

f(x)= EXP(x)

end function


解决方案

几个问题与您的代码:


  • f 是一个函数,但在同时定义一个数组 f(i)

  • 定义一个固定大小的数组时,大小必须在编译时已知时间。所以 real :: f(i)只对一个常量有效 i

  • exp()需要一个真实变量,而不是一个整数
  • 整数算术可能会导致意外的结果: 1/2 = 0 而不是 0.5



怎么样?(这并不试图解决数学问题,但请参阅我的评论):

<$ p












整数,意图(in): :x

f = EXP(real(x))

结束函数
结束模块

程序梯形图
使用函数
隐式无

整数:: i,n,b,a
real :: sumation,mean,deltax,整数


!使用梯形法则的积分值可以使用
! (*,*)键入限制b,a(b),(b),以及间隔的数量
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 $ b * b

积分= deltax *(平均值+ sumation)
写入(*,*)使用梯形方法的积分值为,积分

end program

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

I'm having some troubles to calcule the integral of e^x inside and interval [b.a] using fortran.

I think I'm doing something wrong in the funcion calls. Thanks for helping me.

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

解决方案

There are a couple of issues with your code:

  • f is a function, but at the same time you define an array f(i)
  • When defining an array of fixed size, the size has to be known at compile time. So real :: f(i) is only valid for a constant i
  • exp() expects a real variable, not an integer
  • Integer arithmetic might lead to unexpected results: 1/2 = 0 and not 0.5!

What about (This does not try to fix the maths, though - see my comment):

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 

Note that the use of modules enables the compiler to check for the arguments of the function. Additionally, you do not need to define the return value of the function in the main program.

这篇关于梯形法则的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆