#### Question Details

I'm very lost on how to do number 4 for this project.

MATLAB PROJECT 4 Please open and read the file Instructions on Project, placed on

Project Assignment page, prior to working on the Project: ?How to

Create a Diary file? and ?How to Create and run Function in MATLAB?.

Failure to fulfill the requirements on the format will result in a loss of

points. BEGIN with creating a diary file Project4. Note: All exercises in this project will be placed in this diary file. Each exercise in the diary

file should begin with the line

% Exercise#

format compact

to suppress extra line-feeds. Part I. Eigenvectors &amp; Diagonalization Difficulty: Hard Exercise 1 (6 points) 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 eigenvalue, you will find a

?rational? basis (if it exists) for the corresponding eigenspace and the dimension of that

eigenspace. Then, you will decide whether A is diagonalizable by applying the general theory.

If the matrix A is diagonalizable, the output has to contain an invertable matrix P and the

diagonal matrix D, such that, A = PDP ?1 , or, equivalently, AP = PD .

**Create a function in MATLAB:

function [ ] = eigen(A)

Your function eigen(A) will have a set of commands which will produce the following

outputs for an n ? n matrix A (each output has to be supplied with the corresponding message

- you could use the commands disp, or fprintf, or sprintf):

(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. 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 (see help unique). 1 (3) The sum of the multiplicities, Q, of all eigenvalues (Q must 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 (7) below).

(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&lt;Q?. If

matrix A is not diagonalizable, the program terminates.

(6) 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 will need to sort the entries of L to

ensure that D would be constructed properly ? use the MATLAB command sort when

calculating L). Verify that the matrix F=closetozeroroundoff( A ? P ? P ? D ) 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.?

(7) (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?.

The MATLAB for loop can be used to write the code, if needed, along with the MATLAB

functions eig, null( ,?r?), size within your code.

**Create the function closetozeroroundoff in MATLAB. Here is the code:

function B=closetozeroroundoff(A)

[m,n]=size(A);

for i=1:m

for j=1:n

if abs(A(i,j))&lt;10^(-7)

A(i,j) = 0;

end

end

end

B=A;

Below is a possible code for J = jord(n, r) that creates an n ? n Jordan block matrix with r on

the main diagonal, 1?s on the diagonal above, and with all other entries 0?s.

**Create the function jord is MATLAB

function J = jord (n, r)

A = ones(n);

2 J = tril(triu(A),1);

for i = 1: n

J(i, i) = r;

end

INSTRUCTIONS:

**Type the functions eigen, closetozeroroundoff, and jord in your diary file.

**Run the function eigen(A) on the following matrices:

4 0 0 0 1 3 0 0 2 2 (a) A = ; (b) = 0 ? 1 3 0 ; (c) A = jord(5,3); (d) A = diag([3, 3, 3, 2, 2, 1]);

0

2 0 ? 1 5 4 (e) A = magic(4); (f) A= ones(5); (g) A = magic(5). % Place a comment in your diary file on a result that you have received for the matrix in (g).

Justify whether it is correct or not and explain why. If it is not correct, make a change to your

code (you might need to replace a rational bases for eigenspaces null( , ' r ' ) with null( )) that

will give you the correct set of outputs. Call it the new function eigen_1, type it in the diary

file, and explain in words what change you have made in the function eigen to fix a

?problem?. Run the function eigen_1 on the matrix in (g).

Exercise 2 (4 points) In this exercise you will use a MATLAB command to find a

diagonalization of an n ? n matrix A (if it is possible), and to find an eigenvector basis of A

for n (if it exists).

**Create a function in MATLAB

function L = diagonal(A)

Begin the function with the commands

n=size(A,1)

[P, D] = eig(A);

(Note: these two lines indicate that you will get n as a direct output but not [P, D].) The

command [P, D] = eig(A) produces a diagonal matrix D of eigenvalues and a full matrix P

whose columns are corresponding eigenvectors so that AP=PD. Obviously, if P is invertible,

its columns form a basis for n and A is diagonalizable. If P is not invertible, the matrix A is

not diagonalizable, or equivalently, A does not have enough linearly independent eigenvectors

that would form a basis for n . To output P, you should verify that P has exactly n linearly

independent columns. Within your function diagonal, you will find the number k of linearly

independent columns of P and output a message:

?The number of linearly independent columns in P is k = (your value)).

Comparing k with n, you should make a conclusion whether A is diagonalizable.

If A is diagonalizable, your output has to have two messages:

3 (1) ?A is diagonalizable?.

(2) ?A basis for R^n is? (output matrix P).

If A is not diagonalizable, the output has to contain two messages:

(1) ?A is not diagonalizable?;

(2) ?A does not have enough linearly independent eigenvectors to create a basis for n ?.

The output for the function diagonal is the vector L of all eigenvalues of A, as indicated in the

heading of the function. The row vector L has to be defined within your code as

L=transpose(diag(D));

and you should run the function exactly as

L=diagonal(A)

to get the vector L as an output.

INSTRUCTIONS:

**Type the function diagonal within your diary file.

**Run the function L=diagonal(A) on each of the matrices (a)-(g) (they are the same as in the

previous exercise):

4 0 0 0 1 3 0 0 2 2 ; (c) A = jord(5,3); (d) A = diag([3, 3, 3, 2, 2, 1]);

(a) A = ;

(b) = 0 ? 1 3 0 0 2 0 ? 1 5 4 (e) A = magic(4); (f) A= ones(5); (g) A = magic(5). Part II. Orthogonal Projections &amp; Least-Squares Solutions

Exercise 3 (6 points) Difficulty: Moderate In this exercise, you will write a MATLAB

function p = proj(A,b)

which computes the projection of b onto the Column Space of an m ? n matrix A. Your

program should allow a possibility that the columns of A are not linearly independent. In

order for the algorithm to work, you will need to create a basis for Col A.

**A basis can be produced with the function shrink that you should create in MATLAB.

Here is the code:

function B = shrink(A)

format compact,

[~, pivot] = rref(A);

B = A(: , pivot); 4 Also, to save a space in your file (and paper), all input vectors must be row vectors and you

can use a MATLAB command transpose to make the adjustments for the outputs to be row

vectors as well. Note: the MATLAB command transpose(X) is the same as X ' .

Recall: A vector p is the orthogonal projection of a vector b onto Col A, when the columns of

A are linearly independent, if and only if p = Ax? , where x? is the solution of the normal

equations

AT Ax? = AT b .

Also, if p is the orthogonal projection of a vector b onto Col A, then the vector b can be

written as b= p + z , where z is the unique vector orthogonal to Col A.

Your function proj(A,b) should begin with

format compact,

A=shrink(A);

b=transpose(b);

(Remember, the input vector b must be a row vector, therefore, we convert it into a column

vector by transposing in order to solve the normal equations).

Then, the program has to check if the input vector b has exactly m entries, where m is the

number of columns in AT . If it doesn?t, the program breaks with a message: ?No solution:

dimensions of A and b disagree? and returns ?p=empty_vector?. You could type the following

disp(?No solution: dimensions of A and b disagree?)

p=sym(?empty_vector?);

return

If b has exactly m components, proceed to the next step:

Determine whether it is the case that the vector b ? Col A. (You could use a MATLAB

command rank.) If b ? Col A, the program should have outputs (without any computations!):

the vector p, the corresponding vector z, and a message ?b is in the Col A?. After that, the

program terminates.

If b ? Col A, proceed to the next step.

Determine whether it is a case when b is orthogonal to Col A. If it is the case, the program

should have outputs (without any computations!): the vector p, the corresponding vector z,

and a message ?b is orthogonal to Col A?. After that, the program terminates. If it is not the

case, proceed to the next part:

Find the solution to the normal equations (see above) ? please use the backslash operator, \ ,

Your output for this part should include:

vector p;

vector z = b ? p, which is the component of b orthogonal to Col A; 5 One more command should be written to check whether the vector p is, indeed, orthogonal to

the vector z = b ? p - check whether the inner product of these vectors is zero. (For purpose

of this program, we assume that a number is zero if its absolute value is less than 10?7 ).

If your code confirms that p ? z , please display a message: ?Yes, p and z are orthogonal!

Great Job!? Otherwise, the message should be something like: ?Oops! Is there a bug in my

code??

INSTRUCTIIONS:

**Type the functions shrink and proj within your diary file.

**Run the function p=proj(A,b) for the following matrices A and vectors b. Notice please

that some of the matrices are created in a few steps.

Please input the matrices exactly in the following formats (notice that some outputs below are

suppressed):

(a) A= magic(6); A=A( : , 1 : 4), b = (1 : 6)

(b) A= magic(6), E= eye(6); b = E( 6, :)

(c) A = magic(4), b = (1 : 5)

(d) A = magic(5), b = rand(1,5)

(e) A= ones(6); A( : ) = 1 : 36, b = [1,0,1,0,1,0]

(f) A= ones(6); A( : ) = 1 : 36; A= null(A,?r?), b = ones(1,6) The Exact and Least-Squares Solutions

Orthonormal Basis

An m ? n matrix U has orthonormal columns if and only if U T U = I n , where I n is an n ? n

identity matrix.

If an m ? n matrix U has orthonormal columns and W = Col U, then, for any vector b ? n ,

b? = projW b = UU T b.

If matrix U is an orthogonal matrix, that is, U is an n ? n matrix with orthonormal columns,

T

T

then (1) U T = U ?1 ; (2) W = n ; (3) UU

=

U=

U I n ; (4) b? = projW b = b . Exercise 4 (6 points)

Difficulty: Moderate

In this exercise, you will find the ?exact? solution of a matrix equation Ax = b if the equation

is consistent, when b ? Col A; or you will find the ?least-squares? solution of Ax = b if the

equation is inconsistent, when b ? ColA

**Create a function in MATLAB

function X = solvemore(A,b)

The inputs are an m ? n matrix A ( n ? m ) and a vector b ? m . 6 First, you will shrink matrix A leaving only linearly independent columns. Your output will

be the new matrix (also denoted A) whose columns form a basis for Col A. You should have

the function shrink created already in MATLAB; if not, below is a code:

function B = shrink(A)

format compact,

[~, pivot] = rref(A);

B = A(: , pivot);

format long,

A=shrink(A);

[m, n] = size(A);

The next step is to check whether b ? Col A or b ? Col A ? please use the MATLAB function

rank.

(a) If b ? Col A, give a message: ?The equation is consistent ? look for the exact solution?

and go through the following steps:

Use the general form of the ?if? statement to determine whether the matrix A: (1) does not

have orthonormal columns, (2) has orthonormal columns but is not orthogonal, (3) is

orthogonal. Instead of verifying the condition AT A = eye(n) , which may not be always

checked accurately due to a round off error, introduce a matrix

B=closetozeroroundoff( A '? A ? eye(n) ) and compare it with the zero matrix. Below is a code

for closetozeroroundoff:

function B=closetozeroroundoff(A)

[m,n]=size(A);

for i=1:m

for j=1:n

if abs(A(i,j))&lt;10^(-7)

A(i,j) = 0;

end

end

end

B=A;

For the cases (1) and (2), find the solution x1 of Ax = b by using the backslash operator \ .

For the case (3), use both: the backslash operator, \ , to find a solution x1 and the formula

x 2 = AT b (since A?1 = AT ) to find the solution x 2 of the same equation. Compare the

solutions obtained for this part by calculating the number N = norm ( x1 ? x 2 ) .

Your outputs for part (a) have to be: if (1) holds, you should have the message ?A does not

have orthogonal columns? and the vector X=x1; if (2) holds, you should have the message ?A

has orthonormal columns but is not orthogonal? and the vector X=x1; if (3) holds, your

7 outputs have to be the message ?A is orthogonal?, the matrix of the solutions X= [ x1 , x 2 ], and

the number N = norm ( x1 ? x 2 ) supplied with the message: ?The norm of the difference

between two solutions is N = (output N)?

(b) If b ? Col A, give a message ?The system is inconsistent ? look for the least-squares

solution? and, then, find it in two ways (1) and (2) given below:

(1) Find the unique least-squares solution x3 by solving the normal equations

AT Ax = AT b

and estimate the least-squares error of the approximation: n1 = norm ( b ? A ? x3) .

Present the output in the form of two messages: ?The solution of the normal equations is x3?

(output x3); and ?The least-squares error of the approximation is n1= (output n1)?.

(2) Find an orthonormal basis U for Col A. First, check if the matrix A has orthonormal

columns (see part (a) of this exercise). If yes, output the message: ?A has orthonormal

columns: an orthonormal basis for Col A is U=A? and assign U = A . If not, find the

orthonormal basis U for Col A, U = orth(A). The output for this part has to be the message:

?An orthonormal basis for Col A is U= (give U)?. Next, calculate the vector b1= b?= projColAb

by the formula b1 = UU T b . Output the vector b1 with the message: ?The projection of b onto

Col A is? (output b1). Find the least-squares solution x 4 as the exact solution to the equation

Ax = b1 , that is, x 4 = A \ (b1) and output it with the message: ?The least-squares solution by

using the projection onto Col A is x 4 = (output x 4 )?. Estimate the least-squares error of the

approximation n2 = norm ( b ? A ? x 4 )

Output it with the message: ?The least-squares error of this approximation is n2 = (output

n2)?.

Calculate and present as an output (with a corresponding message) the norm n3 of the

difference between the solutions x3 and x4: n3 = norm(x3 ? x4).

Demonstrate that x3 (or x4) is a least-squares solution that minimizes the distance between b

and Ax by computing an error of approximation n4 = norm ( b ? A ? x ) for x = rand(n,1). Give

the message: ?An error of approximation of b by Ax for a random vector x in n is (output

n4)?

The general output for the part (b) should be the matrix of the solutions X = [ x3, x 4] .

INSTRUCTIONS:

**Type the functions shrink and solvemore within your diary file.

**Run the function X = solvemore(A,b) for the following matrices. Please type the matrices

in exactly the same format as they appear below.

(a)

(b)

(c)

(d)

(e) A=magic(4); b=A( : ,1), A=orth(A)

A= magic(5); A= orth(A), b = rand(5,1)

A= magic(4), b = ones(4,1)

A = magic(4), b = rand(4, 1)

A= magic(4); A = orth(A), b = rand(4,1) **Close out the diary file.

8 Part III. Application to Polynomials

Applications to Linear Models - Regression Lines

In this part of the project, you will write a code that outputs the least-squares fit equation for

the given data. An equation is determined by the parameter vector c (that has to be

calculated), the design matrix X, and the observation vector y such that the 2-norm of the

residual vector e = y ? Xc is the minimum.

Assume we are given the data points ( x ( i ) , y ( i ) ) ( i = 1: m ). A choice of an equation of a

curve that gives the least-squares fit to the data is, in general, arbitrary. First, we will take a

look at the equation of a straight line =

y c1 x + c0 that minimizes the sum of squares of the

residuals ? it is called the least-squares regression line. The parameter vector c can be

determined by computing the least-squares solution of Xc = y , where y1 x1 1 x 1 , c = c1 , y = y2 .

X = 2

c 0 ym xm 1

Note: the MATLAB command c = lscov(X, y) calculates the least-squares solution of the

equation Xc = y in the same way as we would have found the solution by solving the normal

equations,

( X T X )c = X T y , using either the backslash operator, that is, c = ( X T X ) \ ( X T y ) , or the inverse matrix. The data vectors x and y will be represented by the row vectors ? we use the transposes of x

and y in the code below to convert them into the column vectors. The parameter vector c, used

for plotting with polyplot, has to be a row vector ? in the code below it appears as c?.

Exercise 5 (3 points)

**Create the following two functions in MATLAB:

function [ ] = polyplot(a, b, p)

x = (a : (b ? a)/50 : b)?;

y = polyval(p, x);

plot (x, y)

end

and

function c = lstsqline(x, y)

format rat,

x = x?;

y = y?;

a = x(1);

m = length(x);

9 Difficulty: Easy b = x(m);

X = [x, ones(m,1)];

c = lscov(X, y); % the same result will be obtained if c = ( X T * X ) *( X T * y ) or c = ( X T * X ) \ ( X T * y )

?1 % and we are verifying it:

c1 = ( inv ( X ? * X ) ) *( X ? * y )

c2 = ( X ? * X ) \ ( X ? * y ) % the next command calculates the 2-norm of the residual vector

N=norm(y - X*c)

% plot data points and the least-squares regression line:

plot(x, y, ? * ?), hold

polyplot(a, b, c?);

% output the polynomial:

P=poly2sym(c)

end

Note: you may skip the comment lines in the body of lstsqline while creating the function ?

they are placed there for your convenience.

**Type the functions polyplot and lstsqline in your diary file.

**Run the function c=lstsqline(x, y) on the vectors

x = [0, 2, 3, 5, 6] and y = [4, 3, 2, 1, 0].

The outputs have to be: the vectors c1 and c2, the 2-norm of the residual vector ? number N,

the regression line in the form of the polynomial P in x of degree n = 1, the vector c, and the

plot containing both the data points and the graph of the regression line.

**Save the plot as a JPEG file image0.jpg and insert it in your word document for Project 4

following this exercise.

Exercise 6 (5 points) Difficulty: Moderate **Create a new function in MATLAB

function c = lstsqpoly(x, y, n)

which is a modification of the function lstsqline in the way that it generates the least-squares

polynomial of degree n. You could use a single for loop within your code to create the design

matrix X whose form is defined by the degree of the polynomial. For example, for n = 3 the

design matrix X should have the form: x13 x12 x1 1 3 x2 x2 2 x2 1 X = xm 3 xm 2 xm 1

10 **Type the function lstsqpoly in the diary file.

**Run the function c = lstsqpoly(x, y, n) for each n = 1, 2, 3, 4 on the vectors from Exercise 5

x = [0, 2, 3, 5, 6] and y = [4, 3, 2, 1, 0]

to find the least-squares polynomial for each of the four values of n.

Your output for each n has to contain: the vectors c1 and c2 (see Exercise 5), the 2-norm of

the residual vector ? number N, the least-squares polynomial P in x of degree n, the vector c,

and the plot containing both the data points and the graph of the polynomial.

% For each n, verify that the vectors of coefficients c, c1, and c2 matches. Write a comment

% Verify that your code for this exercise for n = 1 is consistent with the code for Exercise 5,

that is, both functions give the same outputs for n = 1. Write a comment about it.

% Based on the plots and the norms of the residual vectors, tell polynomials of which degree

(1, 2, 3, or 4) for your data fits them the best.

**Save the 4 plots as JPEG files, image1.jpg ? image4.jpg, and insert them in your word

document for Project 4 following the part to which each image relates.

**Close out the diary file 11

Solution details:
STATUS
QUALITY
Approved

This question was answered on: Sep 18, 2020

Solution~0001013696.zip (25.37 KB)

This attachment is locked

We have a ready expert answer for this paper which you can use for in-depth understanding, research editing or paraphrasing. You can buy it or order for a fresh, original and plagiarism-free copy from our tutoring website www.aceyourhomework.com (Deadline assured. Flexible pricing. TurnItIn Report provided)

STATUS

QUALITY

Approved

Sep 18, 2020

EXPERT

Tutor