rnumerical-integrationintegralwolframalpha

Divergent Integral in R is solvable in Wolfram


I know that I asked the same question before, but as I am pretty new here the question was asked poorly and not reproducible. Therefore I try to do it better here. (If I only edit the old one probably nobody will read it)

I have this double integral that I would like to integrate:Here is a picture

enter image description here

ff<-function(g,t) exp((16)*g)*exp(-8*t-(-t-0.01458757)^2/(0.0001126501))

integrate(Vectorize(function(t) integrate(function(g) 
                                          ff(g,t), -2.5,0)$value), -2, 2)

Running this in R gives me the error:

  the integral is probably divergent

When I try to run the sam function in Wolfram it gives me a proper value: (i had to switch g=x and t=y)

Link:

http://www.wolframalpha.com/input/?i=integration+[%2F%2Fmath%3Aexp%28%2816%29*x%29*exp%28-8*y-%28-y-0.01458757%29^2%2F%280.0001126501%29%29%2F%2F]+[%2F%2Fmath%3Adx+dy%2F%2F]+for+x+from+[%2F%2Fmath%3A-2.5%2F%2F]+to+[%2F%2Fmath%3A0%2F%2F]+for+y+from+[%2F%2Fmath%3A-2%2F%2F]+to+[%2F%2Fmath%3A2%2F%2F]

As you can see it gets a finite result, can somebody help me out here?

enter image description here

I plotted the function on the defined area and couldn't find a singularity issue. see:

library('Plot3D')
x <- seq(-2.5,0, by = 0.01) #to see the peak change to: seq(-0.2,0, by = 0.001)
y <- seq(-2,2, by = 0.01) #"": seq(-0.1,0.1, by = 0.001)
grid <- mesh(x,y) 
z <- with(grid,exp((16)*x)*
  exp(-8*y-(-0.013615734-y-0.001+0.5*0.007505^2*1)^2/(2*0.007505^2)))
persp3D(z = z, x = x, y = y)

Thanks for your help and I hope the question is better structured then the old one.


Solution

  • It's also worth noting that in the integrate.c source file, the description for the error message is

    error messages
    ...
    ier = 5 the integral is probably divergent, or
        slowly convergent. it must be noted that
        divergence can occur with any other value of ier.
    

    so despite the fact the message says "probably-divergent" it seems with your code it is more likely to be slowly convergent.

    Also, you can continue to run when you get this message and extract the error if you set stop.on.error=FALSE

    r <- integrate(Vectorize(function(t) 
        integrate(function(g) ff(g,t), -2.5,0)$value
    ), -2, 2, stop.on.error=FALSE); 
    r$value
    

    R doesn't claim to be a fancy mathematical solver like the Wolfram products such as Mathematica. It doesn't do any symbolic simplifications of integrals and that's the kind of stuff that Wolfram's been perfecting over the years. if you're just looking to numerically solve a bunch of double integrals, programs like Mathematica or Maple are probably better choices. That just doesn't seem to be where R spends as much of its development resources.