-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simpson_Integral.f95
57 lines (56 loc) · 1.79 KB
/
Simpson_Integral.f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
! Simpson辛普森积分法
! 采样点的个数必须为奇数
! 计算公式:Integeral(fx) = (f0 + 4*f1 + 2*f2 + 4*f3 + 2*f4 + ... + 4*fn-1 + fn) * h / 3
! 其中h为程序中的width变量
module MY_INTEGRAL
implicit none
real, parameter :: PI = 3.1415926
contains
! 获取采样点
subroutine Sample(s, lower, upper, func)
implicit none
real, intent(inout) :: s(:)
real, intent(in) :: lower, upper
real, external :: func
integer :: i, N
real :: width, r
r = lower
N = size(s, 1)
width = (upper - lower) / (N-1)
do i=1, N
s(i) = func(r)
r = r + width
end do
return
end subroutine
! 利用公式进行计算积分
real function GetIntegeral(s, lower, upper)
implicit none
real, intent(in) :: s(:)
real, intent(in) :: lower, upper
integer :: N, i
real :: ans = 0.0
real :: width
N = size(s, 1)
width = (upper-lower)/(N-1)
ans = ans + s(1) + s(N)
do i=2, N-2, 2
ans = ans + 4*s(i) + 2*s(i+1)
end do
GetIntegeral = ans * width / 3
return
end function
end module
program main
use MY_INTEGRAL
implicit none
real :: s(10001)
real :: lower=0
real :: upper=PI
real, intrinsic :: sin
real :: ans = 0.0
call Sample(s, lower, upper, sin)
ans = GetIntegeral(s, lower, upper)
write(*, *) ans
end program
! 运行结果:1.999978 此为sin(x)在0到PI上的积分