Advertisement
High_Light

poliom lagr

Nov 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. X=[-3, -2, -1, 0, 1, 2, 3]
  3. Y=[9, 4, 1, 0, 1, 0, 0]
  4.  
  5. cur_x = 4
  6. def lagr(x, y, cur_x):
  7.     res = 0.0
  8.     n = len(X)
  9.     for i in range(n):
  10.         l_i=1.0
  11.         for j in range(n):
  12.             if i == j:
  13.                 continue
  14.             l_i*=(cur_x-float(x[j]))/(x[i]-x[j])
  15.         res+=y[i]*l_i
  16.     return res
  17.  
  18. X_new = []
  19. Y_new = []
  20. step=0.001
  21. X_start,X_stop = X[0],X[-1]
  22. while X_start<X_stop+step:
  23.     X_new.append(X_start)
  24.     Y_new.append(lagr(X, Y, X_start))
  25.     X_start+=step
  26. plt.plot(X, Y)
  27. plt.plot(X_new,Y_new)
  28. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement