Search code examples
mathhaskellrecursiontaylor-series

Taylor series in haskell


How to write Taylor series recursion data like this one:

fib = 0 : scanl (+) 1 fib

For example, I want promt something like this:

fac n = product[1..n]
sin' x = x : x^3/fac(3) : x^5/fac(5) : ...
sum $ take 10 (sin' (pi/6))

And get sum of 10 first elements of sine Taylor series.


Solution

  • That's not quite the Taylor series for sin! But here's a clue ...

    products = scanl (*) 1 [1..]
    powers x = map (x^) [0..]
    exp' x = zipWith (/) (powers x) products
    
    *Main> sum (take 10 (exp' 1))
    2.7182815255731922