Skip to content

Commit ed94d3b

Browse files
Test for trapezoid.f90
1 parent cb9d037 commit ed94d3b

File tree

1 file changed

+155
-33
lines changed

1 file changed

+155
-33
lines changed

tests/maths/numerical_integration/trapezoid.f90

Lines changed: 155 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,190 @@
99
!!
1010
!! This program provides test cases to validate the trapezoidal_rule module against known integral values.
1111

12+
!> Test program for the Trapezoidal Rule module
13+
!!
14+
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
15+
!! in Pull Request: #32
16+
!! https://github.com/TheAlgorithms/Fortran/pull/32
17+
!!
18+
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
19+
!! addressing bugs/corrections to this file. Thank you!
20+
!!
21+
!! This program provides test cases to validate the trapezoidal_rule module against known integral values.
1222

1323
program test_trapezoidal_rule
1424
use trapezoidal_rule
1525
implicit none
1626

17-
real(dp) :: lower_bound, upper_bound, integral_result, expected
18-
real(dp), parameter :: pi = 4.d0*DATAN(1.d0) ! Define Pi. Ensures maximum precision available on any architecture
19-
20-
integer :: panels_number
21-
22-
! Test 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333)
23-
lower_bound = 0.0_dp
24-
upper_bound = 1.0_dp
25-
panels_number = 1000000
26-
expected = 1.0_dp / 3.0_dp
27-
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared)
28-
call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1")
29-
30-
! Test 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667)
31-
lower_bound = 0.0_dp
32-
upper_bound = 2.0_dp
33-
panels_number = 1000000
34-
expected = 8.0_dp / 3.0_dp
35-
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared)
36-
call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2")
37-
38-
! Test 3: ∫ sin(x) dx from 0 to π (Exact result = 2)
39-
lower_bound = 0.0_dp
40-
upper_bound = pi
41-
panels_number = 1000000
42-
expected = 2.0_dp
43-
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_function)
44-
call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π")
27+
! Run test cases
28+
call test_integral_x_squared_0_to_1()
29+
call test_integral_x_squared_0_to_2()
30+
call test_integral_sin_0_to_pi()
31+
call test_integral_e_x_0_to_1()
32+
call test_integral_1_over_x_1_to_e()
33+
call test_integral_cos_0_to_pi_over_2()
34+
call test_integral_x_cubed_0_to_1()
35+
call test_integral_sin_x_squared_0_to_1()
36+
37+
print *, "All tests completed."
4538

4639
contains
4740

