0

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?

Melvin
  • 9

1 Answers1

4

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
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Thank you for your reply! But I need some help using this. For example, if I were to find P for x=0.005 what would the exact command be using the code you have written. – Melvin Aug 01 '21 at 04:07
  • @Melvin You are now reversing the problem, the code answers the question to find `x` given `P`. Write a function, say `g`, with the variables `x` and `P` swapped. – Rui Barradas Aug 01 '21 at 13:53