%% Introduction to matrices and matrix algebra %% Introduction clear all format short e %% Matrices % % A matrix in Matlab is simply a two dimensional % array of real numbers. What makes a matrix conceptually different from % an array is that we allow certain mathematical operations with matrices % that are not always defined for arrays. % % % % Matrices that have the same shape can be added together to form a third % matrix with the same shape % A = ones(4,3) %% B = 2*ones(4,3) %% C = A + B %% % Matrices can be multiplied together if they are "conformable". Matrix A % can be multiplied on the left by matrix B if the number of columns of A % is the same as the number of rows of B. For example, A = ones(3,4) %% B = ones(4,2) %% C = A*B %% % % Trying to multiply A on the left by B will result in an error. % % % %
% C = B*A
% 
%

% Error using *
% Inner matrix dimensions must agree.
%
% Error in lab_5 (line 391)
% C = B*A
% 
% % % % The reason for this is that the number of columns of B is two, but the % number of rows in A is 4. The "inner dimensions" refer to these two % dimensions, respectively. % %% Elementary matrix operations % %% % % Create A by reshaping the integer sequence 1:25 % into a 5x5 matrix. Notice how the reshaping is done. % A = reshape(1:25,5,5) %% % % The matrix transpose turns rows into columns and columns into rows. % A' %% % % When we transpose matrices that are not square, the shape of the matrix % changes as well as its entries. % B = reshape(1:12,6,2) %% % % B' is a 2x6 matrix. % B' %% Some special matrices % %% % % The $N\times N$ identity matrix. % N = 5; %% % % The identity matrix % eye(N) %% % % A matrix of zeros % zeros(N,N) %% % % A matrix of ones % ones(N,N) %% % The lower triangular part of A tril(A) %% % The upper triangular part of A triu(A) %% % The diagonal part of A diag(A) %% Creating sparse matrices % % In many common situations in engineering applications, the matrix % itself is too large % to explicitly write down every individual entry. Often, the matrix % contains mostly zeros. In this case, the matrix is said to be % sparse. %
% The simplest such matrix is the diagonal matrix, which has non-zero % entries only on the diagonal of the matrix. A simple example is the % identity matrix, % eye(4) %% % % We can construct more general sparse banded matrices with the diag command. This command takes vectors that % will be placed in diagonal and off-diagonal entries. For example, % A = diag(3*ones(4,1),-1) + diag(2*ones(5,1),0) + diag(ones(4,1),1) %% % % The second argument indicates where to put the diagonal vector, i.e. % along the sub-diagonal (indicated by the second argument -1), % the main diagonal (indicated by the second argument 0), or the % super-diagonal (indicated by the second argument 1). %
% When the matrix is used in solving a linear system, it is much more % efficient to store only the non-zero entries. We can do this using the % spdiags command, i.e. % z = ones(5,1); A_sparse = spdiags([3*z 2*z z],[-1 0 1],N,N) %% % % This command requires that you provide the diagonals and off-diagonals, % the corresponding diagonal position, and the size of the resulting % matrix. Note that the matrix is shown in sparse format, in which % only the non-zero entries are stored. To see what the matrix looks like, % we can use the full command, % full(A_sparse) %% Lab exercises % %
%

Part I : Basic vector and matrix manipulation

% For the following exercises, you will use the matrices and vectors given % below. %

%
% $% A = \left[ % \begin{array}{ccc} % 3 & -1 & 2 \\ % -1 & 1 & 7 \\ % 2 & 2 & 1 % \end{array}\right],\quad % B = \left[ % \begin{array}{cccc} % 2 & 1 & 1 & -1\\ % 0 & 2 & 0 & 12\\ % -1 & 1 & 1 & 8 % \end{array}\right], \quad % C = \left[ % \begin{array}{cc} % 2 & 1 \\ % 0 & 2 \\ % -1 & 1 % \end{array}\right] %$

%
% $% {\bf x} = \left[\begin{array}{ccc}1 \\ 4 \\ -1\end{array}\right], \quad % {\bf y} = \left[\begin{array}{ccc}0 \\ -3 \\ 5\end{array}\right] %$

