Applied Numerical Methods Using MATLAB. Won Y. Yang

Чтение книги онлайн.

Читать онлайн книгу Applied Numerical Methods Using MATLAB - Won Y. Yang страница 18

Applied Numerical Methods Using MATLAB - Won Y. Yang

Скачать книгу

array?

      12 (A13) Use the ‘ length()’, ‘ size()’, and ‘ numel()’ commands as indicated as follows:>length(D) ans = 4 >[M,N]=size(A) M = 3, N = 3 >Number_of_elements=numel(A) Number_of_elements = 9

      MATLAB enables us to handle vector/matrix operations in almost the same way as scalar operations. However, we must make sure of the dimensional compatibility between vectors/matrices, and put a dot ( .) in front of the operator for elementwise (element‐by‐element) operations. The addition of a matrix and a scalar adds the scalar to every element of the matrix. The multiplication of a matrix by a scalar multiplies every element of the matrix by the scalar.

      There are several things to know about the matrix division and inversion.

      Remark 1.1 Rules of Vector/Matrix Operation

      1 (1) For a matrix to be invertible, it must be square and nonsingular, i.e. the numbers of its rows and columns must be equal and its determinant must not be zero.

      2 (2) The MATLAB command ‘pinv(A)’ provides us with a matrix X of the same dimension as AT such that AXA = A and XAX = X. We can use this command to get the right/left pseudo (generalized) inverse AT[AAT]−1/[ATA]−1AT for a matrix A given as its input argument, depending on whether the number (M) of rows is smaller or greater than the number (N) of columns, so long as the matrix is of full rank, i.e. rank(A) = min(M,N) [K-2, Section 6.4]. Note that AT[AAT]−1/[ATA]−1AT is called the right/left inverse since it is multiplied onto the right/left side of A to yield an identity matrix.

      3 (3) You should be careful when using the ‘ pinv(A)’ command for a rank‐deficient matrix, because its output is no longer the right/left inverse, which does not even exist for rank‐deficient matrices.

      4 (4) The value of a scalar function having an array value as its argument is also an array with the same dimension.

       >a1=[-1 2 3]; a2=[4 5 2]; b1=[1 -3]'; b2=[-2 0]; images, images, images, images >A1=[a1;a2], A2=[a1;[b2 1]], B=[b1 b2'] images , images, images

      The results of various operations on these vectors/matrices are as follows (pay attention to the error message):

       >A3=A1+A2, A4=A1-A2, 1+A1 % matrix/scalar addition/subtraction A3= -2 4 6 A4= 0 0 0 ans= 0 3 4 2 5 3 6 5 1 5 6 3 >AB=A1*B % images matrix multiplication? Error using * Inner matrix dimensions must agree. >BA1=B*A1 % regular matrix multiplication BA1=-9 -8 -1 3 -6 -9 >AA=A1.*A2 % element-wise (term-wise) multiplication AA= 1 4 9 -8 0 2 >AB=A1.*B %AB(m, n) = A1(m, n)B(m, n) element-wise multiplication Error using .* Matrix dimensions must agree. >A1_1=pinv(A1),A1'*(A1*A1')̂-1,eye(size(A1,2))/A1 %images A1_1= -0.1914 0.1399 %right inverse of a 2x3 matrix A1 0.0617 0.0947 0.2284 -0.0165 >A1*A1_1 %A1/A1=I implies the validity of A5_1 as the right inverse ans= 1.0000 0.0000 0.0000 1.0000 >A5=A1'; % a 3x2 matrix >A5_1=pinv(A5),(A5'*A5)̂-1*A5',A5\eye(size(A5,1)) % images A5_1= -0.1914 0.0617 0.2284 %left inverse of a 3x2 matrix A5 0.1399 0.0947 -0.0165 >A5_1*A5 %A5\A5=I implies the validity of A5_1 as the left inverse ans= 1.0000 -0.0000 -0.0000 1.0000 >A1_li=(A1'*A1)̂-1*A1' %the left inverse of matrix A1 with M<N? Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 6.211163e-18. A1_li = -0.2500 0 0.2500 -0.5000 0.5000 0.2500

      1 (Q14) Does the left inverse of a matrix having rows fewer than columns exist?

      2 (A14) No. There is no N × M matrix that is premultiplied on the left of an M × N matrix with M < N to yield a nonsingular matrix, far from an identity matrix. In this context, MATLAB should have rejected the above case on the ground that is singular and so its inverse does not exist. But, because the round‐off errors make a very small number appear to be a zero or a real zero appear to be a very small number (as will be mentioned in Remark 2.3), it is not easy for MATLAB to tell a near‐singularity from a real singularity. That is why MATLAB dares not to declare the singularity case and instead issues just a warning message to remind you to check the validity of the result so that it will not be blamed for a delusion. Therefore, you must be alert for the condition mentioned in Remark 1.1(2), which says that, in order for the left inverse to exist, the number of rows must not be less than the number of columns.>A1_li*A1 %No identity matrix, since A1_li isn't the left inverse ans = 1.2500 0.7500 -0.2500 -0.2500 0.5000 0.7500 1.5000 3.5000 2.5000 >det(A1'*A1) %A1 is not left-invertible for A1'*A1 is singular ans = 0

      3 (cf) Let us be nice to MATLAB as it is to us. From the standpoint of promoting mutual understanding between us and MATLAB, we acknowledge that MATLAB tries to show us apparently good results to please us like always, sometimes even pretending not to be obsessed by the demon of ‘ill‐condition’ in order not to make us feel uneasy. How kind MATLAB is! But, we should be always careful not to be spoiled by its benevolence and not to accept the computing results every inch as it is. In this case, even though the matrix [A1'*A1] is singular and so not invertible, MATLAB tried to invert it and that's all. MATLAB must have felt something abnormal as can be seen from the ominous warning message prior to the computing result. Who would blame MATLAB for being so thoughtful and loyal to us? We might well be rather touched by its sincerity and smartness.

      In the aformentioned statements, we see the slash( /)/backslash( \) operators. These operators are used for right/left division, respectively; B/A is the same as B*inv(A) and A\B is the same as inv(A)*B when A is invertible and the dimensions of A and B are compatible. Noting that B/A is equivalent to (A'\B')', let us take a close look at the function of the backslash( \) operator.

      >X=A1\A1 % an identity matrix? X= 1.0000 0 -0.8462 0 1.0000 1.0769 0 0 0

      1 (Q13) It seems that A1\A1 should have been an identity matrix, but it is not, contrary to our expectation. Why?

      2 (A13) We should know more about the various functions of the backslash( \), which can be seen by typing ‘ help slash’ into the MATLAB Command window. Let Remark 1.2 answer this question in cooperation with the next case.>A1*X-A1 %zero if X is the solution to A1*X=A1? ans= 1.0e-015 * 0 0 0 0 0 -0.4441

      Remark 1.2 The Function of Backslash(\) Operator

      Overall, for the command ‘ A\B’, MATLAB finds a solution to the equation A*X=B. Let us denote the row/column dimension of the matrix A by M and N.

      1 (1) If matrix A is square and upper/lower‐triangular in the sense that all of its elements below/above the diagonal are

Скачать книгу