-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22. Simpson 1-3rd Rule.f08
70 lines (61 loc) · 1.4 KB
/
22. Simpson 1-3rd Rule.f08
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
58
59
60
61
62
63
64
65
66
67
68
69
70
! File: Simpson 1-3rd Rule.f08
! Author: Aakash Gajjar
! Created on August 26, 2016, 6:28 PM
!
! Subject: Implementation of Simpson's
! One third integration rule
! to integrate a given function
! between specified interval
!
! This is a utility function that calculates
! X^Y, x to the power y
function pow(base, power)
implicit none
real :: pow, base
integer :: power, i
pow = 1.0
do i = 1, power
pow = pow * base
end do
end function pow
! The function which we want to integrate
function f(x)
implicit none
real :: f, x, pow
f = 0.2 + 25 * pow(x, 1) &
-200 * pow(x, 2) &
+675 * pow(x, 3) &
-900 * pow(x, 4) &
+400 * pow(x, 5)
end function
! The Simpson's One Third Rule
! Procedure
function S13(low, high, steps)
implicit none
real :: S13, low, high, f
integer :: steps
real :: stepSize, a, b, c
integer :: i
! Steps should be even
stepSize = (high - low)/(2 * steps)
S13 = 0.0
! This loop calculates rectangles area
! as a,b,c and uses those to estimate
! area S13
do i = 1, steps
a = low + (2 * i - 2) * stepSize
b = low + (2 * i - 1) * stepSize
c = low + (2 * i - 0) * stepSize
S13 = S13 + (f(a) + f(c) + 4.0 * f(b))
end do
S13 = S13 * stepSize/3.0
end function S13
! The main container program
program Simpson1_3Rule
implicit none
real :: S13
!Exact value is 1.640533
print *, S13(0.0, 0.8, 200)
end program Simpson1_3Rule
! OUTPUT
! 1.64053357