s-news
[Top] [All Lists]

Re: Double integration 2

To: "'Dr. Takashi Kikuchi'" <takashi.kikuchi@st-hughs.oxford.ac.uk>, s-news@wubios.wustl.edu
Subject: Re: Double integration 2
From: Bob Baskin <BobBaskin@WESTAT.com>
Date: Thu, 28 Jun 2001 09:47:54 -0400
Cc: Richard Valliant <VALLIAR1@WESTAT.com>
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
> 

<Prev in Thread] Current Thread [Next in Thread>