Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def rqi(A, x, k, verbose=False):
- partida = time()
- """
- Program 12.3 Rayleigh Quotient Iteration
- Input: matrix A, initial (nonzero) vector x, number of steps k
- Output: eigenvalue lam, eigenvector of inv(A-sI)
- """
- if verbose: print("Rayleigh Quotient Iteration\n%s"%('='*80))
- for j in range(k):
- u = x/np.linalg.norm(x)
- lam = np.dot(u.T, np.dot(A, u))
- try:
- x = solve(A -lam*np.eye(*A.shape), u)
- except np.linalg.LinAlgError:
- break
- if verbose: print("k=%d, lambda=%+.3f, u=%s"%(j,lam,str(u.T)))
- u = x/np.linalg.norm(x)
- lam = float(np.dot(u.T, np.dot(A, u)))
- if verbose: print("k=%d, lambda=%+.3f, u=%s\n"%(j+1,lam,str(u.T)))
- final = time()
- tiempo = final - partida
- iteracion = tiempo/k
- return (lam, u, iteracion)
- # Probamos el algoritmo
- lam, v, tiempo = rqi(A, x, k=2)
- print("lambda = {0}".format(lam))
- print("v = {0}".format(v))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement