# Eigenvalue and eigenvectors calculation

$$
A\mathbf{x} = \lambda \mathbf{x}
$$

### Power method (vector iteration)
- find the largest eigenvalue $\lambda_{max}$
\begin{align}
\mathbf{q}_k & = \frac{\mathbf{z}_{k-1}}{\|\mathbf{z}_{k-1}\|_2}\\
\mathbf{z}_k & = A\mathbf{q}_{k}\\
\lambda_{max}^k & = \mathbf{q}^T_k \mathbf{z}_k
\end{align}


```python

```


```python
%matplotlib inline
from numpy import *
from matplotlib.pyplot import *
import numpy.linalg
import scipy.linalg

n = 9
h = 1./(n-1)

x=linspace(0,1,n)

a = -ones((n-1,))
b = 2*ones((n,))
A = (diag(a, -1) + diag(b, 0) + diag(a, +1))

A /= h**2

print(A)



z0 = ones_like(x)
print(z0)


def PM(A,z0,tol=1e-5,nmax=500):
    q = z0/numpy.linalg.norm(z0,2)
    it = 0
    err = tol + 1.
    while it < nmax and err > tol:
        z = dot(A,q)
        l = dot(q.T,z)
        err = numpy.linalg.norm(z-l*q,2)
        q = z/numpy.linalg.norm(z,2)
        
        it += 1
    print("error =", err, "iterations =", it)
    print("lambda_max =", l)
    return l,q

l,x = PM(A,z0)
    
l_np, x_np = numpy.linalg.eig(A)

print("numpy")
print(l_np)
```

    [[128. -64.   0.   0.   0.   0.   0.   0.   0.]
     [-64. 128. -64.   0.   0.   0.   0.   0.   0.]
     [  0. -64. 128. -64.   0.   0.   0.   0.   0.]
     [  0.   0. -64. 128. -64.   0.   0.   0.   0.]
     [  0.   0.   0. -64. 128. -64.   0.   0.   0.]
     [  0.   0.   0.   0. -64. 128. -64.   0.   0.]
     [  0.   0.   0.   0.   0. -64. 128. -64.   0.]
     [  0.   0.   0.   0.   0.   0. -64. 128. -64.]
     [  0.   0.   0.   0.   0.   0.   0. -64. 128.]]
    [1. 1. 1. 1. 1. 1. 1. 1. 1.]
    error = 8.456086477502974e-06 iterations = 82
    lambda_max = 249.7352340857781
    numpy
    [249.73523409 231.55417528 203.23651229 167.55417528 128.
       6.26476591  24.44582472  88.44582472  52.76348771]


### Power method with shift
- find the eigenvalue $\lambda$ **closest** to $\mu$
\begin{align}
M & = A-\mu I\\
M & = LU \\
& \\
M\mathbf{x}_k &= \mathbf q_{k-1}\\
\mathbf{q}_k & = \frac{\mathbf{x}_k}{\|\mathbf{x}_k\|_2}\\
\mathbf{z}_k & = A\mathbf{q}_{k}\\
\lambda^k & = \mathbf{q}^T_k \mathbf{z}_k
\end{align}



```python
def IPM(A,x0,mu,tol=1e-5,nmax=500):
    M = A - mu * eye(A.shape[0])
    Minv = numpy.linalg.inv(M)
    y = x0/numpy.linalg.norm(x0, 2)
    l = 1/dot(y.T,dot(Minv,y))
    it = 0
    err = tol + 1
    while it < nmax and err > tol:
        x = dot(Minv,y)
        y = x/numpy.linalg.norm(x,2)
        l_new = 1/dot(y.T,dot(Minv,y))
        err = abs(l_new - l)
        l = l_new
        it += 1
    print("error =", err, "iterations =", it)
    print("lambda_max =", l + mu)
    return l + mu,y
    pass


l,x = IPM(A,z0,6.)

```

    error = 8.49941671821064e-07 iterations = 2
    lambda_max = 6.264765914246636



```python
M = A - 6 * eye(A.shape[0])
```


```python
A.shape[0]
```




    9




```python

```

b949397c923a3a1e6f94958e445010da642f8afc
ipynb
Jupyter Notebook
notes/08_eigenvalues.ipynb
gsarti/P1.4_seed
e09dc4381c5da0c5781e185d7bce69679b8fca66
CC-BY-4.0
2018-11-08T07:53:28.000Z
2018-11-08T07:53:28.000Z
notes/08_eigenvalues.ipynb
gsarti/P1.4_seed
e09dc4381c5da0c5781e185d7bce69679b8fca66
CC-BY-4.0
notes/08_eigenvalues.ipynb
gsarti/P1.4_seed
e09dc4381c5da0c5781e185d7bce69679b8fca66
CC-BY-4.0
2019-01-26T11:41:55.000Z
2019-01-26T11:41:55.000Z
Qwen/Qwen-72B
1. YES
2. YES