Wilkinson's polynomial is defined as $$ W(x) = P_{N}(x) = \prod_{m=1}^{N}(x-m) $$ where $ N = 20 $
It is defined by the $(N+1) \;$ - dimensional array of coefficients $ p $ : $$ $$ $$ P_{N}(x) = p[0] x^{N} + p[1] x^{N-1} + \cdots + p[N] $$
import numpy as np
N = 10
Initial value $P_1 (x) = (x - 1)$
p = np.array([1,-1])
Recursion: $p_n = [ p_{n-1},0 ] - [ 0, n \cdot p_{n-1} ]$
# arrays are zero-based
for n in range(2, N+1):
p = np.append(p,[0]) - np.append([0], p*n)
Now find the roots
r = np.roots(p)
print r
The same with $ N = 20 $
N = 20
p = np.array([1,-1])
for n in range(2, N+1):
p = np.append(p,[0]) - np.append([0], p*n)
x = np.arange(1,21)
print x
z = np.polyval(p,x)
print z
r = np.roots(p)
print r
Double precision (float - shorthand for float64)
p = np.array([1.0,-1.0],dtype=float)
for n in range(2, 21):
p = np.append(p,[0.0]) - np.append([0.0], p*n)
x = np.arange(1,21,dtype=float)
print x
z = np.polyval(p,x)
print z
xx = np.arange(0.5, 20.5, 0.05, dtype=float)
yy = np.zeros_like(xx)
for i in range(xx.size):
y = np.polyval(p,xx[i])
yy[i] = sign(y)*log(1.0 + abs(y))
zz = np.zeros_like(xx)
plot(xx,yy, xx,zz)
title('Log of Wilkinson''s polynomial')
xlabel('$x$')
r = np.roots(p)
print r
Now we modify the coefficient at $ x^{19}$
p[1] = p[1] - 2**(-23)
r = np.roots(p)
print r
xx = np.arange(0.5, 21.5, 0.05, dtype=float)
yy = np.zeros_like(xx)
for i in range(xx.size):
y = np.polyval(p,xx[i])
yy[i] = sign(y)*log(1.0 + abs(y))
zz = np.zeros_like(xx)
plot(xx,yy, xx,zz)
title('Modified Wilkinson''s polynomial')
xlabel('$x$')