I have the below Matlab code based on what my professor gave me in class. The last line of this code is giving me an incompatible dimensions error. When I checked the size, I got that the matrix $Q2D$ is $(n-1) \times n$, but that $vv^T$ is $n \times n$. What should I be doing to modify the last line so the algorithm works? It should be equivalent to calculating $Q_nQ_{n-1}...Q_1AQ^T_1Q^T_2...Q_T^N$. Thank you!
for k=1:n
x = Q2D(k:n,k);
e = zeros(n-k+1,1); % Initialize e as the standard basis vector e_1
e(1) = 0;
if sign(x(1)) == 0 % Calculate v
v = norm(x)*e + x;
else
v = sign(x(1))*norm(x)*e + x;
end
v = v/norm(v); % Orthonormalize v
Q2D(k:n,k:n) = Q2D(k:n,k:n) - 2*v*(v.'*Q2D(k:n,k:n));
% size(v*v.')
% size(Q2D(1:n,k+1:n))
Q2D(1:n,k+1:n) = Q2D(1:n,k+1:n)-2*(Q2D(1:n,k+1:n)*v)*v.'; % This line is where
% I'm having issues
end
Step by step:
1) Transforming a matrix to the upper Hessenberg form means we want to introduce some zeros in the columns $1,\ldots,n-2$. So why a loop over $1,\ldots,n$? Replace
by
2) Transforming a matrix to the upper Hessenberg form also means that we want to zero out components $k+2,\ldots, n$ in the given column $k$ (and not $k+1,\ldots,n$). Also, there is no need to introduce two vectors. Hence replace
by
3) I do not understand the meaning of
It results in the zero vector.
4) There is no need to use any vector $e$ as it contains only one nonzero component and adding/subtracting it to/from a vector does mostly nothing except changing one single component. Replace
by
Note that $\alpha$ is the number we should obtain in the entry $(k+1,k)$.
5) Normalisation is OK. Leave
untouched :-).
6) Replace
by
According to point (2), we do not triangularise the matrix! Also, you can use the fact that actually you know what exactly should be in the column $k$ and use this instead:
This in particular avoids creating tiny nonzeros (due to roundoff) in entries which should be exactly zero.
7) The line
is actually correct!
So this is what remains at the end: