Search code examples
rnewtons-method

R: Find roots of polynomial equation


I have this equation in R.

f <- function(x) {first +second*x +third*x^2  +fourth*filter_factor - log(myBITRATE)}

where

first= -5.219078
second = 0.7613156
third = -0.01298033
fourth = -0.05218249
filter_factor = 1
myBITRATE = 184.47

Is there a way to find the roots of this equation?

I need a starting point for the newton-raphson method.


Solution

  • Your function is a second order polynome with one variable, so you can do a trivial calculation:

    delta = second^2 - 4*third*(first + fourth*filter_factor - log(myBITRATE))
    

    Since your delta is strictly positive:

    x1 = (- second - sqrt(delta))/(2*third)
    x2 = (- second + sqrt(delta))/(2*third)
    
    #> x1
    #[1] 36.53336
    #> f(x1)
    #[1] 0
    #> x2
    #[1] 22.11812
    #> f(x2)
    #[1] 8.881784e-16