[R] Numerical Integration in 1D

Max mnevill at exitcheck.net
Fri Mar 7 20:14:21 CET 2008


Prof Brian Ripley formulated on Friday :
> On Fri, 7 Mar 2008, Max wrote:
>
>> Dear UseRs,
>> 
>> I'm curious about the derivative of n!.
>> 
>> We know that Gamma(n+1)=n! So when on takes the derivative of
>> Gamma(n+1) we get Int(ln(x)*exp(-x)*x^n,x=0..Inf).
>> 
>> I've tried code like
>> 
>>> integrand<-function(x) {log(x)*exp(x)*x^n}
>>> integrate(integrand,lower=0,upper=Inf)
>> 
>> It seems that R doesn't like to integrate for any n, and I was
>> wondering if anyone knew a way around this?
>
> ln(x) e^x x^n is not integrable on (0, Inf).  You presumably slipped over 
> a minus sign, but your definition of gamma(n) is wrong -- see ?gamma.
>
> integrate(function(x) exp(-x)*x^n, lower=0, upper=Inf)
>
> will work for gamma(n+1).

I did miss a minus sign in the integration, which explains part of my 
problems. The function of interest is the derivative of Gamma(n+1) with 
respect to n, but obviously integrated over x from 0 to Infinity.

What happens now is:

> integrand<-function(x) {log(x)*exp(-x)*x^n}
> integrate(integrand,lower=0,upper=Inf)
Error in f(x, ...) : object "n" not found

Any ideas on how to get around this error?



More information about the R-help mailing list