-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Ramy-Badr-Ahmed/feature/numerical_integra…
…tion Implemented Several Numerical Integration Algorithms in `maths/`
- Loading branch information
Showing
26 changed files
with
770 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
examples/maths/numerical_integration/gaussian_legendre.f90
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.