Solve linear system of equations
X = linsolve(A,B)
X = linsolve(A,B,opts)
[X,r] = linsolve(___)
A is square,
linsolve uses LU factorization with partial pivoting.
For all other cases,
linsolve uses QR factorization with
linsolve warns if
A is ill
conditioned (for square matrices) or rank deficient (for rectangular matrices).
uses an appropriate solver as determined by the options structure
X = linsolve(
The fields in
opts are logical values describing properties of the matrix
A. For example, if
A is an upper triangular matrix,
you can set
opts.UT = true to make
linsolve use a
solver designed for upper triangular matrices.
linsolve does not test
to verify that
A has the properties specified in
r] = linsolve(___)
r, which is the reciprocal of the condition number of
A (for square matrices) or the rank of
rectangular matrices). You can use any of the input argument combinations in previous
syntaxes. With this syntax,
linsolve does not warn if
A is ill conditioned or rank deficient.
Solve a linear system with both
linsolve to compare performance.
mldivide is the recommended way to solve most linear systems of equations in MATLAB ®. However, the function performs several checks on the input matrix to determine whether it has any special properties. If you know about the properties of the coefficient matrix ahead of time, then you can use
linsolve to avoid time-consuming checks for large matrices.
Create a 10000-by-10000 magic square matrix and extract the lower triangular portion. Set the
LT field of the
opts structure to
true to indicate that
A is a lower triangular matrix.
A = tril(magic(1e4)); opts.LT = true;
Create a vector of ones for the right-hand side of the linear equation . The number of rows in
b must be equal.
b = ones(size(A,2),1);
Solve the linear system using
mldivide and time the calculation.
tic x1 = A\b; t1 = toc
t1 = 1.0226
Now, solve the system again using
linsolve. Specify the options structure so that
linsolve can select an appropriate solver for a lower triangular matrix.
tic x2 = linsolve(A,b,opts); t2 = toc
t2 = 0.7282
Compare the execution times to see how much faster
linsolve is. As with any timing comparison, the results can vary between different computers and releases of MATLAB.
speedup = t1/t2
speedup = 1.4042
Solve a linear system using
linsolve with two outputs to suppress matrix conditioning warnings.
Create a 20-by-20 Hilbert test matrix. This matrix is nearly singular, with the largest singular value being about
2e18 larger than the smallest.
A = hilb(20);
Solve a linear system involving
A is nearly singular,
linsolve returns a warning.
b = ones(20,1); x = linsolve(A,b);
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.465792e-19.
Now, solve the same linear system, but specify two outputs to
linsolve. MATLAB® suppresses the warning, and the second output
r contains the reciprocal condition number of
A. You can use this syntax to handle ill-conditioned matrices with special cases in your code, without the code producing a warning.
[x,r] = linsolve(A,b)
x = 20×1 109 × -0.0000 0.0000 -0.0000 -0.0000 0.0039 -0.0418 0.2098 -0.5513 0.6272 0.3062 ⋮
r = 1.4658e-19
A— Coefficient matrix
A appears in the system of linear equations
on the left as AX =
B. The number of rows in
A must equal the number of
Complex Number Support: Yes
B— Input array
Input array, specified as a vector or matrix.
B appears in the
system of linear equations on the right as AX =
B is a matrix, then each column in the matrix
represents a different vector for the right-hand side.
The number of rows in
A must equal the number of rows in
Complex Number Support: Yes
opts— Coefficient matrix properties
Coefficient matrix properties, specified as a structure. Use this structure to
specify properties of
linsolve uses to
select an appropriate solver for the linear system. The fields in the structure contain
false values to indicate whether
A has each property. By default all fields in the structure are
assumed to be
false. This table lists the possible fields in
opts and their corresponding matrix properties.
Lower triangular (nonzero values appearing only on or below the main diagonal)
Upper triangular (nonzero values appearing only on or above the main diagonal)
Upper Hessenberg (all zero values below the first subdiagonal)
Real symmetric or complex Hermitian (matrix equal to its transpose)
Positive definite (all positive eigenvalues)
Rectangular matrix (different number of rows and columns)
Conjugate transpose — Specifies whether the function solves
opts.UT = true specifies that
opts.SYM = true, opts.POSDEF = true sets two fields to
A is symmetric and positive definite.
The rows of this table list all combinations of field values in
opts that are valid for
cells are the default value of
false, and a
false entry indicates that
linsolve accepts either value.
A has the properties in
linsolve is faster compared to
linsolve invokes the
appropriate solver immediately and does not perform any tests to verify that
A has the specified properties.
A does not have the properties that
you specify in
incorrect results and does not return an error message. Therefore, if you are
A has the specified properties, use
X— Linear system solution
Linear system solution, returned as a vector or matrix that satisfies AX =
B (or ATX =
opts.TRANSA = true). The size of
X depends on whether
opts.TRANSA = true:
k and is the
solution to AX =
opts.TRANSA = true, then
k. In this case,
k and is the solution to ATX
r— Reciprocal condition number or rank
Reciprocal condition number or rank, returned as a scalar.
A is a square matrix, then
r is the
reciprocal condition number of
A is a rectangular matrix, then
the rank of
opts is specified, then
r is the
reciprocal of the condition number of
true and both
false, in which case,
r gives the rank of
For small problems, using
linsolve has no speed benefit as
opposed to using the
mldivide function. The speed benefit with
linsolve arises by avoiding costly checks on the properties of
Usage notes and limitations:
opts structure must be a constant scalar. Code generation
does not support arrays of options structures.
Code generation only optimizes these cases:
TRANSA can be either
Other options are equivalent to using
Code generation does not support sparse matrix inputs for this function.