Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- n = 12
- x_coordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
- y_coordinates = [2, 4, 9, 12, 15, 19, 21, 19, 16, 12, 8, 4]
- def DrawFunction(x_points, y_points, degree):
- # show function
- plt.plot(x_points, y_points, color='green', label='Aproksimuota kreive')
- # show starting points
- plt.plot(x_coordinates, y_coordinates, color='blue', linestyle='None', marker='.', markerSize=12, label='Pradiniai taskai')
- plt.xlabel('x asis')
- plt.ylabel('y asis')
- plt.title(str(degree) + ' eiles daugianaris')
- plt.legend()
- plt.show()
- def GetG(x, degree):
- G = []
- for i in range(len(x)):
- g = []
- for j in range(degree):
- g.append(np.power(x[i], j))
- G.append(g)
- return G
- def GetApproximationFunction(degree):
- G = GetG(x_coordinates, degree)
- c = np.linalg.solve(np.matmul(np.transpose(G), G), np.matmul(np.transpose(G), y_coordinates))
- print(c)
- x_points = []
- for x in np.arange(0, n, 0.1):
- x_points.append(x)
- GForResults = GetG(x_points, degree)
- y_points = np.matmul(GForResults, c)
- DrawFunction(x_points, y_points, degree)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement