QR Factorization Program - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

QR Factorization Program

veronicak5678veronicak5678 Posts: 4Member
I am writing my first Matlab program. It is supposed to take an mxn matrix of rank n and perform a QR factorization, then display the Q matrix.

I am using the Gram-Schmidt process to generate my basis vectors.
On this matrix
[1 -1 4 1
-2 0 2 1
0 0 0 0
1 -1 4 0]

I get an extra, incorrect column in Q. It must have something to do with the deflation. When I tried to debug, I see that u in step 3 (j =3), u is a vector with 5 elements, the first being something weird with an exponential and an asterisk afterwards. Why would that happen? Can anyone see the problem?

[code]clear
a=input ('Enter an mxn matrix of rank n: ');
[m,n]=size(a);
k=0;
q=[];
c=[];

for j= 1:n ;
u(:,j) = a(:,j);
for i = 1:j-1
u(:,j)=u(:,j)-q(:,i)'*a(:,j)*q(:,i) ;
end
n=norm(u(:,j));
if n~=0
k=k+1;
q(:,k)=u(:,j)/n;
else
u(:,j)=[];
end
end

display 'Matrix A is :'
disp( a)
display 'Matrix Q is :'
disp (q)
[/code]
Sign In or Register to comment.