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, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- lst_y = [5, 6, 8, 10, 12, 13, 12, 10, 8, 10, 8, 11, 7, 9, 11, 10, 9, 12, 11, 6]
- lst_x_l = []
- lst_y_l = []
- x = 1
- while x <= 3:
- lst_x_l.append(x)
- lst_y_l.append(Lagrange(x, lst_x[0:6], lst_y[0:6]))
- x += 0.25
- i = 1
- j = 0
- while x <= 20:
- while x <= 4+j:
- lst_x_l.append(x)
- lst_y_l.append(Lagrange(x, lst_x[i:i+6], lst_y[i:i+6]))
- x += 0.25
- j += 1
- if i >= 14:
- i = 14
- else:
- i += 1
- plt.plot(lst_x, lst_y, lst_x_l, lst_y_l)
- plt.xticks(range(1, 22, 1)) # отметки на оси 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