Re: [R] Numerical Integration in 1D

From: Ravi Varadhan <rvaradhan_at_jhmi.edu>
Date: Fri, 07 Mar 2008 14:10:45 -0500

Hi max,

Prof. Ripley is right. Your problem is that you missed a (-) sign in the exponential. Here is a demonstration showing the agreement between numerical and analytical results:

gx <- function(x, n) exp(-x) * x^n * log(x)

df <- function(n) {integrate(gx, lower=0, upper=Inf, n=n)$val}

library(numDeriv)

fx <- function(x, n) exp(-x) * x^n

gf <- function(n) {integrate(fx, lower=0, upper=Inf, n=n)$val}

> grad(x=6, func=gf)

[1] 1348.405
>
> df(6)

[1] 1348.405
>

Ravi.



Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan_at_jhmi.edu

Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html  



-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Max
Sent: Friday, March 07, 2008 1:41 PM
To: r-help_at_stat.math.ethz.ch
Subject: [R] Numerical Integration in 1D

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?

-Max



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 07 Mar 2008 - 19:29:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 07 Mar 2008 - 19:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive