Exercise 4 only please. Can't figure it out at all
1 MATLAB PROJECT 4 Please read the Instructions located on the Assignments page prior to working on the Project. BEGIN with creating Live Script Project4. Note: All exercises in this project will be completed in the Live Script using the Live Editor. Each exercise has to begin with the line Exercise# You should also mark down the parts such as (a), (b), (c), and etc. This makes grading easier. Important: we use the default format short for the numbers in all exercises unless it is specified otherwise. If format long or format rat has been used, please make sure to return to the default format in the next exercise. Part I. Eigenvalues, Eigenvectors & Diagonalization Exercise 1 (5 points) Difficulty: Hard In this exercise, you will work with the eigenvalues of a given n n× matrix A. First, you will find all eigenvalues and, then, you will take 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 the diagonal matrix D, such that, 1A PDP−= , or, equivalently, AP PD= and P is invertible. Next, 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 [P,D]=eigen(A) format compact [~,n]=size(A); Your function [P,D]=eigen(A)will have a set of commands which generates the following outputs for an n n× matrix A. Each output has to be supplied with a corresponding message - you could use the commands disp and fprintf. Part 1. Output the vector L of eigenvalues of the matrix. To do that, you will go through several steps. Please suppress all intermediate outputs and display only the final output vector L. **Find 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 transpose() to get a row vector. **You will also need to sort the entries of L – use the MATLAB command sort() to output the entries of L in ascending order. 2 To output vector L correctly, you will go through two more steps: **In your code, you have to ensure that the multiple eigenvalues show as the same number. You will use closetozeroroundoff function with the parameter 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 of them in that set. Note: please work with the eigenvalues that are stored in MATLAB – we do not round off any eigenvalues on this step. **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 run the function closetozeroroundoff with p = 7 on the vector of the eigenvalues to ensure that the zero eigenvalues show as a zero. To check whether a matrix is not invertible, please use a MATLAB command rank(). After performing all the tasks outlined above, you will display your final output L – a row vector of the sorted eigenvalues of A where all multiple eigenvalues 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 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 the eigenvalue M(i). Display it with the message: fprintf('A basis for eigenvalue lambda = %d is:\n',M(i)) W Hint: An appropriate command in MATLAB that creates an orthonormal basis for the null space of a matrix is 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 14) **First, we determine if A is diagonalizable. You will work with the multiplicity of the eigenvalue M(i), m(i), and the dimension of the corresponding eigenspace, d(i), for every i. If A is not diagonalizable, output a corresponding message, assign the empty outputs P=[];D=[]; and terminate the program. If A is diagonalizable, output a corresponding message, and your function will continue with constructing a diagonalization: **Output and display a matrix P constructed from the combined bases W for the eigenspaces. **Output and display a diagonal matrix D with the corresponding eigenvalues on its main diagonal. 3 **Verify that both conditions hold: AP=PD and P is invertible. To verify the first condition, you will need to use the function closetozeroroundoff with the parameter p = 7. To verify the second condition, please use the command rank(). 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 corresponding ones of a MATLAB built-in 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: The sets of columns of P and U are the same or match up to a scalar (-1), where the order of the columns does not matter. If it is the case, output a message 'Sets of 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 sets of columns of the matrices P and U. **Verify that the matrices D and V have the same set of 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. **Print the functions eigen, closetozeroroundoff,jord in the Live Script. (The function jord was created in Exercise 1 of Project 1.) **Type in the Live Script format **Run the function [P,D]=eigen(A); on the following matrices: %(a) A=[3 3; 0 3] %(b) A=[4 0 0 0; 1 3 0 0; 0 -1 3 0; 0 -1 5 4] %(c) A=jord(5,5) %(d) A=diag([3, 3, 3, 2, 2, 1]) %(e) A=magic(4) %(f) 4 A=ones(4) %(g) A=magic(5) %(h) A=hilb(7) %(k) A=[5 8 -4;8 5 -4;-4 -4 -1] Exercise 2 (3 points) Difficulty: Easy In this exercise, we will construct an orthogonal diagonalization of an n n× symmetric matrix. Theory: A square matrix A is called symmetric if TA A= . 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= . An n n× matrix A is orthogonally diagonalizable if and only if A is symmetric. **Create a function in MATLAB function []=symmetric(A) **First, your function