Polynomial fitting by QR decomposition

9 views (last 30 days)
Vít
Vít on 12 Oct 2013
Hello, 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 (I've read the topic of uniqueness of QR decomposition). 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 anzbody 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

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!