[R] Integrate a 1-variable function with 1 parameter (Jose L.Romero)

Ravi Varadhan RVaradhan at jhmi.edu
Thu Aug 28 15:45:10 CEST 2008


Hi,

The answer can be obtained in "closed" form using the "pgamma" function,
which is closely related to the incomplete gamma function, as follows:


integrand <- function (t, x) {
	exp(-2*t)*(2*t)^x/(10*factorial(x))
}

upper <- 10

x <- 0:44

ans1 <- sapply(x, function(x) integrate(integrand, lower=0, upper=upper,
x=x)) 

ans2 <- gamma(x+1) * pgamma(q=2*upper, shape=x+1, rate = 1, scale = 1,
lower.tail = TRUE) / (20*factorial(x))    # using the "pgamma" function 

cbind(x=x, ans1=unlist(ans1[1,]), ans2=ans2)  # both answers are identical


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 Moshe Olshansky
Sent: Wednesday, August 27, 2008 9:58 PM
To: r-help at r-project.org; jlaurentum at yahoo.com
Subject: Re: [R] Integrate a 1-variable function with 1 parameter (Jose
L.Romero)

This can be done analytically: after changing a variable (2*t -> t) and some
scaling we need to compute
f(x) = integral from 0 to 20 of (t^x*exp(-t))dt/factorial(x)

f(0) = int from 0 to 20 of exp(-t)dt = 1 - exp(-20) and integration by parts
yields (for x=1,2,3,...) 

f(x) = -exp(-20)*20^x/factorial(x) + f(x-1) so that

f(x) = 1 - exp(-20)*sum(20^k/factorial(k)) where the sum is for k=0,1,...,x

If I did not a mistake, your original quantity should be f(x)/20.


--- On Thu, 28/8/08, jose romero <jlaurentum at yahoo.com> wrote:

> From: jose romero <jlaurentum at yahoo.com>
> Subject: [R] Integrate a 1-variable function with 1 parameter (Jose L. 
> Romero)
> To: r-help at r-project.org
> Received: Thursday, 28 August, 2008, 12:23 AM Hey fellas:
> 
> I would like to integrate the following function:
> 
> integrand <- function (x,t) {
> 	exp(-2*t)*(2*t)^x/(10*factorial(x))
> }
> 
> with respect to the t variable, from 0 to 10.
> The variable x here works as a parameter: I would like to integrate 
> the said function for each value of x in 0,1,..,44.
> 
> I have tried Vectorize to no avail.
> 
> Thanks in advance,
> jose romero
> 
> ______________________________________________
> 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.



More information about the R-help mailing list