整合/不合理

时间:2015-05-25 19:51:13

标签: r numerical-methods

所以,我试图计算隐函数的积分 fx01

fx01<-function(y,z0)    pnorm(z0+y)-pnorm(z0-y)-0.5
fx11<-function(z0){
    uniroot(fx01,interval=c(0,10),z0=z0)$root
}
integrate(fx11,lower=1,upper=1.1)$value

我得到了:

Error in uniroot(fx01, interval = c(0, 10), z0 = z0) : 
  f() values at end points not of opposite sign
In addition: Warning messages:
1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
  the condition has length > 1 and only the first element will be used
2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
  the condition has length > 1 and only the first element will be used

现在,我不明白导致这些错误的原因:  函数fx01返回所有的有界标量  z0 in -infty + infty(它是标准正态分布)的值,如果有人检查:

interval=c(0,10)
fx01(min(interval),z0=1)
fx01(max(interval),z0=1)

fx01(min(interval),z0=1.1)
fx01(max(interval),z0=1.1)

的值符号相反。这些错误消息在这种情况下意味着什么?

1 个答案:

答案 0 :(得分:1)

您的fx11功能未正确矢量化。尝试

integrate(Vectorize(fx11),lower=1,upper=1.1)$value

问题是integrate会立即将值向量传递给您尝试集成的函数,它不会单独评估每个点。 z0长度为

时,您的函数有效
uniroot(fx01,interval=c(0,10),z0=1)$root
# [1] 1.050555

但不是当它是值的向量

uniroot(fx01,interval=c(0,10),z0=c(1,1.05))$root
# Error in uniroot(fx01, interval = c(0, 10), z0 = c(1, 1.05)) : 
#   f() values at end points not of opposite sign
# In addition: Warning messages:
# 1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
#   the condition has length > 1 and only the first element will be used
# 2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
#   the condition has length > 1 and only the first element will be used

Vectorize()函数将分别运行z0的每个值。