Previous Up Next

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.

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.

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:

     
 



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









         
Solving a system in factored form.

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
          

Previous Up Next