Skip to content

Instantly share code, notes, and snippets.

@zdwolfe
Created January 27, 2013 19:49
Show Gist options
  • Save zdwolfe/4650078 to your computer and use it in GitHub Desktop.
Save zdwolfe/4650078 to your computer and use it in GitHub Desktop.
% Zachary Wolfe
% wolfe.zach@gmail.com
% CMPSC 456 SPR 2013
function [aQR, rQR, aMGS, rMGS, aCHOL, rCHOL] = fit()
t = 0:0.05:1;
V = vander(t);
X = V(:,17:21);
b = sin(pi*t);
b = b'; % make b a column vector
%mQ, mR are Q,R from Matlab's QR factorization
[mQ, mR] = qr(X);
[aQR, rQR] = mgs_solve(mQ, mR,b);
%gQ, gR are from modified Gram - Schmidt defined in mgs.m
[gQ, gR] = mgs(X);
[aMGS,rMGS]= mgs_solve(gQ, gR,b);
% To solve least squares using cholesky
% First find the cholesky decomposition of X'X, then solve
% cR'cR*yLS = X'b
cR = chol(X'*X);
aCHOL = (X'*b)\(cR'*cR);
aCHOL = inv(cR'*cR)*(X'*b);
rCHOL = b - X*aCHOL;
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment