5.61.6 Linear system solving: linsolve
linsolve is used to solve a system of linear equations.
linsolve takes its arguments in two different ways.
-
It can take two arguments, the first is a list of equations or
expressions (in that case the convention is that the equation
is expression = 0), and a list of variable names.
linsolve returns the solution of the system in a list.
Input:
linsolve([2*x+y+z=1,x+y+2*z=1,x+2*y+z=4],[x,y,z])
Output :
[1/-2,5/2,1/-2]
Which means that
is the solution of the system :
⎧
⎪
⎨
⎪
⎩ | 2x+y+z | =1 |
x+y+2z | =1 |
x+2y+z | =4
|
|
|
- It can take two arguments, the matrix of coefficients of a
system and values of the right hand side in the form of a list.
Input:
linsolve ([[2,1,1], [1,1,2], [1,2,1]], [1,1,4])
Output:
[-1/2,5/2,-1/2]
- It can take four arguments; the matrices P, L,
U from the lu decomposition and the values of the
right hand side in the form of a list. This is useful when you have
several systems of equations which only differ on their right hand side.
Input:
p,l,u:=lu([[2,1,1],[1,1,2],[1,2,1]])
linsolve(p,l,u,[1,1,4])
Output:
[-1/2,5/2,-1/2]
If the Step by step option is checked in the general
configuration, a window will also pop up showing:
Matrix [[1,1,2, -1], [0,1, -1, -3], [0, -1, -3,1]]
Row operation L2 <- (1) * L1- (1) * L2
Matrix [[1,0,3,2], [0,1, -1, -3], [0, -1, -3,1]]
Row operation L2 <- (1) * L3 - (- 1) * L2
Matrix [[1,0,3,2], [0,1, -1, -3], [0,0, -4, -2]]
Reducing column 3 using pivot -4 at row 3
Matrix [[1,0,3,2], [0,1, -1, -3], [0,0, -4, -2]]
Row operation L3 <- (-4) * L1- (3) * L3
Matrix [[-4,0,0, -2], [0,1, -1, -3], [0,0, -4, -2]]
Row operation L3 <- (-4) * L2 - (- 1) * L3
End reduction [[-4,0,0, -2], [0, -4,0,10], [0,0, -4, -2]]
The linsolve command also solves systems with coefficients in
ℤ/nℤ.
Input:
linsolve([2*x+y+z-1,x+y+2*z-1,x+2*y+z-4]%3,[x,y,z])
Output:
[1 % 3,1 % 3,1 % 3]