*** Title - How to Solve a System of Equations in Python keywords - [system of equation in python] tags - [system of equations], [linear equations], [python], [numpy] Author - Jay Shaw *** ## How To Solve A System Of Equations In Python Python is a high-level programming language that offers numerous helpful libraries. One such library is known as Numpy(Numerical Python) that can easily and quickly perform complex mathematical calculations. In this article, you will learn how to use the Numpy library in Python to solve the system of equations in Python. ### What Is a System of Equations A system of equations comprises a linear set of equations that are satisfied by the same values. In mathematics, these systems of equations can be solved through methods such as substitution, elimination, and matrices, among others. The primary objective of these methods is to find the values of variables that satisfy all the equations. Example: Let's take equations: 3x − y = and 4x + 3y = 48 The above equation is solved by finding the values of x and y. Both equations can be solved using methods like substitution, elimination, or matrix operations. In this example, we will use a matrix to solve the problem. ``` The two equation are given below: 3x − y = 23 → (1) 4x + 3y = 48 → (2) ``` At first, the linear equations need to be represented in a matrix form. The given two equations can be written in matrix format like this: ``` A = [[ 3 -1] [ 4 3]] X = [[x] [y]] B = [[23] [48]] ``` In the above matrix, we can find the values of `x` and `y` by solving the matrix X. It is solved by finding the dot product of the inverse of matrix A with matrix B, as shown below: ``` X = inverse(A).B ``` In this equation, after solving X, the values of `x` and `y` are found to be 9 and 4 respectively. Interestingly in Python, the same solution can be achieved through a few lines of code. Let's look at how we can write a Python program to find the solution to the above linear equations. ### Solve System of Equations Through Numpy Solving a system of linear equations in Python requires the Python library package Numpy. If you do not have Numpy in your system, then you can install it by going to the command prompt and executing the command: ``` pip install numpy ``` It's good practice to verify the version of a Python package after installation to ensure it was installed correctly. You can check the version of Numpy by executing the following command: ``` pip show numpy ``` Once you have installed the Numpy package, let's learn how to write a Python program to solve systems of linear equations in Python. **Import the Numpy Package** Before writing any code, it is necessary to import the Numpy package into your Python script. You can import the package using the following syntax: ```python import numpy as np ``` **Initialize variables to store the coefficients of matrix A and constants of matrix B** The coefficients of the matrix A needs to be stored inside a list to perform operations on it. Here, the coefficients of matrix A are defined as a NumPy array: ```python # Define the coefficient matrix A = np.array([[3, -1], [4, 3]]) ``` Now another list needs to be initialized to store the right-hand side of the equations. The syntax below defines the constants of term vector B as a NumPy array. ```python # Define the constant term vector B = np.array([23, 48]) ``` **Use the inv() function to find the inverse of matrix A** The `inv()` function is part of the NumPy library and it returns the inverse of a given square matrix. Here, the syntax to use the inv() function is: ```python A_inv = np.linalg.inv(A) ``` The program uses the `np.linalg.inv()` function to calculate the inverse of the coefficient matrix A and assigns it to the variable `A_inv`. Here are a few key points to keep in mind regarding the `np.linalg.inv()` function: - The matrix `A` must be square, meaning it has an equal number of rows and columns. Otherwise, the function will raise a `LinAlgError`. - If the matrix `A` is not invertible (i.e., it is singular or has a determinant of 0), the function will raise a `LinAlgError`. Once the inverse of matrix A is calculated, it needs to multiplied with the matrix B. **Use the dot() function to find the dot product of the inverse of A with matrix B** To perform the mathematical multiplication of the inverse of A with the matrix B, the dot() function is used. The dot() function performs the dot product of two arrays, which can be matrices, vectors, or a combination of both. The below syntax solves the system of linear equations by multiplying the inverse of the coefficient matrix A with the constant term vector B and stores the result in vector X. ```python X = np.dot(A_inv, B) ``` **Extract the values from the vector X and print the result** After operating the `dot()` function, the resultant values of x and y are stored inside the solution vector `X`. In simple words, `X` is an array with the values of x and y located at positions `X[0]` and `X[1]`, respectively. These values can be extracted by initializing two variables and assigning the values according to the indices. The syntax below uses two variables x and y to pass the values from vector X: ```python # Extract the values of x and y x = X[0] y = X[1] ``` Lastly, print the variables x and y to display the solution of the system of linear equations: ```python # Print the solutions print("Solution:") print("x =", x) print("y =", y) ``` The above syntax prints the solutions of the system of linear equations. The values of `x` and `y` are displayed using the `print()` function. The complete code is provided below: ```python import numpy as np # Define the coefficient matrix A = np.array([[3, -1], [4, 3]]) # Define the constant term vector B = np.array([23, 48]) # Find the inverse of A A_inv = np.linalg.inv(A) # Solve for the variable vector X = np.dot(A_inv, B) # Extract the values of x and y x = X[0] y = X[1] # Print the solutions print("Solution:") print("x =", x) print("y =", y) ``` Output: ```text Solution: x = 9.0 y = 3.999999999999999 ``` Earlier in the article, we found that the value of x is 9 and y is 4. As Numpy uses floating-point numbers in its calculations, the value of y is calculated as the nearest approximation of 4. ### Conclusion In this article, we have seen how to solve the system of linear equations through Python by using the library function NumPy. Stay tuned to find more such articles on important topics. Happy learning!