0

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 24enter image description here

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.

Olof R
  • 113
  • 4
  • Was `divided_differences` supposed to be used somewhere? – user2357112 Dec 03 '20 at 21:37
  • And what are `X` and `F`? – user2357112 Dec 03 '20 at 21:38
  • I removed the divided_differences part, it is used elsewhere but not crucial for this computation. I've added a description of X and F. Thanks for pointing out inaccuracies – Olof R Dec 03 '20 at 21:41
  • 3
    What is the `n` used in `f(x)`? Is that constant? This question is crying out for a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). If you could supply us with working code that demonstrates this, that would give you a better chance of getting a useful answer. – CryptoFool Dec 03 '20 at 21:42
  • I've added the full code, sorry for being too vague – Olof R Dec 03 '20 at 21:44
  • I've changed the question to focus on the essential problem. – Olof R Dec 03 '20 at 22:00
  • 1
    Thanks for posting the full code. Very infrequently do we ask for a runnable example and actually get one. – CryptoFool Dec 03 '20 at 22:05
  • I don't know a lot about this stuff, but I sometimes come up with answers despite being kinda naive on a subject. Do you know what's up with the red x's not being on the red/green lines. A couple of them don't seem to be even quite on the curve itself. Those sure look like points on the curve that cross those other lines until you get to the far right. Is that unexpected as well...in addition to the lack of smoothness? – CryptoFool Dec 03 '20 at 22:11
  • The crosses should represent the interpolation points. I've trimmed the code to isolate the problem, and added a new graph. Could the fact that it is a 24 degree polynomial be a problem in itself? – Olof R Dec 03 '20 at 22:18
  • Here's something. If you take the +x axis out to even just 2.2 instead of 2, the right end looks much more like the rest of the result. By 2.5, the whole curve is smooth and x's are on the red/green lines. I'm not a math guy...just a computer nerd...so I don't know what any of this actually means. I just like fooling around with code. – CryptoFool Dec 03 '20 at 22:20
  • Whoa! What just happened? You totally changed the question, didn't you? I'm reproducing the graph you had up there before, but now I see something completely different. I'm confused. - Ooops...I see your last comment...sorry. – CryptoFool Dec 03 '20 at 22:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225496/discussion-between-steve-and-olof-r). – CryptoFool Dec 03 '20 at 22:29

0 Answers0