Main Content

linsolve

Solve linear equations in matrix form

Description

example

X= linsolve(A,B)solves the matrix equationAX=B, whereBis a column vector.

example

[X,R] = linsolve(A,B)also returns the reciprocal of the condition number ofAifAis a square matrix. Otherwise,linsolvereturns the rank ofA.

Examples

collapse all

解决这个系统的线性equations in matrix form by usinglinsolve.

[ 2 1 1 1 1 1 1 2 3 ] [ x y z ] = [ 2 3 10 ]

A = [ 2 1 1; -1 1 -1; 1 2 3]; B = [2; 3; -10]; X = linsolve(A,B)
X = 3 1 -5

FromX,x= 3,y= 1andz= –5.

Compute the reciprocal of the condition number of the square coefficient matrix by using two output arguments.

syms a x y z A = [a 0 0; 0 a 0; 0 0 1]; B = [x; y; z]; [X, R] = linsolve(A, B)
X = x/a y/a z R = 1/(max(abs(a), 1)*max(1/abs(a), 1))

If the coefficient matrix is rectangular,linsolvereturns the rank of the coefficient matrix as the second output argument. Show this behavior.

syms a b x y A = [a 0 1; 1 b 0]; B = [x; y]; [X, R] = linsolve(A, B)
Warning: Solution is not unique because the system is rank-deficient. In sym.linsolve at 67 X = x/a -(x - a*y)/(a*b) 0 R = 2

Input Arguments

collapse all

Coefficient matrix, specified as a symbolic matrix.

Right side of equations, specified as a symbolic vector or matrix.

Output Arguments

collapse all

Solution, returned as a symbolic vector or matrix.

Reciprocal condition number or rank, returned as a symbolic number of expression. IfAis a square matrix,linsolvereturns the condition number ofA. Otherwise,linsolvereturns the rank ofA.

More About

collapse all

Matrix Representation of System of Linear Equations

A system of linear equations is as follows.

a 11 x 1 + a 12 x 2 + + a 1 n x n = b 1 a 21 x 1 + a 22 x 2 + + a 2 n x n = b 2 a m 1 x 1 + a m 2 x 2 + + a m n x n = b m

This system can be represented as the matrix equation A x = b , whereAis the coefficient matrix.

A = ( a 11 a 1 n a m 1 a m n )

b is the vector containing the right sides of equations.

b = ( b 1 b m )

Tips

  • If the solution is not unique,linsolveissues a warning, chooses one solution, and returns it.

  • If the system does not have a solution,linsolveissues a warning and returnsXwith all elements set toInf.

  • Callinglinsolvefor numeric matrices that are not symbolic objects invokes the MATLAB®linsolvefunction. This function accepts real arguments only. If your system of equations uses complex numbers, usesymto convert at least one matrix to a symbolic matrix, and then calllinsolve.

Version History

Introduced in R2012b