I am attempting to make a Lagrange interpolation function however after construction I get the error index 9 is out of bounds for axis 0 with size 9. Why am a receiving this error and how can I fix it to perform my interpolation?
import numpy as np
b = np.arange(3,12)
y = np.arange(9)
from sympy import Symbol
t = Symbol('t')
d = len(b)
def interpolation(x, z):
if len(x) != len(z):
print("Error: the length of x and z is different")
else:
L = 0
for i in range (d 1):
p = 1
for j in range (d 1):
if j != i:
p *= (t-x[j]/(x[i] - x[j]))
L = z[i]*p
print(interpolation(b, y))
CodePudding user response:
Because the first index is a zero you can only go to the index 8 and 9 is then out of bounds. Your 9 indices are 0, 1, 2, 3, 4, 5, 6, 7, 8.
So you should not loop through d 1. Use only d.