Raksha
Raksha

Reputation: 1699

Lagrange interpolation with Python doing something weird

Does anyone know why it's returning such crazy numbers?

from scipy.interpolate import lagrange

x = [2458723, 2458724, 2458725, 2458727, 2458728, 2458729]
y = [-2000, -900, 1000, 400, -4700, -1900]

poly = lagrange(x, y)

x0 = list(np.linspace(min(x), max(x), num=10))
y0 = poly(x0)
print(y0)

output: [-2.88230376e+18 -6.91752903e+18 -3.45876451e+18 -8.07045053e+18 -2.30584301e+18 -2.88230376e+18 -5.76460752e+18 -4.03522527e+18 -2.88230376e+18 -4.03522527e+18]

Upvotes: 0

Views: 145

Answers (1)

Michael
Michael

Reputation: 2414

The implementation may be making assumptions about the domain. Try shifting your domain closer to x=0

from scipy.interpolate import lagrange
import numpy as np

x = np.array([2458723, 2458724, 2458725, 2458727, 2458728, 2458729])
y = np.array([-2000, -900, 1000, 400, -4700, -1900])

poly = lagrange(x - x.min(), y)

x0 = np.linspace(x.min(), x.max(), num=10)
y0 = poly(x0 - x0.min())
print(y0)

Upvotes: 1

Related Questions