Skip to content

Commit 27c1038

Browse files
Tests for midpoint.f90
1 parent ed6dea2 commit 27c1038

File tree

2 files changed

+184
-1
lines changed

2 files changed

+184
-1
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
!> Test program for the Midpoint Rule module
2+
!!
3+
!! Created by: Your Name (https://github.com/YourGitHub)
4+
!! in Pull Request: #XX
5+
!! https://github.com/TheAlgorithms/Fortran/pull/XX
6+
!!
7+
!! This program provides test cases to validate the midpoint_rule module against known integral values.
8+
9+
program test_midpoint_rule
10+
use midpoint_rule
11+
implicit none
12+
13+
! Run test cases
14+
call test_integral_x_squared_0_to_1()
15+
call test_integral_x_squared_0_to_2()
16+
call test_integral_sin_0_to_pi()
17+
call test_integral_e_x_0_to_1()
18+
call test_integral_1_over_x_1_to_e()
19+
call test_integral_cos_0_to_pi_over_2()
20+
call test_integral_x_cubed_0_to_1()
21+
call test_integral_sin_x_squared_0_to_1()
22+
23+
print *, "All tests completed."
24+
25+
contains
26+
27+
! Test case 1: ∫ x^2 dx from 0 to 1 (Exact result = 1/3 ≈ 0.3333)
28+
subroutine test_integral_x_squared_0_to_1()
29+
real(dp) :: lower_bound, upper_bound, integral_result, expected
30+
integer :: panels_number
31+
lower_bound = 0.0_dp
32+
upper_bound = 1.0_dp
33+
panels_number = 1000000 ! Must be a positive integer
34+
expected = 1.0_dp / 3.0_dp
35+
call midpoint(integral_result, lower_bound, upper_bound, panels_number, f_x_squared)
36+
call assert_test(integral_result, expected, "Test 1: ∫ x^2 dx from 0 to 1")
37+
end subroutine test_integral_x_squared_0_to_1
38+
39+
! Test case 2: ∫ x^2 dx from 0 to 2 (Exact result = 8/3 ≈ 2.6667)
40+
subroutine test_integral_x_squared_0_to_2()
41+
real(dp) :: lower_bound, upper_bound, integral_result, expected
42+
integer :: panels_number
43+
lower_bound = 0.0_dp
44+
upper_bound = 2.0_dp
45+
panels_number = 1000000 ! Must be a positive integer
46+
expected = 8.0_dp / 3.0_dp
47+
call midpoint(integral_result, lower_bound, upper_bound, panels_number, f_x_squared)
48+
call assert_test(integral_result, expected, "Test 2: ∫ x^2 dx from 0 to 2")
49+
end subroutine test_integral_x_squared_0_to_2
50+
51+
! Test case 3: ∫ sin(x) dx from 0 to π (Exact result = 2)
52+
subroutine test_integral_sin_0_to_pi()
53+
real(dp) :: lower_bound, upper_bound, integral_result, expected
54+
integer :: panels_number
55+
real(dp), parameter :: pi = 4.D0 * DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture.
56+
lower_bound = 0.0_dp
57+
upper_bound = pi
58+
panels_number = 1000000 ! Must be a positive integer
59+
expected = 2.0_dp
60+
call midpoint(integral_result, lower_bound, upper_bound, panels_number, sin_function)
61+
call assert_test(integral_result, expected, "Test 3: ∫ sin(x) dx from 0 to π")
62+
end subroutine test_integral_sin_0_to_pi
63+
64+
! Test case 4: ∫ e^x dx from 0 to 1 (Exact result = e - 1 ≈ 1.7183)
65+
subroutine test_integral_e_x_0_to_1()
66+
real(dp) :: lower_bound, upper_bound, integral_result, expected
67+
integer :: panels_number
68+
lower_bound = 0.0_dp
69+
upper_bound = 1.0_dp
70+
panels_number = 1000000 ! Must be a positive integer
71+
expected = exp(1.0_dp) - 1.0_dp
72+
call midpoint(integral_result, lower_bound, upper_bound, panels_number, exp_function)
73+
call assert_test(integral_result, expected, "Test 4: ∫ e^x dx from 0 to 1")
74+
end subroutine test_integral_e_x_0_to_1
75+
76+
! Test case 5: ∫ (1/x) dx from 1 to e (Exact result = 1)
77+
subroutine test_integral_1_over_x_1_to_e()
78+
real(dp) :: lower_bound, upper_bound, integral_result, expected
79+
integer :: panels_number
80+
lower_bound = 1.0_dp
81+
upper_bound = exp(1.0_dp)
82+
panels_number = 1000000 ! Must be a positive integer
83+
expected = 1.0_dp
84+
call midpoint(integral_result, lower_bound, upper_bound, panels_number, log_function)
85+
call assert_test(integral_result, expected, "Test 5: ∫ (1/x) dx from 1 to e")
86+
end subroutine test_integral_1_over_x_1_to_e
87+
88+
! Test case 6: ∫ cos(x) dx from 0 to π/2 (Exact result = 1)
89+
subroutine test_integral_cos_0_to_pi_over_2()
90+
real(dp) :: lower_bound, upper_bound, integral_result, expected
91+
real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture.
92+
integer :: panels_number
93+
lower_bound = 0.0_dp
94+
upper_bound = pi / 2.0_dp
95+
panels_number = 1000000 ! Must be a positive integer
96+
expected = 1.0_dp
97+
call midpoint(integral_result, lower_bound, upper_bound, panels_number, cos_function)
98+
call assert_test(integral_result, expected, "Test 6: ∫ cos(x) dx from 0 to π/2")
99+
end subroutine test_integral_cos_0_to_pi_over_2
100+
101+
! Test case 7: ∫ x^3 dx from 0 to 1 (Exact result = 1/4 = 0.25)
102+
subroutine test_integral_x_cubed_0_to_1()
103+
real(dp) :: lower_bound, upper_bound, integral_result, expected
104+
integer :: panels_number
105+
lower_bound = 0.0_dp
106+
upper_bound = 1.0_dp
107+
panels_number = 1000000 ! Must be a positive integer
108+
expected = 0.25_dp
109+
call midpoint(integral_result, lower_bound, upper_bound, panels_number, f_x_cubed)
110+
call assert_test(integral_result, expected, "Test 7: ∫ x^3 dx from 0 to 1")
111+
end subroutine test_integral_x_cubed_0_to_1
112+
113+
! Test case 8: ∫ sin(x^2) dx from 0 to 1 (Approximate value)
114+
subroutine test_integral_sin_x_squared_0_to_1()
115+
real(dp) :: lower_bound, upper_bound, integral_result, expected
116+
integer :: panels_number
117+
lower_bound = 0.0_dp
118+
upper_bound = 1.0_dp
119+
panels_number = 1000000 ! Must be a positive integer
120+
expected = 0.310268_dp ! Approximate value, adjust tolerance as needed
121+
call midpoint(integral_result, lower_bound, upper_bound, panels_number, sin_squared_function)
122+
call assert_test(integral_result, expected, "Test 8: ∫ sin(x^2) dx from 0 to 1")
123+
end subroutine test_integral_sin_x_squared_0_to_1
124+
125+
! Function for x^2
126+
real(dp) function f_x_squared(x)
127+
real(dp), intent(in) :: x
128+
f_x_squared = x**2
129+
end function f_x_squared
130+
131+
! Function for e^x
132+
real(dp) function exp_function(x)
133+
real(dp), intent(in) :: x
134+
exp_function = exp(x)
135+
end function exp_function
136+
137+
! Function for 1/x
138+
real(dp) function log_function(x)
139+
real(dp), intent(in) :: x
140+
log_function = 1.0_dp / x
141+
end function log_function
142+
143+
! Function for cos(x)
144+
real(dp) function cos_function(x)
145+
real(dp), intent(in) :: x
146+
cos_function = cos(x)
147+
end function cos_function
148+
149+
! Function for x^3
150+
real(dp) function f_x_cubed(x)
151+
real(dp), intent(in) :: x
152+
f_x_cubed = x**3
153+
end function f_x_cubed
154+
155+
! Function for sin(x^2)
156+
real(dp) function sin_squared_function(x)
157+
real(dp), intent(in) :: x
158+
sin_squared_function = sin(x**2)
159+
end function sin_squared_function
160+
161+
! Function for sin(x)
162+
real(dp) function sin_function(x)
163+
real(dp), intent(in) :: x
164+
sin_function = sin(x)
165+
end function sin_function
166+
167+
!> Subroutine to assert the test results
168+
subroutine assert_test(actual, expected, test_name)
169+
real(dp), intent(in) :: actual, expected
170+
character(len=*), intent(in) :: test_name
171+
real(dp), parameter :: tol = 1.0e-6_dp
172+
173+
if (abs(actual - expected) < tol) then
174+
print *, test_name, " PASSED"
175+
else
176+
print *, test_name, " FAILED"
177+
print *, " Expected: ", expected
178+
print *, " Got: ", actual
179+
stop 1
180+
end if
181+
end subroutine assert_test
182+
183+
end program test_midpoint_rule

tests/maths/numerical_integration/simpson.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end subroutine test_integral_x_squared_0_to_2
5252
subroutine test_integral_sin_0_to_pi()
5353
real(dp) :: lower_bound, upper_bound, integral_result, expected
5454
integer :: panels_number
55-
real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi
55+
real(dp), parameter :: pi = 4.D0*DATAN(1.D0) ! Define Pi. Ensure maximum precision available on any architecture.
5656
lower_bound = 0.0_dp
5757
upper_bound = pi
5858
panels_number = 1000000 ! Must be even

0 commit comments

Comments
 (0)