The problem can be solved with ?uniroot
.
First define a function with the expression, then apply uniroot
with the values of P
you want.
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