ALA K+Kf+Bt, del a, ht 2002

Goal:

To get into matlabs syntax for vector algebra and matrix-vector multiplication.

Program:

1. To create a list-variable/vector v=(2, -3, 7) in matlab you may specify each entry/component of v separately by writing (at the matlab prompt) v(1)=2, then v(2)=-3, then v(3)=7, or you may use the [ ] syntax, like in w=[1.5 3.9 -6.1], with the components sepated by blanks or commas. Try both the "v(index)=value" and the "w=[. . .]" method!

2. Recall that length(v) gives the number of components (the "dimension") of the vector v. Try it!

3. Recall that you may redefine individual components of an existing vector like w by directly addressing the component you would like to change, for example by writing w(2)=5.7. Try this.

4. Vectors may be scaled by multiplication with a common scale factor, that is, a real number. For example you may double all the components of v by writing 2*v. Try this and check the result. Note that if you write 2*v the result is put in the "answer box" ans, while if you actually want to change v, that is, to have v hold the result, you would write v=2*v. How could you get back the old v after this? Try!

5. Further recall that vectors (of the same length) can be added by writing for example v+w. Try this and check the result! Also try v-w. Does this perform the action you expected? Good! Of course you may also combine scaling and adding into so called linear combinations of v and w by writing, for example, 2*v+3*w. Try.

6. The scalar product of two vectors v and w can be obtained by writing sum(v.*w) or dot(v,w). Recall that v.*w computes the list of products (v(1)w(1), v(2)w(2), v(3)w(3)), and that sum then adds the products of this list together. Before you check all this you may want to redefine v and w for example as v=[1 2 3] and w=[4 5 6] so that you can more easily check what is going on. Do you then get the scalar product of these to be 4+10+18=32 as expected? Try both sum(v.*w) and dot(v,w)! Is dot(w,v) the same as dot(v,w)? Always?

7. Can you now compute the projection Pv of v in the direction of w? Recall that the projection should go in the direction of w so that Pv=cw for some c, and that the formula for c is dot(v,w)/dot(w,w), or if you like, dot(v,w)/norm(w)^2. Recall that norm(w) is the Euclidean length of w given by the square root of dot(w,w). So, what is the projection of v in the direction of w?

8. The vector product, or cross product of two (3-)vectors v and w is given by cross(v,w) in matlab. Try it, and check that the result is (-3, ?. ?) as expected. What should the question marks be here?

9. Recall that vectors may also appear as columns. For example, you may view the column counterpart of w by writing w' in matlab, and you may transform w to column form by writing w=w'. Try this! Similarly, you may transform a column vector back to a row vector by another '. That is, w=w' brings w back to its "row form". Note that also column vectors may be scaled and added (if they have the same length) as before. However, matlab refuses to add row vectors to column vectors, and vice versa. Vector algebra requires vectors of the same form or type!

10. Let us now consider a "column vector of row vectors", or a "row of column vectors". Such a construction is called a matrix. Rather, a matrix is a rectangular scheme of numbers organized in m rows and n columns, but which therefore can be considered as a row of n columns, each of height m, or alternatively, a column of n row vectors, each of length n. Check that this makes sense to you!

11. You may create a matrix A with 2 rows and 3 columns by giving each of the 6 entries like A(1,1)=11, A(1,2)=12, A(1,3)=13, A(2,1)=21, etc. Note that the first index i in A(i,j) is the row number and the second index j is the column index. That is A(2,1)=21 specifies that the number in column 1 and row 2 of the matrix should be 21. You may also specify the matrix using the [ ] syntax as A=[11 12 13;21 22 23], giving the entries row by row with a semi-colon (;) indicating the start of a new row. Try this!

12. Like vectors (which are special cases of matrices with only one row or only one column), matrices can be scaled, by multiplication of a (real) number/scale factor, and added to other matrices (of the same form, i.e. with the same number of rows and comumns). A matrix with m rows and n columns is called a mxn matrix, expressed as "m-by-n matrix".

13. Let us now consider the important "matrix-vector multiplication". The product A*v, where A is mxn matrix and v is a nx1 column vector (that is, a matrix with only one column), produces a mx1 column vector. That is, the matrix A transforms nx1 column vectors to mx1 column vectors. What happens here could be view (at least) two ways. One way to see what happens is that A*v produces a linear combination of the columns of A with the coefficients given by v. For example, for A=[1 2 3; 4 5 6] with (recall the ') columns [1 4]', [2 5]' and [3 6]', and v=[1 2 3]', A*v gives the sum of 1 [1 4]', 2 [2 5]' and 3 [3 6]', which is [1 4]'+[4 10]'+[9 18]'=[14 32]'. Check this! The other way to view what happens as a result of writing A*v is that each row of A is multiplied with v using the scalar product multiplication, and that the m scalar products produced this way (recall that A has m rows), is delivered in a mx1 column vector. Check that this is indeed the case!

14. Seek (by some trial and error) a linear combination of the columns of A that equals b=[10 20]'. That is, succesivly change v until A*v equals b. In fact, there are many such combinations to find. This is because there are 3 columns to use and only 2 numbers to equal (10 and 20).

15. If, on the other hand, A has only 2 columns, there is, in general, a unique combination of the two columns that gives the desired result b=[10 20]'. Matlab has a built-in functionality for finding this combination v, using the famous matlab "backslash" \. To get v you write v=A\b. Note that v has now only two entries because there are only two columns in A to combine!

/Kenneth


Last modified: Tue Aug 15 15:18:36 MET DST 2000