Solve and equation for a unknown value

Hi
I have an equation and I need to solve it for the value of u. please help
e.g
n=100
m=100
p=0.95

p=(1-u)/(nu+1-u)+(nu*(1-u))/((nu+mu+1-u)(nu+mu+1-2u))

find
u

after much tinkering I found 3 roots

 -0.007555473
 -0.001045225
  0.001686094


code:

library(Ryacas)
library(rootSolve)
library(tidyverse)
poly <- "(1-u)/(n*u+1-u)+(n*u*(1-u))/((n*u+m*u+1-u)*(n*u+m*u+1-2*u)) - p"
poly2 <- stringr::str_replace_all(
  string = poly,
  pattern = "n",
  replacement = "100"
)
poly3 <- stringr::str_replace_all(
  string = poly2,
  pattern = "m",
  replacement = "100"
)
poly4 <- stringr::str_replace_all(
  string = poly3,
  pattern = "p",
  replacement = "0.95"
)


(expr <- yac_expr(poly4))
myfunc <- function(x) {
  eval(expr, list(u = x))
}

(myroots <- rootSolve::uniroot.all(myfunc, c(-1, 1),
  maxiter = 10000, n = 10000,
  tol = 2 * 10^-15
))
goodroots <- which(abs(myfunc(myroots) / 2 * 10^-16) %>% exp() <= 1)
(myroots <- myroots[goodroots])


p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))

(inzoom <- p +
  stat_function(fun = myfunc, n = 500) +
  xlim(min(myroots) * 1.1, max(myroots) * 1.1) +
  ylim(-1, 1) + geom_vline(xintercept = myroots[1], color = "blue") +
  geom_vline(xintercept = myroots[2], color = "red") +
  geom_vline(xintercept = myroots[3], color = "green"))

I'm not sure your use case but outside of R, I find WolframAlpha useful for this sort of problem.

https://www.wolframalpha.com/input/?i=solve+%281-u%29%2F%28100u%2B1-u%29%2B%28100u*%281-u%29%29%2F%28%28100u%2B100u%2B1-u%29*%28100u%2B100u%2B1-2*u%29%29+%3D+0.95+

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.