% Compute the the following expressions by hand % and then check your answers with Matlab. To check your answers, use Matlab's % matrix and vector multiplication and the dot % function. Start by creating matrices the matrices and vectors above. %
%
1. Identify entries $a_{3,2}$, $b_{1,4}$ and $c_{2,1}$
2. %
3. ${\bf x}^T {\bf y} = \phantom{xxxxxxx}$ % (Is this an inner product or an outer product?)
4. %
5. ${\bf x} {\bf y}^T = \phantom{xxxxxxx}$ % (Is this an inner product or an outer product?)
6. %
7. ${\bf x} \cdot {\bf y} = \phantom{xxxxxxx} %$(Is this an inner product or an outer product?)
8. %
9. The dot product of row 2 of $A$ with the vector ${\bf x}$
10. %
11. The dot product of column 1 of $B$ with the vector ${\bf y}$
12. %
13. $A{\bf x} =$
14. %
15. ${\bf y}^T B =$
16. %
17. $B^T{\bf x} =$ %
18. Show that the product $A{\bf % x}$ can be expressed as a linear combination of the columns of $A$. %
19. Show that the product ${\bf % y}^T B$ can be expressed as a linear combination of the rows of $B$. %
20. Show that $(AB)^T = B^T % A^T$
21. %
22. What are the dimensions of $C^TB$?
23. %
24. What are the dimensions of $C^T{\bf x}$?
25. %
%
%
%

Part II : Special matrices

% Using the matrix $A$ and vector % ${\bf x}$ above, construct the % following matrices and vectors. Use the commands triu, % tril, diag and eye. The matrices L, U, D and I refer to the lower % triangular portion of A, the upper triangular portion of A, the diagonal % of A, and the identity matrix, respectively. %
%
1. Construct the matrices L, U, D and I.
2. %
3. Use the matrices L, U, D to reconstruct A.
4. %
5. Show that $AI = IA$ and % that $I{\bf x} = {\bf x}$
6. %
%
%
%

Part III : Matrix properties

%
%
1. The symmetric and skew parts of a square matrix A are given by % $$% A_{sym} = \frac{A + A^T}{2}, \quad A_{skew} = % \frac{A - A^T}{2} %$$
% and show that the following facts are true for A. %
%
%
1. % $A_{sym} + A_{skew} = A$
2. %
3. % $(A_{sym})^T = A_{sym}$
4. %
5. % $(A_{skew})^T = -A_{skew}$
6. %
%
%
%

Part IV : Matrix and vector norms

% A matrix norm measures the "size" of a matrix. % Compute the following vector and matrix norms (1) by hand, (2) using % elementary Matlab functions, and (3) using Use the vectors and % matrices from Part I. %
%
1. $\parallel {\bf x} \!\! % \parallel_1 \; =$
2. %
3. $\parallel {\bf x} \!\! % \parallel_2 \; =$
4. %
5. $\parallel {\bf x} \!\! % \parallel_{\infty} \; =$
6. %
7. $\parallel A \!\! \parallel_1 \; = %$
8. %
9. $\parallel A \!\! \parallel_2 \; = %$
10. %
11. $\parallel A \!\! % \parallel_{\infty} \; =$
12. %
13. Normalize (in any norm) the vector ${\bf x}$ so that has length 1 in the norm that % you chose. Verify that your normalized vector has length 1.
14. %
15. Use the rand function to generate % random vectors of length 5. Then, verify by experimentation that the % following is always true : % $$% \parallel {\bf x} \! \! \parallel_{\infty} \; \le \; % \parallel \! {\bf x} \! \parallel_1 \; \le \; % 5\parallel \! {\bf x} \parallel_{\infty} %$$.
% Can you explain why this is always true?
16. %
%
%
%

Part V : More on matrix-vector multiplication

% Construct the following matrices and vectors. %

% $% A = \left[\begin{array}{rrrr} % 1 & 5 & -8 & 7 \\ % -2 & 0 & 6 & 5 \\ % 1 & 2 & 2 & 1 % \end{array}\right] %$, %          % $% B = \left[\begin{array}{rrr} 1 & 1 & 2\\ % -5 & 1 & 4 \\ 2 & 1 & 0 % \end{array}\right] %$, %
%
%
% $u = \left[\begin{array}{r} 0 \\ 1 \\ 0 % \end{array}\right]$, %          % $v = \left[\begin{array}{r} 1 & 1 & 1 % \end{array}\right]$ %
%

% The following questions ask you about how to combine the above matrices % and vectors using matrix multiplication and the transpose operator. %
%
1. How many valid matrix-matrix and matrix-vector multiplication % operations can you find?
2. %
3. What operation can you find that will sum the rows of A? The rows of B? % The colums of B?
4. %
5. What operation will return the second column of A? The second row of % B?
6. %
7. What operation will result in exactly the scalar value % 1?
8. %
9. What operation will result in a 3x3 matrix of 0s and % 1s?
10. %
11. What operation will result in exactly the scalar value % 65?
12. %
%
%