daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest May 16th, 2018 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. """
  3. Интерполяционный многочлен Лагранжа
  4. """
  5.  
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8.  
  9. class Interpolation:
  10.    
  11.     def __init__(self, x_points, y_points, x_Lagrange, y_Lagrange):
  12.         self.x_points = x_points
  13.         self.y_points = y_points
  14.         self.x_Lagrange = x_Lagrange
  15.         self.y_Lagrange = y_Lagrange
  16.  
  17.     def Lagrange_Interpolating_Polynomial(self, x_array, y_array, x_argument):
  18.         y_argument = 0
  19.         for i in range(len(y_array)):
  20.             p1 = 1; p2 = 1
  21.             for j in range(len(x_array)):
  22.                 if i == j:
  23.                     p1 = p1; p2 = p2  
  24.                 else:
  25.                     p1 *= (x_argument - x_array[j])
  26.                     p2 *= (x_array[i] - x_array[j])
  27.             y_argument += y_array[i] * p1 / p2
  28.         return y_argument
  29.    
  30.     def Plot(self):
  31.         x_interpolation = np.linspace(np.min(self.x_points), np.max(self.x_points), 100)
  32.         y_interpolation = [self.Lagrange_Interpolating_Polynomial(self.x_Lagrange, self.y_Lagrange, i) for i in x_interpolation]
  33.         plt.plot(self.x_points, self.y_points, 'o--', x_interpolation, y_interpolation, '-.')
  34.         plt.legend(['Точки', 'Многочлен Лагранжа'])
  35.         plt.grid(True)
  36.         plt.show()
  37.                
  38. if __name__ == '__main__':
  39.    
  40.     x_points = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  41.     y_points = np.array([88, 136, 188, 244, 304, 368, 436, 508, 584, 664, 748])
  42.     x_Lagrange = np.array([0, 1, 2])
  43.     y_Lagrange = np.array([88, 136, 188])
  44.     interpolation = Interpolation(x_points, y_points, x_Lagrange, y_Lagrange)
  45.     interpolation.Plot()
  46.     #Проверка
  47.     for i in x_points:
  48.         print(i*0.5, interpolation.Lagrange_Interpolating_Polynomial(x_Lagrange, y_Lagrange, i*0.5))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top