14 1.14 Routine for Matrix MultiplicationAssuming that MATLAB cannot perform direct multiplication on vectors/matrices, supplement the following incomplete function l.l ‘ multiply_matrix(A,B)’ so that it can multiply two matrices given as its input arguments only if their dimensions are compatible, but display an error message if their dimensions are not compatible. Try it to get the product of two arbitrary 3 × 3 matrices generated by the command ‘ rand(3)’ and compare the result with that obtained by using the direct multiplicative operator *. Note that the matrix multiplication can be described as(P1.14.1) function C=<b>multiply_matrix</b><![CDATA[(A,B) [M,K]=size(A); [K1,N]=size(B); if K1∼=K error('The # of columns of A is not equal to the # of rows of B') else for m=1:? for n=1:? C(m,n)=A(m,1)*B(1,n); for k=2:? C(m,n)=C(m,n)+A(m,k)*B(k,n); end end end end
15 1.15 Function for Finding Vector NormAssuming that MATLAB does not have the ‘ norm()’ command finding us the norm of a given vector/matrix, make a routine ‘ norm_vector(v,p)’ which computes the norm of a given vector as(P1.15.1) for any positive integer p, finds the maximum absolute value of the elements for p=inf, and computes the norm as if p=2, even if the second input argument p is not given. If you have no idea, permutate the statements in the below box and save it in a file named “norm_vector.m”. Additionally, try it to get the norm with p=1, 2,∞ ( inf) for an arbitray vector generated by the command ‘ rand(2,1)’. Compare the result with that obtained by using the ‘ norm()’ command.function nv=<b>norm_vector</b><![CDATA[(v,p) if nargin<2, p=2; end nv= sum(abs(v).̂p)̂(1/p); nv= max(abs(v)); if p>0&p∼=inf elseif p==inf end
16 1.16 Backslash (\) OperatorLet us play with the backslash ( \) operator.Use the backslash ( \) command, the minimum‐norm solution (2.1.7), and the ‘ pinv()’ command to solve the following equations, find the residual error ||Aix ‐bi||'s and the rank of the coefficient matrix Ai, and fill in Table P1.16 with the results.(P1.16.1) (P1.16.2) (P1.16.3) (b) Use the backslash( \) command, the least‐squares (LSs) solution (2.1.10), and the ‘ pinv()’ command to solve the following equations and find the residual error ||Aix ‐bi||'s and the rank of the coefficient matrix Ai, and fill in Table P1.16 with the results.(P1.16.4) (P1.16.5) (P1.16.6) (cf) If some or all of the rows of the coefficient matrix A in a system of linear equations can be expressed as a linear combination of other row(s), the corresponding equations are dependent, which can be revealed by the rank deficiency, i.e. rank(A) < min(M,N) where M and N are the row and column dimensions, respectively. If some equations are dependent, they may have either inconsistency (no exact solution) or redundancy (infinitely many solutions), which can be distinguished by checking if augmenting the RHS vector b to the coefficient matrix A increases the rank or not, that is, rank([Ab]) > rank(A) or not [M-2].(c) Based on the results obtained in (a) and (b) and listed in Table P1.16, answer the following questions:Based on the results obtained in (a‐(i)), which one yielded the nonminimum‐norm solution among the three methods, i.e. the backslash ( \) operator, the minimum‐norm solution (2.1.7), and the ‘ pinv()’ command? Note that the minimum‐norm solution means the solution whose norm (||x||) is the minimum over the many solutions.Based on the results obtained in (a), which one is most reliable as a means of finding the minimum‐norm solution among the three methods?Based on the results obtained in (b), choose two reliable methods as a means of finding the LS solution among the three methods, i.e. the backslash ( \) operator, the LS solution (2.1.10), and the ‘ pinv()’ command. Note that the LS solution means the solution for which the residual error (||Ax−b||) is the minimum over the many solutions.Table P1.16 Results of operations with backslash( \) operator and ‘ pinv()’ command.backslash ( \)Minimum‐norm or least‐squares (LS) pinv()Remarkx||Aix − bi||x||Aix − bi||x||Aix − bi||rank(Ai) redundant/inconsistentA1x − b11.5000 0 1.50006.4047 × 10−15A2x − b20.3143 0.6286 0.94291.7889A3x − b3∞ ∞ ∞∞A4x − b42.5000 0.00001.2247A5x − b5A6x − b6
17 1.17 Operations on VectorsFind the mathematical expression for the computation to be done by the following MATLAB statements: >n=0:100; S=sum(2.̂-n)Write a MATLAB statement which performs the following computation:Write a MATLAB statement that uses the commands ‘ prod()’ and ‘ sum()’ to compute the product of the sums of each row of a 3 × 3 random matrix.How does the following function ‘ repetition(x,M,N)’ convert a scalar or a matrix given as the first input argument x to make a new sequence y? How does it compare with the MATLAB built‐in function ‘ repmat()’?function y=<b>repetition</b><![CDATA[(x,M,N) y1=x; for n=2:N, y1 = [y1 x]; end y=y1; for m=2:M, y = [y; y1]; endComplete the following function ‘ zero_insertion(x,M,m)’ so that it can insert m zeros just after every Mth element of a given row vector sequence x to make a new sequence. Write a MATLAB statement to use the function for inserting two zeros just after every third element of x = [1 3 7 2 4 9] to gety = [1 3 7 0 0 2 4 9 0 0]function y=<b>zero_insertion</b><![CDATA[(x,M,m) Nx=length(x); N=floor(Nx/M); y=[reshape(x(1:M*N),?,N); ????s(m,N)]; y=[y(:)' x(M*N+1:Nx)];How does the following function ‘ zeroing(x,M,m)’ convert a given row vector sequence x to make a new sequence y?function y=<b>zeroing</b><![CDATA[(x,M,m)