Loading [MathJax]/jax/output/HTML-CSS/jax.js
Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

18.3: Using Cramer’s rule to solve Ax=b

( \newcommand{\kernel}{\mathrm{null}\,}\)

Let Ax=b be a system of n linear equations in n variables such that |A|0. The system has a unique solution given by:

x1=|A1||A|,x2=|A2||A|,,xn=|An||A|

where Ai is the matrix obtained by replacing column i of A with b. The following function generates Ai by replacing the ith column of A with b:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

def makeAi(A,i,b):
    '''Replace the ith column in A with b'''
    if type(A) == np.matrix:
        A = A.tolist()
    if type(b) == np.matrix:
        b = b.tolist()
    Ai = copy.deepcopy(A)
    for j in range(len(Ai)):
        Ai[j][i] = b[j][0]
    return Ai
def makeAi(A,i,b):
    '''Replace the ith column in A with b'''
    if type(A) == np.matrix:
        A = A.tolist()
    if type(b) == np.matrix:
        b = b.tolist()
    Ai = copy.deepcopy(A)
    for j in range(len(Ai)):
        Ai[j][i] = b[j][0]
    return Ai
Do This

Create a new function called cramersRule, which takes A and b and returns x using the Cramer’s rule.

Note: Use numpy and NOT mydet to find the required determinants. mydet is too slow.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

# Stub code. Replace the np.linalg.solve code with your answer

def cramersRule(A,b):
    detA = np.linalg.det(A)
    x = []    
    #####Start of your code here#####  
 

    #####End of your code here#####  
    
    return x
# Stub code. Replace the np.linalg.solve code with your answer

def cramersRule(A,b):
    detA = np.linalg.det(A)
    x = []    
    #####Start of your code here#####  
 

    #####End of your code here#####  
    
    return x
Question

Test your cramersRule function on the following system of linear equations:

x1+2x2=3

3x1+x2=1

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Put your answer to the above question here
#Put your answer to the above question here
Question

Verify the above answer by using the np.linalg.solve function:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Put your answer to the above question here
#Put your answer to the above question here
Question

Test your cramersRule function on the following system of linear equations and verify the answer by using the np.linalg.solve function:

x1+2x2+x3=9

x1+3x2x3=4

x1+4x2x3=7

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Put your answer to the above question here
#Put your answer to the above question here
Question

Cramer’s rule is a O(n!) algorithm and the Gauss-Jordan (or Gaussian) elimination is O(n3). What advantages does Cramer’s rule have over elimination?


This page titled 18.3: Using Cramer’s rule to solve Ax=b is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?