Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib import pyplot as plt
- X=[-3, -2, -1, 0, 1, 2, 3]
- Y=[9, 4, 1, 0, 1, 0, 0]
- cur_x = 4
- def lagr(x, y, cur_x):
- res = 0.0
- n = len(X)
- for i in range(n):
- l_i=1.0
- for j in range(n):
- if i == j:
- continue
- l_i*=(cur_x-float(x[j]))/(x[i]-x[j])
- res+=y[i]*l_i
- return res
- X_new = []
- Y_new = []
- step=0.001
- X_start,X_stop = X[0],X[-1]
- while X_start<X_stop+step:
- X_new.append(X_start)
- Y_new.append(lagr(X, Y, X_start))
- X_start+=step
- plt.plot(X, Y)
- plt.plot(X_new,Y_new)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement