% Illustration of Householder & Givens for tridiagonalization

A = [4 1 2 1 2;
     1 3 0 -3 4;
     2 0 1 2 2;
     1 -3 2 4 1;
     2 4 2 1 1];

% Householder

x = A(2:5,1);
v1 = x - sign(x(1))*sqrt(x'*x)*[1,0,0,0]',
v = [0;v1];
H = eye(5) - 2/(v'*v)*v*v';
A1 = H*A*H'

% next step
x = A1(3:5,2);
v1 = x - sign(x(1))*sqrt(x'*x)*[1,0,0]',
v = [0;0;v1];
H = eye(5) - 2/(v'*v)*v*v';
A2 = H*A1*H'

% --------------------------

% plane/Givens rotations:

% rotate in 5-4 plane:
x5=A(5,1); x4=A(4,1);
r = sqrt(x4^2+x5^2);
c = x4/r;
s = x5/r;

% do rotation from left:
A(4:5,:) = [c s; -s c]*A(4:5,:);

% do rotation from right:
A(:,4:5) = A(:,4:5)*[c s; -s c]';

A

% rotate in 4-3 plane:
x4=A(4,1); x3=A(3,1);
r = sqrt(x3^2+x4^2);
c = x3/r;
s = x4/r;

% do rotation from left:
A(3:4,:) = [c s; -s c]*A(3:4,:);

% do rotation from right:
A(:,3:4) = A(:,3:4)*[c s; -s c]';


A