jose romero
2008-Aug-27 14:23 UTC
[R] Integrate a 1-variable function with 1 parameter (Jose L. Romero)
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
Peter Dalgaard
2008-Aug-27 14:39 UTC
[R] Integrate a 1-variable function with 1 parameter (Jose L. Romero)
jose romero wrote:> 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. > >Will this do? sapply(0:44,function(x)integrate(integrand,x=x, lower=0, upper=10)$value -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Ravi Varadhan
2008-Aug-27 14:42 UTC
[R] Integrate a 1-variable function with 1 parameter (Jose L. Romero)
Here is one way:
integrand <- function (t, x) {
exp(-2*t)*(2*t)^x/(10*factorial(x))
}
x <- 0:44
ans <- sapply(x, function(x) integrate(integrand, lower=0, upper=10, x=x))
cbind(x=x, integral=unlist(ans[1,]), abs.error=unlist(ans[2,]))
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 jose romero
Sent: Wednesday, August 27, 2008 10:24 AM
To: r-help at r-project.org
Subject: [R] Integrate a 1-variable function with 1 parameter (Jose L.
Romero)
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.
jose romero
2008-Aug-27 17:36 UTC
[R] Integrate a 1-variable function with 1 parameter (Jose L. Romero)
To Ravi Varadhan and Peter Dalgaard: Infinite thanks for your help and suggestions- they were very instructive, as i have still to learn on the appropriate use of the apply family of functions. jlrp --- On Wed, 8/27/08, Ravi Varadhan <RVaradhan at jhmi.edu> wrote:> From: Ravi Varadhan <RVaradhan at jhmi.edu> > Subject: RE: [R] Integrate a 1-variable function with 1 parameter (Jose L. Romero) > To: jlaurentum at yahoo.com, r-help at r-project.org > Date: Wednesday, August 27, 2008, 2:42 PM > Here is one way: > > integrand <- function (t, x) { > exp(-2*t)*(2*t)^x/(10*factorial(x)) > } > > x <- 0:44 > ans <- sapply(x, function(x) integrate(integrand, > lower=0, upper=10, x=x)) > cbind(x=x, integral=unlist(ans[1,]), > abs.error=unlist(ans[2,])) > > > 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 jose romero > Sent: Wednesday, August 27, 2008 10:24 AM > To: r-help at r-project.org > Subject: [R] Integrate a 1-variable function with 1 > parameter (Jose L. > Romero) > > 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.
Moshe Olshansky
2008-Aug-28 01:57 UTC
[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.
Ravi Varadhan
2008-Aug-28 13:45 UTC
[R] Integrate a 1-variable function with 1 parameter (Jose L.Romero)
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.