MATLAB hints for HW#8A

(Writing a Gram-Schmidt function)

% 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)

- Here are some additional MATLAB commands that may have come in handy...
`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)`"). - Unlike in ordinary m-files, in m-files that define a new function (like this one), all variables are local. This means that they don't interact with variables in the workspace of the same name, and initializations like "
`B=[]`" are unnecessary. - What's going on with "
`for u=A`"? Okay... I assume you've seen "`for n=1:5`" for the standard "for" loop. What is "1:5"? Go to the MATLAB command line and type "1:5". What do you get? [1 2 3 4 5]. "1:5" is actually a vector. The each trip thru the loop sets n to the next entry in this vector. Now try typing this at the command line (the loop won't execute until you type "end")...

for n=[1 2 4 7 -2 0.5] n end

The "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. - "
`if norm(x)>.0000001`" is our crude check that x is not zero (for our simple integer examples, "norm(x)" should always be larger than this if x isn't 0... a real MATLAB function would be a lot more careful about this point). - In the final "for" loop, we change the columns of B from orthogonal to orthonormal. Note that here, I
*am*using a dummy variable to index to columns of B. Why? Because I want to read*and*write into the column, so setting a variable equal to it is not sufficient. There still might be a way to do this using "for v=B", but I couldn't think of any that I liked better. - At the end of the function code, the return variable for the function (Namely, "B" from "
`function B = gs(A)`") should contain the value you want returned. In this case, B's columns should be an orthonormal basis for the column space of the argument matrix A. - As a final random note, a semicolon at the end of a line supresses output (as in "
`x=u;`"). This is not neccessary for control statements (for, if, end, etc.).