My Hessenberg Decomposition code is not working
显示 更早的评论
I am writing a function to find the Hessenberg decomposition of a matrix. I tried to check my code with the following matrix: [2 1 1; 1 2 1; 1 1 2]. When I did
househess(A) the output was [ 2 -1.41 0; -1.41 3 0; 0 0 1]. This is different from the built in function hess on matlab. I cannot see what I am doing
wrong in my code that I attached below:
function H = househess(A) % returns househess form
n = size(A); % square matrix
H = A;
for k= 1:n-2
x = H(k+1:n,k);
x(1)= sign(x(1))*norm(x) + x(1);
v = x/norm(x);
H(k+1:n,:) = H(k+1:n,:) - 2*v*(v'*H(k+1:n,:));
H(:,k+1:n) = H(:,k+1:n)-(H(:,k+1:n)*(2*v))*v';
end
回答(1 个)
Christine Tobler
2021-3-2
1 个投票
Your code looks correct, try it with A = randn(10) for example - this matches the outputs of HESS quite closely. When the input to HESS is symmetric, a specific algorithm for that case is used which returns an exactly symmetric and tridiagonal matrix (try with A = randn(10); A = A + A').
That algorithm seems to be using some different scalings in the Householder transformations, but the final result has the same properties as in the nonsymmetric case otherwise (that is, eig(hess(A)) matches eig(A)).
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!