Below is an example of how an output of your work for the commands



GROUP # ___________






By signing your names above, each of you had confirmed that you did the work and

agree with the work submitted. 


Please follow the instructions below unless a specific requirement on

the submission format is indicated in the exercise. Failure to fulfill the

requirements on the format will result in a loss of points on the Project.

Below is an example of how an output of your work for the commands executed in MATLAB

should look. A page similar to the one below has to be created in MATLAB for each exercise

and called exercise# (please put the corresponding number). The file will be created by using

the command diary exercise#. After the file diary exercise# is initiated, the next command

will be diary on – the beginning of recording. Everything that you put in the Command

Window will be recorded in the created file. The command diary off will end up the

recording. Within the diary file: the command type function name types the function that has

to be previously created using the text editor and saved in MATLAB with the extension .m.

Then you should assign values to the arguments in the function and call the function. Please

refer to MATLAB help page on the subject of creating functions in files:

After you got the required outputs, execute the last command diary off and your file is ready

for printing. The printed “diary” file for each exercise has to be turned in as a part of the


Example of the function “add” placed into a “diary file” and executed:

diary on

type add

function c=add(a,b)

% c=add(a,b). This is the function which adds

% the matrices a and b. It duplicates the MATLAB

% function a+b.*



if m~=k | n~=l,

 disp(‘ERROR using add: matrices are not the same size’)




for i=1:m,

 for j=1:n,




format compact

a=[1 2 3;4 5 6]

a =

 1 2 3

 4 5 6



c =

 2 3 4

 5 6 7

diary off

*Comments are supplied with % sign – they are not executed in MATLAB.


Part I. Eigenvectors & Diagonalization

Exercise4_1a (8 points) Difficulty: Hard

In this exercise, you will find all eigenvalues of a given n n × matrix A. Then you will choose

the distinct eigenvalues and, for each of them, you will find a “rational” basis for the

corresponding eigenspace and the dimension of that eigenspace. Then you will decide whether

A is diagonalizable applying the general theory. If the matrix is diagonalizable, an

OPTIONAL output has to also contain an invertable matrix P and the diagonal matrix D, such

that, 1 A PDP− = , or, equivalently, AP PD = .

You will write a set of commands for an n n × matrix A which will produce the following

outputs. Each output has to be supplied with the corresponding message (you could use the

commands disp or fprintf)

(1) A row vector L (use the command transpose) of all eigenvalues – each eigenvalue repeats

as many times as its multiplicity. You should also use closetozeroroundoff function (the code

is below) to ensure that zero eigenvalues will be the 0’s. The basic MATLAB command for

this part is eig(A) which returns a column vector of all eigenvalues of A.

(2) A row vector M of all distinct eigenvalues (no repetition is allowed). The MATLAB

command unique can be used here, which returns the same values of the array but sorted (in

ascending order) and with no repetition.

(3) The sum of multiplicities Q of all eigenvalues (Q has to be equal to n) with the message:

“The sum of multiplicities of the eigenvalues is Q =” (output Q).

(4) For each distinct eigenvalue, the output has to include a “rational” basis W for the

corresponding eigenspace and its dimension. An appropriate command in MATLAB that

creates a rational basis by using the row-operations is null( , ‘ ‘ r ). (Note: command null( )

creates an orthonormal basis for the null space – use help null command in MATAB for more

information). Within the code for this part, BONUS points can be earned (see (5) and (6)


(5) The total sum N of the dimensions of all eigenspaces with the message: “The total sum of

the dimensions of the eigenspaces is N =” (output N). The value of N has to be compared with

Q (or n) and, based on the comparison, one of the two messages has to be displayed: “Yes,

matrix A is diagonalizable since N=Q” or “No, matrix A is not diagonalizable since N<Q”. If

matrix A is not diagonalizable, the program terminates.

(BONUS! 2 points) If A is diagonalizable output the matrix P whose columns are the bases

for the eigenspaces, which you have calculated above, and the diagonal matrix D with the

corresponding eigenvalues on the main diagonal (you might need to sort the elements of L to

ensure that D would be constructer property – use the MATLAB command sort when

calculating L). Verify that the matrix F=closetozeroroundoff( AP PD ∗−∗ ) is the zero

matrix. If it is the case, the output has to be the message: “Great! I got a diagonalization!”

And, if it is not the case, the output will be the message: “Oops! I got a bug in my code.”

(6) (BONUS! 2 points) display the multiplicity of each eigenvalue in part (4). Compare each

multiplicity with the dimension of the corresponding eigenspace and, if they do not match,

display a message: “Matrix A is not diagonalizable since the dimension of the eigenspace is

less than the multiplicity of the corresponding eigenvalue”.



A for loop can be used, if needed, along with the MATLAB functions eig, null( ,’r’), size

within your code.

Create a function in MATLAB:

function L = eigen(A)

Please! use the command

format compact (a few blank lines will be printed)

Also, print 2 pages per sheet if possible (it will save a lot of paper!)

Here is a code for closetozeroroundoff:

function B=closetozeroroundoff(A)


for i=1:m

for j=1:n

if abs(A(i,j))<10^(-7)

A(i,j) = 0;





Run the function eigen(A) on the following matrices

(a) A =

2 2

0 2

 

   

; (b)