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):
- self.x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
- self.y = np.array([88, 136, 188, 244, 304, 368, 436, 508, 584, 664, 748])
- self.x_test = np.array([0, 1, 2])
- self.y_test = np.array([88, 136, 188])
- def Lagrange_Interpolating_Polynomial(self, x, y, t):
- z = 0
- for i in range(len(y)):
- p1 = 1; p2 = 1
- for j in range(len(x)):
- if i == j:
- p1 = p1; p2 = p2
- else:
- p1 *= (t-x[j])
- p2 *= (x[i]-x[j])
- z += y[i] * p1/p2
- return z
- def Graphics(self):
- x_interpolation = np.linspace(np.min(self.x), np.max(self.x), 100)
- y_interpolation = [self.Lagrange_Interpolating_Polynomial(self.x_test, self.y_test, i) for i in x_interpolation]
- plt.plot(self.x, self.y, 'o', x_interpolation, y_interpolation)
- plt.legend(['Точки', 'Многочлен Лагранжа'])
- plt.grid(True)
- plt.show()
- if __name__ == '__main__':
- i = Interpolation()
- i.Graphics()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement