Solve systems of linear equations Ax = B for x (2024)

Solve systems of linear equations Ax = B for x

collapse all in page

Syntax

x = A\B

x = mldivide(A,B)

Description

example

x = A\B solves the system of linear equations A*x = B. The matrices A and B must have the same number of rows. MATLAB® displays a warning message if A is badly scaled or nearly singular, but performs the calculation regardless.

  • If A is a scalar, then A\B is equivalent to A.\B.

  • If A is a square n-by-n matrix and B is a matrix with n rows, then x = A\B is a solution to the equation A*x = B, if it exists.

  • If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with m rows, then A\B returns a least-squares solution to the system of equations A*x= B. x may not be the minimum-norm solution.

x = mldivide(A,B) isan alternative way to execute x = A\B,but is rarely used. It enables operator overloading for classes.

Examples

collapse all

System of Equations

Open Live Script

Solve a simple system of linear equations, A*x = B.

A = magic(3);B = [15; 15; 15];x = A\B
x = 3×1 1.0000 1.0000 1.0000

Linear System with Singular Matrix

Open Live Script

Solve a linear system of equations A*x = b involving a singular matrix, A.

A = magic(4);b = [34; 34; 34; 34];x = A\b
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.306145e-17.

When rcond is between 0 and eps, MATLAB® issues a nearly singular warning, but proceeds with the calculation. When working with ill-conditioned matrices, an unreliable solution can result even though the residual (b-A*x) is relatively small. In this particular example, the norm of the residual is zero, and an exact solution is obtained, although rcond is small.

When rcond is equal to 0, the singular warning appears.

A = [1 0; 0 0];b = [1; 1];x = A\b
Warning: Matrix is singular to working precision.
x = 2×1 1 Inf

In this case, division by zero leads to computations with Inf and/or NaN, making the computed result unreliable.

Least-Squares Solution of Underdetermined System

Open Live Script

Solve a system of linear equations, A*x = b.

A = [1 2 0; 0 4 3];b = [8; 18];x = A\b
x = 3×1 0 4.0000 0.6667

Linear System with Sparse Matrix

Open Live Script

Solve a simple system of linear equations using sparse matrices.

Consider the matrix equation A*x = B.

A = sparse([0 2 0 1 0; 4 -1 -1 0 0; 0 0 0 3 -6; -2 0 0 0 2; 0 0 4 2 0]);B = sparse([8; -1; -18; 8; 20]);x = A\B
x = (1,1) 1.0000 (2,1) 2.0000 (3,1) 3.0000 (4,1) 4.0000 (5,1) 5.0000

Input Arguments

collapse all

A, BOperands
vectors | full matrices | sparse matrices

Operands, specified as vectors, full matrices, or sparse matrices. A and B must have the same number of rows.

  • If A or B has an integer data type, the other input must be scalar. Operands with an integer data type cannot be complex.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

Output Arguments

collapse all

x — Solution
vector | full matrix | sparse matrix

Solution, returned as a vector, full matrix, or sparse matrix.If A is an m-by-n matrixand B is an m-by-p matrix,then x is an n-by-p matrix,including the case when p==1.

If A has full storage, x isalso full. If A is sparse, then x hasthe same storage as B.

Tips

  • The operators / and \ are related to each other by the equation B/A = (A'\B')'.

  • If A is a square matrix, then A\B is roughly equal to inv(A)*B, but MATLAB processes A\B differently and more robustly.

  • If the rank of A is less than the number of columns in A, then x = A\B is not necessarily the minimum-norm solution. You can compute the minimum-norm least-squares solution using x = lsqminnorm(A,B) or x = pinv(A)*B.

  • Use decomposition objects to efficiently solve a linear system multiple times with different right-hand sides. decomposition objects are well-suited to solving problems that require repeated solutions, since the decomposition of the coefficient matrix does not need to be performed multiple times.

Algorithms

collapse all

The versatility of mldivide in solving linearsystems stems from its ability to take advantage of symmetries inthe problem by dispatching to an appropriate solver. This approachaims to minimize computation time. The first distinction the functionmakes is between full (also called “dense”)and sparse input arrays.

