18.06 MATLAB Transcript September 12, 2012 >> A=[3 7 2 ; 17 -9 0 ; 1000 1001 pi] A = 1.0e+003 * 0.0030 0.0070 0.0020 0.0170 -0.0090 0 1.0000 1.0010 0.0031 >> format bank >> A A = 3.00 7.00 2.00 17.00 -9.00 0 1000.00 1001.00 3.14 >> M=[A eye(3)] M = Columns 1 through 4 3.00 7.00 2.00 1.00 17.00 -9.00 0 0 1000.00 1001.00 3.14 0 Columns 5 through 6 0 0 1.00 0 0 1.00 >> format long >> M M = 1.0e+003 * Columns 1 through 3 0.003000000000000 0.007000000000000 0.002000000000000 0.017000000000000 -0.009000000000000 0 1.000000000000000 1.001000000000000 0.003141592653590 Columns 4 through 6 0.001000000000000 0 0 0 0.001000000000000 0 0 0 0.001000000000000 >> format bank >> M M = Columns 1 through 4 3.00 7.00 2.00 1.00 17.00 -9.00 0 0 1000.00 1001.00 3.14 0 Columns 5 through 6 0 0 1.00 0 0 1.00 >> A A = 3.00 7.00 2.00 17.00 -9.00 0 1000.00 1001.00 3.14 >> M(2,:)=M(2,:)-(M(2,1)/M(1,1) )*M(1,:) M = Columns 1 through 4 3.00 7.00 2.00 1.00 0 -48.67 -11.33 -5.67 1000.00 1001.00 3.14 0 Columns 5 through 6 0 0 1.00 0 0 1.00 >> M(3,:)=M(3,:)-(M(3,1)/M(1,1) )*M(1,:) M = Columns 1 through 4 3.00 7.00 2.00 1.00 0 -48.67 -11.33 -5.67 0 -1332.33 -663.53 -333.33 Columns 5 through 6 0 0 1.00 0 0 1.00 >> M(1,:)=M(1,:)-(M(1,2)/M(2,2) )*M(2,:) M = Columns 1 through 4 3.00 0 0.37 0.18 0 -48.67 -11.33 -5.67 0 -1332.33 -663.53 -333.33 Columns 5 through 6 0.14 0 1.00 0 0 1.00 >> M(3,:)=M(3,:)-(M(1,3)/M(2,2) )*M(2,:) M = Columns 1 through 4 3.00 0 0.37 0.18 0 -48.67 -11.33 -5.67 0 -1332.70 -663.61 -333.38 Columns 5 through 6 0.14 0 1.00 0 0.01 1.00 >> M(3,:)=M(3,:)+(M(1,3)/M(2,2) )*M(2,:) M = Columns 1 through 4 3.00 0 0.37 0.18 0 -48.67 -11.33 -5.67 0 -1332.33 -663.53 -333.33 Columns 5 through 6 0.14 0 1.00 0 0 1.00 >> M(3,:)=M(3,:)-(M(3,2)/M(2,2) )*M(2,:) M = Columns 1 through 4 3.00 0 0.37 0.18 0 -48.67 -11.33 -5.67 0 0 -353.26 -178.20 Columns 5 through 6 0.14 0 1.00 0 -27.38 1.00 >> M(1,:)=M(1,:)-(M(1,3)/M(3,3) )*M(3,:) M = Columns 1 through 4 3.00 0 0 -0.00 0 -48.67 -11.33 -5.67 0 0 -353.26 -178.20 Columns 5 through 6 0.12 0.00 1.00 0 -27.38 1.00 >> M(2,:)=M(2,:)-(M(2,3)/M(3,3) )*M(3,:) M = Columns 1 through 4 3.00 0 0 -0.00 0 -48.67 -0.00 0.05 0 0 -353.26 -178.20 Columns 5 through 6 0.12 0.00 1.88 -0.03 -27.38 1.00 >> M(1,:)=M(1,:)/M(1,1) M = Columns 1 through 4 1.00 0 0 -0.00 0 -48.67 -0.00 0.05 0 0 -353.26 -178.20 Columns 5 through 6 0.04 0.00 1.88 -0.03 -27.38 1.00 >> M(2,:)=M(2,:)/M(2,2) M = Columns 1 through 4 1.00 0 0 -0.00 0 1.00 0.00 -0.00 0 0 -353.26 -178.20 Columns 5 through 6 0.04 0.00 -0.04 0.00 -27.38 1.00 >> M(3,:)=M(3,:)/M(3,3) M = Columns 1 through 4 1.00 0 0 -0.00 0 1.00 0.00 -0.00 0 0 1.00 0.50 Columns 5 through 6 0.04 0.00 -0.04 0.00 0.08 -0.00 >> M(:,4:6) ans = -0.00 0.04 0.00 -0.00 -0.04 0.00 0.50 0.08 -0.00 >> format long >> M(:,4:6) ans = -0.000548214335572 0.038390621028586 0.000349004085521 -0.001035515967192 -0.038595493612671 0.000659229939317 0.504446627388532 0.077498296101471 -0.002830810915891 >> inv(A) ans = -0.000548214335572 0.038390621028586 0.000349004085521 -0.001035515967192 -0.038595493612671 0.000659229939317 0.504446627388532 0.077498296101471 -0.002830810915891 >> b=rand(3,1) b = 0.814723686393179 0.905791937075619 0.126986816293506 >> inv(A)*b ans = 0.034371590700301 -0.035719432796722 0.480822471934664 >> A\b ans = 0.034371590700301 -0.035719432796722 0.480822471934664 >> help lu LU LU factorization. [L,U] = LU(A) stores an upper triangular matrix in U and a "psychologically lower triangular matrix" (i.e. a product of lower triangular and permutation matrices) in L, so that A = L*U. A can be rectangular. [L,U,P] = LU(A) returns unit lower triangular matrix L, upper triangular matrix U, and permutation matrix P so that P*A = L*U. [L,U,p] = LU(A,'vector') returns the permutation information as a vector instead of a matrix. That is, p is a row vector such that A(p,:) = L*U. Similarly, [L,U,P] = LU(A,'matrix') returns a permutation matrix P. This is the default behavior. Y = LU(A) returns the output from LAPACK'S DGETRF or ZGETRF routine if A is full. If A is sparse, Y contains the strict lower triangle of L embedded in the same matrix as the upper triangle of U. In both full and sparse cases, the permutation information is lost. [L,U,P,Q] = LU(A) returns unit lower triangular matrix L, upper triangular matrix U, a permutation matrix P and a column reordering matrix Q so that P*A*Q = L*U for sparse non-empty A. This uses UMFPACK and is significantly more time and memory efficient than the other syntaxes, even when used with COLAMD. [L,U,p,q] = LU(A,'vector') returns two row vectors p and q so that A(p,q) = L*U. Using 'matrix' in place of 'vector' returns permutation matrices. [L,U,P,Q,R] = LU(A) returns unit lower triangular matrix L, upper triangular matrix U, permutation matrices P and Q, and a diagonal scaling matrix R so that P*(R\A)*Q = L*U for sparse non-empty A. This uses UMFPACK as well. Typically, but not always, the row-scaling leads to a sparser and more stable factorization. Note that this factorization is the same as that used by sparse MLDIVIDE when UMFPACK is used. [L,U,p,q,R] = LU(A,'vector') returns the permutation information in two row vectors p and q such that R(:,p)\A(:,q) = L*U. Using 'matrix' in place of 'vector' returns permutation matrices. [L,U,P] = LU(A,THRESH) controls pivoting in sparse matrices, where THRESH is a pivot threshold in [0,1]. Pivoting occurs when the diagonal entry in a column has magnitude less than THRESH times the magnitude of any sub-diagonal entry in that column. THRESH = 0 forces diagonal pivoting. THRESH = 1 is the default. [L,U,P,Q,R] = LU(A,THRESH) controls pivoting in UMFPACK. THRESH is a one or two element vector which defaults to [0.1 0.001]. If UMFPACK selects its unsymmetric pivoting strategy, THRESH(2) is not used. It uses its symmetric pivoting strategy if A is square with a mostly symmetric nonzero structure and a mostly nonzero diagonal. For its unsymmetric strategy, the sparsest row i which satisfies the criterion A(i,j) >= THRESH(1) * max(abs(A(j:m,j))) is selected. A value of 1.0 results in conventional partial pivoting. Entries in L have absolute value of 1/THRESH(1) or less. For its symmetric strategy, the diagonal is selected using the same test but with THRESH(2) instead. If the diagonal entry fails this test, a pivot entry below the diagonal is selected, using THRESH(1). In this case, L has entries with absolute value 1/min(THRESH) or less. Smaller values of THRESH(1) and THRESH(2) tend to lead to sparser LU factors, but the solution can become inaccurate. Larger values can lead to a more accurate solution (but not always), and usually an increase in the total work and memory usage. [L,U,p] = LU(A,THRESH,'vector') and [L,U,p,q,R] = LU(A,THRESH,'vector') are also valid for sparse matrices and return permutation vectors. Using 'matrix' in place of 'vector' returns permutation matrices. See also colamd, luinc, qr, rref. Overloaded methods: gf/lu codistributed/lu Reference page in Help browser doc lu >> [L, ] ??? Undefined function or variable 'L'. >> [L,U,P]=lu(A) L = 1.000000000000000 0 0 0.017000000000000 1.000000000000000 0 0.003000000000000 -0.153630318637814 1.000000000000000 U = 1.0e+003 * 1.000000000000000 1.001000000000000 0.003141592653590 0 -0.026017000000000 -0.000053407075111 0 0 0.001982370276072 P = 0 0 1 0 1 0 1 0 0 >> P*L*U ans = 1.0e+003 * 0.003000000000000 0.007000000000000 0.002000000000000 0.017000000000000 -0.009000000000000 0 1.000000000000000 1.001000000000000 0.003141592653590 >> A A = 1.0e+003 * 0.003000000000000 0.007000000000000 0.002000000000000 0.017000000000000 -0.009000000000000 0 1.000000000000000 1.001000000000000 0.003141592653590 >> M\b ans = 0.814723686393179 0.905791937075619 0.126986816293506 0 0 0 >> A\b ans = 0.034371590700301 -0.035719432796722 0.480822471934664 >> (P'*b) ans = 0.126986816293506 0.905791937075619 0.814723686393179 >> b b = 0.814723686393179 0.905791937075619 0.126986816293506 >> y=L\(P'*b) y = 0.126986816293506 0.903633161198630 0.953168176430939 >> x=U\y x = 0.034371590700301 -0.035719432796722 0.480822471934664 >> A\b ans = 0.034371590700301 -0.035719432796722 0.480822471934664 >> E21=eye(3) E21 = 1 0 0 0 1 0 0 0 1 >> E31=E21 E31 = 1 0 0 0 1 0 0 0 1 >> E32=E21 E32 = 1 0 0 0 1 0 0 0 1 >> E21(2,1)=5 E21 = 1 0 0 5 1 0 0 0 1 >> E31(3,1)=10 E31 = 1 0 0 0 1 0 10 0 1 >> E32(3,2)=20 E32 = 1 0 0 0 1 0 0 20 1 >> inv(E32) ans = 1 0 0 0 1 0 0 -20 1 >> inv(E31) ans = 1 0 0 0 1 0 -10 0 1 >> inv(E31)*inv(E32) ans = 1 0 0 0 1 0 -10 -20 1 >> inv(E21) ans = 1 0 0 -5 1 0 0 0 1 >> inv(E21)*inv(E31)*inv(E32) ans = 1 0 0 -5 1 0 -10 -20 1 >> a=rand(400); >> tic,a*a;toc Elapsed time is 0.064897 seconds. >> tic,a*a;toc Elapsed time is 0.021466 seconds. >> tic,a*a;toc Elapsed time is 0.012885 seconds. >> tic,a*a;toc Elapsed time is 0.022099 seconds. >> tic,a*a;toc Elapsed time is 0.021547 seconds. >> tic,a*a;toc Elapsed time is 0.013176 seconds. >> tic,a*a;toc Elapsed time is 0.014252 seconds. >> a=rand(800); >> tic,a*a;toc Elapsed time is 0.112677 seconds. >> tic,a*a;toc Elapsed time is 0.117518 seconds. >> tic,a*a;toc Elapsed time is 0.116449 seconds. >> a=rand(1600); >> tic,a*a;toc Elapsed time is 0.936886 seconds. >> tic,a*a;toc Elapsed time is 0.950110 seconds. >> tic,a*a;toc Elapsed time is 0.956027 seconds. >> a=rand(3200); >> tic,a*a;toc Elapsed time is 7.279893 seconds. >> tic,a*a;toc Elapsed time is 7.305957 seconds. >> sum( (1:100).^2) ans = 338350 >> (100)^3/3 ans = 3.333333333333333e+005 >> format bank >> ans ans = 333333.33 >> A A = 3.00 7.00 2.00 17.00 -9.00 0 1000.00 1001.00 3.14 >> A' ans = 3.00 17.00 1000.00 7.00 -9.00 1001.00 2.00 0 3.14 >> M=A' M = 3.00 17.00 1000.00 7.00 -9.00 1001.00 2.00 0 3.14 >> A(1,3) ans = 2.00 >> M(3,1) ans = 2.00