Matrix operations can transform one vector into another vector. These operations will help us to find the solution for linear equations. NumPy provides the solve() function to solve linear equations in the form of Ax=B. Here, A is the n*n matrix, B is a one-dimensional array and x is the unknown one-dimensional vector. We will also use the dot() function to compute the dot product of two floating-point number arrays.
Let's solve an example of linear equations, as follows:
- Create matrix A and array B for a given equation, like this:
x1+x2 = 200
3x1+2x2 = 450
This is illustrated in the following code block
# Create matrix A and Vector B using NumPy
A=np.mat([[1,1],[3,2]])
print("Matrix A:\n",A)
B = np.array([200,450])
print("Vector B:", B)
This results in the following output:
Matrix A:
[[1 1]
[3 2]]
Vector B: [200 450]
In the preceding code block, we have created a 2*2 matrix and a vector.
- Solve a linear equation using the solve...