linsolve - Solve linear system of equations - MATLAB (original) (raw)
Solve linear system of equations
Syntax
Description
[X](#mw%5F71546c16-6dd2-499c-9e78-916b923fdf59) = linsolve([A](#mw%5F52023500-1d5a-4bba-9a6d-2a77ab1fde3d),[B](#mw%5F6fc752ec-2a12-4102-99a0-f2bb6a1c1f56))
solves the linear system A X = B using one of these methods:
- When
A
is square,linsolve
uses LU factorization with partial pivoting. - For all other cases,
linsolve
uses QR factorization with column pivoting.
linsolve
warns if A
is ill conditioned (for square matrices) or rank deficient (for rectangular matrices).
[X](#mw%5F71546c16-6dd2-499c-9e78-916b923fdf59) = linsolve([A](#mw%5F52023500-1d5a-4bba-9a6d-2a77ab1fde3d),[B](#mw%5F6fc752ec-2a12-4102-99a0-f2bb6a1c1f56),[opts](#mw%5Ff23b9376-00ee-4eba-8af3-48ee240f0b77))
uses an appropriate solver as determined by the options structure opts
. The fields in opts
are logical values describing properties of the matrixA
. 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 inopts
.
[[X](#mw%5F71546c16-6dd2-499c-9e78-916b923fdf59),[r](#mw%5Fcc8f0b87-752f-4f69-8716-ddfe6aeec9f6)] = linsolve(___)
also returns r
, which is the reciprocal of the condition number ofA
(for square matrices) or the rank of A
(for rectangular matrices). You can use any of the input argument combinations in previous syntaxes. With this syntax, linsolve
does not warn ifA
is ill conditioned or rank deficient.
Examples
Solve Linear System
Solve a linear system with both mldivide
and 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 Ax=b. The number of rows in A
and b
must be equal.
Solve the linear system Ax=b using mldivide
and time the calculation.
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
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.
Suppress Matrix Condition Warnings
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.
Solve a linear system involving A
with linsolve
. Since 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 = 5.628781e-20.
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 = 20×1 109 ×
-0.0000 0.0000 -0.0004 0.0071 -0.0592 0.2819 -0.7821 1.1830 -0.7030 -0.1061 ⋮
Input Arguments
A
— Coefficient matrix
matrix
Coefficient matrix. A
appears in the system of linear equations on the left as A X =B. The number of rows in A
must equal the number of rows in B
.
A
cannot be sparse. To solve a linear system involving a sparse matrix, use mldivide or decomposition instead.
Data Types: single
| double
Complex Number Support: Yes
B
— Input array
vector | matrix
Input array, specified as a vector or matrix. B
appears in the system of linear equations on the right as A X =B. If 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 inB
.
Data Types: single
| double
Complex Number Support: Yes
opts
— Coefficient matrix properties
structure
Coefficient matrix properties, specified as a structure. Use this structure to specify properties of A
that linsolve
uses to select an appropriate solver for the linear system. The fields in the structure containtrue
/false
values to indicate whetherA
has each property. By default all fields in the structure are assumed to be false
. This table lists the possible fields inopts
and their corresponding matrix properties.
Field | Matrix Property |
---|---|
LT | Lower triangular (nonzero values appearing only on or below the main diagonal) |
UT | Upper triangular (nonzero values appearing only on or above the main diagonal) |
UHESS | Upper Hessenberg (all zero values below the first subdiagonal) |
SYM | Real symmetric or complex Hermitian (matrix equal to its transpose) |
POSDEF | Positive definite (all positive eigenvalues) |
RECT | Rectangular matrix (different number of rows and columns) |
TRANSA | Conjugate transpose — Specifies whether the function solvesA*X = B (when opts.TRANSA = false) or the transposed problem A'*X = B (when opts.TRANSA = true) |
Example: opts.UT = true
specifies that A
is upper triangular.
Example: opts.SYM = true, opts.POSDEF = true
sets two fields to specify that A
is symmetric and positive definite.
Valid Combinations
The rows of this table list all combinations of field values inopts
that are valid for linsolve
. Empty cells are the default value of false
, and atrue
/false
entry indicates thatlinsolve
accepts either value.
| | LT | UT | UHESS | SYM | POSDEF | RECT | TRANSA | | | --------------------- | ---- | ----- | ---- | ------ | ---------- | ---------- | ---------- | | A is lower triangular | true | | | | | true/false | true/false | | A is upper triangular | | true | | | | true/false | true/false | | A is upper Hessenberg | | | true | | | | true/false | | A is symmetric | | | | true | true/false | | true/false | | A is rectangular | | | | | | true/false | true/false |
Notes on Usage
- If
A
has the properties inopts
, thenlinsolve
is faster compared tomldivide
, becauselinsolve
invokes the appropriate solver immediately and does not perform any tests to verify thatA
has the specified properties. - If
A
does not have the properties that you specify inopts
, thenlinsolve
returns incorrect results and does not always return an error message. Therefore, if you are unsure whetherA
has the specified properties, usemldivide or decomposition instead.
Data Types: struct
Output Arguments
X
— Linear system solution
vector | matrix
Linear system solution, returned as a vector or matrix that satisfies A X =B (or A_T_X =B if opts.TRANSA = true
). The size ofX
depends on whether opts.TRANSA = true
:
- If
A
ism
-by-n
andB
ism
-by-k
, thenX
isn
-by-k
and is the solution to A X =B. - If
opts.TRANSA = true
, thenA
ism
-by-n
andB
isn
-by-k
. In this case,X
ism
-by-k
and is the solution to A_T_X = B.
r
— Reciprocal condition number or rank
scalar
Reciprocal condition number or rank, returned as a scalar.
- If
A
is a square matrix, thenr
is the reciprocal condition number ofA
. - If
A
is a rectangular matrix, thenr
is the rank ofA
. - If
opts
is specified, thenr
is the reciprocal of the condition number ofA
unlessRECT
istrue
and bothLT
andUT
arefalse
, in which case,r
gives the rank ofA
.
Tips
- The speed benefit of
linsolve
can vary depending on the matrix structure and the relative optimization of the underlying algorithms. In some cases (such as with small matrices) there might not be any speed-up compared tomldivide
. The speed benefit withlinsolve
arises by avoiding costly checks on the properties of large matrices, or by choosing an algorithm that is better suited to the input than the choice thatmldivide
makes.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- The
opts
structure must be a constant scalar. Code generation does not support arrays of options structures. - Code generation only optimizes these cases:
UT
LT
UHESS
=true
(theTRANSA
can be eithertrue
orfalse
)SYM
=true
andPOSDEF
=true
Other options are equivalent to usingmldivide
.
- Code generation does not support sparse matrix inputs for this function.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
- The
opts
structure must be a constant scalar. Code generation does not support arrays of options structures. - Code generation only optimizes these cases:
UT
LT
UHESS
=true
(theTRANSA
can be eithertrue
orfalse
)SYM
=true
andPOSDEF
=true
Other options are equivalent to usingmldivide
.
- Code generation does not support sparse matrix inputs for this function.
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:
- The two-output syntax
[x,r] = linsolve(___)
is not supported. - The MATLAB®
linsolve
function prints a warning ifA
is badly scaled, nearly singular, or rank deficient. ThegpuArray
linsolve
is unable to check for this condition. Take action to avoid this condition.
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™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a