mldivide - Solve systems of linear equations Ax = B for

                            x - MATLAB ([original](https://in.mathworks.com/help/matlab/ref/double.mldivide.html)) ([raw](?raw))

Solve systems of linear equations Ax = B for_x_

Syntax

Description

[x](#btg5qam-x) = [A](#mw%5Fb8923d43-9b0d-494b-9dd6-97428b8963bf)\[B](#mw%5Fb8923d43-9b0d-494b-9dd6-97428b8963bf) solves the system of linear equations A*x = B. The matrices A andB must have the same number of rows. MATLAB® displays a warning message ifA is badly scaled or nearly singular, but performs the calculation regardless.

example

[x](#btg5qam-x) = mldivide([A](#mw%5Fb8923d43-9b0d-494b-9dd6-97428b8963bf),[B](#mw%5Fb8923d43-9b0d-494b-9dd6-97428b8963bf)) is an alternative way to execute x = 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×1

1.0000
1.0000
1.0000

Solve a linear system of equations A*x = b involving 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 = 1.306145e-17.

x = 4×1

1.5000
2.5000

-0.5000 0.5000

When rcond is between 0 and 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.

When 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.

In this case, division by zero leads to computations with Inf and/or 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

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 = 5×1 sparse double column vector (5 nonzeros) (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. A andB must have the same number of rows.

If A is scalar, element-wise division is performed. Integer data types are only supported for element-wise division. See ldivide for more information.

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. If A is anm-by-n matrix and B is anm-by-p matrix, then x is ann-by-p matrix, including the case whenp==1.

If A has full storage,x is also full. IfA is sparse, thenx has the same storage asB.

Tips

Algorithms

collapse all

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 inputsA and B are full.

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

If A is full and B is sparse then mldivide converts B to 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 ofB and mldivide follows the algorithm path for sparse inputs, shown below.

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

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 Capabilities

expand all

This function supports tall arrays with the limitation:

For the syntax Z = X\Y, the arrayX must be a scalar or a tall matrix with the same number of rows asY.

For more information, see Tall Arrays for Out-of-Memory Data.

Usage notes and limitations:

Usage notes and limitations:

The mldivide function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

Sparse matrices of type double are supported for code generation.

The mldivide function shows improved performance when solving linear systems A*x = b with a full tridiagonal coefficient matrix A.mldivide now detects tridiagonal structures in both dense and sparse matrices and uses a specific solver for these cases.

Previously, mldivide detected tridiagonal structures only in sparse matrices and used the relevant solver only if more than half of the values were nonzero.

For example, this code solves a linear system specified for tridiagonal matrix D. The code is about 6.5x faster than in the previous release.

function t = timingTest n = 5e3; A = randn(n); [L,D,P] = ldl(A,"upper"); b = randn(n,1);

f = @() D\b; t = timeit(f); end

The approximate execution times are:

R2023b: 0.13 s

R2024a: 0.02 s

The code was timed on a Windows® 11, AMD EPYC™ 74F3 24-Core Processor @ 3.19 GHz test system by calling the timingTest function.

The mldivide function shows improved performance when solving linear systems A*x = b with a small coefficient matrix A. The performance improvement applies to real matrices that are 16-by-16 or smaller, and complex matrices that are 8-by-8 or smaller.

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 thetimeit function:

The LDL factorization is no longer used for full matrices that are Hermitian indefinite. Instead, the LU factorization is used for these matrices.