SHARE
TWEET

lab4(CM) ver 1.0

allekco Nov 22nd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib.pyplot as plt
  2.  
  3.  
  4. def sum_aij(x=[]):   # k-степень от 1 до 9 включительно
  5.     sum = []
  6.     for i in range(9+1):
  7.         sum.append([])
  8.         for j in range(9+1):
  9.             a = 0
  10.             for k in range(20):
  11.                 a += x[k]**(i+j)
  12.             sum[i].append(a)
  13.  
  14.     return sum
  15.  
  16.  
  17. def sum_b(x=[], y=[]):
  18.     b = []
  19.     for i in range(9+1):
  20.         sum = 0
  21.         for k in range(20):
  22.             sum += x[k]**i * y[k]
  23.         b.append(sum)
  24.     return b
  25.  
  26.  
  27. def diagonal(a=[], b=[]):
  28.     tmp = 0
  29.     for i in range(9+1):
  30.         if a[i][i] == 0:
  31.             for j in range(9+1):
  32.                 if j == i:
  33.                     continue
  34.                 if (a[j][i] != 0) and (a[i][j] != 0):
  35.                     for k in range(9+1):
  36.                         tmp = a[j][k]
  37.                         a[j][k] = a[i][k]
  38.                         a[i][k] = tmp
  39.                     tmp = b[j]
  40.                     b[j] = b[i]
  41.                     b[i] = tmp
  42.                     break
  43.     return a
  44.  
  45.  
  46. def function(x, a=[]):
  47.     sum = 0
  48.     for i in range(9+1):
  49.         sum += a[i]*x**i
  50.     return sum
  51.  
  52.  
  53. lst_x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  54. lst_y = [5, 6, 8, 10, 12, 13, 12, 10, 8, 10, 8, 11, 7, 9, 11, 10, 9, 12, 11, 6]
  55. aij = []
  56. aij = sum_aij(lst_x)
  57. b = sum_b(lst_x, lst_y)
  58. aij = diagonal(sum_aij(lst_x), sum_b(lst_x, lst_y))
  59.  
  60. for k in range(9+1):
  61.     for i in range(k+1, 9+1):
  62.         if aij[k][k] == 0:
  63.             print("Solution isn't exist")
  64.         M = aij[i][k]/aij[k][k]
  65.         for j in range(k, 9+1):
  66.             aij[i][j] -= M * aij[k][j]
  67.         b[i] -= M * b[k]
  68.  
  69. i = 9
  70. a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  71. while i >= 0:
  72.     i -= 1
  73.     s = 0
  74.     for j in range(i, 9+1):
  75.         s += aij[i][j] * a[j]
  76.     a[i] = (b[i] - s) / aij[i][i]
  77. for i in range(9+1):
  78.     print(a[i])
  79.  
  80. x = 1
  81. lst_x_a = []
  82. lst_y_a = []
  83. while x < 20:
  84.     lst_x_a.append(x)
  85.     lst_y_a.append(function(x, a))
  86.     x += 0.5
  87. lst_x_a.append(20)
  88. lst_y_a.append(function(20, a))
  89.  
  90. plt.plot(lst_x, lst_y, lst_x_a, lst_y_a)
  91. plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top