Skip to content

Instantly share code, notes, and snippets.

@gbluma
Created March 12, 2013 04:34
Show Gist options
  • Save gbluma/5140384 to your computer and use it in GitHub Desktop.
Save gbluma/5140384 to your computer and use it in GitHub Desktop.
Simpson's rule to calculate integrals in Idris.
-- Using Simpson's rule to calculate integrals in Idris.
-- Date: March 11, 2013
-- Author: Garrett Bluma
module Simpson
mysum : (Float -> Float) -> Float -> (Float -> Float) -> Float -> Float
mysum term a next b =
if (a > b)
then 0.0
else (term a) + (mysum term (next a) next b);
cube : Float -> Float
cube a = a*a*a
y : (Float -> Float) -> Float -> Float -> Float -> Float
y f a h k = f (a + k*h)
next_n : Float -> Float
next_n n = n + 2.0
simpson : (Float->Float) -> Float -> Float -> Float -> Float
simpson f a b n =
let h = (b - a)/n in
(h / 3.0) * ( (y f a h 0.0)
+ (y f a h n)
+ (4.0 * (mysum (y f a h)) 1.0 next_n (n - 1.0))
+ (2.0 * (mysum (y f a h)) 2.0 next_n (n - 2.0)))
main : IO ()
main =
print $ simpson cube 0.0 1.0 2.0
-- 0.25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment