Table of Contents
ToggleLinear equations are mathematical equations that involve linear terms, meaning the highest power of the variable is one.
A system of linear equations can be expressed as a set of equations with multiple variables. The goal is to find the values of these variables that satisfy all equations simultaneously.
In matrix form, a system of linear equations can be represented as −
A * x = b
Here,
The goal is to find the vector x, which contains the values of the unknown variables. NumPy provides methods to solve such systems of equations using matrix operations.
NumPy provides several methods to solve linear equations. The most commonly used method is by using the numpy.linalg.solve() function, which directly solves the system of linear equations.
In the following example, the system of equations −
3x + 2y = 5
x + 2y = 5
has been solved to give the solution x = 1 and y = 2.
# Open Compiler
import numpy as np
# Define the coefficient matrix A and constant vector b
A = np.array([[3, 2], [1, 2]])
b = np.array([5, 5])
# Solve the system of linear equations
x = np.linalg.solve(A, b)
print("Solution vector x:", x)
Output:
Following is the output obtained −
Solution vector x: [0. 2.5]
The numpy.linalg.solve(A, b) function computes the solution to the linear system A * x = b. The function takes two arguments:
The function returns the solution vector x that satisfies the equation. The function uses efficient methods, such as Gaussian elimination or LU decomposition, to solve the system.
In addition to numpy.linalg.solve() function, NumPy provides other ways to solve linear equations, such as using matrix inversion or the numpy.dot() function.
These methods are useful when you need more control over the solving process or want to explore the mathematical background of solving linear systems.
One way to solve a system of linear equations is by finding the inverse of the coefficient matrix A. If A is invertible, the solution can be found by multiplying the inverse of A with the vector b:
x = A-1 * b
NumPy provides the numpy.linalg.inv() function to compute the inverse of a matrix. Let us see how to use it −
# Open Compiler
import numpy as np
# Define the coefficient matrix A and constant vector b
A = np.array([[3, 2], [1, 2]])
b = np.array([5, 5])
# Compute the inverse of A
A_inv = np.linalg.inv(A)
# Solve the system of equations using matrix inversion
x = np.dot(A_inv, b)
print("Solution vector x:", x)
Output:
Following is the output obtained −
Solution vector x: [4.4408921e-16 2.5000000e+00]
This method also produces the same result as the numpy.linalg.solve() function. However, using matrix inversion is more expensive and less stable for large matrices.
If the system of equations is overdetermined (more equations than unknowns), you can use the least squares solution.
The numpy.linalg.lstsq() function is used to find the least squares solution to such systems. It minimizes the error between the observed and predicted values.
Let us look at an example −
# Open Compiler
import numpy as np
# Define an overdetermined system (more equations than unknowns)
A = np.array([[1, 1], [2, 1], [3, 1]])
b = np.array([6, 8, 10])
# Solve the system using the least squares method
x, residuals, rank, s = np.linalg.lstsq(A, b, rcond=None)
print("Solution vector x:", x)
Output:
The least squares solution minimizes the error in the system, and in this case, it finds the best-fit values for the unknowns x and y −
Solution vector x: [2. 4.]
Solving linear equations has numerous applications in various fields, such as −
Using NumPy to solve linear equations has several advantages, they are −
linalg.solve() and linalg.lstsq() functions that handle complex calculations with minimal effort.Key Takeaway: Master solving linear equations with NumPy at Vista Academy!
