function [Q,R]=mygs5(V) % Sample function for typical in-class fifth-try to implement % Graham Schmidt Orthogonalization = general QR-factorization % Now also returning the triangular matrix of col'ops as well. % % All rights reserved. Original version: Nov 2003 % Matthias Kawski http://math.asu.edu/~kawski if nargin<1 % self-executing --- when no data V=5-round(10*rand(7,4)) % are provided, then create and end % work random example Q=V; n=size(V,2); R=eye(n); for i=1:n w=V(:,i); r=Q(:,1:i-1)'*w; % coefficients of the projection R(1:i-1,i)=r; w=w-Q(:,1:i-1)*r; % subtract the projection len=sqrt(w'*w); % normalization factor Q(:,i)=w/len; % normalize the new i-th basis vector R(i,i)=len; % multiply old R on the LEFT! by diagmatrix end % check1=Q'*Q % orthogonality % check2=rref([Q,V]) % same span (no new direx added)? check = V-Q*R