from numpy import *
# ligne suivante seulement pour ipython
%matplotlib inline
import matplotlib as plt
import pylab as pl
Une matrice 2x2 à coefficients positifs
A = array([[ 0.00356101, 0.53584758],
[ 0.33824661, 0.02571842]])
On génère 200 points le long du cercle unité $\{ {}^t(\cos(\theta), \sin(\theta) ) | \theta \in [0, 2\pi] \}$
theta = linspace(0,2*pi,200)
v = array([cos(theta), sin(theta)])
On itère les puissances successives normalisées $$\frac{A^i v}{|| A^i v ||},$$ pour i allant de 1 à 50.
for i in range(0,50): # on itere Av/||v||
v = dot(A,v) # 1 produit Av
mv = max(linalg.norm(v,axis=0))
v = v/mv
# plot un r sur 10
if i % 10 == 0:
pl.plot(v[0,:],v[1,:])
pl.show()
Les vecteurs de v doivent converger vers un vecteur x qui satisfait $Ax = \lambda x$. Le quotient de Rayleigh en $v$ est $r(v) = {}^tv A v/{}^t v v$. On prend par exemple comme approximation de x, le vecteur v[:,0]
, mais on aurait pu en prendre un autre vecteur, sachant que certains sont plus près du vrai vecteur propre.
vk = v[:,0]
rv = dot(vk.T,dot(A,vk))/dot(vk.T,vk)
print "vecteur propre {v}\nvaleur propre {w}\n".format(v = vk, w = rv)
ws, vs = linalg.eig(A)
print "linalg.eig(A):\nvecteurs propres {v}\nvaleurs propres {w}".format(v = vs, w = ws)
vecteur propre [ 0.49122419 0.37502812] valeur propre 0.433317964104 linalg.eig(A): vecteurs propres [[-0.79076033 -0.7749946 ] [ 0.61212588 -0.63196785]] valeurs propres [-0.41123746 0.44051689]