Skip to content

Commit f19eeed

Browse files
committed
Improving efficiency of algorithms for rect, simps and trapez
1 parent e1540f7 commit f19eeed

18 files changed

+26
-42
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ julia:
33
- nightly
44
- 1.4
55
- 1.5
6-
- 1.6
76
os:
87
- linux

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FunctionIntegrator"
22
uuid = "7685536e-2581-4f83-bef1-2ba363c9cb91"
33
authors = ["Brenton Horne <[email protected]>"]
4-
version = "0.5.1"
4+
version = "0.6.0"
55

66
[deps]
77
FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838"

src/Rectangle.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function rectangle_rule_left(f::Function, N::Number, a::Number, b::Number)
1313
y = 0;
1414
x = a;
1515
for i=1:N
16-
y += h*f(x)
16+
y += h*f(x);
1717
x += h;
1818
end
1919
return y
@@ -34,7 +34,7 @@ function rectangle_rule_midpoint(f::Function, N::Number, a::Number, b::Number)
3434
y = 0;
3535
x = a+h/2;
3636
for i=1:N
37-
y += h*f(x)
37+
y += h*f(x);
3838
x += h;
3939
end
4040
return y
@@ -55,7 +55,7 @@ function rectangle_rule_right(f::Function, N::Number, a::Number, b::Number)
5555
y = 0;
5656
x = a+h;
5757
for i=1:N
58-
y += h*f(x)
58+
y += h*f(x);
5959
x += h;
6060
end
6161
return y

src/Simpson.jl

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
function stepwise_simpsons(f::Function, h::Number, x::Number, i::Integer, N::Number)
2-
if i == 1 || i == N + 1
3-
return h / 3 * f(x)
4-
elseif (i % 2) == 1
5-
return 2 * h / 3 * f(x)
6-
else
7-
return 4 * h / 3 * f(x)
8-
end
1+
function stepwise_simpsons(f::Function, h::Number, x::Number)
2+
return h/6 * (f(x) + 4*f(x+h/2) + f(x+h));
93
end
104

