Skip to content
Prev 318560 / 398503 Next

solving x in a polynomial function

Hi Peter,

With the edit you suggested, now I'm just getting back the value of a
that I put in, not the expected value of b...
a   b
 [1,]  1 1.0
 [2,]  2 2.0
 [3,]  3 2.5
 [4,]  4 3.0
 [5,]  5 3.5
 [6,]  6 4.0
 [7,]  7 6.0
 [8,]  8 7.0
 [9,]  9 7.5
[10,] 10 8.0

#a of 5 should return b of 3.5
+         is.zero <- function(x, tol = .Machine$double.eps^0.5) abs(x) < tol
+         if(names(coef(model))[1] == "(Intercept)")
+
+                 r <- polyroot(c(coef(model)[1] - b, coef(model)[-1]))
+         else
+                 r <- polyroot(c(-b, coef(model)))
+         Re(r[is.zero(Im(r))])
+ }
1 2
5 5


This function just returns what I feed it as written.

Mike
On 3/1/13, Peter Ehlers <ehlers at ucalgary.ca> wrote: