好吧,我通过 Householder 变换对方阵 A 进行 QR 分解,但算法似乎存在一些问题,因此结果总是错误的。
clear;
clc;
function [P,Q,R] = QR_decompose(A)
n = size(A,1);
for j = 1:n
sigma = 0;
for i = j:n
sigma = sigma + A(i,j)*A(i,j);//compute norm
end
if sigma == 0 then disp('Singular');
return;
end
s = -sign(A(j,j));
alpha = 1/(s*sqrt(sigma)*A(j,j)-sigma);
A(j,j) = A(j,j)- s*sqrt(sigma);
for k = (j+1):n
x = 0
for i = j:n
x = x + A(i,j)* A(i,k);
end
x = alpha* x;
for i = j:n
A(i,k)=A(i,k)+A(i,j)*x;
end
end
end
R = A
Q = A^-1
endfunction
A = [116 80 98 113; 80 66 80 93; 98 80 98 114; 113 93 114 133];
[P,Q,R]=QR_decompose(A)
disp('The permutation matrix is',P)
disp('The upper triangular matrix is',R)
disp('The orthogonal matrix is', Q)
输出 R 并不是我期望的三角矩阵,但尽管我付出了很多努力也无法修复它
感谢任何帮助。非常感谢。
首先请注意: