You are on page 1of 7

Lecture 8: Cramer’s Rule

Review of Cramer’s Rule


Let’s see an examples of solving a system Ax = b by using Cramer’s Rule.
• Ex1. To use Cramer’s Rule to solve the system

3x1 − 2x2 = 6
−5x1 + 4x2 = 8,

we let        
3 −2 6 6 −2 3 6
A= ,b = , A1 = , and A2 =
−5 4 8 8 4 −5 8
where A1 is matrix from A by replacing the first column by b and A2 is matrix from
A by replacing the second column by b.
By Cramer’s Rule, we obtain
det(A1 ) det(A2 )
x1 = = 20, and x2 = = 27.
det(A) det(A)

We can try another example with a 3 × 3 matrix:


• Ex2. To use Cramer’s Rule to solve the system

x1 − 2x2 + x3 = 0
2x2 − 8x3 = 8
−4x1 + 5x2 + 9x3 = −9

we let      
1 −2 1 0 0 −2 1
B= 0 2 −8 , b =  8 , B1 =  8 2 −8 ,
−4 5 9 −9 −9 5 9
   
1 0 1 1 −2 0
B2 =  0 8 −8 , and B3 =  0 2 8
−4 −9 9 −4 5 −9
where Bi is matrix from B by replacing the column i by b.
By Cramer’s Rule, we obtain
det(B1 ) det(B2 ) det(B3 )
x1 = = 29, x2 = = 16, and x3 = = 3.
det(B) det(B) det(B)

Besides solving system equation, we can use Cramer’s rule to find the inverse matrix of
a given matrix. Here we see an example:

1
 
2 1 3
• Ex3. Given a matrix C =  1 −1 1. To find the inverse matrix D of C, we need
1 4 −2
find D such that CD = I:
    
2 1 3 d11 d12 d13 1 0 0
 1 −1 1 d21 d22 d23  = 0 1 0 .
1 4 −2 d31 d32 d33 0 0 1

We can solve D by splitting this system into three systems:


    
2 1 3 d11 1
 1 −1 1  d21 = 0 = e1 ,
 
1 4 −2 d31 0
    
2 1 3 d12 0
 1 −1 1  d22 = 1 = e2 ,
 
1 4 −2 d32 0
    
2 1 3 d13 0
 1 −1 1  d23 = 0 = e3 .
 
1 4 −2 d33 1
     
d11 d12 d13
 d   d
Set d1 = 21 , d2 = 22 , and d3 = d23 .
 
x31 x32 x33
To solve Cd1 = e1 by using Cramer’s rule, we have we let
     
1 1 3 2 1 3 2 1 1
C1 =  0 −1 1 , C2 =
  1 0 1 , and C3 =
  1 −1 0 .
0 4 −2 1 0 −2 1 4 0

One can see that it is easy to get the determinants of C1 , C2 , C3 because one of the
columns contains two zeros in each Ci where

det(C1 ) = −2, det(C2 ) = 3, det(C3 ) = 5.

Then we have
det(C1 ) −2 det(C2 ) 3 det(C3 ) 5
d11 = = , d21 = = , and d31 = =
det(C) 14 det(C) 14 det(C) 14

where det(C) = 14.

Similarly, we can find d2 , d3 in the same way:


Solving d2 :

2
 
0 1 3
1  14
d12 = 1 −1 1 = = 1,
det(C) 14
0 4 −2
 
2 0 3
1  −7 −1
d22 = 1 1 1 = = ,
det(C)
14 2
1 0 −2
 
2 1 0
1  −7 −1
d32 = 1 −1 1 = = .
det(C)
14 2
1 4 0
Solving d3 :
 
0 1 3
1  4 2
d13 = 0 −1 1 = = ,
det(C)
14 7
1 4 −2
 
2 0 3
1  1
d23 = 1 0 1 = ,
det(C) 14
1 1 −2
 
2 1 0
1  −3
d33 = 1 −1 0 = .
det(C)
14
1 4 1
Then we got the inverse matrix D = (d1 , d2 , d3 ):
−1 2
 
 7 1
 7 

 
 3 −1 1 
D=  14
.
 2 14 

 
 5 −1 −3 
14 2 14
We can check it by multiplying D on C directly and see if we can get an idenity.

Solving System Equations in MATLAB by using Cramer’s Rule


We can use two different ways in MATLAB to check the anwers of system equations above.

• In example 1, we have
>> A=[3 −2;−5 4 ] ; b = [ 6 ; 8 ] ;
>> A1=[b ,A ( : , 2 ) ] ; A2=[A( : , 1 ) , b ] ;

3
 
x
Then we can find x = 1 via
x2
>> x=[ d e t (A1) / de t (A ) ; d et (A2) / d et (A ) ]
x =

20.0000
27.0000

which means x1 = 20, x2 = 27. We also can use command A\b to check the solution of
Ax = b:
>> A\b
ans =

20.0000
27.0000

• We can do the similar thing for example 2:


>> B=[1 −2 1 ; 0 2 −8; −4 5 9 ] ; b = [ 0 ; 8 ; − 9 ] ;
>> B1=[b , B ( : , 2 : 3 ) ] ; B2=[B ( : , 1 ) , b , B ( : , 3 ) ] ; B3=[B ( : , 1 : 2 ) , b ] ;
 
x1
Then we can find x = x2  via

x3
>> x=[ d e t (B1) / d et (B ) ; d et ( B2) / d et (B ) ; d e t (B3) / d et (B ) ]
x =
29
16
3

which means x1 = 29, x2 = 16, x3 = 3.


Again, we can use command B\b to check the solution of Bx = b:
>> B\b
ans =

29
16
3

For the inverse matrix part, here is the way we do in MATLAB:

• In example 3, we have

4
>> C=[2 1 3 ; 1 −1 1 ; 1 4 −2];
>> e1 = [ 1 ; 0 ; 0 ] ; e2 = [ 0 ; 1 ; 0 ] ; e3 = [ 0 ; 0 ; 1 ] ;

and set C1 , C2 , C3 to solve d1


>> C1=[ e1 ,C ( : , 2 : 3 ) ] ; C2=[C ( : , 1 ) , e1 ,C ( : , 3 ) ] ; C3=[C ( : , 1 : 2 ) , e1 ] ;
>> d1=[ d e t (C1) / d e t (C ) ; d e t (C2) / d e t (C ) ; d et (C3) / d et (C ) ]
d1 =
−0.1429
0.2143
0.3571

We can change the format to see the fractions by using format rat ( and you can use
the command format short or format long to change the number representation
back):
>> format r a t
>> d1=[ d e t (C1) / d e t (C ) ; d e t (C2) / d e t (C ) ; d et (C3) / d et (C ) ]
d1 =

−1/7
3/14
5/14

which are the values we got earlier. Similarly, we can get the other two vectors in D:
>> C1=[ e2 ,C ( : , 2 : 3 ) ] ; C2=[C ( : , 1 ) , e2 ,C ( : , 3 ) ] ; C3=[C ( : , 1 : 2 ) , e2 ] ;
>> d2=[ d e t (C1) / d e t (C ) ; d e t (C2) / d e t (C ) ; d et (C3) / d et (C ) ]
d2 =

1
−1/2
−1/2

and
>> C1=[ e3 ,C ( : , 2 : 3 ) ] ; C2=[C ( : , 1 ) , e3 ,C ( : , 3 ) ] ; C3=[C ( : , 1 : 2 ) , e3 ] ;
>> d3=[ d e t (C1) / d e t (C ) ; d e t (C2) / d e t (C ) ; d et (C3) / d et (C ) ]
d3 =

2/7
1/14
−3/14

Finally, we can use the inversre matrix command inv(C) to check our answer:

5
i n v (C)

ans =

−1/7 1 2/7
3/14 −1/2 1/14
5/14 −1/2 −3/14

6
Exercises
   
2 1 3 3 0 0
1. Given a matrix C =  1 −1 1 and a matrix E =
  0 1 0. Use Cramer’s
1 4 −2 0 0 2
rule to find a matrix D such that CD = E.

2. Use Cramer’s rule to compute the solution of the system

2x1 + x2 + x3 = 4
−x1 + 2x3 = 2
3x1 + x2 + 3x3 = −2
 
3 6 7
3. Given a matrix F =  0 2 1. Use Cramer’s rule to find the inverse matrix of
2 3 4
F.
 
1 2 4
4. Given a matrix G =  0 −3 1. Use Cramer’s rule to find the inverse matrix of
0 0 3
G.
 
3 0 0
5. Given a matrix H = −1
 1 0. Use Cramer’s rule to find the inverse matrix of
−2 3 2
H.

You might also like