### 15.7.6 Solving linear systems

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 15.3.5), which can be useful if you have several systems which differ only by their right-hand side.

If the Step by step box is is checked in the CAS configuration (see Section 2.5.9), linsolve will show you the steps in getting a solution.

##### Solving a system in symbolic form.

- To solve a system of symbolic 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

To solve the system

⎧ ⎪ ⎨ ⎪ ⎩ |
2x+y+z=1,
x+y+2z=1,
x+2y+z=4,

input:

linsolve([2*x+y+z=1,x+y+2*z=1,x+2*y+z=4],[x,y,z])

| ⎡ ⎢ ⎢ ⎣ | − |
1
2
| , |
5
2
| ,− |
1
2
| ⎤ ⎥ ⎥ ⎦

Therefore x=−1/2, y=5/2 and z=−1/2.

An example of solving an underdetermined system:

linsolve([x+2*y+3*z=1,3*x+2*y+z=2],[x,y,z])

| ⎡ ⎢ ⎢ ⎣ | z+ |
1
2
| ,−2 z+ |
1
4
| ,z | ⎤ ⎥ ⎥ ⎦

##### Solving a system in matrix form.

- To solve a system represented by its coefficient matrix, 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

linsolve([[2,1,1],[1,1,2],[1,2,1]],[1,1,4])

| ⎡ ⎢ ⎢ ⎣ | − |
1
2
| , |
5
2
| ,− |
1
2
| ⎤ ⎥ ⎥ ⎦

If the Step by step option is checked in the CAS configuration, a window will also pop up showing:

⎡ ⎢ ⎢ ⎣ |
2 | 1 | 1 | −1
1 | 1 | 2 | −1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
| Reduce column 1 with pivot 1 at row 2 |  |  |  |  |  |  |  |  |
| Exchange row 1 and row 2 |  |  |  |  |  |  |  |  |
L2 <−(1)· L2 −(2)· L1 on |
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
2 | 1 | 1 | −1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
L3 <− 1· L3 −(1)· L1 on |
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | −1 | −3 | 1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | −1 | −3 | 1
0 | 1 | −1 | −3
| ⎤ ⎥ ⎥ ⎦
| Reduce column 2 with pivot 1 at row 3 |  |  |  |  |  |  |  |  |
| Exchange row 2 and row 3 |  |  |  |  |  |  |  |  |
L1 <− (1)· L1−(1)· L2 on |
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | 1 | −1 | −3
0 | −1 | −3 | 1
| ⎤ ⎥ ⎥ ⎦
L3 <− (1)· L3−(−1)· L2 on |
⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | −1 | −3 | 1
| ⎤ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎦
| Reduce column 3 with pivot -4 at row 3 |  |  |  |  |  |  |  |  |
L1 <− (1)· L1−(−3/4)· L3 on |
⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎦
L2 <− (1)· L2−(1/4)· L3 on |
⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 |
1
2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦
End reduction |
⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 |
1
2
0 | 1 | 0 |
− |
5
2
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
2 | 1 | 1 | −1
1 | 1 | 2 | −1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
| Reduce column 1 with pivot 1 at row 2 |  |  |  |  |  |  |  |  |
| Exchange row 1 and row 2 |  |  |  |  |  |  |  |  |
L2 <− (1)· L2 −(2)· L1 on |
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
2 | 1 | 1 | −1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
L3 <− (1)· L3−(1)· L1 on |
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | −1 | −3 | 1
1 | 2 | 1 | −4
| ⎤ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | −1 | −3 | 1
0 | 1 | −1 | −3
| ⎤ ⎥ ⎥ ⎦
| Reduce column 2 with pivot 1 at row 3 |  |  |  |  |  |  |  |  |
| Exchange row 2 and row 3 |  |  |  |  |  |  |  |  |
L1 <− (1)· L1 −(1)· L2 on |
⎡ ⎢ ⎢ ⎣ |
1 | 1 | 2 | −1
0 | 1 | −1 | −3
0 | −1 | −3 | 1
| ⎤ ⎥ ⎥ ⎦
L3 <− (1)· L3−(−1)· L2 on |
⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | −1 | −3 | 1
| ⎤ ⎥ ⎥ ⎦
⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎦
| Reduce column 3 with pivot -4 at row 3 |  |  |  |  |  |  |  |  |
L1 <− (1)· L1−(−3/4)· L3 on |
⎡ ⎢ ⎢ ⎣ |
1 | 0 | 3 | 2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎦
L2 <− (1)· L2−(1/4)· L3 on |
⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 |
1
2
0 | 1 | −1 | −3
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦
End reduction |
⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
1 | 0 | 0 |
1
2
0 | 1 | 0 |
− |
5
2
0 | 0 | −4 | −2
| ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

##### Solving a system in factored form.

- To solve a system represented by its matrix 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

p,l,u:=lu([[2,1,1],[1,1,2],[1,2,1]]); linsolve(p,l,u,[1,1,4])

| ⎡ ⎢ ⎢ ⎣ | − |
1
2
| , |
5
2
| ,− |
1
2
| ⎤ ⎥ ⎥ ⎦

##### Solving a system with coefficients modulo n.

The linsolve command also solves systems with coefficients in ℤ/nℤ. For example:

linsolve([2*x+y+z-1,x+y+2*z-1,x+2*y+z-4]%3,[x,y,z])

| ⎡ ⎣ | 1%3,1%3,1%3 | ⎤ ⎦