Is there way to find a solution/Approximate the value of P in this equation in R-studio

123 Views Asked by At

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)

What I am trying to do is find P (capital) for a given value p (lower-case).

Is there any way to find a solution or approximate value (if it's unsolvable) for P in R?

1

There are 1 best solutions below

2
Rui Barradas On

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