[R] How to double integrate a function in R

David Winsemius dwinsemius at comcast.net
Fri Jul 26 19:21:24 CEST 2013


On Jul 26, 2013, at 9:33 AM, David Winsemius wrote:

> fun2 <- function(x,y)   { (x<y)*(x>0)* (1/(2*pi))*exp(-y/2)* sqrt((x/(y-x)))}
> persp(outer(0:5, 0:6, fun2) )

There does seem to be some potential pathology at the edges of the range, Restricting it to x >= 0.03 removes most of that concern. 

fun2 <- function(x,y)   { (x<y)*(x>0)* (1/(2*pi))*exp(-y/2)* sqrt((x/(y-x)))}
persp(outer(seq(0.01,5,by=.01), seq(.02,6,by=.01), fun2) ,ticktype="detailed")


> fun <- function(x)   { (x[1]<x[2])*(x[1]>0)* (1/(2*pi))*exp(-x[2]/2)*if(x[1]>x[2]){0}else{ sqrt((x[1]/(x[2]-x[1])) )}}
>   adaptIntegrate(fun, lower = c(0.03,0.03), upper =c(5, 6), tol=1e-2 )
$integral
[1] 0.7605703

$error
[1] 0.00760384

$functionEvaluations
[1] 190859

$returnCode
[1] 0

I tried decreasing the tolerance to 1e-3 but the wait exceeds the patience I have allocated to the problem.

-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list