P10.2.2Wilkinson's polynomial
Use NumPy's Polynomial class (see Section 6.4) to generate an object representing Wilkinson's polynomial,
\[\begin{align*}
P(x) &= \prod_{i=1}^{20}(x-i) = (x-1)(x-2)\cdots(x-20)\\
&= x^{20} - 210x^{19} + 20615x^{18} + \cdots + 2432902008176640000
\end{align*}\]
from its roots to the available numerical precision; then find the roots of this representation of the polynomial.
Show solution
We can generate Wilkinson's polynomial from its roots using Polynomial.fromroots(). In earlier versions of NumPy this would calculate the coefficients, which could then be used to (re-)find the roots:
In [x]: import numpy as np
In [x]: Polynomial = np.polynomial.polynomial.Polynomial
In [x]: w = Polynomial.fromroots(range(1, 21))
In [x]: w.roots()
Out[x]:
array([ 1. , 2. , 3. , 3.99999998, 5.00000023,
5.99999869, 7.00000553, 7.99996558, 9.00025449, 9.99864996,
11.0048996 , 11.98779702, 13.02274764, 13.96986755, 15.02984391,
15.97805904, 17.0110895 , 17.99607564, 19.00082636, 19.9999193 ])
Note how the smaller roots are reproduced quite well (they are stable with respect to perturbation of the coefficients) but some of the larger ones are very poorly reproduced.