Advertisement
brospresident

Untitled

Dec 9th, 2021
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. #%%
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. rng = np.random.default_rng(seed=1)
  6. n = 5
  7.  
  8. maxIter = 1001 # numarul maxim de iteratii
  9. toleranta = 0.0001 # nivelul de toleranta
  10.  
  11. y = np.random.rand(n) # se genereaza random un vector pentru vectorul propriu
  12. ySave = y # salvez y initial pentru a putea fi folosit la ambele metode
  13.  
  14. def powerMethod(M):
  15.     '''
  16.        Metoda primeste parametrii M si y\n
  17.        M - matricea pe care se lucreaza\n
  18.        y - vector propriu asociat valorii proprii dominante
  19.    '''
  20.  
  21.     y = np.random.rand(n) # se genereaza random un vector pentru vectorul propriu
  22.  
  23.     # Se initializeaza un vector gol pentru erorile pe care le vom calcula mai tarziu
  24.     erori = np.zeros(maxIter)
  25.  
  26.     y = y / np.linalg.norm(y)
  27.  
  28.     i = 0 # numarul curent de iteratii
  29.     e = 1 # eroarea initiala
  30.  
  31.     # Se calculeaza vectorul propriu unitar asociat valorii proprii a matricei
  32.     while e > toleranta:
  33.         if i > maxIter:
  34.             print("S-a atins numarul maxim de iteratii fara a se fi obtinut nivelul prescris al tolerantei")
  35.             break
  36.  
  37.         z = np.dot(M, y)
  38.         z = z / np.linalg.norm(z)
  39.  
  40.         zTrans = np.transpose(z)
  41.         e = abs(1 - abs(zTrans @ y)) # se calculeaza eroarea
  42.         erori[i] = e # se face push in vectorul de erori la eraoarea curenta
  43.  
  44.         y = z # se actualizeaza vectorul propriu
  45.  
  46.         i += 1
  47.  
  48.     print("Vectorul propriu:\n", y)
  49.    
  50.     return erori
  51.  
  52.  
  53. def inversePowerMethod(M):
  54.     '''
  55.        Metoda primeste parametrii M si y\n
  56.        M - matricea pe care se lucreaza\n
  57.        y - vector propriu asociat valorii proprii dominante
  58.    '''
  59.  
  60.     y = ySave / np.linalg.norm(ySave)
  61.     i = 0
  62.     e = 1
  63.  
  64.     erori = np.zeros(maxIter)
  65.  
  66.     while e > toleranta:
  67.         if i > maxIter:
  68.             print("S-a atins numarul maxim de iteratii fara a se fi obtinut nivelul prescris al tolerantei")
  69.             break
  70.  
  71.         miu = np.transpose(y) @ M @ y
  72.  
  73.         In = np.eye(n)
  74.         z = np.linalg.solve(miu * In - M, y)
  75.  
  76.         z = z / np.linalg.norm(z)
  77.  
  78.         zTrans = np.transpose(z)
  79.         e = abs(1 - abs(zTrans @ y)) # se calculeaza eroarea
  80.         erori[i] = e # se face push in vectorul de erori la eraoarea curenta
  81.  
  82.         y = z # se actualizeaza vectorul propriu
  83.         i += 1
  84.  
  85.     print("Vectorul propriu:\n", y)
  86.  
  87.     return erori
  88.  
  89.  
  90.  
  91. M1 = rng.integers(0, 20, size=(n, n))
  92. print("Matricea generata:\n", M1)
  93.  
  94. w, v = np.linalg.eig(M1)
  95. print("V:\n", v)
  96.  
  97. erori1 = powerMethod(M1)
  98. plt.plot(erori1)
  99. plt.title("Eroarea pentru metoda puterii")
  100. plt.show()
  101.  
  102. erori1 = inversePowerMethod(M1)
  103. print("V:\n", v)
  104. plt.plot(erori1)
  105. plt.title("Eroarea pentru metoda puterii inverse")
  106. plt.show()
  107.  
  108. M2 = [
  109.     [1.556, 0.6, 0.8, 6.3, 4.4],
  110.     [0, 1.344, 0.8, 6.1, 8.2],
  111.     [0, 0, 1.345, 0.7, 5.3],
  112.     [0, 0, 0, 1.356, 1.25],
  113.     [0, 0, 0, 0, 1.681]
  114. ]
  115.  
  116. w, v = np.linalg.eig(M2)
  117. print("V:\n", v)
  118.  
  119. erori2 = powerMethod(M2)
  120. plt.plot(erori2)
  121. plt.title("Eroarea pentru metoda puterii")
  122. plt.show()
  123.  
  124. erori2 = inversePowerMethod(M2)
  125. print("V:\n", v)
  126. plt.plot(erori2)
  127. plt.title("Eroarea pentru metoda puterii inverse")
  128. plt.show()
  129.  
  130. # %%
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement