I'm trying to write a three-parameter method that approximates the gamma function over a certain interval. The approximation should be a right-endpoint Riemann sum.
The gamma function is given by:
GAMMA(s) =
inf
INT x^(s-1) * exp(-x) dx
0
The right-endpoint Riemann sum approximation over the interval (0, m) should therefore be:
GAMMA(s) ~
m
SUM ((m/n)*i)^(s-1) * exp(-(m/n)*i) * delta_x where delta_x = (m/n)
i=1
My code is as follows:
def gamma(x = 4.0, n = 100000, m = 2500)
array = *(1..n)
result = array.inject(0) {|sum, i| sum + ((((m/n)*i)**(x-1))*((2.7183)**(-(m/n)*i))*(m/n))}
end
puts gamma
The code should return an approximation for 3! = 6, but instead it returns 0.0. Any ideas where I may be going wrong?