4.25. Polynomials

4.25.1. Defining

4.25.1.1. Polynomial of degree three

Listing 4.124. Polynomial of degree three
Ax^3 + Bx^2 + Cx^1 + D = 0
1x^3 + 2x^2 + 3x^1 + 4 = 0
import numpy as np


np.poly1d([1, 2, 3, 4])
# poly1d([1, 2, 3, 4])
../../_images/polynomial-3deg.png

Figure 4.16. Polynomial of degree three Ax^3 + Bx^2 + Cx^1 + D = 0 [NP6]

4.25.1.2. Polynomial of degree six

Listing 4.125. Polynomial of degree six
Ax^6 + Bx^5 + Cx^4 + Dx^3 + Ex^2 + Fx + G = 0
1x^6 + 2x^5 + 3x^4 + 4x^3 + 5x^2 + 6x + 7 = 0
import numpy as np


np.poly1d([1, 2, 3, 4, 5, 6, 7])
# poly1d([1, 2, 3, 4, 5, 6, 7])
../../_images/polynomial-6deg.png

Figure 4.17. Polynomial of degree six Ax^6 + Bx^5 + Cx^4 + Dx^3 + Ex^2 + Fx + G = 0 [NP6]

4.25.2. Find coefficients

  • Find the coefficients of a polynomial with the given sequence of roots

  • Specifying the roots of a polynomial still leaves one degree of freedom, typically represented by an undetermined leading coefficient.

import numpy as np


np.poly([0, 0, 0])
# array([1., 0., 0., 0.])

np.poly([1, 2])
# array([ 1., -3.,  2.])

np.poly([1, 2, 3, 4, 5, 6, 7])
# array([ 1.0000e+00, -2.8000e+01,  3.2200e+02, -1.9600e+03,  6.7690e+03,
#        -1.3132e+04,  1.3068e+04, -5.0400e+03])

4.25.3. Roots

  • Return the roots of a polynomial

import numpy as np


np.roots([1, 2])
# array([-2.])

np.roots([0, 1, 3])
# array([-3.])

np.roots([1, 4, -2, 3])
# array([-4.57974010+0.j , 0.28987005+0.75566815j, 0.28987005-0.75566815j])

np.roots([ 1, -11, 9, 11, -10])
#array([10.+0.0000000e+00j, -1.+0.0000000e+00j,
#       1.+9.6357437e-09j, 1.-9.6357437e-09j])

4.25.4. Derivatives

4.25.4.1. Derivative of a polynomial

import numpy as np


np.polyder([1./4., 1./3., 1./2., 1., 0.])
# array([ 1., 1., 1., 1.])

4.25.4.2. Antiderivative (indefinite integral) of a polynomial

  • Return an antiderivative (indefinite integral) of a polynomial

import numpy as np


np.polyint([1, 1, 1, 1])
# array([ 0.25 , 0.33333333, 0.5 , 1. , 0. ])

4.25.5. Evaluation

4.25.5.1. Evaluate a polynomial at specific values

  • Compute polynomial values

import numpy as np


np.polyval([1, -2, 0, 2], 4)
# 34

4.25.5.2. Least squares polynomial fit

  • Least squares polynomial fit

import numpy as np


x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [0, 2, 1, 3, 7, 10, 11, 19]

np.polyfit(x, y, 2)
# array([ 0.375 , -0.88690476, 1.05357143])

4.25.6. Polynomial Arithmetic

  • np.polyadd()

  • np.polysub()

  • np.polymul()

  • np.polydiv()

4.25.6.1. Sum of two polynomials

import numpy as np


np.polyadd([1, 2], [9, 5, 4])
# array([9, 6, 6])

4.25.7. Assignments

4.25.7.1. Numpy Polyfit

  • Complexity level: easy

  • Lines of code to write: 4 lines

  • Estimated time of completion: 5 min

  • Solution: solution/numpy_polyfit.py

English
  1. Use data from "Input" section (see below)

  2. Given are points coordinates in Cartesian system

  3. Separate first row (header) from data

  4. Calculate coefficients of best approximating polynomial of 3rd degree

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Dane są koordynaty punktów w układzie kartezjańskim

  3. Odseparuj pierwszy wiersz (nagłówek) do danych

  4. Oblicz współczynniki najlepiej dopasowanego wielomianu 3 stopnia

Input
DATA = [
    ('x', 'y'),
    (-4.0, 0.0),
    (-3.0, 2.5),
    (-2.0, 2.0),
    (0.0, -2.0),
    (2.0, 0.0),
    (3.0, 7.0),
]