linsolveSolve linear equations in matrix form Show
SyntaxDescriptionexample
example
Examplescollapse all Solve Linear Equations in Matrix FormSolve this system of linear equations in matrix form by using [211 −11−1123][xyz]=[23 −10] A = [ 2 1 1; -1 1 -1; 1 2 3]; B = [2; 3; -10]; X = linsolve(A,B) From Compute Condition Number of Square MatrixCompute the reciprocal of the condition number of the square coefficient matrix by using two output arguments. syms a x y z A = [a 0 0; 0 a 0; 0 0 1]; B = [x; y; z]; [X, R] = linsolve(A, B) X = x/a y/a z R = 1/(max(abs(a), 1)*max(1/abs(a), 1)) Compute Rank of Nonsquare MatrixIf the coefficient matrix is rectangular, syms a b x y A = [a 0 1; 1 b 0]; B = [x; y]; [X, R] = linsolve(A, B) Warning: Solution is not unique because the system is rank-deficient. In sym.linsolve at 67 X = x/a -(x - a*y)/(a*b) 0 R = 2 Input Argumentscollapse all A — Coefficient matrix symbolic matrixCoefficient matrix, specified as a symbolic matrix. B — Right side of equations symbolic vector | symbolic matrixRight side of equations, specified as a symbolic vector or matrix. Output Argumentscollapse all X — Solution symbolic vector | symbolic matrixSolution, returned as a symbolic vector or matrix. R — Reciprocal condition number or rank symbolic number | symbolic expressionReciprocal condition number or rank, returned as a symbolic number of expression. If More Aboutcollapse all Matrix Representation of System of Linear EquationsA system of linear equations is as follows. a11x1+a12x2+…+a1nxn= b1a21x1+a22x2+…+a2nxn=b2⋯am1 x1+am2x2+…+amnxn=bm This system can be represented as the matrix equation A ⋅x→=b→, where A is the coefficient matrix. b→ is the vector containing the right sides of equations. Tips
Version HistoryIntroduced in R2012b
mldivide, \Solve systems of linear equations Ax = B for x SyntaxDescriptionexample
Examplescollapse all System of Equations
Solve a simple system of linear equations, A = magic(3); B = [15; 15; 15]; x = A\B x = 3×1
1.0000
1.0000
1.0000
Linear System with Singular MatrixSolve a linear system of equations A = magic(4); b = [34; 34; 34; 34]; x = A\b Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.306145e-17. x = 4×1
1.5000
2.5000
-0.5000
0.5000
When When A = [1 0; 0 0]; b = [1; 1]; x = A\b Warning: Matrix is singular to working precision. In this case, division by zero leads to computations with Least-Squares Solution of Underdetermined SystemSolve a system of linear equations, A = [1 2 0; 0 4 3]; b = [8; 18]; x = A\b Linear System with Sparse MatrixSolve a simple system of linear equations using sparse matrices. Consider the matrix equation A = sparse([0 2 0 1 0; 4 -1 -1 0 0; 0 0 0 3 -6; -2 0 0 0 2; 0 0 4 2 0]); B = sparse([8; -1; -18; 8; 20]); x = A\B x = (1,1) 1.0000 (2,1) 2.0000 (3,1) 3.0000 (4,1) 4.0000 (5,1) 5.0000 Input Argumentscollapse all A, B — Operands vectors | full matrices | sparse matricesOperands, specified as vectors, full matrices, or sparse matrices.
Data Types: Output Argumentscollapse all x — Solution vector | full matrix | sparse matrixSolution, returned as a vector, full matrix, or sparse matrix. If If Tips
Algorithmscollapse all The versatility of Algorithm for Full InputsThe flow chart below shows the algorithm path when inputs Algorithm for Sparse InputsIf
References[1] Gilbert, John R., and Tim Peierls. “Sparse Partial Pivoting in Time Proportional to Arithmetic Operations.” SIAM Journal on Scientific and Statistical Computing 9, no. 5 (September 1988): 862–874. https://doi.org/10.1137/0909058. [2] Anderson, E., ed. LAPACK Users’ Guide. 3rd ed. Software, Environments, Tools. Philadelphia: Society for Industrial and Applied Mathematics, 1999. https://doi.org/10.1137/1.9780898719604. [3] Davis, Timothy A. "Algorithm 832: UMFPACK V4.3 – an unsymmetric-pattern multifrontal method." ACM Transactions on Mathematical Software 30, no. 2 (June 2004): 196–199. https://doi.org/10.1145/992200.992206. [4] Duff, Iain S. “MA57---a Code for the Solution of Sparse Symmetric Definite and Indefinite Systems.” ACM Transactions on Mathematical Software 30, no. 2 (June 2004): 118–144. https://doi.org/10.1145/992200.992202. [5] Davis, Timothy A., John R. Gilbert, Stefan I. Larimore, and Esmond G. Ng. “Algorithm 836: COLAMD, a Column Approximate Minimum Degree Ordering Algorithm.” ACM Transactions on Mathematical Software 30, no. 3 (September 2004): 377–380. https://doi.org/10.1145/1024074.1024080. [6] Amestoy, Patrick R., Timothy A. Davis, and Iain S. Duff. “Algorithm 837: AMD, an Approximate Minimum Degree Ordering Algorithm.” ACM Transactions on Mathematical Software 30, no. 3 (September 2004): 381–388. https://doi.org/10.1145/1024074.1024081. [7] Chen, Yanqing, Timothy A. Davis, William W. Hager, and Sivasankaran Rajamanickam. “Algorithm 887: CHOLMOD, Supernodal Sparse Cholesky Factorization and Update/Downdate.” ACM Transactions on Mathematical Software 35, no. 3 (October 2008): 1–14. https://doi.org/10.1145/1391989.1391995. [8] Davis, Timothy A. “Algorithm 915, SuiteSparseQR: Multifrontal Multithreaded Rank-Revealing Sparse QR Factorization.” ACM Transactions on Mathematical Software 38, no. 1 (November 2011): 1–22. https://doi.org/10.1145/2049662.2049670. Extended CapabilitiesTall Arrays Calculate with arrays that have more rows than fit in memory.This function supports tall arrays with the limitation: For the syntax For more information, see Tall Arrays for Out-of-Memory Data. C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.Usage notes and limitations:
GPU Code Generation Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.Usage notes and limitations:
Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment. GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.Usage notes and limitations:
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox). Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.Usage notes and limitations:
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox). Version HistoryIntroduced before R2006a expand all R2022b: Improved performance with small matricesThe For example, this code solves a linear system specified by a real 12-by-12 matrix. The code is about 1.7x faster than in the previous release. function mldividePerf A = rand(12); for k = 1:1e5 x = A\A; end end The approximate execution times are: R2022a: 0.72 s R2022b: 0.42 s The code was timed on a Windows® 10, Intel® Xeon® CPU W-2133 @ 3.60 GHz test system using the R2022a: LDL factorization no longer used for full matricesThe LDL factorization is no longer used for full matrices that are Hermitian indefinite. Instead, the LU factorization is used for these matrices. Can we solve equations in MATLAB?Symbolic Math Toolbox™ offers both symbolic and numeric equation solvers. This topic shows you how to solve an equation symbolically using the symbolic solver solve . To compare symbolic and numeric solvers, see Select Numeric or Symbolic Solver.
What is are the command used in MATLAB to solve system of linear equations?Solve System of Linear Equations Using solve
syms x y z eqn1 = 2*x + y + z == 2; eqn2 = -x + y - z == 3; eqn3 = x + 2*y + 3*z == -10; Solve the system of equations using solve . The inputs to solve are a vector of equations, and a vector of variables to solve the equations for.
What is the best way to solve linear systems in MATLAB?mldivide is the recommended way to solve most linear systems of equations in MATLAB®.
|