diff --git a/examples/maths/numerical_integration/gaussian_legendre.f90 b/examples/maths/numerical_integration/gaussian_legendre.f90 index c85c9c4..e510724 100644 --- a/examples/maths/numerical_integration/gaussian_legendre.f90 +++ b/examples/maths/numerical_integration/gaussian_legendre.f90 @@ -22,7 +22,7 @@ program example_gaussian_quadrature ! Call Gaussian quadrature to compute the integral call gauss_legendre_quadrature(integral_result, a, b, n, func) - write(*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574 + write (*, '(A, F12.6)') "Gaussian Quadrature result: ", integral_result !! ≈ 0.858574 contains @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate end function func end program example_gaussian_quadrature \ No newline at end of file diff --git a/examples/maths/numerical_integration/midpoint.f90 b/examples/maths/numerical_integration/midpoint.f90 index 3869da4..4e4ac56 100644 --- a/examples/maths/numerical_integration/midpoint.f90 +++ b/examples/maths/numerical_integration/midpoint.f90 @@ -21,7 +21,7 @@ program example_midpoint ! Call the midpoint rule subroutine with the function passed as an argument call midpoint(integral_result, a, b, n, func) - write(*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196 + write (*, '(A, F12.6)') "Midpoint rule yields: ", integral_result !! ≈ 0.858196 contains @@ -30,7 +30,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate end function func end program example_midpoint diff --git a/examples/maths/numerical_integration/monte_carlo.f90 b/examples/maths/numerical_integration/monte_carlo.f90 index 31cebbc..15423a0 100644 --- a/examples/maths/numerical_integration/monte_carlo.f90 +++ b/examples/maths/numerical_integration/monte_carlo.f90 @@ -22,7 +22,7 @@ program example_monte_carlo ! Call Monte Carlo integration call monte_carlo(integral_result, error_estimate, a, b, n, func) - write(*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421 + write (*, '(A, F12.6, A, F12.6)') "Monte Carlo result: ", integral_result, " +- ", error_estimate !! ≈ 0.858421 contains diff --git a/examples/maths/numerical_integration/simpson.f90 b/examples/maths/numerical_integration/simpson.f90 index 4b7a437..7fdaefc 100644 --- a/examples/maths/numerical_integration/simpson.f90 +++ b/examples/maths/numerical_integration/simpson.f90 @@ -22,7 +22,7 @@ program example_simpson ! Call Simpson's rule with the function passed as an argument call simpson(integral_result, a, b, n, func) - write(*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555 + write (*, '(A, F12.8)') "Simpson's rule yields: ", integral_result !! ≈ 0.85819555 contains @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate end function func end program example_simpson diff --git a/examples/maths/numerical_integration/trapezoid.f90 b/examples/maths/numerical_integration/trapezoid.f90 index 5c08c14..b46854d 100644 --- a/examples/maths/numerical_integration/trapezoid.f90 +++ b/examples/maths/numerical_integration/trapezoid.f90 @@ -22,7 +22,7 @@ program example_tapezoid ! Call the trapezoidal rule with the function passed as an argument call trapezoid(integral_result, a, b, n, func) - write(*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195 + write (*, '(A, F12.6)') 'Trapezoidal rule yields: ', integral_result !! ≈ 0.858195 contains @@ -31,7 +31,7 @@ function func(x) result(fx) real(dp), intent(in) :: x real(dp) :: fx - fx = exp(-x**2) * cos(2.0_dp * x) !! Example function to integrate + fx = exp(-x**2)*cos(2.0_dp * x) !! Example function to integrate end function func end program example_tapezoid diff --git a/modules/maths/numerical_integration/gaussian_legendre.f90 b/modules/maths/numerical_integration/gaussian_legendre.f90 index bc4c678..5f288bb 100644 --- a/modules/maths/numerical_integration/gaussian_legendre.f90 +++ b/modules/maths/numerical_integration/gaussian_legendre.f90 @@ -49,10 +49,10 @@ end function func call gauss_legendre_weights(t, w, n) ! Allocate the function value array - allocate(fx(n)) + allocate (fx(n)) ! Transform the nodes from the reference interval [-1, 1] to [a, b] - x = (b + a) / 2.0_dp + (b - a) * t / 2.0_dp + x = (b + a)/2.0_dp + (b - a)*t/2.0_dp ! Compute function values at the transformed points do i = 1, n @@ -60,10 +60,10 @@ end function func end do ! Apply the Gaussian-Legendre quadrature formula - integral_result = sum(w * fx) * (b - a) / 2.0_dp + integral_result = sum(w*fx)*(b - a)/2.0_dp ! Deallocate fx array - deallocate(fx) + deallocate (fx) end subroutine gauss_legendre_quadrature @@ -74,7 +74,7 @@ subroutine gauss_legendre_weights(t, w, n) real(dp), intent(out), dimension(n) :: t, w !! Nodes (t) and weights (w) ! Predefined nodes and weights for different values of n - select case(n) + select case (n) case (1) t = [0.0_dp] !! Single node at the center for n = 1 w = [2.0_dp] !! Weight of 2 for the single point diff --git a/modules/maths/numerical_integration/midpoint.f90 b/modules/maths/numerical_integration/midpoint.f90 index 2612851..85d82e4 100644 --- a/modules/maths/numerical_integration/midpoint.f90 +++ b/modules/maths/numerical_integration/midpoint.f90 @@ -41,13 +41,13 @@ end function func end interface ! Step size - h = (b - a) / (1.0_dp*n) + h = (b - a)/(1.0_dp*n) ! Allocate array for midpoints - allocate(x(1:n), fx(1:n)) + allocate (x(1:n), fx(1:n)) ! Calculate midpoints - x = [(a + (i - 0.5_dp) * h, i = 1, n)] + x = [(a + (i - 0.5_dp)*h, i=1, n)] ! Apply function to each midpoint do i = 1, n @@ -55,10 +55,10 @@ end function func end do ! Final integral value - integral_result = h * sum(fx) + integral_result = h*sum(fx) ! Deallocate arrays - deallocate(x, fx) + deallocate (x, fx) end subroutine midpoint diff --git a/modules/maths/numerical_integration/monte_carlo.f90 b/modules/maths/numerical_integration/monte_carlo.f90 index 8563e8c..8856180 100644 --- a/modules/maths/numerical_integration/monte_carlo.f90 +++ b/modules/maths/numerical_integration/monte_carlo.f90 @@ -43,11 +43,11 @@ end function func end interface ! Allocate arrays for random samples and function values - allocate(uniform_sample(1:n), fx(1:n)) + allocate (uniform_sample(1:n), fx(1:n)) ! Generate uniform random points in [a, b] call random_number(uniform_sample) - uniform_sample = a + (b - a) * uniform_sample !! Scale to the interval [a, b] + uniform_sample = a + (b - a)*uniform_sample !! Scale to the interval [a, b] ! Evaluate the function at all random points in parallel !$omp parallel do !! OpenMP parallelization to distribute the loop across multiple threads @@ -61,13 +61,13 @@ end function func sum_fx_squared = sum(fx**2) ! Compute the Monte Carlo estimate of the integral - integral_result = (b - a) * (sum_fx / real(n, dp)) + integral_result = (b - a)*(sum_fx/real(n, dp)) ! Estimate the error using the variance of the function values - error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2) / (n-1)) * (b-a) + error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2)/(n - 1))*(b - a) ! Deallocate arrays - deallocate(uniform_sample, fx) + deallocate (uniform_sample, fx) end subroutine monte_carlo diff --git a/modules/maths/numerical_integration/simpson.f90 b/modules/maths/numerical_integration/simpson.f90 index 1e2052a..a65b3d2 100644 --- a/modules/maths/numerical_integration/simpson.f90 +++ b/modules/maths/numerical_integration/simpson.f90 @@ -44,18 +44,18 @@ end function func ! Check if n is even if (mod(n, 2) /= 0) then - write(*, *) 'Error: The number of panels (n) must be even.' + write (*, *) 'Error: The number of panels (n) must be even.' stop end if ! Step size - h = (b - a) / (1.0_dp*n) + h = (b - a)/(1.0_dp*n) ! Allocate arrays - allocate(x(0:n), fx(0:n)) + allocate (x(0:n), fx(0:n)) ! Create an array of x values, contains the endpoints and the midpoints. - x = [(a + i * h, i = 0, n)] + x = [(a + i*h, i=0, n)] ! Apply the function to each x value do i = 0, n @@ -63,10 +63,10 @@ end function func end do ! Apply Simpson's rule using array slicing - integral_result = (fx(0) + fx(n) + 4.0_dp * sum(fx(1: n-1: 2)) + 2.0_dp * sum(fx(2: n-2: 2))) * (h / 3.0_dp) + integral_result = (fx(0) + fx(n) + 4.0_dp*sum(fx(1:n - 1:2)) + 2.0_dp*sum(fx(2:n - 2:2)))*(h/3.0_dp) ! Deallocate arrays - deallocate(x, fx) + deallocate (x, fx) end subroutine simpson end module simpson_rule diff --git a/modules/maths/numerical_integration/trapezoid.f90 b/modules/maths/numerical_integration/trapezoid.f90 index f2f3a11..8f13d7c 100644 --- a/modules/maths/numerical_integration/trapezoid.f90 +++ b/modules/maths/numerical_integration/trapezoid.f90 @@ -42,13 +42,13 @@ end function func end interface ! Step size - h = (b - a) / (1.0_dp*n) + h = (b - a)/(1.0_dp*n) ! Allocate arrays - allocate(x(0:n), fx(0:n)) + allocate (x(0:n), fx(0:n)) ! Create an array of x values - x = [(a + i * h, i = 0, n)] + x = [(a + i*h, i=0, n)] ! Apply the function to each x value do i = 0, n @@ -56,10 +56,10 @@ end function func end do ! Apply trapezoidal rule using array slicing - integral_result = ((fx(0) + fx(n)) * 0.5_dp + sum(fx(1: n))) * h + integral_result = ((fx(0) + fx(n))*0.5_dp + sum(fx(1:n)))*h ! Deallocate arrays - deallocate(x, fx) + deallocate (x, fx) end subroutine trapezoid end module trapezoidal_rule