【问题标题】:Uniroot Upper and Lower values in RR中的Uniroot上限和下限
【发布时间】:2021-08-01 01:29:02
【问题描述】:

我试图在R 中使用uniroot() 找到以下解决方案。

library(rootSolve)
set.seed(2)
y=rgamma(10,5,2)
myfun=function(y,t)as.numeric(integrate(function(x){ ((x^4) * exp(-x/2))/768 },0,upper=2)[1])-t
myfun(y, y)
final_fun=function(y)uniroot(myfun,c(-2, 2),tol=0.01,t=y)
final_fun(y)

但是,我收到以下错误。

 Error in uniroot(myfun, c(-2, 2), tol = 0.01, t = y) : 
  f() values at end points not of opposite sign 

我为upperlower 限制尝试了几个值,但R 给出了相同的错误。我的问题是,如何找到正确的 upperlower 值?感谢您的帮助。

【问题讨论】:

  • 尝试绘制您的myfun 以确定其零点可能在哪里。你确定它甚至有零吗?
  • @Limey 从上下文来看,我猜 OP 认为 uniroot 函数中的 y 参数将从 rgamma 调用创建的 y 中获取值(但它们不会。 ) 我想知道是否打算沿着身份线找到零,但这很难说。如果 OP 在明天之前没有回复,可能需要关闭。

标签: r function integral uniroot


【解决方案1】:

我不认为 y=t 参数会起作用,所以我通过使其成为一个变量的函数来以不同的方式定义函数(因为 y 参数仅用于为其中的 t 提供一个值功能)。请注意,这个表达式是一个常量:

 integrate(function(x){ ((x^4) * exp(-x/2))/768 },0,upper=2)
 $   ----> 0.00366 with absolute error < 4.1e-1:
# So that gives the same result as what was written above, regardless of y values

myfun=function(y)as.numeric(integrate(function(x){ ((x^4) * exp(-x/2))/768 },0,upper=2)[1])-y

 final_fun=function(y)uniroot(myfun,interval =c(-2, 2), tol=0.01)
 final_fun(y)
#----------------------
$root
[1] 0.00366

$f.root
[1] -1.7e-16

$iter
[1] 2

$init.it
[1] NA

$estim.prec
[1] 0.005

我也不认为y 值是从您创建的y 对象从全局环境中提取的。由于您还没有真正解释要解决什么问题,因此很难判断该解决方案对您是否有价值,但也许它会提供您可以使用的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    相关资源
    最近更新 更多