41+
! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333)
42+
subroutine test_integral_x_squared_0_to_1()
43+
real(dp) :: lower_bound, upper_bound, integral_result, expected
44+
integer :: panels_number
45+
lower_bound = 0.0_dp
46+
upper_bound = 1.0_dp
47+
panels_number = 1000000
48+
expected = 1.0_dp/3.0_dp
49+
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared)
50+
call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1")
51+
end subroutine test_integral_x_squared_0_to_1
52+
53+
! Test case 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667)
54+
subroutine test_integral_x_squared_0_to_2()
55+
real(dp) :: lower_bound, upper_bound, integral_result, expected
56+
integer :: panels_number
57+
lower_bound = 0.0_dp
58+
upper_bound = 2.0_dp
59+
panels_number = 1000000
60+
expected = 8.0_dp/3.0_dp
61+
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_squared)
62+
call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2")
63+
end subroutine test_integral_x_squared_0_to_2
64+
65+
! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2)
66+
subroutine test_integral_sin_0_to_pi()
67+
real(dp) :: lower_bound, upper_bound, integral_result, expected
68+
integer :: panels_number
69+
real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture.
70+
lower_bound = 0.0_dp
71+
upper_bound = pi
72+
panels_number = 1000000
73+
expected = 2.0_dp
74+
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_function)
75+
call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π")
76+
end subroutine test_integral_sin_0_to_pi
77+
78+
! Test case 4: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183)
79+
subroutine test_integral_e_x_0_to_1()
80+
real(dp) :: lower_bound, upper_bound, integral_result, expected
81+
integer :: panels_number
82+
lower_bound = 0.0_dp
83+
upper_bound = 1.0_dp
84+
panels_number = 1000000
85+
expected = exp(1.0_dp) - 1.0_dp
86+
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, exp_function)
87+
call assert_test(integral_result, expected, "Test 4: ∫ e^x dx from 0 to 1")
88+
end subroutine test_integral_e_x_0_to_1
89+
90+
! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1)
91+
subroutine test_integral_1_over_x_1_to_e()
92+
real(dp) :: lower_bound, upper_bound, integral_result, expected
93+
integer :: panels_number
94+
lower_bound = 1.0_dp
95+
upper_bound = exp(1.0_dp)
96+
panels_number = 1000000
97+
expected = 1.0_dp
98+
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, log_function)
99+
call assert_test(integral_result, expected, "Test 5: ∫ (1/x) dx from 1 to e")
100+
end subroutine test_integral_1_over_x_1_to_e
101+
102+
! Test case 6: ∫ cos(x) dx from 0 to π/2 (Exact result = 1)
103+
subroutine test_integral_cos_0_to_pi_over_2()
104+
real(dp) :: lower_bound, upper_bound, integral_result, expected
105+
integer :: panels_number
106+
real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture.
107+
lower_bound = 0.0_dp
108+
upper_bound = pi/2.0_dp
109+
panels_number = 1000000
110+
expected = 1.0_dp
111+
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, cos_function)
112+
call assert_test(integral_result, expected, "Test 6: ∫ cos(x) dx from 0 to π/2")
113+
end subroutine test_integral_cos_0_to_pi_over_2
114+
115+
! Test case 7: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25)
116+
subroutine test_integral_x_cubed_0_to_1()
117+
real(dp) :: lower_bound, upper_bound, integral_result, expected
118+
integer :: panels_number
119+
lower_bound = 0.0_dp
120+
upper_bound = 1.0_dp
121+
panels_number = 1000000
122+
expected = 0.25_dp
123+
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, f_x_cubed)
124+
call assert_test(integral_result, expected, "Test 7: ∫ x^3 dx from 0 to 1")
125+
end subroutine test_integral_x_cubed_0_to_1
126+
127+
! Test case 8: ∫ sin(x^2) dx from 0 to 1 (Approximate value)
128+
subroutine test_integral_sin_x_squared_0_to_1()
129+
real(dp) :: lower_bound, upper_bound, integral_result, expected
130+
integer :: panels_number
131+
lower_bound = 0.0_dp
132+
upper_bound = 1.0_dp
133+
panels_number = 1000000
134+
expected = 0.31026_dp ! Approximate value, you can adjust tolerance as needed
135+
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function)
136+
call assert_test(integral_result, expected, "Test 8: ∫ sin(x^2) dx from 0 to 1")
137+
end subroutine test_integral_sin_x_squared_0_to_1
138+
48139
! Function for x^2
49140
real(dp) function f_x_squared(x)
50141
real(dp), intent(in) :: x
51142
f_x_squared = x**2
52143
end function f_x_squared
53144

145+
! Function for e^x
146+
real(dp) function exp_function(x)
147+
real(dp), intent(in) :: x
148+
exp_function = exp(x)
149+
end function exp_function
150+
151+
! Function for 1/x
152+
real(dp) function log_function(x)
153+
real(dp), intent(in) :: x
154+
log_function = 1.0_dp/x
155+
end function log_function
156+
157+
! Function for cos(x)
158+
real(dp) function cos_function(x)
159+
real(dp), intent(in) :: x
160+
cos_function = cos(x)
161+
end function cos_function
162+
163+
! Function for x^3
164+
real(dp) function f_x_cubed(x)
165+
real(dp), intent(in) :: x
166+
f_x_cubed = x**3
167+
end function f_x_cubed
168+
169+
! Function for sin(x^2)
170+
real(dp) function sin_squared_function(x)
171+
real(dp), intent(in) :: x
172+
sin_squared_function = sin(x**2)
173+
end function sin_squared_function
174+
54175
! Function for sin(x)
55176
real(dp) function sin_function(x)
56177
real(dp), intent(in) :: x
57178
sin_function = sin(x)
58179
end function sin_function
59180

60-
! Assertion subroutine
61-
subroutine assert_test(result, expected, test_name)
62-
real(dp), intent(in) :: result, expected
181+
!> Subroutine to assert the test results
182+
subroutine assert_test(actual, expected, test_name)
183+
real(dp), intent(in) :: actual, expected
63184
character(len=*), intent(in) :: test_name
64185
real(dp), parameter :: tol = 1.0e-5_dp
65186

66-
if (abs(result - expected) < tol) then
187+
if (abs(actual - expected) < tol) then
67188
print *, test_name, " PASSED"
68189
else
69190
print *, test_name, " FAILED"
70191
print *, " Expected: ", expected
71-
print *, " Got: ", result
192+
print *, " Got: ", actual
72193
stop 1
73194
end if
74195
end subroutine assert_test
75196

76197
end program test_trapezoidal_rule
198+

0 commit comments

Comments
 (0)