I'm trying to define the trapezoid rule in Haskell. I created a helper function innerSum, which is sum portion of the trapezoid rule. Then in the integral definition I multiple by the distance and take in the lower bound, upper bound, a function, and some n number of trapezoids.As n increases the answer should become more accurate. My function seems to work for most cases
except this case (and likely others): definiteIntegral (-1) 1 (\x->x^100) 20.
As I change the value for 20 instead of my answers diverging to a certain and getting more accurate, the numbers just jump randomly. I cannot seem to find the mistake
definiteIntegral :: Double -> Double -> (Double -> Double) -> Integer -> Double
definiteIntegral a b g n | a <= b = (dist a b n)*(innerSum a b g (dist a b n))
| otherwise = (dist a b n)*(innerSum b a g (dist b a n))
where dist a b n = (b-a)/(fromIntegral n::Double)
innerSum :: Double -> Double -> (Double -> Double) -> Double -> Double
innerSum a b g d | a >= b = 0
| otherwise = (((g a) + (g (a + d)))/2)+(innerSum (a + d) b g d)