Scilab QR decomposition by Householder method incorrect output

32 Views Asked by At

Well, I got this following QR decompostion of a square matrix A by Householder's transformation but it seems like there are some problems in the algorithm so the result is always wrong.

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)

The ouput R is not a triangular matrix as I expect, but I can not fix it despite of much efforts

Appreciate any help. Many thanks.

1

There are 1 best solutions below

0
Serge Steer On

First please note that:

  • The QR decomposition already exsits in Scilab (help qr)
  • You can compute the frobenius norm of a matrix with norm(A,"fro") You need to revise your