Solve systems of linear equations Ax = B for x
x = A\B
x = mldivide(A,B)
the system of linear equations
A*x = B. The matrices
have the same number of rows. MATLAB® displays a warning message
A is badly scaled or nearly singular, but
performs the calculation regardless.
A is a scalar, then
A is a square
B is a matrix with
x = A\B is a solution to the equation
= B, if it exists.
A is a rectangular
m ~= n, and
B is a matrix
m rows, then
a least-squares solution to the system of equations
Solve a simple system of linear equations,
A*x = B.
A = magic(3); B = [15; 15; 15]; x = A\B
x = 1.0000 1.0000 1.0000
Solve a linear system of equations
A*x = b involving a singular matrix,
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 = 1.5000 2.5000 -0.5000 0.5000
rcond is between
eps, 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, although
rcond is small.
rcond is equal to
0, the singular warning appears.
A = [1 0; 0 0]; b = [1; 1]; x = A\b
Warning: Matrix is singular to working precision. x = 1 Inf
In this case, division by zero leads to computations with
NaN, 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 = 0 4.0000 0.6667
Solve a simple system of linear equations using sparse matrices.
Consider the matrix equation
A*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
A— Coefficient matrixvector | full matrix | sparse matrix
Coefficient matrix, specified as a vector, full matrix, or sparse
m rows, then
Complex Number Support: Yes
x— Solutionvector | full matrix | sparse matrix
If the rank of
A is less than
the number of columns in
not necessarily the minimum norm solution. The more computationally
x = pinv(A)*B computes the minimum norm
For full singular inputs, you can compute the least-squares
solution using the function
The versatility of
mldivide in solving linear
systems stems from its ability 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 between full (also called "dense")
and sparse input arrays.
The flow chart below shows the algorithm path when inputs
B are full.
A is full and
a full matrix and uses the full algorithm path (above) to compute
a solution with full storage. If
A is sparse, the
storage of the solution
x is the same as that of
the algorithm path for sparse inputs,