115
function stepwise_simpsons38(f::Function, h::Number, x::Number, i::Integer, N::Number)
@@ -28,15 +22,12 @@ uses [Simpson's rule](https://en.wikipedia.org/wiki/Simpson%27s_rule) to approxi
2822
"""
2923
function simpsons_rule(f::Function, N::Number, a::Number, b::Number)
3024
N = convert(Int64, N);
31-
iseven(N) || error("N must be even in order for Simspon's rule to work properly.")
3225
h = (b-a)/N;
3326
y = 0;
3427
x = a;
35-
for i=1:N+1
36-
y = y + stepwise_simpsons(f, h, x, i, N);
37-
if i < N+1
38-
x += h;
39-
end
28+
for i=1:N
29+
y += stepwise_simpsons(f, h, x);
30+
x += h;
4031
end
4132
return y
4233
end

src/Trapezoidal.jl

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
function stepwise_trapezoidal(f, h, x, i, N)
2-
if i == 1 || i == N + 1
3-
return h / 2 * f(x)
4-
else
5-
return h * f(x)
6-
end
2+
return h/2 * (f(x) + f(x+h));
73
end
84

95
"""
@@ -20,11 +16,9 @@ function trapezoidal_rule(f::Function, N::Number, a::Number, b::Number)
2016
h = (b-a)/N;
2117
y = 0;
2218
x = a;
23-
for i=1:N+1
24-
y = y + stepwise_trapezoidal(f, h, x, i, N);
25-
if i < N+1
26-
x += h;
27-
end
19+
for i=1:N
20+
y += stepwise_trapezoidal(f, h, x, i, N);
21+
x += h;
2822
end
2923
return y
3024
end

test/airyai.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ printstyled("Performing the Airy Ai(x) test, where Ai(x) is integrated on the se
3030
printstyled("Running: rombergs_method\n"; color = :magenta)
3131
@time @test rombergs_method(x -> airyai(x), 9, 0, 100) 1.0/3.0
3232
printstyled("Running: simpsons_rule\n"; color = :magenta)
33-
@time @test simpsons_rule(x -> airyai(x), 2512, 0, 100) 1.0/3.0
33+
@time @test simpsons_rule(x -> airyai(x), 1256, 0, 100) 1.0/3.0
3434
printstyled("Running: simpsons38_rule\n"; color = :magenta)
3535
@time @test simpsons38_rule(x -> airyai(x), 3075, 0, 100) 1.0/3.0
3636
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/besselj.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ printstyled("Performing the besselj test, where BesselJ_1(2) is approximated and
3232
printstyled("Running: rombergs_method\n"; color = :magenta)
3333
@time @test rombergs_method(besselj_integrand, 5, 0, pi/2) besselj(1,2)
3434
printstyled("Running: simpsons_rule\n"; color = :magenta)
35-
@time @test simpsons_rule(besselj_integrand, 6, 0, pi/2) besselj(1,2)
35+
@time @test simpsons_rule(besselj_integrand, 3, 0, pi/2) besselj(1,2)
3636
printstyled("Running: simpsons38_rule\n"; color = :magenta)
3737
@time @test simpsons38_rule(besselj_integrand, 9, 0, pi/2) besselj(1,2)
3838
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/cos_cot_integral.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ printstyled("Integrating cos^2(x)/(1+cot(x)) from 0 to pi/2 and comparing the re
3434
printstyled("Running: rombergs_method\n"; color = :magenta)
3535
@time @test rombergs_method(cos_cot_fn, 4, 0, pi/2) 0.25
3636
printstyled("Running: simpsons_rule\n"; color = :magenta)
37-
@time @test simpsons_rule(cos_cot_fn, 78, 0, pi/2) 0.25
37+
@time @test simpsons_rule(cos_cot_fn, 39, 0, pi/2) 0.25
3838
printstyled("Running: simpsons38_rule\n"; color = :magenta)
3939
@time @test simpsons38_rule(cos_cot_fn, 96, 0, pi/2) 0.25
4040
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/cosine.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ printstyled("Integrating cosine from 0 to pi/2 and comparing the result to the a
2828
printstyled("Running: rombergs_method\n"; color = :magenta)
2929
@time @test rombergs_method(x -> cos(x), 3, 0, pi/2) 1
3030
printstyled("Running: simpsons_rule\n"; color = :magenta)
31-
@time @test simpsons_rule(x -> cos(x), 40, 0, pi/2) 1
31+
@time @test simpsons_rule(x -> cos(x), 20, 0, pi/2) 1
3232
printstyled("Running: simpsons38_rule\n"; color = :magenta)
3333
@time @test simpsons38_rule(x -> cos(x), 48, 0, pi/2) 1
3434
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/expnx2datan.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ printstyled("Integrating e^(-x^2)/(1+x^2) on the infinite domain [-inf, inf], or
3434
printstyled("Running: rombergs_method\n"; color = :magenta)
3535
@time @test rombergs_method(x -> exp(-x^2)/(x^2+1), 12, -100, 100) sol_9
3636
printstyled("Running: simpsons_rule\n"; color = :magenta)
37-
@time @test simpsons_rule(x -> exp(-x^2)/(x^2+1), 1240, -100, 100) sol_9
37+
@time @test simpsons_rule(x -> exp(-x^2)/(x^2+1), 620, -100, 100) sol_9
3838
printstyled("Running: simpsons38_rule\n"; color = :magenta)
3939
@time @test simpsons38_rule(x -> exp(-x^2)/(x^2+1), 1767, -100, 100) sol_9
4040
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/gaussian.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ printstyled("Integrate exp(-x^2) from minus infinity to positive infinity and co
3434
printstyled("Running: rombergs_method\n"; color = :magenta)
3535
@time @test rombergs_method(x -> exp(-x^2), 12, -100, 100) sqrt(pi)
3636
printstyled("Running: simpsons_rule\n"; color = :magenta)
37-
@time @test simpsons_rule(x -> exp(-x^2), 536, -100, 100) sqrt(pi)
37+
@time @test simpsons_rule(x -> exp(-x^2), 268, -100, 100) sqrt(pi)
3838
printstyled("Running: simpsons38_rule\n"; color = :magenta)
3939
@time @test simpsons38_rule(x -> exp(-x^2), 780, -100, 100) sqrt(pi)
4040
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/logarithm.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ printstyled("Integrating 1/x from 1 to e and comparing the result to the analyti
2828
printstyled("Running: rombergs_method\n"; color = :magenta)
2929
@time @test rombergs_method(x -> 1/x, 5, 1, exp(1)) 1
3030
printstyled("Running: simpsons_rule\n"; color = :magenta)
31-
@time @test simpsons_rule(x -> x^(-1), 70, 1, exp(1)) 1
31+
@time @test simpsons_rule(x -> x^(-1), 34, 1, exp(1)) 1
3232
printstyled("Running: simpsons38_rule\n"; color = :magenta)
3333
@time @test simpsons38_rule(x -> x^(-1), 84, 1, exp(1)) 1
3434
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/logoverx.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ printstyled("Integrating log(x)/x from 1 to e and comparing the result to the an
2727
printstyled("Running: rombergs_method\n"; color = :magenta)
2828
@time @test rombergs_method(x -> log(x)/x, 5, 1, exp(1)) 0.5
2929
printstyled("Running: simpsons_rule\n"; color = :magenta)
30-
@time @test simpsons_rule(x -> log(x)/x, 100, 1, exp(1)) 0.5
30+
@time @test simpsons_rule(x -> log(x)/x, 50, 1, exp(1)) 0.5
3131
printstyled("Running: simpsons38_rule\n"; color = :magenta)
3232
@time @test simpsons38_rule(x -> log(x)/x, 114, 1, exp(1)) 0.5
3333
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/modbessel0.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ printstyled("Approximating the modified bessel function I_1(1) and comparing it
3636
printstyled("Running: rombergs_method\n"; color = :magenta)
3737
@time @test rombergs_method(dmodified_bessel_1, 5, 0, pi/2) besseli(x,1)
3838
printstyled("Running: simpsons_rule\n"; color = :magenta)
39-
@time @test simpsons_rule(dmodified_bessel_1, 6, 0, pi/2) besseli(x,1)
39+
@time @test simpsons_rule(dmodified_bessel_1, 3, 0, pi/2) besseli(x,1)
4040
printstyled("Running: simpsons38_rule\n"; color = :magenta)
4141
@time @test simpsons38_rule(dmodified_bessel_1, 9, 0, pi/2) besseli(x,1)
4242
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/simppen.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ printstyled("Integrating 1/sqrt(-19.6 sin(x)) from -pi to 0 and comparing the re
3030
printstyled("Running: rombergs_method on [-pi+1e-8, -1e-8]. Only a rough approximation can be realistically achieved with this function, due to the singularities.\n"; color = :magenta)
3131
@time @test abs(rombergs_method(x -> (-19.6*sin(x))^(-0.5), 24, -pi+1e-8, -1e-8) - ellipk(1/2)/sqrt(2.45)) < 1e-4
3232
printstyled("Running: simpsons_rule on [-pi+1e-8, -1e-8]. Only a rough approximation can be realistically achieved with this function, due to the singularities.\n"; color = :magenta)
33-
@time @test abs(simpsons_rule(x -> (-19.6*sin(x))^(-0.5), 1e8, -pi+1e-8, -1e-8) - ellipk(1/2)/sqrt(2.45)) < 1e-4
33+
@time @test abs(simpsons_rule(x -> (-19.6*sin(x))^(-0.5), 5e7, -pi+1e-8, -1e-8) - ellipk(1/2)/sqrt(2.45)) < 1e-4
3434
printstyled("Running: simpsons38_rule on [-pi+1e-8, -1e-8]. Only a rough approximation can be realistically achieved with this function, due to the singularities.\n"; color = :magenta)
3535
@time @test abs(simpsons38_rule(x -> (-19.6*sin(x))^(-0.5), 1e8+2, -pi+1e-8, -1e-8) - ellipk(1/2)/sqrt(2.45)) < 1e-4
3636
printstyled("Running: trapezoidal_rule on [-pi+1e-8, -1e-8]. Only a rough approximation can be realistically achieved with this function, due to the singularities.\n"; color = :magenta)

test/sinexpox.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ printstyled("Integrating sin(x^2)e^(-x)/x from 0 to infinity, with the approxima
4040
printstyled("Running: rombergs_method\n"; color = :magenta)
4141
@time @test rombergs_method(sinexpox, 12, 0, 100) sol_11
4242
printstyled("Running: simpsons_rule\n"; color = :magenta)
43-
@time @test simpsons_rule(sinexpox, 4202, 0, 100) sol_11
43+
@time @test simpsons_rule(sinexpox, 2101, 0, 100) sol_11
4444
printstyled("Running: simpsons38_rule\n"; color = :magenta)
4545
@time @test simpsons38_rule(sinexpox, 5148, 0, 100) sol_11
4646
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/sinxx.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ printstyled("Integrating sin(x)/x from 0 to 100 and comparing it to the exact re
3636
printstyled("Running: rombergs_method\n"; color = :magenta)
3737
@time @test rombergs_method(sinxx, 9, 0, 100) sol_8
3838
printstyled("Running: simpsons_rule\n"; color = :magenta)
39-
@time @test simpsons_rule(sinxx, 678, 0, 100) sol_8
39+
@time @test simpsons_rule(sinxx, 339, 0, 100) sol_8
4040
printstyled("Running: simpsons38_rule\n"; color = :magenta)
4141
@time @test simpsons38_rule(sinxx, 831, 0, 100) sol_8
4242
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

test/test_7.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ printstyled("Integrating (x^3+1)/(x^4 (x+1)(x^2+1)) from 1 to e and comparing th
3535
printstyled("Running: rombergs_method\n"; color = :magenta)
3636
@time @test rombergs_method(partfrac, 6, 1, exp(1)) sol_7
3737
printstyled("Running: simpsons_rule\n"; color = :magenta)
38-
@time @test simpsons_rule(partfrac, 200, 1, exp(1)) sol_7
38+
@time @test simpsons_rule(partfrac, 100, 1, exp(1)) sol_7
3939
printstyled("Running: simpsons38_rule\n"; color = :magenta)
4040
@time @test simpsons38_rule(partfrac, 234, 1, exp(1)) sol_7
4141
printstyled("Running: trapezoidal_rule\n"; color = :magenta)

0 commit comments

Comments
 (0)