Algorithm for Full Inputs

The flow chart below shows the algorithm path when inputs A and B are full.

Solve systems of linear equations Ax = B for x (1)

Algorithm for Sparse Inputs

If A is full and B issparse then mldivide converts B toa full matrix and uses the full algorithm path (above) to computea solution with full storage. If A is sparse, thestorage of the solution x is the same as that of B and mldivide followsthe algorithm path for sparse inputs,shown below.

Solve systems of linear equations Ax = B for x (2)

References

[1] Gilbert, John R., and Tim Peierls. “Sparse Partial Pivoting in Time Proportional to Arithmetic Operations.” SIAM Journal on Scientific and Statistical Computing 9, no. 5 (September 1988): 862–874. https://doi.org/10.1137/0909058.

[2] Anderson, E., ed. LAPACK Users’ Guide. 3rd ed. Software, Environments, Tools. Philadelphia: Society for Industrial and Applied Mathematics, 1999. https://doi.org/10.1137/1.9780898719604.

[3] Davis, Timothy A. "Algorithm 832: UMFPACK V4.3 – an unsymmetric-pattern multifrontal method." ACM Transactions on Mathematical Software 30, no. 2 (June 2004): 196–199. https://doi.org/10.1145/992200.992206.

[4] Duff, Iain S. “MA57---a Code for the Solution of Sparse Symmetric Definite and Indefinite Systems.” ACM Transactions on Mathematical Software 30, no. 2 (June 2004): 118–144. https://doi.org/10.1145/992200.992202.

[5] Davis, Timothy A., John R. Gilbert, Stefan I. Larimore, and Esmond G. Ng. “Algorithm 836: COLAMD, a Column Approximate Minimum Degree Ordering Algorithm.” ACM Transactions on Mathematical Software 30, no. 3 (September 2004): 377–380. https://doi.org/10.1145/1024074.1024080.

[6] Amestoy, Patrick R., Timothy A. Davis, and Iain S. Duff. “Algorithm 837: AMD, an Approximate Minimum Degree Ordering Algorithm.” ACM Transactions on Mathematical Software 30, no. 3 (September 2004): 381–388. https://doi.org/10.1145/1024074.1024081.

[7] Chen, Yanqing, Timothy A. Davis, William W. Hager, and Sivasankaran Rajamanickam. “Algorithm 887: CHOLMOD, Supernodal Sparse Cholesky Factorization and Update/Downdate.” ACM Transactions on Mathematical Software 35, no. 3 (October 2008): 1–14. https://doi.org/10.1145/1391989.1391995.

[8] Davis, Timothy A. “Algorithm 915, SuiteSparseQR: Multifrontal Multithreaded Rank-Revealing Sparse QR Factorization.” ACM Transactions on Mathematical Software 38, no. 1 (November 2011): 1–22. https://doi.org/10.1145/2049662.2049670.

Extended Capabilities

Version History

Introduced before R2006a

expand all

The LDL factorization is no longer used for full matrices that are Hermitian indefinite. Instead, the LU factorization is used for these matrices.

See Also

mrdivide | ldivide | rdivide | inv | pinv | chol | lu | qr | ldl | linsolve | lsqminnorm | spparms | decomposition

Topics

  • Array vs. Matrix Operations
  • Operator Precedence
  • Systems of Linear Equations
  • Operator Overloading
  • MATLAB Operators and Special Characters

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Solve systems of linear equations Ax = B for x (3)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Solve systems of linear equations Ax = B for x (2024)

FAQs

How do you find the solution to the linear system Ax B? ›

One way to find a particular solution to the equation Ax = b is to set all free variables to zero, then solve for the pivot variables. The general solution to Ax = b is given by xcomplete = xp + xn, where xn is a generic vector in the nullspace.

How do you solve for x in a system of linear equations? ›

SOLVE A SYSTEM OF EQUATIONS BY SUBSTITUTION.
  1. Solve one of the equations for either variable.
  2. Substitute the expression from Step 1 into the other equation.
  3. Solve the resulting equation.
  4. Substitute the solution in Step 3 into either of the original equations to find the other variable.
Nov 24, 2022

What does ax + b mean in matrices? ›

1.4 The Matrix Equation Ax = b. Definition. If A is an m × n matrix, with columns a1,..., an, and if x is in Rn, then the product of A. and x, denoted by Ax, is the linear combination of the columns of A using the corresponding entries in x as.

What is the form ax b? ›

In equations of the form ax = b (a times x equals b), x is a variable which represents an unknown number and a and b are constants. To solve an equation we must find the value of the variable so that the original equation is true when the variable is replaced with that value.

How do you solve linear systems step by step? ›

Solve linear equations using a general strategy.
  1. Simplify each side of the equation as much as possible. ...
  2. Collect all the variable terms on one side of the equation. ...
  3. Collect all the constant terms on the other side of the equation. ...
  4. Make the coefficient of the variable term equal to 1. ...
  5. Check the solution.
May 6, 2020

How do you solve a linear equation solution? ›

The steps for solving linear equations are:
  1. Simplify both sides of the equation and combine all same-side like terms.
  2. Combine opposite-side like terms to obtain the variable term on one side of the equal sign and the constant term on the other.
  3. Divide or multiply as needed to isolate the variable.
  4. Check the answer.
Oct 6, 2021

How to write a system of equations as ax + b? ›

A system of m linear equations in n unknowns (note that m and n need not be equal) can be written as Ax = b where A is the m × n coefficient matrix of the system, x is the vector consisting of the n unknowns and b is the vector consisting of the m right-hand sides of the equations, as shown below.

What is the equation ax b? ›

And as you might have guessed already, the importance of the matrix equation Ax=b is to provide a way to solve for x (or better said, for the variables in column vector x), which can be easily done by using the three types of matrix row operations and the method of solving a linear system with matrices using Gaussian ...

What does B mean in algebra? ›

The b-value is the middle number, the number next to the x. The other letters, a and c, are also numbers like b. Each of these can be any number. In combination, they tell you what the quadratic function will look like when graphed.

How to solve for b in an equation? ›

Use the slope and one of the points to solve for the y-intercept (b). One of your points can replace the x and y, and the slope you just calculated replaces the m of your equation y = mx + b. Then b is the only variable left. Use the tools you know for solving for a variable to solve for b.

How to solve equations? ›

The following steps provide a good method to use when solving linear equations.
  1. Simplify each side of the equation by removing parentheses and combining like terms.
  2. Use addition or subtraction to isolate the variable term on one side of the equation.
  3. Use multiplication or division to solve for the variable.

What type of math is matrices? ›

Matrices are properly introduced in a class called “linear algebra,” which is typically taught after calculus (though there's no reason it should be). Many pre-calculus courses introduce things they call matrices.

How do I find the solution to a linear system? ›

A system of linear equations consists of the equations of two lines. The solution to a system of linear equations is the point which lies on both lines. In other words, the solution is the point where the two lines intersect.

How do you find the solution of a linear function? ›

Solving Linear Functions
  1. Substitute the value of f(x) into the problem. In this case: ...
  2. Isolate the variable. In this case, you add 1 to both sides to isolate the variable term by using the opposite operation to move the constant term across the equal sign. ...
  3. Continue to isolate the variable. ...
  4. Simplify.

How do you determine the solution of the given system of linear equations? ›

SOLVE A SYSTEM OF LINEAR EQUATIONS BY GRAPHING.
  1. Graph the first equation.
  2. Graph the second equation on the same rectangular coordinate system.
  3. Determine whether the lines intersect, are parallel, or are the same line.
  4. Identify the solution to the system. ...
  5. Check the solution in both equations.
Feb 13, 2022

What is a linear system in the form ax b? ›

If A is a square n -by- n matrix and B is a matrix with n rows, then x = A\B is a solution to the equation A*x = B , if it exists. If A is a rectangular m -by- n matrix with m ~= n , and B is a matrix with m rows, then A \ B returns a least-squares solution to the system of equations A*x= B .

Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 5985

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.