integrate() in R gives terribly wrong answer

前端 未结 2 1610
礼貌的吻别
礼貌的吻别 2020-12-21 02:02

I was trying to integrate the following function from -infinity to infinity. The answer should be 0.2, but R gives a ridiculously small number. What\'s wrong?



        
2条回答
  •  半阙折子戏
    2020-12-21 02:58

    While in general the advice in ?integrate to explicitly specify +/-Inf as the limits is valid, it may be wrong in special cases. This is one of them.

    > integrate(f, 20, 30)
    0.2 with absolute error < 1.9e-06
    

    The basic problem seems to be that your function is not smooth, in that its derivative is discontinuous at x=25. This may be fooling the algorithm, in particular its use of Wynn's epsilon method to speed up convergence. Basically there's no real substitute to knowing what your function is like, and how its behaviour could cause problems. As pointed out in the answers here, R isn't a symbolic mathematical solver so you do have to exercise more care when trying to get numerical results.

提交回复
热议问题