Matlab: Linear Algebra ----------------------- Practical Exercises ------------------- Session 1 (Basic Matrix Use) ---------------------------- 1.1 Create a matrix M with the following values: 1.1 -5.0 4.7 -4.0 4.7 -22.3 -4.1 2.3 19.2 Create a row vector R with the following values: 1.6 -16.5 10.5 Create a column vector C with the following values: 0.7 -5.1 -4.8 Print the element in the second row and third column of M. Print the rank (number of dimensions) and the individual dimensions of M, R and C. 1.2 Calculate R times M times C, using matrix multiplication. Calculate C times M times R, transposing the vectors as necessary. [ Note that the values will be different. ] Calculate C times (M transpose) times R, transposing the vectors as necessary. [ Note that we have the first value again. ] Calculate C times M times R, without transposing anything. 1.3 Set A to be the 7x7 Hilbert matrix. Set B to be the middle three rows (i.e. rows 3-5) and last four columns (i.e. columns 4-7) of A. Set D to be the second column of B and E to be the second row of B (note B and not A). Set F to be the second column of B as a row vector. Print the the individual dimensions of A, B, D and E. 1.4 Set V to be a row vector of length 5, with all elements zero, using a constructor. Set W to be a 5x5 matrix with all elements two, except that the diagonals are five, using constructors. Set W to be a 5x5 Hilbert matrix, multiplied by the sum of a 5x5 unit diagonal matrix and a 5x5 inverse Hilbert matrix. [ Note that this is a convoluted way of adding one to the diagonal of a Hilbert matrix ] Set X to be the 5th power of the 5x5 Hilbert matrix, with all elements reduced by two (i.e. subtract two off each). Print the diagonal vector of X, and then set Y to be a 5x5 matrix with X as the diagonal and zeroes elsewhere. 1.5 Set R to be a 5x5 matrix with each element the square of the corresponding element of the Hilbert matrix. Set S to be a 7x7 matrix with element i,j having the value cos(pi*(i+j-2)/6). [Clue: use elementwise operations on the Hilbert matrix ] The matrix M has the following values (recreate it if necessary): 1.1 -5.0 4.7 -4.0 4.7 -22.3 -4.1 2.3 19.2 Print the maximum value in each row and the minimum value in each column, using the max and min functions. Print the product of the values in each row and the sum of the values in each column, using the prod and sum functions. 1.6 Set up a row vector A with values 1, 2, ..., 6 and a column vector B with values 1, 2, ..., 4. Print the dot products A.A and A.B, using the dot function Print the dot products A.A and A.B, using matrix multiplication and transposition. Set up a row vector A with values 1, 2, ..., 6 using linspace. Using A, create a 6x6 matrix B with element i,j having the value (1/i*j). [Clue: program the outer product yourself ] Session 2 (Numerical Linear Algebra) ------------------------------------ 2.1 Set the matrix A to the following value: -0.2762 0.0060 0.4593 0.2513 0.1991 0.0472 -0.2449 0.3909 -0.3614 Create a row vector B with the following value: 0.4049 -0.3889 0.0949 And a column vector C with the following value: -0.3152 -0.0611 -0.0913 Solve the equations B = A*x and C = A*y, and multiply the results by A to check that you get back to B and C. Print the condition number and the LAPACK approximate condition number of A. 2.2 Solve the equation B = A*x, and multiply the result by A to check that you get back to B. Note the errors. Print the condition number, the LAPACK approximate condition number and eigenvalues of A. Note the large values and the large number of small eigenvalues. 2.3 Set the matrix C to the following value: 0.2127 -0.4576 0.0005 -0.4286 -0.0289 0.0216 -0.4404 -0.4033 0.1820 0.3181 Set the matrix D to the following value: -0.3501 0.1596 0.0186 0.4730 0.1490 Solve the equation D = C*x. 2.4 There are no answers to this question, and I suggest leaving it until later. However, unless you are a matrix expert, I advise doing it before using either the '/' or '\' operators with two matrix arguments. Alternatively, avoid doing that. Create two 3x3 random matrices A and B, and investigate the relationships between linsolve(x,y), x/y and x\y, with x and y being either A and B or B and A. Use the formulae given in the lecture as a starting point. The objective is to get a firm understanding of exactly what the '/' or '\' operators actually do on matrices. 2.5 Create a vector E of length 16 with the Nth element having value cos(pi*(N-1)/4). Set F to be its Fast Fourier Transform. Print F and the inverse transform of F. 2.6 Create an unsymmetric 5x5 matrix G by [G,y] = eig(hilb(5)). Print its 2-D fast Fourier transform using fftn, and then using fft. Do the latter by row and column, and then by column and row. All of those should be the same, and there are other variations using transposition. 2.7 Set the matrix A to the following value: -0.2762 0.0060 0.4593 0.2513 0.1991 0.0472 -0.2449 0.3909 -0.3614 Set B to the exponential of Z, and C to that of -Z. Print B, C and B times C and C times B. Calculate eye(3)+A+A^2/2+A^3/6+A^4/24+A^5/120 and note the slight differences from B. Calculate the trace of A, both by using the trace function and by summing the diagonal. 2.8 Using the matrix A above, calculate D to be A times A transpose. [Note that D is now symmetric ] Print D's 2-norm and determinant. Print its eigenvalues, their product, the singular values and their product. Note the relationships to each other, and to the norm and determinant. Print its characteristic polynomial. 2.9 Using the matrix A above (i.e. the original input), print its 2-norm and determinant. Print its eigenvalues, their product, and the singular values. Note the relationships to each other, and to the norm and determinant. [Note that A is not normal ] Print its 1-norm, infinity-norm and eigenvalue condition numbers. 2.10 Using the matrix D above, print its eigenvectors and singular value decomposition. Note the relationships (i.e. they are the same results, rearranged). Use the relationships given in the lecture to recreate the matrix D from the eigenvalues and eigenvectors, and the singular value decomposition. 2.11 Using the matrix A above, print its eigenvectors and singular value decomposition. Note the relationships, and that they differ. [Note that I said A was not normal. ] Use the relationships given in the lecture to recreate the matrix D from the eigenvalues and eigenvectors, and the singular value decomposition.