I am trying to reduce a matrix to a tridiagonal matrix using householder reflectors. This was the code that my professor gave us and I have written it exactly how she wrote it but it is not working for me. I have tried a bunch of different things but have had no luck so far. Any help is appreciated.
function A=tridiag(S)
[m,m]=size(S);
e1=eye(1);
for k=1:m-2
x=S(k+1:m,k);
v=x+(sign(x(1))*norm(x,2)*e1);
v=v/norm(v,2);
S(k+1:m,k:m)=S(k+1:m,k:m)-2*v*(v'*S(k+1:m,k:m));
S(k:m,k+1:m)=S(k:m,k+1:m)-2*(S(k:m,k+1:m)*v)*v'
end
end
Since it is symmetric you just need
Now you can do the reduction via a one-liner;
As a bonus you get the transformation matrix;
Verify