【发布时间】: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
我为upper 和lower 限制尝试了几个值,但R 给出了相同的错误。我的问题是,如何找到正确的 upper 和 lower 值?感谢您的帮助。
【问题讨论】:
-
尝试绘制您的
myfun以确定其零点可能在哪里。你确定它甚至有零吗? -
@Limey 从上下文来看,我猜 OP 认为 uniroot 函数中的 y 参数将从 rgamma 调用创建的 y 中获取值(但它们不会。 ) 我想知道是否打算沿着身份线找到零,但这很难说。如果 OP 在明天之前没有回复,可能需要关闭。
标签: r function integral uniroot