From 89024c6670092f597fffa9698a5237e922a69b18 Mon Sep 17 00:00:00 2001 From: vil02 Date: Sat, 4 Nov 2023 09:34:09 +0100 Subject: [PATCH] fix: format new code --- src/math/runge_kutta_integration.jl | 18 ++++++++++++------ test/math.jl | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/math/runge_kutta_integration.jl b/src/math/runge_kutta_integration.jl index c8e2fd36..bcac9e04 100644 --- a/src/math/runge_kutta_integration.jl +++ b/src/math/runge_kutta_integration.jl @@ -58,19 +58,25 @@ julia> exp.([0.0, 0.1]) # Contributors: - [E-W-Jones](https://github.com/E-W-Jones) """ -function runge_kutta_integration(f::Function, x0::Real, y0::Real, h::Real, x_stop::Real) +function runge_kutta_integration( + f::Function, + x0::Real, + y0::Real, + h::Real, + x_stop::Real, +) h > 0 || throw(DomainError(h, "The step size `h` should be >0.")) x = Float64(x0) y = Float64(y0) output_x = [x] output_y = [y] - + while x < x_stop k1 = f(x, y) - k2 = f(x + h/2, y + k1*h/2) - k3 = f(x + h/2, y + k2*h/2) - k4 = f(x + h , y + k3*h ) + k2 = f(x + h / 2, y + k1 * h / 2) + k3 = f(x + h / 2, y + k2 * h / 2) + k4 = f(x + h, y + k3 * h) y += h * (k1 + 2k2 + 2k3 + k4) / 6 x += h @@ -80,4 +86,4 @@ function runge_kutta_integration(f::Function, x0::Real, y0::Real, h::Real, x_sto end return output_x, output_y -end \ No newline at end of file +end diff --git a/test/math.jl b/test/math.jl index 931d5784..ce6b5ab6 100644 --- a/test/math.jl +++ b/test/math.jl @@ -378,12 +378,20 @@ using TheAlgorithms.Math end @testset "Math: Runge_Kutta Integration" begin - @test runge_kutta_integration((x, y)->1, 0, 0, 1, 3) == ([0.0, 1.0, 2.0, 3.0], [0.0, 1.0, 2.0, 3.0]) + @test runge_kutta_integration((x, y) -> 1, 0, 0, 1, 3) == + ([0.0, 1.0, 2.0, 3.0], [0.0, 1.0, 2.0, 3.0]) @test begin - x, y = runge_kutta_integration((x, y)->cos(x), 0, 0, 1e-4, π/2) - isapprox(x[end], π/2; rtol=1e-4) && isapprox(y[end], 1; rtol=1e-4) + x, y = runge_kutta_integration((x, y) -> cos(x), 0, 0, 1e-4, π / 2) + isapprox(x[end], π / 2; rtol = 1e-4) && + isapprox(y[end], 1; rtol = 1e-4) end - @test_throws DomainError runge_kutta_integration((x, y)->(), 0, 0, 0, 0) + @test_throws DomainError runge_kutta_integration( + (x, y) -> (), + 0, + 0, + 0, + 0, + ) end @testset "Math: Sum of Arithmetic progression" begin