Section 5.5 Python/MATLAB recap
Eigenvalues and eigenvectors. MATLAB/Python has its own function to evaluate eigenvalues and eigenvectors of a matrix. Given a n×n matrixA
, one can fill up a vector e
with the list of all eigenvalues with the command e=eig(A)
. By writing, instead, [V,D]=eig(A)
, the variables V
and D
are filled with two n×n matrices. The columns of the first are all eigenvectors of length 1. The second is a diagonal matrix whose diagonal elements are the eigenvalues of A
so that the k-th column of V
is en eigenvector of A
with eigenvalue equal to the k-th element of the diagonal of D
. xxxxxxxxxx
clear all
format long
A = [ 1 2; 3 4]
e = eig(A)
[V,D] = eig(A)
v
. We could have accomplish the same thing using MATLAB/Python's own function norm(v,1)
. In general, norm(v,p)
gives the p-norm
[|v1|p+⋯+|vn|p](1/p).
In particular, the taxicab norm is given by norm(v,1)
and the Euclidean one by norm(v,2)
. Below we re-write the power method code using this function: xxxxxxxxxx
clear all
format long
A=[ 1 2; 3 4]
v0=[7,-2]';
v = v0;
d = 1;
tol=1e-4;
i=0;
while d > tol
i = i+1;
v0 = v;
z = A*v;
v = z/norm(z,1);
d = norm(v-v0,1);
end
iterations = i
distance = d
lambda = v'*A*v/(v'*v)
eigenvector = v
eig(A)
[Q,R]=qr(A)
gives the standard QR decomposition of a square matrix A, namely A=QR. The command [Q,R,P]=qr(A)
gives the 'column pivoting' QR decomposition of a square matrix A. In this case, AP=QR, where P is a permutation matrix. It makes sense to use a pivoted decomposition when A is close to being degenerate.