Anupznk

tamimbot

Jul 2nd, 2021
14
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3.  
  4.  
  5. def showGraphScatter(x, y):
  6. plt.scatter(x, y, color='red')
  7. plt.show()
  8.  
  9.  
  10. def polynomialFunc(x, a_0, a_1, a_2):
  11. return a_0 + a_1 * x + a_2 * x ** 2
  12.  
  13.  
  14. def showGraph(lowerLim, upperLim, a_0, a_1, a_2, xarr, yarr):
  15. x = np.arange(lowerLim, upperLim, .1)
  16. y = []
  17. for i in range(x.size):
  18. y.append(polynomialFunc(x[i], a_0, a_1, a_2))
  19. plt.plot(x, y, color='blue')
  20. # plt.ylim(0, 0.4)
  21. plt.xlabel('x')
  22. plt.ylabel('y')
  23. plt.scatter(xarr, yarr, color='red')
  24. plt.grid(True, which='both')
  25. plt.axhline(y=0, color='green')
  26. plt.axvline(x=0, color='green')
  27. plt.show()
  28.  
  29.  
  30. def gaussianElimination(A, B, d=0):
  31. # FORWARD ELIMINATION
  32. i = 0
  33. n = B.size
  34. while i < n:
  35. j = i + 1
  36. while j < n:
  37. # RATIO = R2/R1
  38. r = A[j][i] / A[i][i]
  39.  
  40. k = 0
  41. while k < n:
  42. # (R2 = R2 - R1 * r)
  43. A[j][k] -= r * A[i][k]
  44.  
  45. k += 1
  46.  
  47. B[j] -= r * B[i]
  48. if d:
  49. print('Coefficient Matrix:')
  50. print(A)
  51. print('Constant Matrix:')
  52. print(B)
  53.  
  54. j += 1
  55.  
  56. i += 1
  57.  
  58. # BACK SUBSTITUTION
  59. solution = np.zeros(n)
  60. # CALCULATING THE VALUE OF THE LAST VARIABLE
  61. solution[n - 1] = B[n - 1] / A[n - 1][n - 1]
  62.  
  63. # FINDING THE REST OF THE SOLUTIONS USING THE PREVIOUS SOLUTIONS
  64. i = n - 2
  65. while i >= 0:
  66. solution[i] = B[i]
  67. j = i + 1
  68. while j < n:
  69. solution[i] = solution[i] - A[i][j] * solution[j]
  70. j += 1
  71. solution[i] /= A[i][i]
  72. i -= 1
  73.  
  74. return solution
  75.  
  76.  
  77. def polynomialRegressionSp(x, y, numOfDataPoints):
  78. # y = a_0 + a_1x + a_2x^2 (2nd degree polynomial)
  79. sum_x = 0
  80. sum_y = 0
  81. sum_x2 = 0
  82. sum_x3 = 0
  83. sum_x4 = 0
  84. sum_x_y = 0
  85. sum_x2_y = 0
  86.  
  87. for i in range(numOfDataPoints):
  88. sum_x += x[i]
  89. sum_y += y[i]
  90. sum_x2 += x[i] ** 2
  91. sum_x3 += x[i] ** 3
  92. sum_x4 += x[i] ** 4
  93. sum_x_y += x[i] * y[i]
  94. sum_x2_y += x[i] ** 2 * y[i]
  95.  
  96. matA = [[numOfDataPoints, sum_x, sum_x2], [sum_x, sum_x2, sum_x3], [sum_x2, sum_x3, sum_x4]]
  97. matB = [sum_y, sum_x_y, sum_x2_y]
  98. constants = gaussianElimination(matA, matB)
  99. return constants
  100.  
  101.  
  102. def polynomialRegression(x, y, numOfDataPoints, degree):
  103. x = np.array(x)
  104. y = np.array(y)
  105. x_exp_sums = np.zeros(2 * (degree + 1))
  106. x_exp_y_sums = np.zeros(2 * (degree + 1))
  107. x_exp_sums[0] = numOfDataPoints
  108. x_exp_y_sums[0] = sum(y)
  109. #
  110. # for i in range(1, 2 * (degree + 1)):
  111. # x_exp_sums[i] = sum(x_exp_sums[i] * x)
  112. # x_exp_y_sums[i] = sum(y * x_exp_sums[i])
  113.  
  114. for i in range(1, 2 * (degree) + 1):
  115. x_exp_y_sums[i] += y[i] * (x[i] ** i)
  116. for j in range(numOfDataPoints):
  117. x_exp_sums[i] += x[j] ** i
  118.  
  119. matA = np.zeros((degree + 1, degree + 1))
  120. matB = np.zeros(degree + 1)
  121.  
  122. for i in range(degree + 1):
  123. matB[i] = x_exp_y_sums[i]
  124. for j in range(degree + 1):
  125. matA[i][j] = x_exp_sums[i + j]
  126.  
  127. print(matA)
  128. print(matB)
  129.  
  130. return gaussianElimination(matA, matB)
  131.  
  132.  
  133. x = [80, 40, -40, -120, -200, -280, -340]
  134. y = [6.47e-6, 6.24e-6, 5.72e-6, 5.09e-6, 4.30e-6, 3.33e-6, 2.45e-6]
  135. # showGraphScatter(x, y)
  136. constants = polynomialRegression(x, y, 7, 2)
  137. showGraph(-400, 200, constants[0], constants[1], constants[2], x, y)
  138. print('result', constants)
  139.  
RAW Paste Data