Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
POLYNOMIAL MODELS WITH PYTHON José M. Garrido Department of Computer Science January 2016 College of Computing and Software Engineering Kennesaw State University c 2015 J. M. Garrido Polynomial Models with Python 1 2 General Forms of Polynomial Functions Linear and quadratic equations are special cases of polynomial functions. The degree of a polynomial function is the highest degree among those in its terms. A linear function such as: y = 3x + 8, is a polynomial equation of degree 1 and a quadratic equation, such as: y = 4.8x2 + 3x + 7, is a polynomial function of degree 2; thus the term “second degree equation”. A function such as: y = 2x4 + 5x3 − 3x2 + 7x − 10.5, is a polynomial function of degree 4 because 4 is the highest exponent of the independent variable x. A polynomial function has the general form: y = p1 xn + p2 xn−1 + p3 xn−2 + . . . pk−1 x + pk This function is a polynomial equation of degree n, and p1 , p2 , p3 , . . . pk are the coefficients of the equation and are constant values. Another general form of a polynomial function P(x) is: P (x) = c0 + c1 x + c2 x2 + . . . + cn−1 xn−1 + cn xn In this second form of a polynomial, c0 , c1 , c2 , c3 , . . . cn are the coefficients of the equation and are constant values. In addition to the algebraic form of a polynomial function, the graphical form is also important; a polynomial function is represented by a graph. As mentioned previously, Gnuplot is used for plotting data. 2 The polynomial Module of the numpy Package The polynomial module of the Numpy package provides several functions for manipulating polynomial series. A polynomial function is represented by a vector of coefficients in ascending order. The following is a list of the relevant functions for dealing with polynomials. c 2015 J. M. Garrido Polynomial Models with Python 3 polyval(x,c[,tensor])Evaluate a polynomial at points x. polyval2d(x,y,c) Evaluate a 2-D polynomial at points (x, y). polyval3d(x,y,z,c) Evaluate a 3-D polynomial at points (x, y, z). polygrid2d(x,y,c) Evaluate a 2-D polynomial on the Cartesian product of x and y. polygrid3d(x,y,z,c) Evaluate a 3-D polynomial on the Cartesian product of x, y and z. polyroots(c) Compute the roots of a polynomial. polyfromroots(roots) Generate a monic polynomial with given roots. polyfit(x,y,deg Least-squares fit of a polynomial to data. [,rcond,full,w]) polyvander(x,deg) Vandermonde matrix of given degree. polyvander2d(x,y,deg)Pseudo-Vandermonde matrix of given degrees. polyvander3d(x,y,z, Pseudo-Vandermonde matrix of given dedeg) grees. polyder(c[,m,scl, Differentiate a polynomial. axis]) polyint(c[,m,k,lbnd, Integrate a polynomial. scl,axis]) polyadd(c1,c2) Add one polynomial to another. polysub(c1,c2) Subtract one polynomial from another. polymul(c1,c2) Multiply one polynomial by another. polymulx(c) Multiply a polynomial by x. polydiv(c1,c2) Divide one polynomial by another. polypow(c,pow[, Raise a polynomial to a power. maxpower]) polyline(off,scl) Returns an array representing a linear polynomial. 3 Evaluation of Polynomial Functions With a computer program, a relatively large number of values of x can be used to evaluate the polynomial function for every value of x. The set of values of x that are used to evaluate a polynomial function are taken from an interval a ≤ x ≤ b. Where a is the lower bound of the interval and b is the upper bound. The interval is known as the domain of the polynomial function. In a similar manner, the interval of the values of the function y is known as the range of the polynomial function. A polynomial function is evaluated by using various values of the independent variable x and computing the value of the dependent variable y. In a general mathematical sense, a polynomial function defines y as a function of x. With the appropriate expression, several values of polynomial can be computed and graphs can be c 2015 J. M. Garrido Polynomial Models with Python 4 produced. The functions in the polynomial module of the Numpy package evaluate a polynomial, P(x), using Horner’s method for stability. Using the general form of the polynomial of degree n: P (x) = c0 + c1 x + c2 x2 + . . . + cn−1 xn−1 + cn xn Function polyval evaluates polynomials with real coefficients for the real variable x. The function returns the values of P(x) for the given values in list x. The following assignment statement has a call to function polyval that evaluates a polynomial at the values in list x with the coefficients in list c . y = polyval (x, c) The function computes the values of the polynomial (values of y) for all the given values of x. The following example evaluates the polynomial function y = 2x3 − 3x2 − 36x + 14 and the values in the two arrays x and y are computed by the program in file polyval1.py. Only 20 values of x are evaluated in this example. The following listing is the output produced by the program. $ python polyval1.py Coefficient list [ 14. -36. -3. 2.] Evaluating a polynomial -6.0 -310.0 -5.31578947368 -179.827525878 -4.63157894737 -82.3265782184 -3.94736842105 -13.6534480245 -3.26315789474 30.0355736988 -2.57894736842 52.5841959469 -1.89473684211 57.8361277154 -1.21052631579 49.6350779997 -0.526315789474 31.8247557953 0.157894736842 8.24887009768 0.842105263158 -17.2488700977 1.52631578947 -40.8247557953 2.21052631579 -58.6350779997 2.89473684211 -66.8361277154 3.57894736842 -61.5841959469 4.26315789474 -39.0355736988 4.94736842105 4.65344802449 5.63157894737 73.3265782184 c 2015 J. M. Garrido Polynomial Models with Python 5 6.31578947368 170.827525878 7.0 301.0 Figure 1: Graph of the equation y = 14 − 36x − 3x2 + 2x3 . Listing 1 shows the source code of the Python program in file polyval1.py. The program evaluates the polynomial and computes the values in vector y. Line 18 defines the array with the polynomial coefficients: a, b, c, and d. Line 10 defines array x with 20 different values from the interval −6.0 ≤ x ≤ 7.0. Line 14 calls function polyval that computes the value of the function for every value in array x. Listing 1: A program that computes the values of a polynomial. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # This program evaluates a polynomial given # the coefficients in list c # at points in list x # J M Garrido, August 2, 2014 import numpy as np from numpy.polynomial.polynomial import polyval M = 20 # number of data points xi = -6.0 # first value of x xf = 7.0 # final value x = np.linspace(xi, xf, M) c = np.array([14.0, -36.0, -3.0, 2.0]) print "Coefficient list" print c y = polyval(x, c) print "Evaluating a polynomial" c 2015 J. M. Garrido Polynomial Models with Python 6 16 for j in range(M): 17 print x[j], y[j] A graph is easily produced with Gnuplot using the values in arrays of x and y computed. Figure 1 shows the graph of the polynomial function y = 14 − 36x − 3x2 +2x3 with the data computed previously. The graph is produced with the script files polyval1.cgp. To evaluate the polynomial equation y = −2 + 3x5 , the corresponding coefficient vector is [−2.0, 0, 0, 0, 0, 3.0]. The program calls function polyval to evaluate the polynomial in the interval −2.5 ≤ x ≤ 2.5. The program is basically the same as the previous example. The following listing shows the Python interpreter running the program. $ python polyval2.py Coefficient list [-2. 0. 0. 0. 0. 3.] Evaluating polynomial -2.5 -294.96875 -2.23684210526 -169.995597296 -1.97368421053 -91.8482429544 -1.71052631579 -45.9308953696 -1.44736842105 -21.0553407421 -1.18421052632 -8.98659937214 -0.921052631579 -3.98858195139 -0.657894736842 -2.36974585578 -0.394736842105 -2.02875143775 -0.131578947368 -2.00011831867 0.131578947368 -1.99988168133 0.394736842105 -1.97124856225 0.657894736842 -1.63025414422 0.921052631579 -0.011418048612 1.18421052632 4.98659937214 1.44736842105 17.0553407421 1.71052631579 41.9308953696 1.97368421053 87.8482429544 2.23684210526 165.995597296 2.5 290.96875 In a similar manner, executing the command file polyval2.cgp with Gnuplot produces a graph of polynomial equation y = −2 + 3x5 . Figure 2 shows the graph of this polynomial function. c 2015 J. M. Garrido Polynomial Models with Python 7 Figure 2: Graph of the equation y = −2 + 3x5 . 4 Solving Polynomial Functions The solution to quadratic equation, which is a second degree equation, is relatively straight forward. The solution to this equation involves finding two values of x that give y value zero. These two values of x are known as the roots of the function. For higher order polynomial functions, the degree of the polynomial determines the number of roots of the function. A polynomial function of degree 7 will have 7 roots. It is not generally feasible to find the roots of polynomial equations of degree 4 or higher by analytical methods. Function polyroots of the polynomial module computes the complex roots of a polynomial function. This function takes the coefficients vector of the polynomial function as the argument, and returns a vector with the roots. This vector is known as the roots vector. The function implements an algorithm that uses an iterative method to find the approximate locations of roots of polynomials. The only parameter of this function is the array of coefficients of the polynomial function. Listing 2 shows the Python source code of a program that computes the roots of the polynomial function 3 + 3x − 14x2 − 17x3 + 23x4 . The program is stored in file polyrootsp.py. Listing 2: A program that computes the roots of a polynomial function. 1 2 3 4 # This program computes the roots of a polynomial # given the coefficients in list c # J M Garrido, Aug 2, 2014. Program: polyrootsp.py import numpy as np c 2015 J. M. Garrido Polynomial Models with Python 5 6 7 8 9 10 11 12 8 import numpy.polynomial.polynomial as poly c = np.array([3.0, 3.0, -14.0, -17.0, 23.0]) print "Solving a polynomial" print "Coefficient list" print c r = poly.polyroots(c) print "Roots of the polynomial" print r The following output was produced after starting the Python interpreter and running the program. The solution vector, r, has the values of the (complex) roots of the polynomial function. $ python polyrootsp.py Solving a polynomial Coefficient list [ 3. 3. -14. -17. 23.] Roots of the polynomial [-0.43665695-0.19425443j -0.43665695+0.19425443j 0.52528824+0.j 1.08715609+0.j ] For the polynomial function: y = 2x3 − 3x2 − 36x + 14, the following output was produced after starting the Python interpreter and running the program. The solution vector, r, has the values of the (real) roots of the polynomial function. The individual values of vector r are: r0 = −3.7688, r1 = 0.3799, and r2 = 4.8889. $ python polyroots2.py Solving a polynomial Coefficient list [ 14 -36 -3 2] Roots of the polynomial [-3.76883165 0.37990763 4.88892403] The solution to the polynomial equation y = −2 + 3x5 is computed in a similar manner. Four of the roots computed are complex values, which are known as complex roots. $ python polyroots3.py Solving a polynomial Coefficient list [-2. 0. 0. 0. 0. 3.] c 2015 J. M. Garrido Polynomial Models with Python Roots of the polynomial [-0.74600097-0.54200143j -0.74600097+0.54200143j 0.28494702-0.87697674j 0.28494702+0.87697674j 0.92210791+0.j ] c 2015 J. M. Garrido 9