Printed from: Wolfram Mathematica - Complete Documentation Solving Linear Systems Many calculations involve solving systems of linear equations. the equations explicitly, In many cases, you will find it convenient to write down and then solve them using Solve. In some cases, however, you may prefer to convert the system of linear equations into a matrix equation, apply matrix manipulation operations part of a general algorithm, and then to solve it. This approach is often useful when the system of equations arises as and you do not know in advance how many variables will be involved. A system of linear equations can be stated in matrix form as m.x = b, where x is the vector of variables. Note that if your system of equations is sparse, so that most of the entries in the matrix m are zero, then it is best to represent the matrix as a SparseArray object. As discussed in "Sparse Arrays: Linear Algebra" , you can convert from symbolic equations to SparseArray objects using CoefficientArrays. All the functions described here work on SparseArray objects as well as ordinary matrices. LinearSolve@m,bD a vector x which solves the matrix equation m.x == b NullSpace@mD a list of linearly independent vectors whose linear combinations solutions to the matrix equation m.x == 0 MatrixRank@mD the number of linearly independent RowReduce@mD a simplified form of m obtained by making linear combinations span all rows or columns of m of rows Solving and analyzing linear systems. Here is a 2x2 matrix. In[1]:= Out[1]= m = 881, 5<, 82, 1<< 881, 5<, 82, 1<< This gives two linear equations. In[2]:= Out[2]= m.8x, y< == 8a, b< 8x + 5 y, 2 x + y< 8a, b< You can use Solve directly to solve these equations. In[3]:= Solve@%, 8x, y<D Out[3]= ::x ® 1 H-a + 5 bL, y ® 9 1 H2 a - bL>> 9 You can also get the vector of solutions by calling LinearSolve. The result is equivalent to the one you get from Solve. In[4]:= LinearSolve@m, 8a, b<D Out[4]= : 1 9 H-a + 5 bL, 1 H2 a - bL> 9 © 1988– 2007 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com 1 Printed from: Wolfram Mathematica - Complete Documentation Another way to solve the equations is to invert the matrix m, and then multiply 8a, b< by the inverse. This is not as efficient as using LinearSolve. In[5]:= [email protected], b< Out[5]= :- a 5b 2a , + 9 9 b - 9 > 9 RowReduce performs a version of Gaussian elimination and can also be used to solve the equations. In[6]:= RowReduce@881, 5, a<, 82, 1, b<<D Out[6]= ::1, 0, 1 H-a + 5 bL>, :0, 1, 9 1 H2 a - bL>> 9 If you have a square matrix m with a non -zero determinant, equation m.x = b for any b. If, however, the matrix then you can always find a unique solution to the matrix m has determinant infinite number of vectors x which satisfy m.x = b for a particular zero, then there may be either no vector, or an b. This occurs when the linear equations embodied in m are not independent. When m has determinant of vectors x satisfying zero, it is nevertheless this equation expressed as a linear combination always possible to find non -zero vectors x that satisfy m.x = 0. The set form the null space or kernel of the matrix of a particular m. Any of these vectors can be set of basis vectors, which can be obtained using NullSpace@mD. Here is a simple matrix, corresponding to two identical linear equations. In[7]:= Out[7]= m = 881, 2<, 81, 2<< 881, 2<, 81, 2<< The matrix has determinant zero. In[8]:= Det@mD Out[8]= 0 LinearSolve cannot find a solution to the equation m.x b in this case. In[9]:= LinearSolve@m, 8a, b<D Out[9]= LinearSolve@881, 2<, 81, 2<<, 8a, b<D LinearSolve::nosol : Linear equation encountered that has no solution. There is a single basis vector for the null space of m. In[10]:= Out[10]= NullSpace@mD 88-2, 1<< Multiplying the basis vector for the null space by m gives the zero vector. In[11]:= Out[11]= m.%@@1DD 80, 0< There is only 1 linearly independent row in m. In[12]:= MatrixRank@mD Out[12]= 1 NullSpace and MatrixRank have to determine whether particular combinations of matrix elements are zero. For numerical Tolerance option can be ©approximate 1988– 2007 Wolfram Research, matrices, Inc. All rights the reserved. http://reference.wolfram.com used to specify how close to zero is considered good enough. sometimes For exact symbolic matrices, you may need to specify something ZeroTest -> HFullSimplify@ðD == 0 &L to force more to be done to test whether symbolic expressions are zero. like 2 Printed from: Wolfram Mathematica NullSpace and MatrixRank have to determine approximate enough. numerical For matrices, exact whether particular combinations of matrix - Complete Documentation elements are zero. For the Tolerance option can be used to specify how close to zero is considered good symbolic matrices, you may sometimes need to specify something ZeroTest -> HFullSimplify@ðD == 0 &L to force more to be done to test whether symbolic expressions are zero. like Here is a simple symbolic matrix with determinant zero. In[13]:= Out[13]= m = 88a, b, c<, 82 a, 2 b, 2 c<, 83 a, 3 b, 3 c<< 88a, b, c<, 82 a, 2 b, 2 c<, 83 a, 3 b, 3 c<< The basis for the null space of m contains two vectors. In[14]:= NullSpace@mD Out[14]= ::- c , 0, 1>, :- a b , 1, 0>> a Multiplying m by any linear combination of these vectors gives zero. In[15]:= Out[15]= [email protected] %@@1DD + y %@@2DDLD 80, 0, 0< An important feature of functions like LinearSolve and NullSpace is that they work with rect angular , as well as square, matrices. When you represent a system of linear equations by a matrix equation of the form m.x = b, the number of columns in m gives the number of variables, and the number of rows gives the number of equations. There are a number of cases. Unde r de t e r m ine d number of equations less than the number of variables; many solutions may exist Ove r de t e r m ine d number of equations more than the number of variables; or may not exist N onsingula r number of independent equations equal to the number of variables, and determinant non-zero; a unique solution exists Consist e nt I nconsist e nt at least one solution exists no solutions or solutions may no solutions exist Classes of linear systems represented by rectangular matrices. This asks for the solution to the inconsistent set of equations x = 1 and x = 0. In[16]:= LinearSolve@881<, 81<<, 81, 0<D Out[16]= LinearSolve@881<, 81<<, 81, 0<D LinearSolve::nosol : Linear equation encountered that has no solution. This matrix represents two equations, for three variables. In[17]:= Out[17]= m = 881, 3, 4<, 82, 1, 3<< 881, 3, 4<, 82, 1, 3<< LinearSolve gives one of the possible solutions to this underdetermined set of equations. In[18]:= Out[18]= v = LinearSolve@m, 81, 1<D 2 1 : , , 0> 5 5 When a matrix represents an underdetermined system of equations, the matrix has a non-trivial null space. In this case, the null space is spannedResearch, by a single © 1988– 2007 Wolfram Inc.vector. All rights reserved. http://reference.wolfram.com 3 Printed from: Wolfram Mathematica - Complete Documentation When a matrix represents an underdetermined system of equations, the matrix has a non-trivial null space. In this case, the null space is spanned by a single vector. In[19]:= Out[19]= NullSpace@mD 88-1, -1, 1<< If you take the solution you get from LinearSolve, and add any linear combination of the basis vectors for the null space, you still get a solution. In[20]:= Out[20]= m.Hv + 4 %@@1DDL 81, 1< The number of independent equations is the rank of the matrix MatrixRank@mD. The number of redundant Length@NullSpace@mDD. Note that the sum of these quantities equations is is always equal to the number of columns in m. generate a function for solving equations of the form m.x = b LinearSolve@mD Generating LinearSolveFunction objects. In some applications, you will want to solve equations of the form m.x = b many times with the same m, but different You can do this efficiently in Mat hem at ica by using LinearSolve@mD to create a single LinearSolveFunction that you can apply to as many vectors as you want. This creates a LinearSolveFunction. In[21]:= f = LinearSolve@881, 4<, 82, 3<<D Out[21]= LinearSolveFunction@82, 2<, <>D You can apply this to a vector. In[22]:= f@85, 7<D Out[22]= : 13 3 , 5 > 5 You get the same result by giving the vector as an explicit second argument to LinearSolve. In[23]:= LinearSolve@881, 4<, 82, 3<<, 85, 7<D Out[23]= : 13 3 , 5 > 5 But you can apply f to any vector you want. In[24]:= f@8-5, 9<D Out[24]= : 51 19 ,- 5 > 5 LeastSquares@m,bD b. give a vector x that solves the least -squares problem m.x == b Solving least-squares problems. © 1988– 2007 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com 4 Printed from: Wolfram Mathematica This linear system is inconsistent. In[25]:= LinearSolve@881, 2<, 83, 4<, 85, 6<<, 8-1, 0, 2<D LinearSolve::nosol : Linear equation encountered that has no solution. Out[25]= LinearSolve@881, 2<, 83, 4<, 85, 6<<, 8-1, 0, 2<D LeastSquares finds a vector x that minimizes m.x - b in the least-squares sense. In[26]:= Out[26]= LeastSquares@881, 2<, 83, 4<, 85, 6<<, 8-1, 0, 2<D 8 23 : ,> 3 12 © 1988– 2007 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com - Complete Documentation 5
© Copyright 2026 Paperzz