【问题标题】:How to integrate a function when the lower limit is unknown?下限未知时如何集成一个函数?
【发布时间】:2021-08-26 22:08:20
【问题描述】:

我在大学使用 RStudio。我得到了这个需要帮助的任务:

测量了人们的反应时间。找到以下密度函数:

f (x) = 0.62 * (1 / x)

仅测量了 1 到 5 秒之间的阳性反应时间。
前 30% 的响应时间落在哪个区间 [c, 5] 中?计算c!

通常我会以这种方式整合:

integrand_2 <- function(x) {0.62 * (1/x)}

integrate(integrand, lower = , upper = 5)

但是如你所见,我的问题是下限未知。如何找到这个未知的下限 (c)?

【问题讨论】:

    标签: r numerical-integration


    【解决方案1】:

    你在找这个吗?

    f <- function(z) integrate(function(x) 0.62 / x, z, 5)$value - 0.3
    res <- uniroot(f, c(1, 5))$root
    

    那么我们有

    > res
    [1] 3.081973
    
    > integrate(function(x) 0.62 / x, res, 5)$value
    [1] 0.2999982
    

    【讨论】:

    • 谢谢!你的回答很有帮助。我得到了这个任务的正确答案。但是我有一个问题,你为什么在f函数中写-0.3?
    • @missnobodycares 我需要解决f 的零点,这给出了z 的下限,这样从z5 的积分就是0.3
    【解决方案2】:

    这是一种方法,但结果不是ThomasIsCoding's answer

    pdf <- function(x) {
      0.62/x
    }
    cdf <- function(x){
      integrate(pdf, lower = 1, upper = x)$value
    }
    
    u <- uniroot(function(x) cdf(x) - 0.7, c(1, 5))
    u$root
    #[1] 3.092671
    
    1 - cdf(u$root)
    #[1] 0.2999982
    

    但如果使用正确的归一化常数1/log(5)而不是舍入值0.62,则结果会更接近。

    pdf <- function(x) {
      1/log(5)/x
    }
    # Same cdf
    u <- uniroot(function(x) cdf(x) - 0.7, c(1, 5))
    u$root
    #[1] 3.085178
    
    1 - cdf(u$root)
    #[1] 0.2999982
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2021-02-08
      • 2021-03-19
      • 2015-05-30
      • 2017-09-28
      • 2017-06-06
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多