Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. n = 12
  5. x_coordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  6. y_coordinates = [2, 4, 9, 12, 15, 19, 21, 19, 16, 12, 8, 4]
  7.  
  8. def DrawFunction(x_points, y_points, degree):
  9. # show function
  10. plt.plot(x_points, y_points, color='green', label='Aproksimuota kreive')
  11.  
  12. # show starting points
  13. plt.plot(x_coordinates, y_coordinates, color='blue', linestyle='None', marker='.', markerSize=12, label='Pradiniai taskai')
  14.  
  15. plt.xlabel('x asis')
  16. plt.ylabel('y asis')
  17. plt.title(str(degree) + ' eiles daugianaris')
  18. plt.legend()
  19. plt.show()
  20.  
  21. def GetG(x, degree):
  22. G = []
  23. for i in range(len(x)):
  24. g = []
  25. for j in range(degree):
  26. g.append(np.power(x[i], j))
  27. G.append(g)
  28.  
  29. return G
  30.  
  31. def GetApproximationFunction(degree):
  32. G = GetG(x_coordinates, degree)
  33. c = np.linalg.solve(np.matmul(np.transpose(G), G), np.matmul(np.transpose(G), y_coordinates))
  34. print(c)
  35.  
  36. x_points = []
  37. for x in np.arange(0, n, 0.1):
  38. x_points.append(x)
  39.  
  40. GForResults = GetG(x_points, degree)
  41. y_points = np.matmul(GForResults, c)
  42.  
  43. DrawFunction(x_points, y_points, degree)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement