MATLAB Function Reference |
Solve a linear system of equations
Syntax
Description
X = linsolve(A,B)
solves the linear system A*X = B
using LU factorization with partial pivoting when A is square and QR factorization with column pivoting otherwise. The number of columns of A
must equal the number of rows of B
. If A
is m-by-n and B
is n-by-k, then X
is m-by-k. linsolve
returns a warning if A
is square and ill conditioned or if it is not square and rank deficient.
[X, R] = linsolve(A,B)
suppresses these warnings and returns R
, which is the reciprocal of the condition number of A
if A
is square, or the rank of A
if A
is not square.
X = linsolve(A,B,opts)
solves the linear system A*X = B
or A'*X = B
, using the solver that is most appropriate given the properties of the matrix A
, which you specify in opts
. For example, if A
is upper triangular, you can set opts.UT = true
to make linsolve
use a solver designed for upper triangular matrices. If A
has the properties in opts
, linsolve
is faster than mldivide
, because linsolve
does not perform any tests to verify that A
has the specified properties.
Caution
If A does not have the properties that you specify in opts , linsolve returns incorrect results and does not return an error message. If you are not sure whether A has the specified properties, use mldivide instead.
|
The TRANSA
field of the opts
structure specifies the form of the linear system you want to solve:
opts.TRANSA = false
, linsolve(A,B,opts)
solves A*X = B
.
opts.TRANSA
= true
, linsolve(A,B,opts)
solves A'*X = B
.
The following table lists all the field of opts
and their corresponding matrix properties. The values of the fields of opts
must be logical
and the default value for all fields is false
.
The following table lists all combinations of field values in opts
that are valid for linsolve
. A true/false entry indicates that linsolve
accepts either true or false.
Example
The following code solves the system A'x = b
for an upper triangular matrix A
using both mldivide
and linsolve
.
A = triu(rand(5,3)); x = [1 1 1 0 0]'; b = A'*x; y1 = (A')\b opts.UT = true; opts.TRANSA = true; y2 = linsolve(A,b,opts) y1 = 1.0000 1.0000 1.0000 0 0 y2 = 1.0000 1.0000 1.0000 0 0
See Also
linkprop | linspace |
© 1994-2005 The MathWorks, Inc.