I have following problem.
My task is to fit a polynomial to the data. I want to implenet QR algorithm using Gram-Schimdt orthogonalization process. It is built in this function:
function [ Q,R ] = QRDec( A )
n = length(A(1,:));
for i=1:n
Q(:,i) = A(:,i);
for j=1:i-1
R(j,i) = (Q(:,j)')*Q(:,i);
Q(:,i) = Q(:,i)-R(j,i)*Q(:,j);
end
R(i,i) = norm(Q(:,i),2);
if R(i,i) == 0
break;
end
Q(:,i)=Q(:,i)/R(i,i);
end
end
Matrices Q,R are almost the same as these Q,R which are obtained from implemented in MatLab function. The only difference is in signs. If I solve my system of equations R*x=Q*y with MatLab functions, I get exact solution. But if I use my own matrices Q and R, then I get wrong result. Can anybody tell me where is the problem in my method? I also enclose code of my script.
% clear variables
clear; clc;
N = 100;
p = ones(1,15);
d = 14;
x = linspace(0,1,N)';
y = polyval(p,x);
A = zeros(N,d+1);
for i = 1 : d+1
A(:,i) = x.^(i-1);
end
[Qm,Rm] = QRDec(A);
[Q,R] = qr(A,0);
a_qrm = Rm\(Qm'*y);
a_qr = R\(Q'*y);
end
Do you think that such a big mistake can be caused by computation errors? I am really desperate because it seems that I have two same linear systems of equations and the solutions are different.