I'm trying to implement a program which uses interpolation via the divided difference method and Newton polynomials. However I get the following graph for a polynomial of degree 24
when interpolating a function. Code to reproduce this is: the following
import cmath
import numpy as np
import matplotlib.pyplot as plt
from numpy.polynomial import Polynomial as P
from numpy import arange
from numpy import meshgrid
n = 25
def main():
F = [67108864.00961801, -718212653.839357, 3387063287.4524894, -9732233957.732841, 15654609927.640694, -20854784065.308754, 23574145314.553154, -23013899263.30777, 19661056613.705776, -14847776308.452465, 9988671723.257048, -6021375949.105198, 1607902354.9379263, -410853707.789418, 101496137.39764662, -24235365.605056357, 5591333.440087351, -1245606.9150912648, 267749.8051192703, -55390.29772932518, 11142.929793849675, -2009.5080906227304, 471.945647087449, -15.198503830656275, 33.28147698352074, 0.0]
X = [-2.0, -1.967967967967968, -1.907907907907908, -1.82982982982983, -1.6006006006006006, -1.5205205205205206, -1.4404404404404403, -1.3603603603603602, -1.2802802802802802, -1.2002002002002001, -1.12012012012012, -1.04004004004004, 1.0, 1.1191191191191192, 1.1991991991991993, 1.2792792792792793, 1.3593593593593594, 1.4394394394394394, 1.5195195195195195, 1.5995995995995997, 1.6796796796796798, 1.7597597597597598, 1.910910910910911, 1.9629629629629628, 1.995995995995996, 1.997997997997998]
E = [t for t in np.linspace(-2,-1,1000)]+[t for t in np.linspace(1,2,1000)]
I = np.linspace(E[0]-0.5,E[-1]+0.5,1000)
plt.plot(I, [f(x) - compute_interpolation(x, X, F) for x in I])
max = 1.2931496314704418
plt.ylim(-max*1.1,max*1.1)
plt.show()
def compute_interpolation(x, X, F):
p = 0
omega = 1
for k in range(len(X)):
p+=F[k]*omega
if k<len(F):
omega*=(x-X[k])
return p
if __name__=="__main__":
main()
def f(x):
return x**(n+1)
The function f is a polynomial x^n so what I'm plotting is a polynomial however it is very much non-smooth.