简体   繁体   中英

Solve Equation in R for L

I have the following equation and would like R to solve for L.

Any thought?

Average = 370.4
m = 2
p = 0.2
n = 5 
#L = ?


log10(Average)  = 0.379933834   -0.107509315* m + 0.104445717   * p + 0.016517169   * n -0.025566689*   L + 0.014393465 * m * p + 0.001601271   * m * n - 0.014250365   * n * L + 0.002523518   * m^2 + 0.237090759 * L^2

Your equation is a quadratic, so the quadratic formula works. Alternatively, you can solve numerically using uniroot :

Average = 370.4
m = 2
p = 0.2
n = 5 
#L = ?

f0 <- function(L) {
  0.379933834 - 0.107509315*m + 0.104445717*p + 0.016517169*n - 0.025566689*L + 0.014393465*m*p + 0.001601271*m*n - 0.014250365*n*L + 0.002523518*m^2 + 0.237090759*L^2 - log10(Average)
}

# solve numerically using uniroot
(nroots <- c(uniroot(f0, c(0, 10))$root, uniroot(f0, c(-10, 0))$root))
#> [1]  3.304099 -2.895724

# solve analytically using the quadratic formula
a <- 0.237090759
b <- -0.025566689 - 0.014250365*n
c <- 0.379933834 - 0.107509315*m + 0.104445717*p + 0.016517169*n + 0.014393465*m*p + 0.001601271*m*n + 0.002523518*m^2 - log10(Average)
(aroots <- (-b + c(1, -1)*sqrt(b^2 - 4*a*c))/(2*a))
#> [1]  3.304084 -2.895724

# check the solutions
f0(c(nroots, aroots))
#> [1]  2.255707e-05 -5.932209e-08  4.440892e-16  4.440892e-16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM