6.56.6 Linear system solving: linsolve
The linsolve command solves systems of linear equations.
It can take its arguments as a list of equations or as a matrix of
coefficients followed by a vector of the right hand side. It can also
take the matrix of coefficients after an LU factorization (see
Section 6.49.5), which can be useful when you have several systems
of equations which only differ on their right hand side.
If the Step by step box is
is checked in the general configuration (see Section 3.5.9),
linsolve will show you the steps in
getting a solution.
Given a system of equations:
-
linsolve takes two arguments:
-
eqns, a list of linear equations or expressions
(where each expression is assumed to be equal to zero).
- vars, a list of variable names.
- linsolve(eqns,vars) returns the
solution of the equations as a list.
Examples.
-
Input:
linsolve([2*x+y+z=1,x+y+2*z=1,x+2*y+z=4],[x,y,z])
Output:
Which means that
is the solution of the system:
⎧
⎪
⎨
⎪
⎩ | 2x+y+z | =1 |
x+y+2z | =1 |
x+2y+z | =4
|
|
|
- Input:
linsolve([x+2*y+3*z=1,3*x+2*y+z=2],[x,y,z])
Output:
Given the matrix of coefficients:
-
linsolve takes two arguments:
-
A, the matrix of coefficients of a linear system.
- b, a list of the values of the right hand side of the
system.
- linsolve(A,b) returns the solution of the
corresponding equations as a list.
Example.
Input:
linsolve ([[2,1,1], [1,1,2], [1,2,1]], [1,1,4])
Output:
If the Step by step option is checked in the general
configuration, a window will also pop up showing:
| | | | | | | | | | | |
| Reduce column 1 with pivot 1 at row 2 | | | | | | | | | |
| Exchange row 1 and row 2 | | | | | | | | | |
| L2 <−(1)*L2 −(2)*L1 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| L3 <− 1*L3 −(1)*L1 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| | | | | | | | | | |
| Reduce column 2 with pivot 1 at row 3 | | | | | | | | | |
| Exchange row 2 and row 3 | | | | | | | | | |
| L1 <− (1)*L1−(1)*L2 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| L3 <− (1)*L3−(−1)*L2 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| | | | | | | | | | |
| Reduce column 3 with pivot -4 at row 3 | | | | | | | | | |
| L1 <− (1)*L1−(−3/4)*L3 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| L2 <− (1)*L2−(1/4)*L3 on | ⎡
⎢
⎢
⎢
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎥
⎥
⎥
⎦ |
| | | | | | | | | |
| End reduction | ⎡
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎦ |
| | | | | | | | | |
| | | | | | | | | | |
| Reduce column 1 with pivot 1 at row 2 | | | | | | | | | |
| Exchange row 1 and row 2 | | | | | | | | | |
| L2 <− (1)*L2 −(2)*L1 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| L3 <− (1)*L3−(1)*L1 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| | | | | | | | | | |
| Reduce column 2 with pivot 1 at row 3 | | | | | | | | | |
| Exchange row 2 and row 3 | | | | | | | | | |
| L1 <− (1)*L1 −(1)*L2 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| L3 <− (1)*L3−(−1)*L2 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| | | | | | | | | | |
| Reduce column 3 with pivot -4 at row 3 | | | | | | | | | |
| L1 <− (1)*L1−(−3/4)*L3 on | ⎡
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎦ |
| | | | | | | | | |
| L2 <− (1)*L2−(1/4)*L3 on | ⎡
⎢
⎢
⎢
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎥
⎥
⎥
⎦ |
| | | | | | | | | |
| End reduction | ⎡
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎣ | | ⎤
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎦ |
| | | | | | | | | |
|
Given the matrix of coefficients in factored form:
-
linsolve takes four arguments:
-
P, L, U, the LU decomposition of the matrix of coefficients.
- b, a list of the values of the right hand side of the
system.
- linsolve(P,L,U,b) returns the solution of the
corresponding equations as a list.
Example.
Input:
p,l,u:=lu([[2,1,1],[1,1,2],[1,2,1]]) |
linsolve(p,l,u,[1,1,4])
|
Output:
The linsolve command also solves systems with coefficients in
ℤ/nℤ.
Example.
Input:
linsolve([2*x+y+z-1,x+y+2*z-1,x+2*y+z-4]%3,[x,y,z])
Output: