Advertisement
Guest User

Untitled

a guest
May 16th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Интерполяционный полином Лагранжа
  4. """
  5.  
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8.  
  9. class Interpolation:
  10.     def __init__(self):
  11.         self.x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  12.         self.y = np.array([88, 136, 188, 244, 304, 368, 436, 508, 584, 664, 748])
  13.         self.x_test = np.array([0, 1, 2])
  14.         self.y_test = np.array([88, 136, 188])
  15.  
  16.     def Lagrange_Interpolating_Polynomial(self, x, y, t):
  17.         z = 0
  18.         for i in range(len(y)):
  19.             p1 = 1; p2 = 1
  20.             for j in range(len(x)):
  21.                 if i == j:
  22.                     p1 = p1; p2 = p2  
  23.                 else:
  24.                     p1 *= (t-x[j])
  25.                     p2 *= (x[i]-x[j])
  26.             z += y[i] * p1/p2
  27.         return z
  28.    
  29.     def Graphics(self):
  30.         x_interpolation = np.linspace(np.min(self.x), np.max(self.x), 100)
  31.         y_interpolation = [self.Lagrange_Interpolating_Polynomial(self.x_test, self.y_test, i) for i in x_interpolation]
  32.         plt.plot(self.x, self.y, 'o', x_interpolation, y_interpolation)
  33.         plt.legend(['Точки', 'Многочлен Лагранжа'])
  34.         plt.grid(True)
  35.         plt.show()
  36.        
  37.        
  38. if __name__ == '__main__':
  39.     i = Interpolation()
  40.     i.Graphics()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement