Solve Ax = b — Linear System Calculator
Solve any system of linear equations using LU decomposition with forward and back substitution. See every step explained.
Enter a matrix A and vector b, then click Solve
or press Enter
What Does Ax = b Mean?
The equation Ax = b is the matrix form of a system of linear equations. Here A is an n×n coefficient matrix, x is the unknown vector of n variables we want to find, and b is a known right-hand side vector. Each row of the matrix equation corresponds to one linear equation in the system. For example, the system
x₁ + 3x₂ = 7
can be written as Ax = b where A = [[2, 1], [1, 3]], x = [x₁, x₂]ᵀ, and b = [5, 7]ᵀ. Solving this system means finding the values of x₁ and x₂ that satisfy every equation simultaneously. The matrix formulation is not just compact notation—it opens the door to powerful computational methods that scale to systems with thousands or even millions of unknowns.
How LU Decomposition Solves Linear Systems
LU decomposition is one of the most efficient and widely used methods for solving Ax = b. The key insight is to split the problem into two simpler triangular systems. First, decompose the coefficient matrix A into a lower triangular matrix L and an upper triangular matrix U, with partial pivoting recorded in a permutation matrix P:
Once this factorization is computed, the original system PA x = Pb becomes LU x = Pb. We introduce an intermediate variable y = Ux and solve in two stages:
- Forward substitution: Solve Ly = Pb for y (working top to bottom).
- Back substitution: Solve Ux = y for x (working bottom to top).
Each of these triangular solves is straightforward and fast. The beauty of this approach is that the expensive part—the LU factorization itself—only needs to be done once. If you later need to solve Ax = b for a different b, you can reuse the same L, U, and P and simply run the two substitution steps again.
Forward Substitution Explained (Ly = Pb)
Forward substitution solves a lower triangular system by computing each unknown in order from top to bottom. Because L is lower triangular (all entries above the diagonal are zero) and the diagonal entries are 1 (in Doolittle form), the first equation gives y₁ directly. Each subsequent equation has only one new unknown, since all the previous y values are already known.
The algorithm for an n×n system is:
yᵢ = (Pb)ᵢ − ∑(j=1 to i−1) Lᵢⱼ · yⱼ for i = 2, 3, …, n
Because each step involves a simple sum and subtraction, forward substitution is very fast—O(n²) operations for an n-dimensional system. There is no division by diagonal elements (since they are all 1), which also avoids any numerical issues at this stage.
Back Substitution Explained (Ux = y)
Back substitution works in the opposite direction, solving the upper triangular system from bottom to top. The last equation in Ux = y involves only xₙ, so it can be solved immediately. Then xₙ₋₁ is found using xₙ, and so on upward.
xᵢ = (yᵢ − ∑(j=i+1 to n) Uᵢⱼ · xⱼ) / Uᵢᵢ for i = n−1, …, 2, 1
Like forward substitution, this requires O(n²) operations. The division by diagonal elements Uᵢᵢ is safe as long as A is non-singular—a zero diagonal in U would mean the matrix is singular and the system either has no solution or infinitely many solutions.
Why This Method Is Efficient
The total cost of solving Ax = b via LU decomposition breaks down into two parts:
- LU factorization: O(n³/3) floating-point operations—this is the expensive step, but it only happens once.
- Forward + back substitution: O(n²) operations per right-hand side—this is cheap compared to the factorization.
This separation is what makes LU decomposition so powerful in practice. Consider a scenario where you need to solve Ax = b for 1000 different b vectors (common in engineering simulations). With Gaussian elimination you would do 1000 × O(n³) work. With LU decomposition you do one O(n³) factorization plus 1000 × O(n²) substitutions—potentially hundreds of times faster for large n.
Solving for Multiple Right-hand Sides
One of the primary advantages of the LU approach is efficient handling of multiple right-hand sides. In many practical applications, the coefficient matrix A remains the same while the right-hand side b changes. Examples include:
- Time-stepping simulations: The stiffness matrix stays fixed while loads change at each time step.
- Matrix inversion: Finding A−¹ is equivalent to solving Ax = eᵢ for each standard basis vector eᵢ.
- Sensitivity analysis: Evaluating how the solution changes under different input conditions.
Once you have stored L, U, and P, each new b requires only O(n²) additional work. This is why numerical libraries like LAPACK separate the factorization step (e.g., dgetrf) from the solve step (e.g., dgetrs).
When the System Has No Solution
Not every system Ax = b has a solution. Two main failure modes can occur:
- Singular matrix: If A is singular (det(A) = 0), the LU factorization will produce a zero (or near-zero) diagonal entry in U. This means the system is either inconsistent (no solution) or underdetermined (infinitely many solutions). The calculator will detect this and report that the matrix is singular.
- Inconsistent system: Even if A is square, a singular A combined with an incompatible b means no solution exists. Geometrically, the equations represent parallel planes or lines that never intersect.
In floating-point arithmetic, a matrix might not be exactly singular but very close to it (ill-conditioned). In such cases the solution may exist mathematically but be extremely sensitive to rounding errors, making it practically unreliable. The condition number of A measures this sensitivity—a large condition number warns that the solution may not be trustworthy.
Comparison with Other Methods
Several other methods exist for solving Ax = b. Here is how they compare to LU decomposition:
- Gaussian elimination: LU decomposition is essentially Gaussian elimination in disguise. The difference is that LU stores the elimination steps (L and P) so they can be replayed for new right-hand sides. Gaussian elimination without storing these factors must redo all the work for each new b.
- Cramer’s rule: Cramer’s rule uses determinants to express each unknown as a ratio of determinants. While elegant in theory, it requires O(n · n!) operations for an n×n system (if determinants are computed naively), making it impractical for anything beyond 3×3 systems. Even with efficient determinant computation, it is slower than LU.
- Matrix inverse: Computing x = A−¹b by first finding A−¹ is possible but wasteful. Finding the inverse requires solving n separate systems, which takes O(n³) work—the same as LU factorization but with a larger constant factor. Moreover, multiplying A−¹b introduces additional rounding error compared to the LU approach. In practice, explicitly computing inverses is almost never recommended for solving linear systems.
- QR decomposition: QR factorization (A = QR) can also solve Ax = b by computing x = R−¹Qᵀb. QR is more numerically stable than LU for ill-conditioned systems, but it costs roughly twice as much (about 2n³/3 operations). QR is preferred for least squares problems and non-square systems.
- Iterative methods: For very large sparse systems (thousands to millions of unknowns), iterative methods such as conjugate gradient, GMRES, or Jacobi iteration can be far more efficient than direct methods like LU. They do not factorize A explicitly but instead progressively improve an approximate solution. However, their convergence depends on the properties of A and they may require preconditioning.
Applications
Solving Ax = b is one of the most fundamental operations in applied mathematics and engineering. Some key application areas include:
- Circuit analysis: Kirchhoff’s laws produce a system of linear equations where A encodes the circuit topology and component values, b represents voltage and current sources, and x gives the unknown node voltages and branch currents. SPICE circuit simulators solve these systems millions of times.
- Structural engineering: The finite element method discretizes a structure into elements connected at nodes. The global stiffness matrix A relates nodal displacements x to applied forces b. For a large building or bridge, this can involve millions of equations.
- Fluid dynamics: Computational fluid dynamics (CFD) simulations discretize the Navier-Stokes equations on a mesh, producing large sparse linear systems at each time step. Pressure correction and velocity updates both require solving Ax = b.
- Economics and finance: Input-output models (Leontief models) describe how industries depend on each other. The equilibrium output vector x is found by solving (I − A)x = d, where A is the technology matrix and d is the final demand vector.
- Machine learning: Linear regression, ridge regression, and Gaussian processes all require solving linear systems. The normal equations AᵀAx = Aᵀb are typically solved via Cholesky or QR factorization, but LU remains a common fallback.
- Computer graphics: Mesh deformation, cloth simulation, and physically-based animation require solving large linear systems at interactive frame rates.
Numerical Considerations and Pivoting
In exact arithmetic, LU decomposition works for any non-singular matrix. In floating-point arithmetic, however, numerical errors can accumulate and degrade the solution. Two key techniques address this:
- Partial pivoting: At each elimination step, the algorithm selects the row with the largest absolute value in the current column as the pivot. This prevents division by small numbers, which would amplify rounding errors. Partial pivoting is the standard in virtually all numerical libraries and is what this calculator uses.
- Scaled partial pivoting: A refinement where the pivot is chosen based on the relative size of the element compared to other entries in its row. This helps when rows have very different magnitudes.
The condition number κ(A) quantifies how much errors in b or A are amplified in the solution x. If κ(A) is large (the matrix is ill-conditioned), even small rounding errors can produce a highly inaccurate solution. In such cases, iterative refinement—solving the system, computing the residual r = b − Ax, then solving Aδx = r to correct x—can improve accuracy without repeating the full factorization.
For well-conditioned systems, LU decomposition with partial pivoting is reliable and efficient. It is the workhorse algorithm behind MATLAB’s backslash operator, NumPy’s numpy.linalg.solve, and LAPACK’s dgesv routine, handling the vast majority of linear system solves in scientific computing.
Related factorizations
LU Decomposition
Factor any square matrix into lower and upper triangular matrices with partial pivoting.
Open calculator →QR Decomposition
Decompose into orthogonal Q and upper triangular R. Ideal for least squares problems.
Open calculator →Singular Value Decomposition
The most general decomposition. Factor any matrix into UΣVᵀ.
Open calculator →Cholesky Decomposition
Efficient factorization for symmetric positive definite matrices.
Open calculator →Eigendecomposition
Find eigenvalues and eigenvectors of square matrices.
Open calculator →