Skip to content

Commit

Permalink
Merge pull request #25 from Ramy-Badr-Ahmed/feature/numerical_integra…
Browse files Browse the repository at this point in the history
…tion

Implemented Several Numerical Integration Algorithms in `maths/`
  • Loading branch information
SatinWukerORIG authored Oct 9, 2024
2 parents 49fffac + 5645569 commit 056b2d3
Show file tree
Hide file tree
Showing 26 changed files with 770 additions and 16 deletions.
6 changes: 6 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# The Algorithms - Directory of Fortran
## Maths
* numerical_integration
* [trapezoidal_rule](/modules/maths/numerical_integration/trapezoid.f90)
* [simpson_rule](/modules/maths/numerical_integration/simpson.f90)
* [midpoint_rule](/modules/maths/numerical_integration/midpoint.f90)
* [monte_carlo](/modules/maths/numerical_integration/monte_carlo.f90)
* [gauss_legendre](/modules/maths/numerical_integration/gaussian_legendre.f90)
* [euclid_gcd](/modules/maths/euclid_gcd.f90)
* [factorial](/modules/maths/factorial.f90)
* [fibonacci](/modules/maths/fibonacci.f90)
Expand Down
45 changes: 45 additions & 0 deletions examples/maths/numerical_integration/gaussian_legendre.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
!> Example Program for Gaussian-Legendre Quadrature Module
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #25
!! https://github.com/TheAlgorithms/Fortran/pull/25
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of Gaussian-Legendre Quadrature Module for numerical integration.
!!
!! It sets the integration limits and the number of quadrature points (n), and calls the
!! gauss_legendre_quadrature subroutine to compute the approximate value of the definite integral
!! of the specified function.
!!
!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x)

program example_gaussian_quadrature
use gaussian_legendre_quadrature
implicit none

real(dp) :: lower_bound, upper_bound, integral_result
integer :: quadrature_points_number

! Set the integration limits and number of quadrature points
lower_bound = -1.0_dp
upper_bound = 1.0_dp
quadrature_points_number = 5 !! Number of quadrature points (order of accuracy) up to 5

! Call Gaussian quadrature to compute the integral with the function passed as an argument
call gauss_legendre_quadrature(integral_result, lower_bound, upper_bound, quadrature_points_number, function)

write (*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574

contains

function function(x) result(fx)
implicit none
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate
end function function

end program example_gaussian_quadrature
45 changes: 45 additions & 0 deletions examples/maths/numerical_integration/midpoint.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
!> Example Program for Midpoint Rule
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #25
!! https://github.com/TheAlgorithms/Fortran/pull/25
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of Midpoint Rule for numerical integration.
!!
!! It sets the integration limits and number of subintervals (panels), and calls the
!! midpoint subroutine to compute the approximate value of the definite integral
!! of the specified function.
!!
!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x)

program example_midpoint
use midpoint_rule
implicit none

real(dp) :: lower_bound, upper_bound, integral_result
integer :: panels_number

! Set the integration limits and number of panels
lower_bound = -1.0_dp
upper_bound = 1.0_dp
panels_number = 400 !! Number of subdivisions

! Call the midpoint rule subroutine with the function passed as an argument
call midpoint(integral_result, lower_bound, upper_bound, panels_number, function)

write (*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196

contains

function function(x) result(fx)
implicit none
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate
end function function

end program example_midpoint
45 changes: 45 additions & 0 deletions examples/maths/numerical_integration/monte_carlo.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
!> Example Program for Monte Carlo Integration
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #25
!! https://github.com/TheAlgorithms/Fortran/pull/25
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of Monte Carlo module for numerical integration.
!!
!! It sets the integration limits and number of random samples, and calls the
!! monte_carlo subroutine to compute the approximate value of the definite integral
!! of the specified function.
!!
!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x)

program example_monte_carlo
use monte_carlo_integration
implicit none

real(dp) :: lower_bound, upper_bound, integral_result, error_estimate
integer :: random_samples_number

! Set the integration limits and number of random samples
lower_bound = -1.0_dp
upper_bound = 1.0_dp
random_samples_number = 1000000 !! 1E6 Number of random samples

