Main Content

mldivide,\

Solve systems of linear equationsAx = Bforx

Description

example

x=A\Bsolves the system of linear equationsA*x = B. The matricesAandBmust have the same number of rows. MATLAB®displays a warning message ifAis badly scaled or nearly singular, but performs the calculation regardless.

  • IfAis a scalar, thenA\B相当于A.\B.

  • IfAis a squaren-by-nmatrix andBis a matrix withnrows, thenx = A\Bis a solution to the equationA*x = B, if it exists.

  • IfAis a rectangularm-by-nmatrix withm ~= n, andBis a matrix withmrows, thenA\Breturns a least-squares solution to the system of equationsA*x= B.

x= mldivide(A,B)is an alternative way to executex=A\B, but is rarely used. It enables operator overloading for classes.

Examples

collapse all

Solve a simple system of linear equations,A*x = B.

A = magic(3); B = [15; 15; 15]; x = A\B
x =3×11.0000 1.0000 1.0000

Solve a linear system of equationsA*x = binvolving a singular matrix,A.

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 = 4.625929e-18.
x =4×10.9804 0.9412 1.0588 1.0196

Whenrcondis between0andeps, MATLAB® issues a nearly singular warning, but proceeds with the calculation. When working with ill-conditioned matrices, an unreliable solution can result even though the residual(b-A*x)is relatively small. In this particular example, the norm of the residual is zero, and an exact solution is obtained, althoughrcondis small.

Whenrcondis equal to0, the singular warning appears.

A = [1 0; 0 0]; b = [1; 1]; x = A\b
Warning: Matrix is singular to working precision.
x =2×11 Inf

In this case, division by zero leads to computations withInfand/orNaN, making the computed result unreliable.

Solve a system of linear equations,A*x = b.

A = [1 2 0; 0 4 3]; b = [8; 18]; x = A\b
x =3×10 4.0000 0.6667

Solve a simple system of linear equations using sparse matrices.

Consider the matrix equationA*x = B.

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 Arguments

collapse all

Operands, specified as vectors, full matrices, or sparse matrices.AandBmust have the same number of rows.

  • IfAorBhas an integer data type, the other input must be scalar. Operands with an integer data type cannot be complex.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char
Complex Number Support:Yes

Output Arguments

collapse all

Solution, returned as a vector, full matrix, or sparse matrix. IfAis anm-by-nmatrix andBis anm-by-pmatrix, thenxis ann-by-pmatrix, including the case whenp==1.

IfAhas full storage,xis also full. IfAis sparse, thenxhas the same storage asB.

Tips

  • The operators/and\are related to each other by the equationB/A = (A'\B')'.

  • IfAis a square matrix, thenA\Bis roughly equal toinv(A)*B, but MATLAB processesA\Bdifferently and more robustly.

  • If the rank ofAis less than the number of columns inA, thenx = A\Bis not necessarily the minimum norm solution. You can compute the minimum norm least-squares solution usingx =lsqminnorm(A,B)orx =pinv(A)*B.

  • Usedecompositionobjects to efficiently solve a linear system multiple times with different right-hand sides.decompositionobjects are well-suited to solving problems that require repeated solutions, since the decomposition of the coefficient matrix does not need to be performed multiple times.

Algorithms

collapse all

The versatility ofmldivide在解决线性系统源于能力to take advantage of symmetries in the problem by dispatching to an appropriate solver. This approach aims to minimize computation time. The first distinction the function makes is betweenfull(also called “dense”) andsparseinput arrays.

Algorithm for Full Inputs

The flow chart below shows the algorithm path when inputsAandBarefull.

The properties of full input matrices determine which algorithm mldivide uses to solve the linear system

Algorithm for Sparse Inputs

IfAis full andBis sparse thenmldivideconvertsBto a full matrix and uses the full algorithm path (above) to compute a solution with full storage. IfAis sparse, the storage of the solutionxis the same as that ofBandmldividefollows the algorithm path forsparseinputs, shown below.

The properties of sparse input matrices determine which algorithm mldivide uses to solve the linear system

Extended Capabilities

Version History

Introduced before R2006a

expand all

Behavior changed in R2022a