Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- def Lagrange(x, lst_x=[], lst_y=[]):
- F = 0
- for k in range(6):
- mult = 1
- for i in range(6):
- if i != k:
- mult = (x-lst_x[i])/(lst_x[k]-lst_x[i])*mult
- F += lst_y[k]*mult
- return(F)
- lst_x = [1, 4, 8, 12, 16, 20]
- lst_y = [5, 10, 10, 11, 10, 6]
- lst_x_l = []
- lst_y_l = []
- x = 1 # начало отрезка
- delta = 0.5
- while x < 20:
- lst_x_l.append(x)
- lst_y_l.append(Lagrange(x, lst_x, lst_y))
- x += delta
- lst_x_l.append(20) # концы отрезка хотим обязательно видеть на графике
- lst_y_l.append(Lagrange(20, lst_x, lst_y))
- plt.plot(lst_x, lst_y, lst_x_l, lst_y_l)
- plt.xticks(range(1, 22, 2)) # отметки на оси x от 1 до 21; шаг 2
- plt.grid(linestyle='--', linewidth=0.5)
- plt.title('Interpolation')
- plt.xlabel('x')
- plt.ylabel('f - исходная , F- полином Лагранжа')
- plt.legend('fF', loc=4)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement