Exercise two only
Microsoft Word - Project4.docx 4 PartII.Eigenvectors&Diagonalization Exercise 2 (5 points) Difficulty: Hard In this exercise, you will work with the eigenvalues of an n n matrix A. First, you will find all eigenvalues of A. Then, you will consider the distinct eigenvalues and find orthonormal bases for the corresponding eigenspaces and the dimensions of the eigenspaces. Next, you will check if A is diagonalizable by applying the general theory. If a matrix A is diagonalizable, the output has to contain an invertible matrix P and a diagonal matrix D, such that, 1A PDP , or, equivalently, AP PD and P is invertible. For diagonalizable matrices, you will run a built-in MATLAB function, which also performs diagonalization, and you will compare its outputs with the outputs P and D of your function. **Create a function in MATLAB that begins with function [L,P,D]=eigen(A) format [~,n]=size(A); P=[]; D=[]; Your function [L,P,D]=eigen(A) will have a set of commands that generates the following outputs for an n n input matrix A. Part 1. Output the vector L of all eigenvalues of A. To do that, you will go through several steps. Please suppress all intermediate outputs and display only the final output vector L. **Output a row vector L of all eigenvalues, where each eigenvalue repeats as many times as its multiplicity. A basic MATLAB command for this part L=eig(A); returns a column vector of all eigenvalues of A, and we use the function L=transpose(L); to get a row vector. The input matrices have real eigenvalues; however, it is possible that the MATLAB command eig() outputs them as complex numbers with negligibly small imaginary parts – to eliminate this discrepancy, we will need to run the function L=real(L); on the vector L. Then, we will “sort” the entries of L using the MATLAB command L=sort(L); To output vector L correctly, you will go through two more steps: (1) In your code, you will need to ensure that the multiple eigenvalues show as the same number. Use the function closetozeroroundoff with p = 7 to compare the eigenvalues and assign the same value to the ones that are within 10^(-7) tolerance of each other. To perform this task, you can go through the sequence of the sorted eigenvalues and, if there is a set of eigenvalues that are within 10^(-7) from each other, you will assign the first eigenvalue that comes across to all others in that set. Important Note: here we work with the eigenvalues that are stored in MATLAB – we do not round off any eigenvalues on this step. 5 (2) A singular matrix A has a zero eigenvalue; however, when running your code, a zero eigenvalue may not show as a zero due to round-off errors in MATLAB. If a matrix A is not invertible, you will need to run the function closetozeroroundoff with p = 7 on the vector of eigenvalues L to ensure that the zero eigenvalues show as a zero, and assign the output to L again. To check whether a matrix A is invertible or not, please use a MATLAB command rank(). After performing all of the tasks outlined above, you will display your final output L with a message: fprintf('all eigenvalues of A are\n') display(L) Please make sure that L is a row vector of the “sorted” real eigenvalues of A where all multiple eigenvalues are equal to each other and the zero eigenvalues (for singular matrices) show as a zero. Part 2. Construct an orthonormal basis W for each eigenspace. **Output and display with a message a row vector M of all distinct eigenvalues (no repetitions are allowed). The MATLAB command unique() can be used here. For each distinct eigenvalue M(i), your function has to do the following: **Find the multiplicity, m(i), of the eigenvalue M(i). Output it with the message: fprintf('eigenvalue %d has multiplicity %i\n',M(i),m(i)) **Output an orthonormal basis W for the eigenspace for an eigenvalue M(i). Display it with the message: fprintf('a basis for the eigenspace for lambda=%d is:\n',M(i)) display(W) Note: to output an orthonormal basis for the null space of a matrix, use a MATLAB command null(). **Calculate the dimension d(i) of W. Output it with the message: fprintf('dimension of eigenspace for lambda = %d is %i\n',M(i),d(i)) Part 3. Construct a Diagonalization when possible. For help with this part, please review the material of Lecture 24. **First, determine if A is diagonalizable: for each eigenvalue M(i), you will compare the multiplicity, m(i), with the dimension of the corresponding eigenspace, d(i). **If A is not diagonalizable, output a corresponding message and terminate the program – the empty outputs for P and D will stay. **If A is diagonalizable, output a corresponding message, and your function will continue with constructing a diagonalization. Output and display a matrix P constructed by combining the bases W for the eigenspaces. Output and display the diagonal matrix D with the corresponding eigenvalues on its main diagonal. Verify that your diagonalization is correct, that is, both conditions hold: AP=PD and P is invertible. To verify the first condition, you will need to use the function closetozeroroundoff with p = 7. To verify the second condition, please use the command rank(). 6 If both conditions hold, output a message: 'Great! I got a diagonalization!' Otherwise, output a message: 'Oops! I got a bug in my code!' and terminate the program. Part 4. Compare your outputs with the outputs of a built-in MATLAB function. **If the diagonalization is confirmed, your code will continue with the following task. There is a MATLAB built-in function that runs as [U,V]=eig(A) which, for a diagonalizable matrix A, generates a diagonal matrix V, with the eigenvalues of A on its main diagonal, and an n n invertible matrix U of the corresponding eigenvectors, such that AU=UV. Place this function in your code and display the outputs U and V. **Compare the output matrix U with the matrix P: write a set of commands that would check on the following case: P and U have either the same columns or the columns match up to a scalar (-1), where the order of the columns does not matter. If it is the case, output a message 'Columns of P and U are the same or match up to scalar (-1)'. If it is not the case (which is possible), the output message could be 'There is no specific match among the columns of P and U' Note: You will need to use the function closetozeroroundoff with p = 7 when comparing the columns of the matrices P and U. **Verify that the matrices D and V have the same diagonal elements (the order does not count either). If it is the case, output a message 'The diagonal elements of D and V match'. If it is not the case, output something like: 'That cannot be true!' Hint: To perform this task, you can “sort” the diagonal elements of V and compare them with the vector L using the function closetozeroroundoff with p = 7. This is the end of the function eigen. BONUS! (1 point) Theory: A matrix A is said to be orthogonally diagonalizable if there exists an orthogonal matrix P ( 1 TP P ) and a diagonal matrix D, such that, 1A PDP , or equivalently, TA PDP . Theorem: An n n matrix A is orthogonally diagonalizable if and only if A is symmetric. (For more information please read Section 7.1 of the textbook.) Add a set of commands to your function eigen for a diagonalizable matrix A that, first, will check if A is symmetric. **If A is not symmetric, output a message: disp('diagonalizable matrix A is not orthogonally diagonalizable') **If A is symmetric, output a message disp('matrix A is symmetric') and verify the orthogonal diagonalization: check if the matrix P is orthogonal (you will need to use here the function closetozeroroundoff with p = 7). If the orthogonal diagonalization is confirmed, output a message: 7 disp('the orthogonal diagonalization is confirmed') otherwise, output something like: disp('Wow! A symmetric matrix is not orthogonally diagonalizable?!') This is the end of your function eigen with the BONUS part if included. **Print the functions eigen, closetozeroroundoff, jord in the Live Script. Note: the function jord was created in Exercise 1 of Project 1. **Run the function [L,P,D]=eigen(A); as indicated below: %(a) A=[3 3; 0 3] [L,P,D]=eigen(A); %(b) A=[2 4 3;-4 -6 -3;3 3 1] [L,P,D]=eigen(A); %(c) A=[4 0 1 0; 0 4 0 1; 1 0 4 0; 0 1 0 4] [L,P,D]=eigen(A); %(d) A=jord(5,4); [L,P,D]=eigen(A); %(e) A=ones(4) [L,P,D]=eigen(A); %(f) A=[4 1 3 1;1 4 1 3;3 1 4 1;1 3 1 4] [L,P,D]=eigen(A); %(g) A=[3 1 1;1 3 1;1 1 3] [L,P,D]=eigen(A); %(h) A=magic(4) [L,P,D]=eigen(A); %(k) A=magic(5) [L,P,D]=eigen(A); %(l) A=hilb(5) [L,P,D]=eigen(A); PartIII.OrthogonalProjections&Least‐SquaresSolutions Exercise 3