Tuesday, March 23, 2010

Bad and Slow Numeric Integration in Haskell

It's not very fast, but at least it'll work on arbitrary real functions.
module BSIntegration where
import System.Random
avg :: [Float] -> Float
avg x = sum x / (fromIntegral $ length x)
nIntegral f x0 x n= (x -x0) * (avg (take n ys))
where ys = map y ((randoms (mkStdGen 42)) :: [Float])
y a = f (x0 + (a*(x - x0)))

On the Length of a Differentiable Curve

So for a function f on an interval (a,b) the length of the curve produced is given by:
\int_a^b \sqrt{f'(x)^2+1} \, dx


You can try it using f(x)=x and get 2^(1/2) x, how thrilling.