1

我正在使用 R 的积分函数,但被积函数给我带来了一些麻烦(它有一个奇点)。考虑到这一点,

distrib <- function(x){
  dnorm(x, mean=700,sd=50)
}

phi <- function(nu, epsilon=20, maxi=Inf){

  fun <- function(nup)  {
    lambdap <- 1e7/nup
    distrib(lambdap) / (nup*(nup - nu))
  }
  # first part: from epsilon to nu - epsilon
  I1 <- integrate(fun, epsilon, nu-epsilon, rel.tol = 1e-8,
                  subdivisions = 200L)
  # second part: from nu + epsilon to Infty
  I2 <- integrate(fun, nu+epsilon, maxi, rel.tol = 1e-8, 
                  subdivisions = 200L)

  I1$value + I2$value 

}


x <- seq(1e7/500, 1e7/1000, length=200)

phi1 <- sapply(x, phi)    
phi1 <- sapply(x, phi, epsilon=5)
#Error in integrate(fun, epsilon, nu - epsilon, rel.tol = 1e-08, subdivisions = #200L) : 
#  the integral is probably divergent

有没有比使用截止参数并手动调整它直到给出结果(不一定准确)更好的方法来计算 R 中的此类主值?

4

1 回答 1

1

如果您考虑以奇点为中心的区间上的积分,您可以使用变量的变化来使被积函数对称:奇点消失,如论文Numerical evaluation of a generalized Cauchy principal value (A. Nyiri and L. Bayanyi) 中所述, 1999)

# Principal value of \( \int_{x_0-a}^{x_0+a} \dfrac{ f(x) }{ h(x) - h(x0) } dx \)
pv_integrate <- function( f, h, x0, a, epsilon = 1e-6 ) {
  # Estimate the derivatives of f and h
  f1 <- ( f(x0+epsilon) - f(x0-epsilon) ) / ( 2 * epsilon )
  h1 <- ( h(x0+epsilon) - h(x0-epsilon) ) / ( 2 * epsilon )
  h2 <- ( h(x0+epsilon) + h(x0-epsilon) - 2 * h(x0) ) / epsilon^2 
  # Function to integrate. 
  # We just "symmetrize" the integrand, to get rid of the singularity,
  # but, to be able to integrate it numerically, 
  # we have to provide a value at the singularity.
  # Details: http://mat76.mat.uni-miskolc.hu/~mnotes/downloader.php?article=mmn_16.pdf
  g <- function(u) 
    ifelse( u != 0,
      f( x0 - u ) / ( h(x0 - u) - h(x0) ) + f( x0 + u ) / ( h(x0+u) - h(x0) ),
      2 * f1 / h1 - f(x0) * h2 / h1^2
    )
  integrate( g, 0, a )
}

# Compare with the numeric results in the article
pv_integrate( function(x) 1,      function(x) x,    0, 1  ) # 0
pv_integrate( function(x) 1,      function(x) x^3,  1, .5 ) # -0.3425633
pv_integrate( function(x) exp(x), function(x) x,    0, 1  ) # 2.114502
pv_integrate( function(x) x^2,    function(x) x^4,  1, .5 ) # 0.1318667
于 2013-03-07T17:48:09.893 回答