Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Интерполяционный многочлен Лагранжа
- """
- import numpy as np
- import matplotlib.pyplot as plt
- class Interpolation:
- def __init__(self, x_points, y_points, x_Lagrange, y_Lagrange):
- self.x_points = x_points
- self.y_points = y_points
- self.x_Lagrange = x_Lagrange
- self.y_Lagrange = y_Lagrange
- def Lagrange_Interpolating_Polynomial(self, x_array, y_array, x_argument):
- y_argument = 0
- for i in range(len(y_array)):
- p1 = 1; p2 = 1
- for j in range(len(x_array)):
- if i == j:
- p1 = p1; p2 = p2
- else:
- p1 *= (x_argument - x_array[j])
- p2 *= (x_array[i] - x_array[j])
- y_argument += y_array[i] * p1 / p2
- return y_argument
- def Plot(self):
- x_interpolation = np.linspace(np.min(self.x_points), np.max(self.x_points), 100)
- y_interpolation = [self.Lagrange_Interpolating_Polynomial(self.x_Lagrange, self.y_Lagrange, i) for i in x_interpolation]
- plt.plot(self.x_points, self.y_points, 'o--', x_interpolation, y_interpolation, '-.')
- plt.legend(['Точки', 'Многочлен Лагранжа'])
- plt.grid(True)
- plt.show()
- if __name__ == '__main__':
- x_points = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
- y_points = np.array([88, 136, 188, 244, 304, 368, 436, 508, 584, 664, 748])
- x_Lagrange = np.array([0, 1, 2])
- y_Lagrange = np.array([88, 136, 188])
- interpolation = Interpolation(x_points, y_points, x_Lagrange, y_Lagrange)
- interpolation.Plot()
- #Проверка
- for i in x_points:
- print(i*0.5, interpolation.Lagrange_Interpolating_Polynomial(x_Lagrange, y_Lagrange, i*0.5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement