Tag: python

  • Solving Systems of Equations Using Cramer’s Rule

    Solving Systems of Equations Using Cramer’s Rule

    Cramer’s Rule is a powerful method for solving systems of linear equations using determinants. It is particularly effective for small systems where calculating determinants manually is feasible. In this post, we will explain the method step-by-step and provide examples to help you master the technique.


    What is Cramer’s Rule?

    Cramer’s Rule is applicable to systems of linear equations where the number of equations matches the number of variables. The system:

        \[\begin{cases}a_{11}x_1 + a_{12}x_2 + \dots + a_{1n}x_n = b_1 \\a_{21}x_1 + a_{22}x_2 + \dots + a_{2n}x_n = b_2 \\\vdots \\a_{n1}x_1 + a_{n2}x_2 + \dots + a_{nn}x_n = b_n\end{cases}\]

    can be expressed in matrix form as:

        \[A \vec{x} = \vec{b},\]

    where A is the coefficient matrix, \vec{x} is the vector of variables, and \vec{b} is the constants vector.

    To solve for x_1, x_2, \dots, x_n using Cramer’s Rule:

    1. Compute the determinant of the coefficient matrix, \det(A).
    2. Replace the i-th column of A with \vec{b} to create a new matrix A_i.
    3. Solve for each variable using the formula:

        \[x_i = \frac{\det(A_i)}{\det(A)},\]

    provided \det(A) \neq 0.


    Steps to Solve Using Cramer’s Rule

    1. Write the coefficient matrix A and constants vector \vec{b}.
    2. Compute \det(A).
    3. For each variable x_i:
    • Replace the i-th column of A with\ ( \vec{b} \) to form A_i.
    • Compute \det(A_i).
    • Solve for x_i using x_i = \frac{\det(A_i)}{\det(A)}.

    Example 1: Solving a 2×2 System

    Consider the system:

        \[\begin{cases}x + 2y = 5 \\3x - y = 4\end{cases}\]

    Step 1: Write the Coefficient Matrix and Constants Vector

        \[A = \begin{bmatrix}1 & 2 \\3 & -1\end{bmatrix}, \quad \vec{b} = \begin{bmatrix} 5 \ 4 \end{bmatrix}.\]

    Step 2: Compute \det(A)

        \[\det(A) = \begin{vmatrix}1 & 2 \\3 & -1\end{vmatrix}\]

        \[\det(A) = (1)(-1) - (2)(3) = -1 - 6 = -7.\]

    Step 3: Solve for Each Variable

    • Replace the 1st column of A with \vec{b} to get A_1:

        \[A_1 = \begin{bmatrix}5 & 2 \\4 & -1\end{bmatrix}.\]

    Compute \det(A_1):

        \[\det(A_1) = \begin{vmatrix}5 & 2 \\4 & -1\end{vmatrix}\]

        \[\det(A_1) = (5)(-1) - (2)(4) = -5 - 8 = -13.\]

    Solve for x:

        \[x = \frac{\det(A_1)}{\det(A)} = \frac{-13}{-7} = \frac{13}{7}.\]

    • Replace the 2nd column of A with \vec{b} to get A_2:

        \[A_2 = \begin{bmatrix}1 & 5 \\3 & 4\end{bmatrix}.\]

    Compute \det(A_2):

        \[\det(A_2) = \begin{vmatrix}1 & 5 \\3 & 4\end{vmatrix}.\]

        \[\det(A_2) = (1)(4) - (5)(3) = 4 - 15 = -11.\]

    Solve for y:

        \[y = \frac{\det(A_2)}{\det(A)} = \frac{-11}{-7} = \frac{11}{7}.\]

    Solution:

        \[x = \frac{13}{7}, \quad y = \frac{11}{7}.\]

    Below, you can see the lines defined by this system of equations and their intersection point, which represents the solution to the system.


    Example 2: Solving a 3×3 System

    Solve the system:

        \[\begin{cases}2x + y - z &= 1, \\3x - 2y + 4z &= 7, \\x + 3y - 2z &= -3.\end{cases}\]

    Step 1: Write the coefficient matrix A and constants vector \vec{b}:

        \[A = \begin{bmatrix}2 & 1 & -1 \\3 & -2 & 4 \\1 & 3 & -2\end{bmatrix}, \quad\vec{b} = \begin{bmatrix}1 \ 7 \ -3\end{bmatrix}.\]

    Step 2: Compute \det(A):

        \[\begin{split}\det(A) = &2(-2 \times -2 - 4 \times 3) \\&- 1(3 \times -2 - 4 \times 1) \\&- 1(3 \times 3 - (-2 \times 1)).\end{split}\]

        \[\begin{split}\det(A) &= 2(4 - 12) - 1(-6 - 4) - 1(9 + 2) \\&= 2(-8) + 10 - 11 \\ &= -16 + 10 - 11 \\&= -17.\end{split}\]

    Step 3: Solve for x_1, x_2, and x_3:

    Solve for x_1:

    Replace the first column of A with \vec{b}:

        \[A_1 = \begin{bmatrix}1 & 1 & -1 \\7 & -2 & 4 \\-3 & 3 & -2\end{bmatrix}.\]


        \[\begin{split}\det(A_1) = &1(-2 \times -2 - 4 \times 3) \\&- 1(7 \times -2 - 4 \times -3) \\&+ (-1)(7 \times 3 - (-2 \times -3)).\end{split}\]


        \[\begin{split}\det(A_1) &= 1(4 - 12) - 1(-14 + 12) - (21 - 6) \\&= -8 + 2 - 15 \\&= -21.\end{split}\]


        \[x_1 = \frac{\det(A_1)}{\det(A)} = \frac{-21}{-17} = \frac{21}{17}.\]

    Solve for x_2:

    Replace the second column of A with \vec{b}:

        \[A_2 = \begin{bmatrix}2 & 1 & -1 \\3 & 7 & 4 \\1 & -3 & -2\end{bmatrix}.\]


        \[\begin{split}\det(A_2) = &2(7 \times -2 - 4 \times -3) \\&- 1(3 \times -2 - 4 \times 1) \\&+ (-1)(3 \times -3 - 7 \times 1).\end{split}\]


        \[\begin{split}\det(A_2) &= 2(-14 +12) - 1(-6 - 4) - 1(-9 - 7) \\&= 2(-2) + 10 +16 \\&= -4 + 10 + 16 \\&= 22.\end{split}\]


        \[x_2 = \frac{\det(A_2)}{\det(A)} = \frac{22}{-17} = -\frac{22}{17}.\]

    Solve for x_3:

    Replace the third column of A with \vec{b}:

        \[A_3 = \begin{bmatrix}2 & 1 & 1 \\3 & -2 & 7 \\1 & 3 & -3\end{bmatrix}.\]


        \[\begin{split}\det(A_3) = &2(-2 \times -3 - 7 \times 3) \\&- 1(3 \times -3 - 7 \times 1) \\&+ 1(3 \times 3 - (-2 \times 1)).\end{split}\]


        \[\begin{split}\det(A_3) &= 2(6 - 21) - 1(-9 - 7) + 1(9 + 2) \\&= 2(-15) + 16 + 11 \\&= -30 + 16 + 11 \\&= -3.\end{split}\]


        \[x_3 = \frac{\det(A_3)}{\det(A)} = \frac{-3}{-17} = \frac{3}{17}.\]

    Solution:

        \[x_1 = \frac{21}{17}, \quad x_2 = -\frac{22}{17}, \quad x_3 = \frac{3}{17}.\]

    Below is an interactive plot displaying three planes defined by the system of equations, along with their intersection point, which represents the solution to the system. You can change the viewpoint by dragging the plot to explore the relationship between the planes and their intersection more effectively. Enjoy visualizing the solution!


    Example 3: Special Case – No Solution

    Solve the system:

        \[\begin{cases}x + y + z &= 2, \\2x + 2y + 2z &= 5, \\x - y + z &= 1.\end{cases}\]

    Step 1: Write the coefficient matrix A and constants vector \vec{b}:

        \[A = \begin{bmatrix}1 & 1 & 1 \\2 & 2 & 2 \\1 & -1 & 1\end{bmatrix}, \quad\vec{b} = \begin{bmatrix}2 \ 5 \ 1\end{bmatrix}.\]

    Step 2: Compute \det(A):

        \[\begin{split}\det(A) &= 1(2 \times 1 - 2 \times -1) \\&- 1(2 \times 1 - 2 \times 1) \\&+ 1(2 \times -1 - 2 \times 1).\end{split}\]


        \[\det(A) = 1(2 + 2) - 1(2 - 2) + 1(-2 - 2) = 4 - 0 - 4 = 0.\]

    Since \det(A) = 0, the system is inconsistent and has no solution.

    Below, you can see a plot of three planes defined by this system of equations. You can change the viewpoint by dragging the plot for a better perspective. As you observe, these three planes do not intersect at any point, indicating that there is no solution for this system of equations.


    Example 4: Special Case – Infinitely Many Solutions

    Solve the system:

        \[\begin{cases}x + y + z &= 3, \\2x + 2y + 2z &= 6, \\x - y + z &= 1.\end{cases}\]

    Step 1: Write the coefficient matrix A and constants vector \vec{b}:

        \[A = \begin{bmatrix}1 & 1 & 1 \\2 & 2 & 2 \\1 & -1 & 1\end{bmatrix}, \quad\vec{b} = \begin{bmatrix}3 \ 6 \ 1\end{bmatrix}.\]

    Step 2: Compute \det(A):

        \[\begin{split}\det(A) =& 1(2 \times 1 - 2 \times -1) \\&- 1(2 \times 1 - 2 \times 1) \\&+ 1(2 \times -1 - 2 \times 1).\end{split}\]


        \[\begin{split}\det(A) &= 1(2 + 2) - 1(2 - 2) + 1(-2 - 2) \\&= 4 - 0 - 4 \\&= 0.\end{split}\]

    Since \det(A) = 0, we check consistency.

    The second equation is a multiple of the first, indicating a dependency between them. As a result, the system has infinitely many solutions. Essentially, instead of three distinct equations, we only have two, which means there are two planes that intersect along a line, resulting in infinitely many solutions.Below, you can see a plot of three planes defined by this system of equations. You can change the viewpoint by dragging the plot for a better perspective. To observe the relationship between the planes, you can toggle their visibility by clicking on their names in the legend. You’ll notice that the first and second planes overlap completely. The infinitely many solutions for this system of equations can be expressed as the line x = 2t, \, y = 1, \, z = 2 - 2t


    Python Code

    Here is the Python code to define and solve the examples above . It includes calculations for determinants and solutions for each case.

    import numpy as np
    
    # Function to compute determinant of a matrix
    def determinant(matrix):
        return round(np.linalg.det(matrix), 2)
    
    # Function to solve a system using Cramer's Rule
    def cramers_rule(A, b):
        det_A = determinant(A)
        if det_A == 0:
            print("Det(A) = 0, system may have no solution or infinitely many solutions.")
            return None
    
        solutions = []
        n = len(b)
        for i in range(n):
            Ai = np.copy(A)
            Ai[:, i] = b
            det_Ai = determinant(Ai)
            solutions.append(det_Ai / det_A)
        return solutions
    
    # Example 1: Solving a 2x2 System
    A1 = np.array([[1, 2],
                   [3, -1]])
    b1 = np.array([5, 4])
    
    print("Example 1: Solving a 2x2 System")
    solutions_1 = cramers_rule(A1, b1)
    if solutions_1:
        print(f"Solutions: x1 = {solutions_1[0]}, x2 = {solutions_1[1]}")
    
    # Example 2: Solving a 3x3 System
    A2 = np.array([[2, 1, -1],
                   [3, -2, 4],
                   [1, 3, -2]])
    b2 = np.array([1, 7, -3])
    
    print("\nExample 2: Solving a 3x3 System")
    solutions_2 = cramers_rule(A2, b2)
    if solutions_2:
        print(f"Solutions: x1 = {solutions_2[0]}, x2 = {solutions_2[1]}, x3 = {solutions_2[2]}")
    
    # Example 3: No Solution
    A3 = np.array([[1, 1, 1],
                   [2, 2, 2],
                   [1, -1, 1]])
    b3 = np.array([2, 5, 1])
    
    print("\nExample 3: No Solution")
    det_A3 = determinant(A3)
    if det_A3 == 0:
        print("Det(A) = 0. The system has no solution (inconsistent).")
    
    # Example 4: Infinitely Many Solutions
    A4 = np.array([[1, 1, 1],
                   [2, 2, 2],
                   [1, -1, 1]])
    b4 = np.array([3, 6, 1])
    
    print("\nExample 4: Infinitely Many Solutions")
    det_A4 = determinant(A4)
    if det_A4 == 0:
        print("Det(A) = 0. The system has infinitely many solutions (dependent equations).")