Olof R
Olof R

Reputation: 113

polynomial evaluation producing non-differentiable behavior in Python

I'm trying to evaluate a polynomial of degree 61. However, around the point sqrt(2) I get some weird behavior. The graph of the polynomial becomes erratic. What could be the reason for this. The following python code is used

I = np.sqrt(2)+1j*np.linspace(0,1e-6,1000, dtype = np.clongdouble)
Y = np.abs(np.polynomial.polynomial.polyval(I, coefficients_rev))
plt.plot(np.linspace(0,1,1000),Y)
plt.show()

with coefficients given by

coefficients = np.array([ 1.00000000e+00+0.j,  0.00000000e+00+0.j, -3.04750157e+01+0.j,
    0.00000000e+00+0.j,  4.49106800e+02+0.j,  0.00000000e+00+0.j,
   -4.26240239e+03+0.j,  0.00000000e+00+0.j,  2.92734180e+04+0.j,
    0.00000000e+00+0.j, -1.54973591e+05+0.j,  0.00000000e+00+0.j,
    6.57829830e+05+0.j,  0.00000000e+00+0.j, -2.29933795e+06+0.j,
    0.00000000e+00+0.j,  6.74451933e+06+0.j,  0.00000000e+00+0.j,
   -1.68346568e+07+0.j,  0.00000000e+00+0.j,  3.61319542e+07+0.j,
    0.00000000e+00+0.j, -6.72090548e+07+0.j,  0.00000000e+00+0.j,
    1.08986264e+08+0.j,  0.00000000e+00+0.j, -1.54736506e+08+0.j,
    0.00000000e+00+0.j,  1.92921639e+08+0.j,  0.00000000e+00+0.j,
   -2.11600531e+08+0.j,  0.00000000e+00+0.j,  2.04319983e+08+0.j,
    0.00000000e+00+0.j, -1.73627642e+08+0.j,  0.00000000e+00+0.j,
    1.29668219e+08+0.j,  0.00000000e+00+0.j, -8.48892421e+07+0.j,
    0.00000000e+00+0.j,  4.85309319e+07+0.j,  0.00000000e+00+0.j,
   -2.41002152e+07+0.j,  0.00000000e+00+0.j,  1.03215590e+07+0.j,
    0.00000000e+00+0.j, -3.77608656e+06+0.j,  0.00000000e+00+0.j,
    1.16509284e+06+0.j,  0.00000000e+00+0.j, -2.97955777e+05+0.j,
    0.00000000e+00+0.j,  6.16343028e+04+0.j,  0.00000000e+00+0.j,
   -9.94806340e+03+0.j,  0.00000000e+00+0.j,  1.18267487e+03+0.j,
    0.00000000e+00+0.j, -9.31129650e+01+0.j,  0.00000000e+00+0.j,
    3.72597504e+00+0.j,  0.00000000e+00+0.j], dtype=np.clongdouble)

coefficients_rev = coefficients[::-1]

How can this plot be explained: plot |P(x)| x close to sqrt(2)

I am interested in computing non-classical Chebyshev polynomials minimizing ||z^n+lower.order|| for large n over compact subsets of the complex plane using the remez algorithm generalized by P.T.P Tang. I get a good enough looking plot, however, I also get artifacts at certain points. In the example below the left most point where the graph becomes erratic corresponds to approaching sqrt(2) from imaginary numbers.

full graph

I am simply interested in understanding this behavior from a numerical standpoint and if something can be done to remedy this.

Upvotes: 0

Views: 67

Answers (1)

Makogan
Makogan

Reputation: 9632

This is either that you are hitting floating point rounding errors, like catastrophic cancellation on some of the basis functions.

Or you are hitting a problem of interpolating polynomials, which lagrange interpolation also suffers from, where as the degree of the polynomial increases the function becomes more and more oscillatory over time, trying to fit all values, creating ridiculously high mins and maxes. I.e. as the degree of the fitting polynomial increases, the range of the function starts dwarfing the domain.

Upvotes: 0

Related Questions