Documentation Center

  • Trial Software
  • Product Updates

mldivide

Solve systems of linear equations Ax = B for x

Syntax

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, A\B performs element-wise division of A into B.

    • If A is a square n-by-n matrix and B is a column vector with n elements or 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 column vector with m elements or a matrix with m rows, then A\B returns a least-squares solution to the system of equations A*x= B.

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

    Examples

    expand all

    System of Equations

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

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

    Linear System with Singular Matrix

    Solve a linear system of equations involving a singular matrix, C.

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

    MATLAB issues a warning but proceeds with the calculation. MATLAB might give a valid solution even when working with a singular matrix. These solutions are often not unique.

    Least-Squares Solution of Underdetermined System

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

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

    Linear System with Sparse Matrix

    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

    expand all

    A — Coefficient matrixvector | full matrix | sparse matrix

    Coefficient matrix, specified as a vector, full matrix, or sparse matrix. If A has m rows, then B must have m rows.

    Data Types: single | double
    Complex Number Support: Yes

    B — Right-hand sidevector | full matrix | sparse matrix

    Right-hand side, specified as a vector, full matrix, or sparse matrix. If B has m rows, then A must have m rows.

    Data Types: single | double
    Complex Number Support: Yes

    Output Arguments

    expand all

    x — Solutionvector | full matrix | sparse matrix

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

    x is sparse only if both A and B are sparse matrices.

    More About

    expand all

    Tips

    • If A is a square matrix, 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. The more computationally expensive x = pinv(A)*B computes the minimum norm least-squares solution.

    • For full singular inputs, you can compute the least-squares solution using the function linsolve.

    See Also

    | | | | | | | | |

    Was this topic helpful?