Skip to content

Commit

Permalink
Add composite simpson's rule for integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentSeidel committed Aug 3, 2023
1 parent d66dd2c commit 85f3e96
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ for some of the operations, if that's important to you.
## Integration
Implemented algorithms are:
* Composite trapezoid
* Composite simpson

## Differentiation

Expand Down
20 changes: 19 additions & 1 deletion src/bbs-integration_real.adb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package body BBS.integration_real is
--
function trapezoid(test : test_func; lower, upper : f; steps : Positive) return f is
step_size : constant f := (upper - lower)/f(steps);
accumulator : f := 0.0;
last_func : f := test(lower);
next_func : f;
base : f := lower;
step_size : f := (upper - lower)/f(steps);
begin
for i in 0 .. steps - 1 loop
base := base + step_size;
Expand All @@ -16,4 +16,22 @@ package body BBS.integration_real is
return accumulator;
end;
--
function simpson(test : test_func; lower, upper : f; steps : Positive) return f is
step_size : constant f := (upper - lower)/f(2*steps);
ends : constant f := test(lower) + test(upper);
sum1 : f := 0.0;
sum2 : f := 0.0;
base : f := lower;
begin
for i in 1 .. steps - 1 loop
base := base + step_size;
sum1 := sum1 + test(base);
base := base + step_size;
sum2 := sum2+ test(base);
end loop;
base := base + step_size;
sum1 := sum1 + test(base);
return step_size*(ends + 2.0*sum2 + 4.0*sum1)/3.0;
end;
--
end;
8 changes: 8 additions & 0 deletions src/bbs-integration_real.ads
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ package BBS.integration_real is
-- the composite trapezoid method with the specified number of steps.
--
function trapezoid(test : test_func; lower, upper : f; steps : Positive) return f;
--
-- Integrate the provided function between the lower and upper limits using
-- the composite simpson's rule with the specified number of steps. Note
-- that since simpson's rule evaluates the function in the midpoint of a
-- segment, the effective number of steps is doubled.
--
function simpson(test : test_func; lower, upper : f; steps : Positive) return f;
--
end;

0 comments on commit 85f3e96

Please sign in to comment.