Si22
Si22

Reputation: 231

Compute value of polynomial in R

I have a polynomial 'p' of degree 4.
How can I compute the value of the polynomial 'p' for x = 1:10

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

Upvotes: 3

Views: 306

Answers (2)

BonStats
BonStats

Reputation: 358

The polynom package suggests using the predict function:

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

Upvotes: 1

Rorschach
Rorschach

Reputation: 32446

try

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 is likely an as. function.

Upvotes: 2

Related Questions