ln(16.1)=ln(16.1+P)(0.81+p)+ln(15.1+P)(0.144)+ln(14.1+P)(0.0064)+ln(8.1+P)(0.0032)+ln(2.1 +P)(0.0004+p)
我想要做的是为给定的值 p(小写)找到 P(大写)。
有没有办法在 R 中找到 P 的解决方案或近似值(如果它无法解决)?
ln(16.1)=ln(16.1+P)(0.81+p)+ln(15.1+P)(0.144)+ln(14.1+P)(0.0064)+ln(8.1+P)(0.0032)+ln(2.1 +P)(0.0004+p)
我想要做的是为给定的值 p(小写)找到 P(大写)。
有没有办法在 R 中找到 P 的解决方案或近似值(如果它无法解决)?
问题可以解决?uniroot
。
首先用表达式定义一个函数,然后用你想要uniroot
的值应用。P
f <- function(x, P){
Const <- log(16.1)
ConstP <- log(15.1+P)*(0.144)+log(14.1+P)*(0.0064)+log(8.1+P)*(0.0032)
X <- log(16.1+P)*(0.81+x)+log(2.1+P)*(0.0004+x)
X + ConstP - Const
}
roots <- vector("list", length = 10)
for(P in 1:10){
roots[[P]] <- tryCatch(uniroot(f, c(-1, 1e3), P = P),
error = function(e) e)
}
ok <- !sapply(roots, inherits, "error")
all(ok)
#[1] TRUE
sapply(roots[ok], '[[', 1)
# [1] 0.0136312608 -0.0003356587 -0.0117852063 -0.0215749891 -0.0301614798 -0.0378226943
# [7] -0.0447436827 -0.0510555144 -0.0568553430 -0.0622177064