Advertisement
Dari_

method upper relaxation

Nov 17th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. from math import *
  2.  
  3. global eps
  4. global n
  5.        
  6.        
  7. def mistake(xk, xkp):                                #stopping criterion
  8.     norm = 0
  9.     for i in range(n):
  10.         norm += (xk[i] - xkp[i])*(xk[i] - xkp[i])
  11.     return (sqrt(norm) < eps)
  12.  
  13.  
  14. eps = 0.0000001
  15.  
  16. print("input matrix range")          
  17. n = int(input())
  18. print("if you want input from keyboard, press 1,\nif you want to use formula press 2,\nif you want input from file press 3.")
  19. inp = int(input())
  20. print("input iterative parametr")
  21. u = float(input())
  22.  
  23.  
  24. transp = [[0 for j in range(n)] for i in range(n)]
  25. mult = [[0 for j in range(n)] for i in range(n)]
  26. answ = [0 for i in range(n)]
  27. prev = [0 for i in range(n)]
  28. ans_col = [0 for i in range(n)]
  29. mult_ans = [0 for i in range(n)]
  30.  
  31. matrix = [[0 for j in range(n)] for i in range(n)]
  32.  
  33. if (inp == 1):                                                #from terminal
  34.     for i in range(n):
  35.         print("input", i + 1, "line")
  36.         line = list(map(float, input().split()))
  37.         for j in range(n):
  38.             matrix[i][j] = line[j]
  39.             transp[j][i] = matrix[i][j]
  40.         ans_col[i] = line[n]
  41.    
  42.  
  43. elif inp == 2:                                                #using formula
  44.     for i in range(n):
  45.         for j in range(n):
  46.             if (i != j):
  47.                 matrix[i][j] = (i + j) / 65
  48.             else:
  49.                 matrix[i][j] = 275 + j / 15 + i / 50
  50.             transp[j][i] = matrix[j][i]
  51.         ans_col[i] = 750 - (i * i * i)
  52.    
  53. elif inp == 3:                                                 #from file named 'input.txt'
  54.     fin = open('input.txt', 'r')
  55.     for i in range(n):
  56.         line = list(map(float, (fin.readline()).split()))
  57.         for j in range(n):
  58.             matrix[i][j] = line[j]
  59.             transp[j][i] = matrix[i][j]
  60.         ans_col[i] = line[n]        
  61.     fin.close()
  62.  
  63.  
  64. for i in range(n):                                             #multiplied matrix
  65.     for j in range(n):
  66.         mult[i][j] = 0
  67.         for k in range(n):
  68.             mult[i][j] += transp[i][k] * matrix[k][j]
  69.  
  70. for i in range(n):                                             #multiplied answers
  71.     mult_ans[i] = 0
  72.     for k in range(n):
  73.         mult_ans[i] += transp[i][k] * ans_col[k]
  74.  
  75. for i in range(n):
  76.     answ[i] = 0
  77.  
  78. count = 1
  79. for i in range(n):                                           #using method of upper relexation
  80.     prev[i] = answ[i]
  81. for i in range(n):
  82.     summa = 0
  83.     for j in range(i):
  84.         summa += (mult[i][j] * answ[j])
  85.     for j in range(i, n):
  86.         summa += (mult[i][j] * prev[j])
  87.     answ[i] = u * (mult_ans[i] - summa) / mult[i][i] + prev[i]
  88.  
  89.    
  90. while (not mistake(answ, prev)):
  91.     count += 1
  92.     for i in range(n):
  93.         prev[i] = answ[i]
  94.     for i in range(n):
  95.         summa = 0
  96.         for j in range(i):
  97.             summa += (mult[i][j] * answ[j])
  98.         for j in range(i, n):
  99.             summa += (mult[i][j] * prev[j])
  100.         answ[i] = u * (mult_ans[i] - summa) / mult[i][i] + prev[i]
  101.      
  102.  
  103. for i in range(n):
  104.     print("answ[" + str(i + 1) + "] = " + str(answ[i]))
  105.  
  106. print("\nIT =", count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement