Divergent Integral in R is solvable in Wolfram

前端 未结 3 1300
梦毁少年i
梦毁少年i 2021-01-26 15:37

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 ed

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 16:00

    Your integrand is significantly nonzero only for a small range around y=0. From ?integrate

    When integrating over infinite intervals do so explicitly, rather than just using a large number as the endpoint. This increases the chance of a correct answer – any function whose integral over an infinite interval is finite must be near zero for most of that interval.

    While you're not strictly integrating over an infinite interval, the same numerical problem applies. And indeed:

    ff <- function(x, y)
    exp(16*x - 8*y - (-y - 0.01458757)^2/0.0001126501)
    
    f <- function(y)
    integrate(ff, lower=-2.5, upper=0, y=y)$value
    
    integrate(Vectorize(f), lower=-Inf, upper=Inf)
    0.001323689 with absolute error < 4.4e-08
    

    It's interesting that the answer is different to that obtained from Wolfram Alpha. I'm not sure who to trust here; on the one hand I've used R's integrate many times and haven't had problems (that I can tell); however as @MrFlick says R isn't a dedicated mathematical solver like Wolfram Alpha.

    You can also set the rel.tol convergence parameter to a more stringent value, say, 1e-7 or 1e-8. This is more important in the inner integral than the outer one, since errors in the former will propagate to the latter. In this case, it didn't make a difference to the final result.

提交回复
热议问题