% HW#9: Gram-schmidt by Prof. Greg % GS(A) performs the Gram-Schmidt process on the columns of A % and returns the results in the columns of a matrix. function B = gs(A) for u=A x=u; for v=B % B's columns are the orthogonal vectors computed so far. % Subtract the appropriate multiple of of v from x end if norm(x)>.0000001 % Append x to B end end for col=1:size(B,2) % divide B's j-th column by it's length end %What are we doing above? % %for each column of A % for each previously generated v in our orthogonal basis % subtract off the appropriate multiple of v % if the result isn't 0 (i.e. if this col of A isn't linearly dependent) % store this result in our orthogonal basis (i.e. append x to B) %for each column in our orthogonal basis % divide the column by its length (orthogonal -> orthonormal)
dot(u,v) returns the dot product of vectors u and v.
norm(v) returns the length (not # of entries) of the vector v.
A=[A x] appends the vector x as a new column of A.
size(A,2) returns the number of columns of A.
A(:,j) indicates the jth column of A, and may be used for reading and writing.
Note that all MATLAB functions are lowercase, their appearance in the on-line help notwithstanding (so "DOT(u,v)" should be "dot(u,v)").
for n=[1 2 4 7 -2 0.5] n endThe "n" in the middle just prints the current value of n, which is each of the given values, in turn. More generally, "for u=A" sets u equal to a new column of A on each trip thru the loop. So "for u=A" can be used to process one column of A at a time, without worring about an artificial indexing variable like "i". The next for loop "for v=B" is doing the same thing... we want to subtract a multiple of each column of B from x, and this for loop handles moving thru these columns.