写作MATLAB程序设计、 辅导data编程

” 写作MATLAB程序设计、 辅导data编程MATLAB PROJECT 4Please read the Instructions located on the Assignments page priorto working on the Project.BEGIN with creating a Live Script Project4.Note: All exercises in this project have to be completed in the Live Script using the LiveEditor. Please refer to the MATLAB video that explains how to use the Live Script: httpss://www.mathworks.com/videos/using-the-live-editor-117940.html?s_tid=srchtitleThe final script has to be generated by exporting the Live Script to PDF.Each exercise has to begin with the lineExercise#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 isspecified otherwise. We do not employ format rat since it may cause problems withrunning the codes and Displaying matrices in the Live Script. If format long had been usedin an exercise, please make sure to return to the default format in the next exercise.Part I. Evaluating EigenvaluesExercise 1 (4 points): Difficulty: EasyIn this exercise, you will calculate the eigenvalues of a matrix by using various methods andfunctions one of the functions, quer, was created earlier in Exercise 4 of Project 3, and theother functions will be MATLAB built-in functions.NOTE: The function quer from Project 3 can be used for approximating eigenvalues byconsecutive iterations, but not for all matrices the process converges. For computingeigenvalues in MATLAB, especially complex eigenvalues, it is advisable to use a MATLABbuilt-in function eig().**Create a function that begins withfunction [q,e,c,S]=eigenval(A)formatThe input is a square matrix A. The outputs will be specified below when you proceed withthe programming.**First Method: your function will calculate the eigenvalues by the QR factorization. Includein your code for eigenval those lines of the function quer that deal with the case when m=nand make the following adjustments:(1) Increase the accuracy from p=7 to p=12 and output and display the number of iterations kneeded to archive this accuracy:fprintf(the number of iterations is %i\n,k)(2) Output the vector of the sorted diagonal entries of the matrix B, which is the lastiteration in the sequence, and assign it to the vector q. Also, convert to a zero the entries of thevector q that are in the range of 10^(-7) from a zero. The outputs for this part should be:2q=sort(diag(B));disp(The eigenvalues of A from QR factorization are:)q=closetozeroroundoff(q,7)Note: Function sort, applied to a real vector, re-arranges its entries in the ascending order.**Second Method: you will calculate the eigenvalues of A using a built-in MATLAB functioneig().The outputs for this Part will be:e=eig(A);e=sort(e,ascend,ComparisonMethod,real);disp(The eigenvalues of A from a MATLAB function are:)e=closetozeroroundoff(e,7)**Third Method: there is a built-in MATLAB function poly(A) that outputs the vector of thecoefficients of the characteristic polynomial of A written in the descending order according tothe degree. Output the vectorS=poly(A);and, then, convert to zero the entries of S that are in the range of 10^(-7) from 0 assign thisvector to S again (do not display S).Next, output the characteristic polynomial of A in a symbolic form as indicated below:disp(the MATLAB characteristic polynomial of A is:)Q=poly2sym(S)Continue working with the vector S: use the MATLAB function roots(S) to output thevector of zeros of the polynomial whose coefficients form the vector S. Sort this vectors inthe ascending order with respect to the real part and assign it to c. Since c is the vector ofzeros of the characteristic polynomial, it is also the vector of the eigenvalues of A. Youroutputs for this part will be as below:c=roots(S);c=sort(c,ascend,ComparisonMethod,real);disp(The eigenvalues from the MATLAB characteristic polynomial are:)c=closetozeroroundoff(c,7)**Then, we will need to see how close the vectors q, e, and c are to each other.Output and display the three 2-norms:eq=norm(e-q)cq=norm(c-q)ec=norm(e-c)**Next, we will use the vector of the Eigenvalues e to build a polynomial whose zeros are theentries of e, that is, to construct the characteristic polynomial of A. We will run a MATLABfunction poly(e) that returns the vector of the coefficients of the polynomial given the vectore of its zeros. Your outputs here will be the characteristic polynomial R of the matrix A,written in a symbolic form, and a message (see below):disp(the constructed characteristic polynomial of A is:)R=poly2sym(poly(e))**Finally, you will chick if the symbolic polynomials Q and R are the same: run a logicalcomparison statement and, if Q and R match, code a message:disp(Yes! Q and R are the same!)If they dont, a message could be something like:disp(What?! Q and R do not match?)3**Print the functions closetozeroroundoff, jord, and eigenval in your Live Script.Note: function jord was created in Exercise 1 of Project 1.**Run the function Eigenval(A); as indicated below:%(a)A=[3 3;0 3][q,e,c,S]=eigenval(A);%(b)A=jord(5,4)[q,e,c,S]=eigenval(A);%(c)A=ones(5);[q,e,c,S]=eigenval(A);%(d)A=tril(magic(4))[q,e,c,S]=eigenval(A);%(e)A=triu(magic(4),-1)[q,e,c,S]=eigenval(A);%(f)A=[4 3 0 0; 7 10 3 0;0 3 1 5;0 0 6 7][q,e,c,S]=eigenval(A);% Analyze the outputs eq, cq, ec after running the function eigenval on the matrices (a)-(f) and write a comment whether the QR factorization gives a good approximation of theeigenvalues of these matrices.BONUS! (2 points)Insert Section Break and run the part below as a separate Section.**Input the matrix and run the function:A=[4 0 0 0; 1 3 0 0; 0 -1 3 0; 0 0 5 4][q,e,c,S]=eigenval(A);(It may take some time for the outputs to be displayed please be patient!)The last output message should indicate that the polynomials Q and R are not equal. You willneed to find out how close to each other are S and poly(e), which are the vectors ofcoefficients of the polynomials Q and R, respectively. Proceed as follows:** Initializep=1;and set up a while loop in your Live Script using the variable p and the functionclosetozeroroundoff(S-poly(e),p)to output the largest value of p (p is a positive integer) for which the functionclosetozeroroundoff outputs the zero vector.Code a message that will display the number of the matching decimals in the vectors of thecoefficients of polynomials Q and R.4Part II. Eigenvectors DiagonalizationExercise 2 (5 points) Difficulty: HardIn this exercise, you will work with the eigenvalues of an n n matrix A. First, you will findall eigenvalues of A. Then, you will consider the distinct eigenvalues and find orthonormalbases for the corresponding eigenspaces and the dimensions of the eigenspaces. Next, you willcheck 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, 1 A PDP = ,or, equivalently, AP PD = and P is invertible. For diagonalizable matrices, you will run abuilt-in MATLAB function, which also performs diagonalization, and you will compare itsoutputs with the outputs P and D of your function.**Create a function in MATLAB that begins withfunction [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 followingoutputs 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 anddisplay only the final output vector L.**Output a row vector L of all eigenvalues, where each eigenvalue repeats as many times asits multiplicity. A basic MATLAB command for this partL=eig(A);returns a column vector of all eigenvalues of A, and we use the functionL=transpose(L);to get a row vector.The input matrices have real eigenvalues; however, it is possible that the MATLAB commandeig() outputs them as complex numbers with negligibly small imaginary parts to eliminatethis discrepancy, we will need to run the functionL=real(L);on the vector L.Then, we will sort the entries of L using the MATLAB commandL=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 samenumber. Use the function closetozeroroundoff with p = 7 to compare the eigenvalues andassign the same value to the Ones that are within 10^(-7) tolerance of each other. To performthis task, you can go through the sequence of the sorted eigenvalues and, if there is a set ofeigenvalues that are within 10^(-7) from each other, you will assign the first eigenvalue thatcomes across to all others in that set.Important Note: here we work with the eigenvalues that are stored in MATLAB we do notround off any eigenvalues on this step.5(2) A singular matrix A has a zero Eigenvalue; however, when running your code, a zeroeigenvalue 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 withp = 7 on the vector of eigenvalues L to ensure that the zero eigenvalues show as a zero, andassign the output to L again. To check whether a matrix A is invertible or not, please use aMATLAB command rank().After performing all of the tasks outlined above, you will display your final output L with amessage: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 allmultiple 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 repetitionsare 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 withthe 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 commandnull().**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 themultiplicity, m(i), with the dimension of the corresponding eigenspace, d(i).**If A is not diagonalizable, output a corresponding message and terminate the program theempty outputs for P and D will stay.**If A is diagonalizable, output a corresponding message, and your function will continuewith 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 maindiagonal.Verify that your diagonalization is correct, that is, both conditions hold: AP=PD and P isinvertible. To verify the first condition, you will need to use the functionclosetozeroroundoff with p = 7. To verify the second condition, please use the commandrank().6If 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 theprogram.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 ofA 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 checkon the following case:P and U have either the same columns or the columns match up to a scalar (-1), wherethe 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 toscalar (-1).If it is not the case (which is possible), the output message could be There is no specificmatch among the columns of P and UNote: You will need to use the function closetozeroroundoff with p = 7 when comparingthe columns of the matrices P and U.**Verify that the matrices D and V have the same diagonal elements (the order does not counteither). If it is the case, output a message The diagonal elements of D and V match. Ifit 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 withthe 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 orthogonalmatrix P ( 1 T P P = ) and a diagonal matrix D, such that, 1 A PDP = , or equivalently,T A 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, willcheck 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 messagedisp(matrix A is symmetric)and verify the orthogonal Diagonalization: check if the matrix P is orthogonal (you will needto use here the function closetozeroroundoff with p = 7).If the orthogonal diagonalization is confirmed, output a message:7disp(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);Part III. Orthogonal Projections Least-Squares SolutionsExercise 3 (4 points) Difficulty: ModerateIn this exercise, you will create a function proj(A,b)which will work with the projection of avector b onto the Column Space of an m n matrix A.For a help with this exercise, please refer to Lectures 28-31.8Theory: When the columns of A are linearly independent, a vector p is the orthogonalprojection of a vector b Col A onto the Col A if and only if p x = A , where x is the uniqueleast-squares solution of Ax b = , or equivalently, the unique solution of the normal equationsT T AA A x b = .Also, if p is the orthogonal projection of a vector b onto Col A, then there exists a uniquevector z, such that, z bp = and z is orthogonal to Col A.**Your program should allow a possibility that the columns of A are not linearly independent.In order for the algorithm to work, we will use the function shrink() and generate a newmatrix, which will be assigned to A, whose columns form a basis for Col A. The functionshrink() was created in Exercise 2 of Project 3 and you should have it in your CurrentFolder in MATLAB. If not, please see the code below:function B=shrink(A)[~,pivot]=rref(A);B=A(:,pivot);end**Create your Function which begins with the lines:function [p,z]=proj(A,b)formatA=shrink(A);m=size(A,1);p=[];z=[];The inputs are an m n matrix A and a column vector b. Your outputs p and z will be eitherempty (when the sizes of A and b do not match) or the projection of b onto the Col A and thecomponent of b orthogonal to the Col A, respectively.**First, your function has to check whether the input vector b has exactly m entries, where mis the number of rows in A. If it doesnt, the program terminates with a message Nosolution: sizes of A and b disagree, and the empty outputs for p and z will stay.If b has exactly m entries, we proceed to the next step:Determine whether it is the case that b Col A use here a MATLAB function rank().**If b Col A, output a message b is in Col A and make assignments to the vectors pand z based on the general theory (no computations are needed). Display the vectors p and z.After that, the program terminates.**If b Col A, your function will continue with the following tasks:Determine whether it is the case that b is orthogonal to Col A. Due to the round-off errors inMATLAB computations, you will need to use the function closetozeroroundoff with p = 7to check if this condition holds. If b is orthogonal to Col A, output a message b isorthogonal to Col A and make assignments to the vectors p and z based on the generaltheory (no computations are needed either). Display the vectors p and z. After that, theprogram terminates.If b is not orthogonal to Col A, your function will continue:9**Find the least-squares solution x as the solution of the normal equations by using theinverse matrix or the backslash operator, \ . Output it with a message:fprintf(the least squares solution of inconsistent system Ax=b is)display(x)**Then, output a vector (do not display it)x1=A\b;and verify by using the function closetozeroroundoff with p=12 that the vectors x and x1match within the given precision. If your code confirms it, display a message:disp(A\b returns the least-squares solution of inconsistent system Ax=b)**Next, output the Projection p by using the least-squares solution x. Display p with themessage:disp(the projection of b onto Col A is)display(p)**Then, output vector z, which is the component of b orthogonal to the Col A. Verify in yourcode whether the vector z is, indeed, orthogonal to the Col A: use the functionclosetozeroroundoff with p=7 here. If your code confirms it, display z with a message:disp(the component of b orthogonal to Col A is)diplay(z)Otherwise, an output message could be:disp(Oops! Is there a bug in my code?)and the program terminates.**Finally, use the vector z to compute the distance, d, from b to Col A. Output d with themessage:fprintf(the distance from b to Col A is %i,d)Hint: use here a MATLAB built-in function norm().This is the end of your function proj.**Print the functions closetozeroroundoff, shrink, proj in your Live Script.**Run the function [p,z]=proj(A,b); as indicated below:%(a)A=magic(4), b=(1:4)[p,z]=proj(A,b);%(b)A=magic(6), b=A(:,6)[p,z]=proj(A,b);%(c)A=magic(6), b=(1:5)[p,z]=proj(A,b);%(d)A=magic(5), b = Rand(5,1)[p,z]=proj(A,b);%(e)A=ones(4); A(:)=1:16, b=[1;0;1;0][p,z]=proj(A,b);%(f)B=ones(4); B(:)=1:16; A=null(B,r), b=ones(4,1)[p,z]=proj(A,b);10% Analyze the outputs in part (d) and write a comment that would explain a reason why arandom vector b belongs to the Col A.% For part (f), based on the input matrix A and the outputs, state to which vector space thevector b has to belong.**You will need to run some command in your Live Script to support your responses. Supplythem with the corresponding output messages and/or comments.Exercise 4 (4 points) Difficulty: ModerateIn this exercise, we will work with the matrix equation Ax b = , where the columns of A arelinearly independent. We find unique exact solution for a consistent system (bCol A) andunique least-squares solution for an inconsistent system (bCol A).Please read the section Theory carefully. You may also find it helpful to review the materialof Lectures 28-31 prior to working on this exercise.Theory: When solving in MATLAB a general linear system Ax b = with a matrix A whosecolumns are linearly independent, we can always find the solution of a consistent system orthe least-squares solution of an inconsistent system by using the MATLAB backslashoperator, \ .When matrix A has orthonormal columns, we can find the exact solution of a consistentsystem using the Decomposition Theorem (Lecture 28) or the least squares-solution of aninconsistent system using the Statement in Lecture 31 both, the Theorem and the Statement,are particular cases of the more general Orthogonal Decomposition Theorem (Lecture 29) andboth employ the same formulas for computing the entries of the solutions.Recall: An m n matrix A has an orthonormal set of columns if and only if A*A=eye(n).If a matrix A does not have orthonormal columns, we can, first, generate an orthonormal basisU for Col A, then, find the projection b1 of the vector b onto the Col A, and, finally, calculatethe exact or the least-squares solution of a system Ax=b as the solution of a consistentsystem Ax=b1. This method may be effective for calculating the least-squares solution, and,for a purpose of an exercise, it can be also extended to a calculation of the exact solution,which would unify an Approach to solving a linear system in general.Note: when b is in the Col A, that is, the system Ax=b is consistent, the projection of b ontothe Col A is the vector b itself, thus, the system Ax=b1 becomes Ax=b.Recall: The projection b1 of b onto the Col A can be computed by the formula b1=U*U*b,where U is a matrix whose columns form an orthonormal basis for Col A.Moreover, if x1 is the solution or the least-squares solution of the system Ax=b, that is,A*x1=b1, we can compute the least-squares error, n1, of approximation of the vector b bythe elements of the Col A, where n1=norm(b-A*x1), or equivalently, n1=norm(b-b1).Geometrically, n1 is the distance from b to the Col A (see Exercise 3 of this Project).**Create a function in MATLAB that begins withfunction [x1,x2]=solveall(A,b)format compactformat long[m,n]=size(A);11The inputs are an m n matrix A, whose columns are linearly independent, and a vector m b . The two outputs x1 and x2 are either two solutions of a consistent system or twoleast-squares solutions of an inconsistent system calculated in two different ways.**Continue your function with a conditional statement that has to determine if bCol A ornot, that is, whether the system is consistent or not use the MATLAB function rank() inthis part. If the system is consistent, output a message:disp(the system is consistent – find the exact solution)otherwise, output a message:disp(the system is inconsistent – find the least-squares solution)**Next, calculate the solution or the least-squares solution of the system Ax=b, vector x1, byusing the backslash operator, \ . Output and display it with a message as indicated below:x1=A\b;disp(the solution calculated by the backslash operator is)display(x1)Notice that the type of the solution has been stated in the previous message.**Then, determine Whether the matrix A has orthonormal columns you will need to use herethe function closetozeroroundoff() with p = 7.If matrix A has orthonormal columns, output a message:disp(A has orthonormal columns)and find the solution (or the least-squares solution) x2 using the Orthogonal DecompositionTheorem (Lecture 29), which is more general form of the Decomposition Theorem (Lecture28) and the Statement in Lecture 31 (see Theory above).Hint: You can either use a for loop to calculate the entries of the vector x2, one by one, oryou can employ the transpose of A to output x2 at once by a MATLAB operation (second wayis preferable remember, we work with a matrix whose columns form an orthonormal set).Display the solution x2 with a message:disp(solution calculated by the Orthogonal Decomposition Theorem is)display(x2)**If matrix A does not have orthonormal columns, output a message:disp(A does not have orthonormal columns)and find the solution (or the least-squares solution) of Ax b = by, first, calculating theprojection of the vector b onto the Col A. Proceed as follows:**Run the MATLAB function orth() to output and display a matrix U whose columns forman orthonormal basis for the Col A supplied with a message:disp(an orthonormal basis for Col A is)U=orth(A)**Next, using the created orthonormal basis U for Col A, calculate the projection b1 of thevector b onto the Col A (see Theory above). Display vector b1 with a message:disp(the projection of b onto Col A is)display(b1)**Then, output the solution (or the least-squares solution) x2 as the exact solution of theequation Ax = b1 (use the backslash operator). Display x2 with the message:disp(the solution calculated using the projection b1 is)display(x2)12**Verify that the solution x2 is calculated correctly: use a conditional statement and thefunction closetozeroroundoff()with p = 12 that you will run on the vector x1-x2. If yourcode confirms that the solutions calculated by two different methods match, code a message:disp(solutions x1 and x2 are sufficiently close to each other)Otherwise, your message could bedisp(Check the code!)and the program terminates.**If the code passes the check point above, your function will continue with a few more tasks:Use the solution x1 to output the least-squares error n1 of approximation of b by theelements of the Col A (see Theory above). Display n1 with a message:disp(least-squares error of approximation of b by elements of Col A is)display(n1)**Next, input a vectorx=rand(n,1);and compute the error of approximation, n2, of the vector b by a vector A*x of the Col A (fora random vector x). Output and display it as below:n2=norm(b-A*x);disp(an error of approximation of b by A*x of Col A for a random x is)display(n2)This is the end of your function solveall.**Print the functions closetozeroroundoff, shrink, solveall in your Live Script.Note: the function shrink was created in Project 2. We use it here to input some matrices. Ifyou do not have it in your Current Folder, please see the code in Exercise3 of this Project.**Run the function [x1,x2]=solveall(A,b); as indicated below:%(a)A=magic(4); b=A(:,4), A=orth(A)[x1,x2]=solveall(A,b);%(b)A=magic(5), b=rand(5,1)[x1,x2]=solveall(A,b);%(c)A=magic(4); A=shrink(A), b=ones(4,1)[x1,x2]=solveall(A,b);%(d)A=magic(4); A=shrink(A), b=rand(4,1)[x1,x2]=solveall(A,b);%(e)A=magic(6); A=orth(A), b=rand(6,1)[x1,x2]=solveall(A,b);% Analyze the outputs n1 and n2 for both consistent and inconsistent systems. Based on thegeometrical meaning of the least-squares error n1 explain the difference between the forms ofthe outputs n1 and n2 for consistent systems vs. inconsistent systems.% Compare n1 and n2 for each inconsistent system and write a comment whether the leastsquaressolution, x1 (or x2) may, indeed, minimize the distance between the vector b and thevectors Ax of the Col A (which has to be true according to the general theory).13Part IV. Or”

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导