All formulas

Numerical methods

Polynomial evaluation

num.polynomialAlso called polynomial, polyval, horner, evaluate polynomial

p(x)=∑k=0n−1akxkp(x) = \sum_{k=0}^{n-1} a_k x^k

Value at x of the polynomial with coefficients a0 … an−1 in increasing order of degree, the order numpy.polynomial.polynomial.polyval uses. numpy.polyval takes them in decreasing order, so coefficients written for one evaluate the reversed polynomial in the other.

Notation

aa

coefficients, constant term first

a ∈ ℝn

xx

the point

x ∈ ℝ

nn

number of coefficients

Size, at least 1

p(x)p(x)

Result: the polynomial's value

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    a=[1,2,3],x=2a = [1, 2, 3], x = 2

    Result

    p(x)=17\displaystyle p(x) = 17
  2. Given

    a=[0,0,1],x=−3a = [0, 0, 1], x = -3

    Result

    p(x)=9\displaystyle p(x) = 9
  3. Given

    a=[0.5,−1],x=4a = [0.5, -1], x = 4

    Result

    p(x)=−72\displaystyle p(x) = - \frac{7}{2}

    ≈ -3.5

  4. Given

    a=[5],x=10a = [5], x = 10

    Result

    p(x)=5\displaystyle p(x) = 5

Common mistakes

  • Coefficients in decreasing order of degree

    The coefficients are read highest degree first, as numpy.polyval expects.

    Fix: Reverse the coefficient array, or use numpy.polynomial.polynomial.polyval.

  • Powers start at x1

    Coefficient ak multiplies xk+1 instead of xk.

    Fix: Start the powers at x0 = 1.

Source

  • Knuth 1997

    Knuth, Donald E. The Art of Computer Programming, Volume 2: Seminumerical Algorithms. 3rd ed. Addison-Wesley, 1997.