MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr MATLAB Lecture2-- Solving Linear Systems of equations 线性方程组求解 Ref: matlab-Mathematics-Matrices and Linear algebra Solving linear Systems of equations ● Vocabulary: coefficient matrix系数矩阵 linear Systems of Equations线性方程组 row elementary transpositions行初等变换 basis基 backslash反斜线符号 最小二乘解 nonsingular matriⅸx非奇异阵,可逆矩阵 particular solution特解 homogeneous system导出组 olution space解空间 near homogeneous equation线性齐次方程,齐次一次方程 non- homogeneous system非齐次系统(线性方程组) linearly independent线性无关 pseudoinverse广义逆 rational有理数 component元素 determinant行列式 rank秩 overdetermined system超定系统(即方程组无精确解) underdetermined system不定系统(即方程组有无穷多解) orthonormal basis正交基 nul零空间,核空间 Some operations and functions nk det iny rref null o Application on solving linear systems of equations Ax=b, A is an m X n matrix ☆ Review b=0 Theorv r(A)=n, there is only one exact solution zero r(A)<n, there are infinite nonzero solutions r(a)=n, only zero is its exact solution r(A)<n, row elementary transpositions-basis for the null space of A The r(A)=r(a bn, there is only one exact solution r(Ar(a bkn, the ere are infinite solutions r(A)≠r(Ab) there is no exact solution m=n& r(AFr(A b=n, Cramer'rule, A b; row elementary transposition m*n& r(A)=r(A bkn basis for the null space of A, particular solution
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec21 MATLAB Lecture2 Solving Linear Systems of Equations 线性方程组求解 Ref: MATLAB→Mathematics→Matrices and Linear Algebra →Solving Linear Systems of Equations l Vocabulary: coefficient matrix 系数矩阵 linear Systems of Equations 线性方程组 row elementary transpositions 行初等变换 basis 基 backslash 反斜线符号 least squares solution 最小二乘解 nonsingular matrix 非奇异阵,可逆矩阵 particular solution 特解 homogeneous system 导出组 solution space 解空间 linear homogeneous equation 线性齐次方程,齐次一次方程 nonhomogeneous system 非齐次系统(线性方程组) linearly independent 线性无关 pseudoinverse 广义逆 rational 有理数 component 元素 determinant 行列式 rank 秩 overdetermined system 超定系统(即方程组无精确解) underdetermined system 不定系统(即方程组有无穷多解) orthonormal basis 正交基 null 零空间,核空间 l Some operations and functions ’ .’ \ rank det inv rref null l Application on solving linear systems of equations Ax=b, A is an m×n matrix ² Review: ¸ b=0 Theory r(A) =n, there is only one exact solution zero. r(A) <n, there are infinite nonzero solutions. Computation r(A) =n, only zero is its exact solution r(A) <n, row elementary transpositions→basis for the null space of A ¸ b≠0 Theory r(A)=r(A b)=n, there is only one exact solution r(A)=r(A b)<n, there are infinite solutions r(A)≠r(A b), there is no exact solution Computation m=n & r(A)=r(A b)=n, Cramer’ rule; A -1b; row elementary transposition m≠n & r(A) =n, row elementary transpositions m≠n & r(A)=r(A b)<n basis for the null space of A, particular solution
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr ☆ MATLAB MATLAB Solves such linear equations without computing the inverse of the matrix The two division symbols, slash, / and backslash, \, are used for the two situations where the unknown matrix appears on the left or right of the coefficient matrix. X-AIB Denotes the solution to the matrix equation AX=B X= B/A Denotes the solution to the matrix equation XA=B The coefficient matrix A need not be square. If A is m-by-n, there are three cases m=n Square system. Seek an exact solution. m>oVerdetermined system. Find a least squares solution. m >A= pascal( 3): % Obtain a Pascal matrix >>u=[3;1;4 >>x=Alu %Try x=pinv(A)*u, xiv(A*A)*A*u Lec2
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec22 ² MATLAB MATLAB solves such linear equations without computing the inverse of the matrix. The two division symbols, slash, /, and backslash, \, are used for the two situations where the unknown matrix appears on the left or right of the coefficient matrix. X = A\B Denotes the solution to the matrix equation AX = B. X = B/A Denotes the solution to the matrix equation XA = B. The coefficient matrix A need not be square. If A is mbyn, there are three cases. m = n Square system. Seek an exact solution. m > n Overdetermined system. Find a least squares solution. m > A = pascal(3); % Obtain a Pascal matrix >> u = [3; 1; 4]; >> x = A\u % Try x=pinv(A)*u; x=inv(A’*A)*A’*u x =
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr ular Coefficient Matrix If A is singular, the solution to AX=B either does not exist, or is not unique. The backslash operator, AB, issues a warning if A is nearly singular and raises an error condition if it detects exact singularity. If A is singular and AX-b has a solution, you can find a particular solution that is not unique typing >> P= pinv(A)b pinv(A)is a Moore-Penrose pseudoinverse of A. The Moore-Penrose pseudoinverse is a matrix B of the same dimensions as a satisfying four conditions A*BA=A B*A*B=B A*B is Hermitian B*A is Hermitian The computation is based on svd(a) If Ax =b does not have an exact solution, pinv(A)returns a least-squares solution. PInV(A=(A'A)A' For example, 1018 is singular, as you can verify by typing ans Exact Solutions For b=[5: 2: 12, the equation AX-b has an exact solution, given by ans 0.3850 -0.1103 0.7066 You can verify that pinv(a)"b is an exact solution by typing >>A'pinv(A)"b Lec2-3
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec23 10 12 5 Singular Coefficient Matrix If A is singular, the solution to AX = B either does not exist, or is not unique. The backslash operator, A\B, issues a warning if A is nearly singular and raises an error condition if it detects exact singularity. If A is singular and AX = b has a solution, you can find a particular solution that is not unique, by typing >> P = pinv(A)*b pinv(A) is a MoorePenrose pseudoinverse of A. The MoorePenrose pseudoinverse is a matrix B of the same dimensions as A' satisfying four conditions: A*B*A = A B*A*B = B A*B is Hermitian B*A is Hermitian The computation is based on svd(A). If AX = b does not have an exact solution, pinv(A) returns a leastsquares solution. pinv(A)=(A’A) 1A’ For example, >> A = [ 1 3 7 1 4 4 1 10 18 ] is singular, as you can verify by typing >> det(A) ans = 0 Exact Solutions. For b =[5;2;12], the equation AX = b has an exact solution, given by >> pinv(A)*b ans = 0.3850 0.1103 0.7066 You can verify that pinv(A)*b is an exact solution by typing >> A*pinv(A)*b ans =
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr 5.0000 2.0000 12.0000 Least Squares Solutions. If b=[3: 6: 0, then AX-b does not have an exact solution. In this case, pinv(a)*b returns a least squares solution. If you type >>A pinv(A)b 10000 4.0000 2.0000 lo not get back the original vector b You can determine whether AX= b has an exact solution by finding the row reduced echelon form of the augmented matrix [A b]. To do so for this example, enter > rref(A b) ans 1.0000 022857 01.00001.5714 0 0 010000 Since the bottom row contains all zeros except for the last entry, the equation does not have a solution. In this case, pinv(A)returns a least-squares solution Overdetermined systems Similar to Least Squares Solutions. Omitted Underdetermined Systems >>R=fix(10*rand(2, 4)) %Obtain an integer matrix by rand and fix function >>b=fx(10*rand(2,1) b > format rat display the solution in rational format >>X0=RIb finds a basic solution, which has at most m nonzero components Lec2-4
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec24 5.0000 2.0000 12.0000 Least Squares Solutions. If b = [3;6;0], then AX = b does not have an exact solution. In this case, pinv(A)*b returns a least squares solution. If you type >> A*pinv(A)*b ans = 1.0000 4.0000 2.0000 you do not get back the original vector b. You can determine whether AX = b has an exact solution by finding the row reduced echelon form of the augmented matrix [A b]. To do so for this example, enter >> rref([A b]) ans = 1.0000 0 2.2857 0 0 1.0000 1.5714 0 0 0 0 1.0000 Since the bottom row contains all zeros except for the last entry, the equation does not have a solution. In this case, pinv(A) returns a leastsquares solution. Overdetermined systems Similar to Least Squares Solutions. Omitted. Underdetermined Systems >> R = fix(10*rand(2,4)) % Obtain an integer matrix by rand and fix function R = 6 8 7 3 3 5 4 1 >> b = fix(10*rand(2,1)) b = 1 2 >> format rat % display the solution in rational format >> x0 = R\b % finds a basic solution, which has at most m nonzero components x0 = 0 5/7 0 11/7
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr >>Z=nul(R,r)% find an orthonormal basis(正交基) for the null space of R Z 7/6 It can be confirmed that R"Z is zero and that any vector x where > syms kl k2 k; Define three symbol variables >>k=[kl;k2] >>x=x0+Z*k %General solutions Column Full Rank Systems >>X=inv(A'A)A'b theoretically computes the same least squares solution x, although the backslash operator does it a does not have full rank x=Alb %a basic solution; it has at most r nonzero components, where r is the rank of A x= pinv(A)"b %the minimal norm solution because it minimizes norm(x X=inv(A'A)*A*b %fails because a"*A is singular >>A=fix(10°rand(3)) >>b=fix(10*rand(3,1)) > ABarA b > d=rref(ABar) %The last column of d is the solution of system d 119/48
MATLAB Lecture 2 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec25 >> Z = null(R,'r') % find an orthonormal basis (正交基) for the null space of R Z = 1/2 7/6 1/2 1/2 1 0 0 1 It can be confirmed that R*Z is zero and that any vector x where >> syms k1 k2 k; % Define three symbol variables >> k=[k1; k2]; >> x = x0 + Z*k %General solutions Column Full Rank Systems >> x = A\b >> x = pinv(A)*b >> x = inv(A'*A)*A'*b theoretically computes the same least squares solution x, although the backslash operator does it faster. A does not have full rank x = A\b %a basic solution; it has at most r nonzero components, where r is the rank of A. x = pinv(A)*b %the minimal norm solution because it minimizes norm(x). x = inv(A'*A)*A'*b %fails because A'*A is singular. Example >> A=fix(10*rand(3)); >> b=fix(10*rand(3,1)); >> ABar=[A b]; >> d=rref(ABar) %The last column of d is the solution of system d = 1 0 0 1/8 0 1 0 73/48 0 0 1 119/48