Original link:tecdat.cn/?p=8445

Original source:Tuo End number according to the tribe public number

 

In this article, you’ll see how to solve linear equations using Python’s Numpy library.

What are linear systems of equations?

Wikipedia defines linear equations as:

In mathematics, a linear system (or system) is a set of two or more linear equations involving the same set of variables.

The ultimate goal of solving linear equations is to find the values of unknown variables. Here is an example of a linear system with two unknown variables:

Equation 1:

4x  + 3y = 20
-5x + 9y = 26
Copy the code

To solve the system of linear equations, we need to find the values of the x and y variables. There are many solutions, such as elimination of variables, Kramer’s rule, matrix solutions. In this article, we introduce matrix solutions.

In the matrix solution, the system of linear equations to be solved is expressed in matrix form AX = B. For example, we can express equation 1 in matrix form as follows:

A = [[ 4   3]
     [-5   9]]

X = [[x]
     [y]]

B = [[20]
     [26]]
Copy the code

To find the values of x and y variable equation 1, we need to find the value x in the matrix. To do this, we can take the dot product of matrix inverse A and matrix B as follows:

X = inverse(A).B
Copy the code

 

 

Use NUMpy to solve linear equations

To solve a system of linear equations, we need to perform two operations: matrix inversion and matrix dot product. Python’s Numpy library supports both operations. If the Numpy library is not installed, the following PIP command can be used:

$ pip install numpy
Copy the code

Now let’s see how to solve linear equations using the Numpy library.

Use the inV () and dot () methods

First, we will find the matrix inverse that A defined in the previous section.

First let’s A create the matrix in Python. To create a matrix, array can use the methods of the Numpy module. A matrix can be thought of as a list of lists, where each list represents a row.

In the following script, we create a list named m_list, which further contains two lists: [4,3] and [-5,9]. These lists are the two rows of A in the matrix. To create A matrix using Numpy, pass m_list to the array method as follows:

 

import numpy as np

m_list = [[4, 3], [-5, 9]]
A = np.array(m_list)
Copy the code

To find the inverse of the matrix, pass the matrix to the linalg.inv()Numpy module:

inv_A = np.linalg.inv(A)

print(inv_A)
Copy the code

The next step is to find the dot product between the inverse matrix A and the matrix B. It is important to mention that it is only possible to obtain the matrix dot product between matrices if the dimensions of the matrices are equal; that is, the number of columns of the left matrix must match the number of rows of the right matrix.

To find dot products using the Numpy library, use the linalg.dot() function.

B = np.array([20, 26])
X = np.linalg.inv(A).dot(B)

print(X)
Copy the code

Output:

/ 2. 4.Copy the code

 

 

To verify, if you insert x into the equation and replace the unknown with 4, you will see that the result is 20.

Now, let’s solve a system of three linear equations, as follows:

4x + 3y + 2z = 25
-2x + 2y + 3z = -10
3x -5y + 2z = -4
Copy the code

The above equations can be solved using the Numpy library as follows:

Formula 2:

 

print(X)
Copy the code

In the script above, the linalg.inv() and linalg.dot() methods are linked together. This variable X contains the solution to Equation 2, and the output is as follows:

[ 5.  3. -2.]
Copy the code

The values of x, y, and are 5, 3, z, and -2. You can plug these values into formula 2 and verify that they are correct.

 

Use the solve () method

In the first two examples, we use the linalg.inv() and linalg.dot() methods to find solutions to the equations. However, the Numpy library includes the linalg.solve() method, which can be used to directly find solutions to linear equations:

 

print(X2)
Copy the code

Output:

[ 5.  3. -2.]
Copy the code

You can see that the output is the same as before.

A real example

Let’s see how linear equations can be used to solve a real problem.

Suppose a fruit seller sells 20 mangoes and 10 oranges in a single day for 350 yuan. The next day, he sold 17 mangoes and 22 oranges for 500 yuan. If the price of fruit stays the same for both days, what is the price of a mango and an orange?

This problem can be easily solved by using two linear equations.

Let’s say the price of a mango x is equal to, and the price of an orange is equal to y. The above question can be converted like this:

20x + 10y = 350
17x + 22y = 500
Copy the code

The solution to the above equations is as follows:

 
X = np.linalg.solve(A,B)

print(X)
Copy the code

Here is the output:

/ 10. 15.Copy the code

The output shows that a mango costs 10 yuan and an orange costs 15 yuan.

conclusion

This article describes how to use Python’s Numpy library to solve linear equations. You can solve linear equations using the linalg.inv() and linalg.dot() methods, or you can simply use the solve() method. The solve() method is the preferred method.


Most welcome insight

1. Use R language to simulate the mixed queuing random service queuing system

2. Queuing theory is used in R language to predict waiting time

3. Realize markov chain Monte Carlo MCMC model in R language

4. Markov regime switching model in R language

5. Matlab Bayesian hidden Markov HMM model

6. Use R language to simulate the mixed queuing random service queuing system

7. Portfolio optimization in Python based on particle swarm optimization

8. Research on accident prediction of traffic casualties by R Language Markov Transformation model

9. Identifying changing Stock Market Conditions with Machine learning: Application of Hidden Markov Models