1.2 数值计算求面积
(1)梯形法
(2)Simpson法
两种方法,分段数目N均为偶数。
(3)Simpson的3/8公式:N必须是3的倍数
(4)Bode公式:N必须是4的倍数
chap1b例子:

下面是梯形法计算程序,
-
!ccccccccccccccccccccccccccccccccccccccccccccccccccc!
-
! chap1b.f90
-
! Numerical quadrature,trapezoidal rule
-
program main
-
!ccccccccccccccccccccccccccccccccccccccccccccccccccc!
-
! variables:
-
implicit none
-
real :: h ! step
-
integer :: N=10
-
real :: x,funx,trap,total
-
integer :: counter ! using in do loop
-
real :: exact,diff
-
-
exact=exp(1.0)-1.0
-
funx=exp(x)
-
h=1.0/real(2*N)
-
x=h
-
total=0.0
-
-
write(*,*)"h=",h
-
-
do counter=1,N,1 !这里多用了一次循环,因为最后一次赋值的x值没有使用,最后一次循环完成后x=21*h
-
write(*,*)"x=",x
-
trap=h/2.0*(exp(x-h)+2.0*exp(x)+exp(x+h))
-
total=total+trap
-
x=x+2.0*h
-
end do
-
!
-
diff=exact-total
-
write(*,*)"exact:",exact,"total:",total,"diff:",diff
-
!
-
stop
-
!
-
end program main
计算结果为:exact: 1.71828175 total: 1.71863997 diff: -3.58223915E-04
使用项数更多的公式,例如Bode公式,可能会出现振荡现象。保险的做法还是使用项数少的,梯形法
和Simpson法,只要使得h值足够小便可以保证精度了。
阅读(693) | 评论(0) | 转发(0) |