Previous Up Next

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:


Examples.


Given the matrix of coefficients:


Example.
Input:

linsolve ([[2,1,1], [1,1,2], [1,2,1]], [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:

     
 



211−1
112−1
121−4



         
 Reduce column 1 with pivot 1 at row 2         
 Exchange row 1 and row 2         
 
L2 <−(1)*L2 −(2)*L1  on  


112−1
211−1
121−4



         
 
L3 <− 1*L3 −(1)*L1  on  


112−1
0−1−31
121−4



         
 



112−1
0−1−31
01−1−3



         
 Reduce column 2 with pivot 1 at row 3         
 Exchange row 2 and row 3         
 
L1 <− (1)*L1−(1)*L2 on 


112−1
01−1−3
0−1−31



         
 
L3 <− (1)*L3−(−1)*L2  on  


1032
01−1−3
0−1−31



         
 



1032
01−1−3
00−4−2



         
 Reduce column 3 with pivot -4 at row 3         
 
L1 <− (1)*L1−(−3/4)*L3  on  


1032
01−1−3
00−4−2



         
 
L2 <− (1)*L2−(1/4)*L3  on  





100
1
2
01−1−3
00−4−2






         
 
End reduction  








100
1
2
010
5
2
00−4−2









         
 



211−1
112−1
121−4



         
 Reduce column 1 with pivot 1 at row 2         
 Exchange row 1 and row 2         
 
L2 <− (1)*L2 −(2)*L1  on


112−1
211−1
121−4



         
 
L3 <− (1)*L3−(1)*L1  on  


112−1
0−1−31
121−4



         
 



112−1
0−1−31
01−1−3



         
 Reduce column 2 with pivot 1 at row 3         
 Exchange row 2 and row 3         
 
L1 <− (1)*L1 −(1)*L2  on  


112−1
01−1−3
0−1−31



         
 
L3 <− (1)*L3−(−1)*L2  on  


1032
01−1−3
0−1−31



         
 



1032
01−1−3
00−4−2



         
 Reduce column 3 with pivot -4 at row 3         
 
L1 <− (1)*L1−(−3/4)*L3  on  


1032
01−1−3
00−4−2



         
 
L2 <− (1)*L2−(1/4)*L3  on  





100
1
2
01−1−3
00−4−2






         
 
End reduction








100
1
2
010
5
2
00−4−2









         


Given the matrix of coefficients in factored form:


Example.
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




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:


1%3,1%3,1%3

Previous Up Next