R 中的发散积分在 Wolfram 中可解

问题描述 投票:0回答:3

我知道我以前问过同样的问题,但由于我是新来的,这个问题问得不好而且不可重现。因此我在这里尝试做得更好。 (如果我只编辑旧的,可能没人会读)

我有这个二重积分,我想积分:这是一张图片

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)

在 R 中运行此命令会出现错误:

  the integral is probably divergent

当我尝试在 Wolfram 中运行 sam 函数时,它给了我一个正确的值:(我必须切换 g=x 和 t=y)

链接:

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]+到+[%2F%2Fmath%3A0%2F%2F]+for+y+from+[%2F%2Fmath%3A-2%2F%2F]+到+[%2F%2Fmath%3A2%2F%2F]

正如你所看到的,它得到了有限的结果,有人可以帮助我吗?

enter image description here

我在定义的区域上绘制了函数,但找不到奇点问题。参见:

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)

感谢您的帮助,我希望这个问题比旧问题的结构更好。

r numerical-integration integral wolframalpha
3个回答
2
投票

还值得注意的是,在integrate.c源文件中,错误消息的描述是

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.

因此,尽管消息说“可能发散”,但您的代码似乎更有可能慢慢收敛。

此外,如果您设置了

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 并不像 Mathematica 等 Wolfram 产品那样声称是一个精美的数学求解器。它没有对积分进行任何符号简化,而这正是 Wolfram 多年来一直在完善的东西。如果您只是想数值求解一堆二重积分,像 Mathematica 或 Maple 这样的程序可能是更好的选择。这似乎并不是 R 花费大量开发资源的地方。


2
投票

您的被积函数仅在 y=0 附近的小范围内显着非零。来自

?integrate

在无限间隔上进行积分时,请明确执行此操作,而不是仅使用较大的数字作为端点。这增加了正确答案的机会 - 任何在无限区间内积分有限的函数在该区间的大部分时间都必须接近于零。

虽然您不是在无限区间上严格积分,但同样的数值问题也适用。确实:

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

有趣的是,答案与从 Wolfram Alpha 获得的答案不同。我不知道该相信谁;一方面,我已经多次使用 R 的

integrate
并且没有遇到任何问题(我可以说);然而,正如 @MrFlick 所说,R 并不是像 Wolfram Alpha 那样的专用数学求解器。

您还可以将

rel.tol
收敛参数设置为更严格的值,例如 1e-7 或 1e-8。这对于内部积分比外部积分更重要,因为前者的误差会传播到后者。在这种情况下,这对最终结果没有影响。


1
投票

对于二重积分,最好使用

cubature
包。

library(cubature)
f <- function(x){
  exp(16*x[1] - 8*x[2] - (x[2] + 0.01458757)^2/0.0001126501)
}

当降低容差时,

hcubature
函数给出的结果不稳定:

> hcubature(f, c(-2.5, -2), c(0,2))$integral
[1] 0.001285129
> hcubature(f, c(-2.5, -2), c(0,2), tol=1e-10)$integral
[1] 0.001293842

pcubature
的结果相反,是稳定的:

> pcubature(f, c(-2.5, -2), c(0,2))$integral
[1] 0.001323689
> pcubature(f, c(-2.5, -2), c(0,2), tol=1e-10)$integral
[1] 0.001323689

p自适应版本,pcubature,反复加倍程度 求积规则直到实现收敛,并且基于 克伦肖-柯蒂斯求积规则的张量积。这个算法是 对于平滑被积函数,通常优于 h 自适应积分 几个(<=3) dimensions, but is a poor choice in higher dimensions or for non-smooth integrands.

接下来,

RcppNumerical
提供了强大的多重集成。

// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppNumerical)]]
#include <RcppNumerical.h>
#include <cmath>
using namespace Numer;

class ValegardIntegrand: public MFunc
{
public:    
  double operator()(Constvec& x)
  {
    return exp(16*x[0] - 8*x[1] - pow(-x[1] - 0.01458757,2)/0.0001126501);
  }
};    

// [[Rcpp::export]]
Rcpp::List Valegard()
{
  ValegardIntegrand f;  
  Eigen::VectorXd lower(2);
  lower << -2.5, -2;
  Eigen::VectorXd upper(2);
  upper << 0.0, 2.0;
  double err_est;
  int err_code;
  double res = integrate(f, lower, upper, err_est, err_code, 
                         10000000, 1e-14, 1e-14);
  return Rcpp::List::create(
    Rcpp::Named("approximate") = res,
    Rcpp::Named("error_estimate") = err_est,
    Rcpp::Named("error_code") = err_code
  );
}

它给出与

pcubature
相同的结果:

> Valegard()
$approximate
[1] 0.001323689

$error_estimate
[1] 9.893371e-14

$error_code
[1] 0

顺便说一下,与 Mathematica 11 的精确集成也提供了这个结果:

Integrate[E^(16 x - 8877.04 (0.0145876 + y)^2 - 8 y), {y, -2, 2}, {x, -2.5, 0}]
0.00132369
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.