Skip to content

Instantly share code, notes, and snippets.

@jonsterling
Created September 20, 2010 06:01
Show Gist options
  • Select an option

  • Save jonsterling/587482 to your computer and use it in GitHub Desktop.

Select an option

Save jonsterling/587482 to your computer and use it in GitHub Desktop.
import Prelude
import Control.Applicative
-- These are some sketchy implementations of integral approximations.
-- They work, but are probably in need of refactoring.
-- I repeat: I SUCK at Haskell, so this is really gross.
midApp n a b f = h * (foldl (+) 0 vals) where
h = (b - a)/n
vals = [midF i | i <- [1..n]] where
midF i = let x = (*h) in f $ (x (i - 1) + x i) / 2
simpApp n a b f = h/3 * (foldl (+) 0 vals) where
h = (b - a)/(fromIntegral n)
vals = (val <$> [0,n]) ++ (vals' odd 4) ++ (vals' even 2) where
val i = f $ (fromIntegral i) * h
vals' p m = (*m) <$> val <$> filter p [1..n-1]
trapApp n a b f = h/2 * (foldl (+) 0 vals) where
h = (b - a)/(fromIntegral n)
vals = (val <$> [0,n]) ++ ((*2) <$> val <$> [1..n-1]) where
val i = f $ (fromIntegral i) * h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment