NumPy - Solving Linear Equations



What is Solving Linear Equations?

Linear 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,

  • A: A matrix representing the coefficients of the linear equations.
  • x: A column vector representing the unknown variables.
  • b: A column vector representing the constants on the right-hand side of the equations.

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.

Solving Linear Equations in NumPy

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.

Example

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.

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)

Following is the output obtained −

Solution vector x: [0.  2.5]

The numpy.linalg.solve() Function

The numpy.linalg.solve(A, b) function computes the solution to the linear system A * x = b. The function takes two arguments:

  • A: The coefficient matrix (2D NumPy array).
  • b: The constant vector (1D NumPy array).

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.

Alternative Ways to Solve Linear Equations

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.

Using Matrix Inversion

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 −

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)

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.

Using numpy.linalg.lstsq() Function

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 −

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)

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.]

Applications of Solving Linear Equations

Solving linear equations has numerous applications in various fields, such as −

  • Physics: Linear equations are used to model physical systems, such as circuits, motion, and energy transfer.
  • Economics: Economists use linear systems to model relationships between variables like supply and demand, production, and consumption.
  • Computer Graphics: In graphics programming, linear equations are used in transformations, rendering, and 3D modeling.
  • Machine Learning: Solving linear equations is a crucial step in algorithms such as linear regression and optimization problems.

Advantages of Solving Linear Equations

Using NumPy to solve linear equations has several advantages, they are −

  • Efficiency: NumPy is optimized for performance, making it much faster than manually solving equations.
  • Ease of Use: NumPy provides simple functions like linalg.solve() and linalg.lstsq() functions that handle complex calculations with minimal effort.
  • Robustness: NumPy handles edge cases, such as singular or ill-conditioned matrices, efficiently.
  • Versatility: NumPy can handle systems with any number of equations and unknowns, making it applicable to a wide range of problems.
Advertisements