[R] how to compute maximum of fitted polynomial?

Hans W Borchers hwborchers at googlemail.com
Wed Jun 5 13:41:53 CEST 2013


David Winsemius <dwinsemius <at> comcast.net> writes:

> [...]
> 
> On Jun 4, 2013, at 10:15 PM, Hans W Borchers wrote:
> 
> > In the case of polynomials, "elementary math ... methods" can  
> > actually be
> > executed with R:

    library(polynomial)                 # -6 + 11*x - 6*x^2 + x^3
    p0 <- polynomial(c(-6, 11, -6, 1))  # has zeros at 1, 2, and 3
    p1 <- deriv(p0); p2 <- deriv(p1)    # first and second derivative
    xm <- solve(p1)                     # maxima and minima of p0
    xmax = xm[predict(p2, xm) < 0]      # select the maxima
    xmax                                # [1] 1.42265

> These look like the functions present in the 'polynom' package  
> authored by Bill Venables [aut] (S original), Kurt Hornik [aut, cre]  
> (R port), Martin Maechler. I wasn't able to find a 'polynomial'  
> package on CRAN. The 'mpoly' package by David Kahle offers  
> multivariate symbolic operations as well.
> 

Sorry, yes of course, it should be `library(polynom)`.
Somehow I'm making this mistake again and again.
And one has to be a bit careful about complex roots.


Hans Werner



More information about the R-help mailing list