Two points of clarification:
I'm not employed by Insightful. Perhaps I responded to a different
question, but I don't think I replied to a numerical integration question.
So someone *at* Insightful is whom you likely meant.
Regards,
Brad
Brad Biggerstaff, Ph.D.
Mathematical Statistician
Division of Vector-Borne Infectious Diseases
National Center for Infectious Diseases
Centers for Disease Control and Prevention
P.O. Box 2087
Fort Collins, CO 80522-2087
(970) 221-6473
-----Original Message-----
From: Bob Baskin [mailto:BobBaskin@WESTAT.com]
Sent: Thursday, June 28, 2001 7:48 AM
To: 'Dr. Takashi Kikuchi'; s-news@wubios.wustl.edu
Cc: Richard Valliant
Subject: Re: [S] Double integration 2
sorry i did't get back to you yesterday. i asked a similar question 3 weeks
ago and did not summarize because i assumed it would be of no interest
(wrong again).
i received two replies, both helpful. i am extremely grateful to both for
replying.
one was from brad biggerstaff at insightful who found my numerical error and
gave correct code to do the double integral (through a mail problem i have
cleverly deleted his email but i have his code)
the second was from nick ellis who a) corrected my error, b) provided
correct code and most importantly c) provided a warning about overwriting
variables through partial matching in parameter passing. i have pilfered
his email and included it without his permission (my apologies to nick if
this offends) because it gives the both the warning and code for double
integration through s+. the inclusion of the warning was an incredible
stroke of brilliance for which i am eternally grateful.
good luck with the integration
bob
note 1: in my original question there is a numerical error in the exact
value of the double integral.
note 2: in the online help is an example of a double integral
note 3!: the warning is extremely important - i was clobbering the k, which
is exactly what nick warned about doing.
<insert clipped email>
Pass t through the argument list as below (I used tt as the dummy argument
name.)
WARNING: Be careful naming dummy arguments. For instance, calling an
argument k won't work because it'll think you're setting keep.xy. That's
because keep.xy comes before the ... and so needs only a partial match.
> args(integrate)
function(f, lower, upper, subdivisions = 100, rel.tol = .Machine$
double.eps^0.25, abs.tol = rel.tol, keep.xy = F, aux = NULL,
...)
double.integral.demo.fn <-
function()
{
fun <- function(upper, integrand)
{
unlist(lapply(upper, function(upper, integrand)
{
integrate(f = integrand,
lower = 0,
upper = upper, tt=upper)$integral
}
, integrand))
}
return(integrate(f = fun,
lower = 1,
upper = 5,
integrand = function(x,tt) {2*(tt^2)*exp(-x*tt)}
)$integral)
}
25-1+exp(-25)-exp(-1) # exact value
double.integral.demo.fn()
> -----Original Message-----
> From: s-news-owner@lists.biostat.wustl.edu
> [mailto:s-news-owner@lists.biostat.wustl.edu]On Behalf Of Bob Baskin
> Sent: Sunday, 3 June 2001 9:39
> To: support@statsci.com; s-news@wubios.wustl.edu
> Subject: [S] problem passing arguments in integrate()
>
>
> i think this a question about passing arguments and not about
> integration
> but you can be the judge of that.
>
> help for the integrate function gives an example of a double integral:
>
> # Example 3: A double integral demo to evaluate the
> # double integral: integral(0,5) integral(0,t) f(x) dx dt
> # where f(x) is x^2. Result should be 5^4/12, or 52.08333.
>
> double.integral.demo.fn <-
> function()
> {
> fun <- function(upper, integrand)
> {
> unlist(lapply(upper, function(upper, integrand)
> {
> integrate(f = integrand,
> lower = 0,
> upper = upper)$integral
> }
> , integrand))
> }
> return(integrate(f = fun,
> lower = 0,
> upper = 5,
> integrand = function(x)
> {
> x^2
> }
> )$integral)
> }
>
> it is passing the integrand as an inline function
> integrand=function{x^2}
> which is a function of x only. cute trick:)
>
> if the integrand is really a function of both x and t how could it be
> passed?
>
> as an example, use 2*(t^2)*exp(x*t) just to be a pain.
>
> integral(0,5) integral(0,t) 2*(t^2)*exp(x*t) dx dt =
>
> integral(0,5) 2*t*exp(t^2) dt =
>
> exp(25) - 1
>
> if you can't integrate non-separable functions of two
> variables, is there a
> trick for integrating separable functions of two variables?
> by separable i
> mean f(x,y) = g(x)*h(y).
>
> thanks
> bob
> ---------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu. To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message: unsubscribe s-news
>
> -----Original Message-----
> From: Dr. Takashi Kikuchi
> [mailto:takashi.kikuchi@st-hughs.oxford.ac.uk]
> Sent: Thursday, June 28, 2001 7:26 AM
> To: s-news@wubios.wustl.edu
> Subject: [S] Double integration 2
>
>
> Dear All
>
> Please pardon me again to ask a beginner's question about
> double integration
> again. Could you please tell me the S codes for following double
> integration? These integrations involve a variable, not a
> numerical value to
> evaluate the inner integration.
>
> 1)
> f(x,y)= 2- 2*x- 2*y/3 over {x, 0,1} and {y, 0, 3-3x}
> 2)
> f(x,y)= 2- 2*x- 2*y/3 over {x, 0,1} and {y, 3-x, 3-3x}
>
> Cheers,
> Takashi Kikuchi
>
>
> ---------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu. To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message: unsubscribe s-news
>
---------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|