Skip to main content
Mathematics LibreTexts

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

  • Page ID
    67884
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

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

    \[x_1 = \frac{|A_1|}{|A|}, x_2 = \frac{|A_2|}{|A|}, \ldots, x_n = \frac{|A_n|}{|A|} \nonumber \]

    where \(A_i\) is the matrix obtained by replacing column \(i\) of \(A\) with \(b\). The following function generates \(A_i\) by replacing the \(i\)th column of \(A\) with \(b\):

    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.

    # 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:

    \[ x_1 + 2x_2 = 3 \nonumber \]

    \[3x_1 + x_2 = -1 \nonumber \]

    #Put your answer to the above question here
    Question

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

    #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:

    \[ x_1 + 2x_2 +x_3 = 9 \nonumber \]

    \[ x_1 + 3x_2 - x_3 = 4 \nonumber \]

    \[ x_1 + 4x_2 - x_3 = 7 \nonumber \]

    #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(n^3)\). 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; a detailed edit history is available upon request.