allekco

lab3(CM) ver 2.0

Nov 23rd, 2019
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib.pyplot as plt
  2.  
  3.  
  4. def Lagrange(x, lst_x=[], lst_y=[]):
  5.     F = 0
  6.     for k in range(6):
  7.         mult = 1
  8.         for i in range(6):
  9.             if i != k:
  10.                 mult = (x-lst_x[i])/(lst_x[k]-lst_x[i])*mult
  11.         F += lst_y[k]*mult
  12.     return(F)
  13.  
  14.  
  15. lst_x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  16. lst_y = [5, 6, 8, 10, 12, 13, 12, 10, 8, 10, 8, 11, 7, 9, 11, 10, 9, 12, 11, 6]
  17. lst_x_l = []
  18. lst_y_l = []
  19.  
  20. x = 1
  21. while x <= 3:
  22.     lst_x_l.append(x)
  23.     lst_y_l.append(Lagrange(x, lst_x[0:6], lst_y[0:6]))
  24.     x += 0.25
  25.  
  26. i = 1
  27. j = 0
  28. while x <= 20:
  29.     while x <= 4+j:
  30.         lst_x_l.append(x)
  31.         lst_y_l.append(Lagrange(x, lst_x[i:i+6], lst_y[i:i+6]))
  32.         x += 0.25
  33.     j += 1
  34.     if i >= 14:
  35.         i = 14
  36.     else:
  37.         i += 1
  38.  
  39. plt.plot(lst_x, lst_y, lst_x_l, lst_y_l)
  40. plt.xticks(range(1, 22, 1))     # отметки на оси x от 1 до 21; шаг 2
  41. plt.grid(linestyle='--', linewidth=0.5)
  42. plt.title('Interpolation')
  43. plt.xlabel('x')
  44. plt.ylabel('f - исходная , F- полином Лагранжа')
  45. plt.legend('fF', loc=4)
  46. plt.show()
RAW Paste Data