Calculate the value of a polynomial in R

I have a polynomial 'p' of degree 4.
How can I calculate the value of the polynomial 'p' forx = 1:10

require(polynom)  
p <- polynomial(coef = c(1:5))
p 
1 + 2x + 3x^2 + 4x^3 + 5x^4

      

+3


source to share


2 answers


try it

as.function(p)(1:10)
# [1]    15   129   547  1593  3711  7465 13539 22737 35983 54321

      



whenever you want something to be something else in R, there might be a function as.

.

+2


source


The polyom package suggests using the function predict

:



predict(p, newdata = 1:10)
# [1]    15   129   547  1593  3711  7465 13539 22737 35983 54321

      

+1


source







All Articles