! Call Monte Carlo integration with the function passed as an argument
call monte_carlo(integral_result, error_estimate, lower_bound, upper_bound, random_samples_number, function)

write (*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421

contains

function function(x) result(fx)
implicit none
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate
end function function

end program example_monte_carlo
45 changes: 45 additions & 0 deletions examples/maths/numerical_integration/simpson.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
!> Example Program for Simpson's Rule
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #25
!! https://github.com/TheAlgorithms/Fortran/pull/25
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of Simpson's rule for numerical integration.
!!
!! It sets the integration limits and number of panels, and calls the
!! simpson subroutine to compute the approximate value of the definite integral
!! of the specified function.
!!
!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x)

program example_simpson
use simpson_rule
implicit none

real(dp) :: lower_bound, upper_bound, integral_result
integer :: panels_number

! Set the integration limits and number of panels
lower_bound = -1.0_dp
upper_bound = 1.0_dp
panels_number = 100 !! Number of subdivisions (must be even)

! Call Simpson's rule with the function passed as an argument
call simpson(integral_result, lower_bound, upper_bound, panels_number, function)

write (*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555

contains

function function(x) result(fx)
implicit none
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate
end function function

end program example_simpson
45 changes: 45 additions & 0 deletions examples/maths/numerical_integration/trapezoid.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
!> Example Program for Trapezoidal Rule
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #25
!! https://github.com/TheAlgorithms/Fortran/pull/25
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of the Trapezoidal rule for numerical integration.
!!
!! It sets the integration limits and number of panels, and calls the
!! trapezoid subroutine to compute the approximate value of the definite integral
!! of the specified function.
!!
!! Example function: f(x) = exp(-x^2) * cos(2.0_dp * x)

program example_tapezoid
use trapezoidal_rule
implicit none

real(dp) :: lower_bound, upper_bound, integral_result
integer :: panels_number

! Set the integration limits and number of panels
lower_bound = -1.0_dp
upper_bound = 1.0_dp
panels_number = 1000000 !! 1E6 Number of subdivisions

! Call the trapezoidal rule with the function passed as an argument
call trapezoid(integral_result, lower_bound, upper_bound, panels_number, function)

write (*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195

contains

function function(x) result(fx)
implicit none
real(dp), intent(in) :: x
real(dp) :: fx

fx = exp(-x**2)*cos(2.0_dp*x) !! Example function to integrate
end function function

end program example_tapezoid
9 changes: 8 additions & 1 deletion examples/sorts/example_usage_gnome_sort.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
!> Test program for the Gnome Sort algorithm

!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #09
!! https://github.com/TheAlgorithms/Fortran/pull/9
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of the gnome_sort_module by sorting an array of integers.

program test_gnome_sort
Expand Down
7 changes: 7 additions & 0 deletions examples/sorts/example_usage_heap_sort.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
!> Example program for the Heap Sort algorithm
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #8
!! https://github.com/TheAlgorithms/Fortran/pull/8
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of the heap_sort_module by sorting an array of integers.

program test_heap_sort
Expand Down
9 changes: 8 additions & 1 deletion examples/sorts/example_usage_merge_sort.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
!> Test program for the Merge Sort algorithm

!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #7
!! https://github.com/TheAlgorithms/Fortran/pull/7
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of the merge_sort_module by sorting an array of integers.

program test_merge_sort
Expand Down
8 changes: 8 additions & 0 deletions examples/sorts/example_usage_quick_sort.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
!> Example program for the Quick Sort algorithm
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #10
!! https://github.com/TheAlgorithms/Fortran/pull/10
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of the quick_sort_module by sorting an array of integers.

program test_quick_sort
Expand Down
9 changes: 8 additions & 1 deletion examples/sorts/example_usage_radix_sort.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
!> Test program for the Radix Sort algorithm

!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #11
!! https://github.com/TheAlgorithms/Fortran/pull/11
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! This program demonstrates the use of the radix_sort_module by sorting an array of integers.
!! The base parameter affects the internal digit processing but does not change the final sorted order
!! of decimal integers. The output is always in decimal form.
Expand Down
Loading

0 comments on commit 056b2d3

Please sign in to comment.