KAHIBARO Bookshelves
Discord Login Register

15.3. Solving Linear Systems with Backslash

The Backslash Operator for Linear Systems

In MATLAB, the recommended way to solve most linear systems is to use the backslash operator \. This operator is designed to solve equations of the form
$$
A x = b
$$
where $A$ is a matrix, $x$ is an unknown vector (or matrix), and $b$ is a right hand side vector (or matrix). You use it in MATLAB as x = A\b;.

This chapter focuses on how to use \, what MATLAB does when you call it, and how to interpret results in practical situations.

Basic Usage: Solving $A x = b$

The simplest case is a square matrix $A$ and a column vector $b$ of matching size. For example, suppose you have a $3 \times 3$ system:
$$
A =
\begin{bmatrix}
1 & 2 & 3 \\
0 & 1 & 4 \\
2 & 0 & 1
\end{bmatrix},
\quad
b =
\begin{bmatrix}
7 \\ 5 \\ 3
\end{bmatrix}.
$$

In MATLAB, you can solve for $x$ as follows:

A = [1 2 3; 0 1 4; 2 0 1];
b = [7; 5; 3];
x = A\b

After this, x contains the vector that satisfies A*x equal to b up to numerical roundoff. You can check the residual by computing:

r = A*x - b

If the system is well conditioned, the entries of r will be close to zero.

Why Backslash Instead of Inverse

You might be tempted to solve $A x = b$ by computing the inverse of $A$ and multiplying:
$$
x = A^{-1} b.
$$

In MATLAB, that would be:

x = inv(A)*b;

However, for numerical work this is discouraged. A\b is usually faster and more accurate than inv(A)*b, and it does not explicitly form the matrix inverse. Internally, MATLAB factors the matrix and solves the system using those factors, which avoids unnecessary rounding errors and extra computation.

As a practical rule, use A\b for solving systems and avoid inv(A) unless you specifically need the inverse for a theoretical reason.

Multiple Right Hand Sides

The backslash operator also works when the right hand side is a matrix. Suppose $A$ is $m \times m$ and $B$ is $m \times k$. Then the equation
$$
A X = B
$$
can be solved for the unknown matrix $X$ as:

X = A\B;

Each column of X is the solution to the system with the corresponding column of B as the right hand side. This is often more efficient than solving each system separately in a loop.

For example:

A = [3 1; 1 2];
B = [9 8; 8 3];  % two right hand sides (columns)
X = A\B;

You can confirm that A*X is very close to B.

Overdetermined Systems and Least Squares

Sometimes you have more equations than unknowns. This corresponds to a matrix $A$ with more rows than columns. For example, an $m \times n$ matrix with $m > n$. The system
$$
A x = b
$$
may not have an exact solution. In this case, MATLAB interprets A\b as a least squares solution. It finds the vector $x$ that minimizes the Euclidean norm of the residual:
$$
\| A x - b \|_2.
$$

In MATLAB, you do not need a different command. You still write:

x = A\b;

For instance, consider:

A = [1 0;
     1 1;
     1 2];
b = [1; 2; 2];
x = A\b;

Here, there are three equations and two unknowns. The result x is the least squares solution. You can inspect how good the fit is by looking at:

r = A*x - b;
norm_r = norm(r)

The norm norm_r is the size of the residual that has been minimized.

Underdetermined Systems and Minimum Norm Solutions

When there are fewer equations than unknowns, $A$ has more columns than rows. In this underdetermined case, there are typically infinitely many solutions that satisfy $A x = b$ if a solution exists at all.

MATLAB uses A\b to compute one particular solution. Specifically, it returns a basic solution that has minimum norm among all solutions that satisfy the equations. Formally, it finds a solution $x$ that solves $A x = b$ and has minimal $\|x\|_2$.

You use the same syntax:

x = A\b;

For example:

A = [1 2 3;
     4 5 6];   % 2 equations, 3 unknowns
b = [7; 8];
x = A\b;

The vector x is one of many possible solutions to the system, but MATLAB chooses it to have minimal length in the Euclidean sense.

Singular and Nearly Singular Systems

A matrix is singular if it is not invertible. For a square system $A x = b$, a singular or nearly singular $A$ can cause numerical difficulties.

If $A is exactly singular or extremely ill conditioned, A\b may not be able to find a stable solution. In that case, MATLAB will typically display a warning about a singular or badly scaled matrix. The result x may have very large entries or be highly sensitive to slight changes in b.

Consider:

A = [1 2; 2 4];  % rows are linearly dependent
b = [3; 6];
x = A\b;

You may see a warning indicating that the matrix is singular or that the system is rank deficient. The computed solution is one of infinitely many that satisfy the equations, and small perturbations can change it a lot.

For nearly singular matrices, the condition number is large. You can get a sense of this with functions such as cond(A) or rcond(A), but the detailed study of conditioning is separate. Here, it is enough to remember that a very large condition number indicates that even A\b will produce a result that can be unreliable if the data is not exact.

Transposed Systems and the Slash Operator

The backslash operator \ solves left division problems of the form $A x = b$. MATLAB also provides the forward slash operator / for right division.

Right division solves problems of the form
$$
x A = b.
$$

You can write:

X = B/A;

This is interpreted internally using a related system, typically by solving $A^T X^T = B^T$. For most work with column vectors and standard linear systems, you will mainly use backslash. Right division becomes more relevant when your equations are naturally written with row vectors or when you need to solve matrix equations on the right.

The key idea is that A\b corresponds to left division, solving $A x = b$, while B/A corresponds to right division, solving $x A = B$.

Checking Solutions and Residuals

Whenever you solve a system with A\b, it is useful to verify the quality of the solution numerically. You can check that the computed $x$ satisfies the system up to floating point accuracy.

A common pattern in MATLAB is:

x = A\b;
residual = A*x - b;
res_norm = norm(residual);

The norm of the residual, res_norm, should be small relative to the norm of b if the system is well posed and A is not badly conditioned.

For multiple right hand sides, you can use the same ideas:

X = A\B;
R = A*X - B;
res_norm = norm(R, 'fro');

Here the Frobenius norm measures the overall size of the residual matrix.

Using Backslash in Larger Workflows

In many applications, solving linear systems with \ appears as one step inside a larger program. You might solve systems in a loop, or combine them with data analysis and plotting.

For example, if you have a time dependent model that produces a different right hand side at each step, you can solve:

A = [3 1; 1 2];
b_list = [1 0 -1; 2 3 4];  % each column a different right hand side
X = A\b_list;  % solves all systems at once

Alternatively, if the matrix $A$ is fixed and you have many right hand sides, it is often better to collect them as columns of a matrix and call A\B once, instead of using a for loop. MATLAB reuses the factorization of $A$, which can be more efficient than factoring it repeatedly.

In simple scripts, the backslash operator often replaces manual formula computations. For example, in small systems you might write explicit formulas for $x$ and $y$. However, such formulas quickly become complicated for larger systems, so A\b is the idiomatic way in MATLAB to handle these equations regardless of size.

Important points to remember:
Use A\b to solve linear systems $A x = b$. It is usually faster and more accurate than inv(A)*b.
For square, nonsingular $A$, A\b returns the exact solution up to numerical roundoff.
For overdetermined systems, A\b gives a least squares solution that minimizes $\|A x - b\|_2$.
For underdetermined systems, A\b returns one solution with minimum Euclidean norm.
If MATLAB warns that $A$ is singular or nearly singular, the solution can be unreliable and highly sensitive to small changes.
Use / for right division problems of the form $x A = b$, but for standard column vector systems $A x = b$ prefer backslash \.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Getting Started with MATLAB
    • ☰ 1.1. What Is MATLAB and What Is It Used For
      • ☰ 1.2. Installing MATLAB and Setting Up the Environment
        • ☰ 1.3. Understanding the MATLAB Desktop Interface
          • ☰ 1.4. Command Window, Workspace, and Current Folder
            • ☰ 1.5. Basic Workflow and Running Simple Commands
              • ☰ 1.6. Getting Help and Documentation
              • ☰ 2. MATLAB Language Basics
                • ☰ 2.1. MATLAB Syntax and Expressions
                  • ☰ 2.2. Variables and Assignment
                    • ☰ 2.3. Data Types and Type Conversion
                      • ☰ 2.4. Vectors and Matrices Fundamentals
                        • ☰ 2.5. Creating and Indexing Arrays
                          • ☰ 2.6. Basic Arithmetic and Elementwise Operations
                            • ☰ 2.7. Using Built-In Constants and Functions
                            • ☰ 3. Working with Variables and the Workspace
                              • ☰ 3.1. Creating, Viewing, and Editing Variables
                                • ☰ 3.2. Workspace Browser and Variable Inspector
                                  • ☰ 3.3. Saving and Loading Data Files
                                    • ☰ 3.4. MAT-Files and Importing External Data
                                      • ☰ 3.5. Clearing Variables and Managing the Workspace
                                      • ☰ 4. Vectors, Matrices, and Arrays
                                        • ☰ 4.1. Row and Column Vectors
                                          • ☰ 4.2. Matrix Creation and Concatenation
                                            • ☰ 4.3. Indexing, Slicing, and Logical Indexing
                                              • ☰ 4.4. Matrix Operations vs Elementwise Operations
                                                • ☰ 4.5. Reshaping and Rearranging Arrays
                                                  • ☰ 4.6. Special Matrices and Functions
                                                  • ☰ 5. Basic Programming Constructs
                                                    • ☰ 5.1. Scripts vs Functions
                                                      • ☰ 5.2. Control Flow with if, elseif, else
                                                        • ☰ 5.3. Loops with for and while
                                                          • ☰ 5.4. break, continue, and return
                                                            • ☰ 5.5. Logical Expressions and Relational Operators
                                                              • ☰ 5.6. Best Practices for Clear and Readable Code
                                                              • ☰ 6. Functions in MATLAB
                                                                • ☰ 6.1. Creating Simple Functions
                                                                  • ☰ 6.2. Function Inputs and Outputs
                                                                    • ☰ 6.3. Function Files and the Path
                                                                      • ☰ 6.4. Local vs Global Variables
                                                                        • ☰ 6.5. Anonymous Functions
                                                                          • ☰ 6.6. Organizing Code into Multiple Functions
                                                                          • ☰ 7. Working with Numbers and Math
                                                                            • ☰ 7.1. Scalars and Vectorized Computations
                                                                              • ☰ 7.2. Basic Mathematical Functions
                                                                                • ☰ 7.3. Trigonometric and Exponential Functions
                                                                                  • ☰ 7.4. Complex Numbers in MATLAB
                                                                                    • ☰ 7.5. Rounding, Modulo, and Special Values
                                                                                      • ☰ 7.6. Numerical Precision and Floating Point Considerations
                                                                                      • ☰ 8. Strings, Characters, and Categorical Data
                                                                                        • ☰ 8.1. Character Arrays and String Arrays
                                                                                          • ☰ 8.2. Creating and Manipulating Strings
                                                                                            • ☰ 8.3. Converting Between Data Types and Strings
                                                                                              • ☰ 8.4. Comparing and Searching Strings
                                                                                                • ☰ 8.5. Categorical Arrays Basics
                                                                                                • ☰ 9. Tables, Structures, and Cell Arrays
                                                                                                  • ☰ 9.1. Introduction to Tables
                                                                                                    • ☰ 9.2. Creating Tables and Accessing Data
                                                                                                      • ☰ 9.3. Modifying and Joining Tables
                                                                                                        • ☰ 9.4. Structures and Nested Structures
                                                                                                          • ☰ 9.5. Cell Arrays and Cell Indexing
                                                                                                            • ☰ 9.6. Choosing the Right Container Type
                                                                                                            • ☰ 10. Importing and Exporting Data
                                                                                                              • ☰ 10.1. Reading Text and CSV Files
                                                                                                                • ☰ 10.2. Importing Excel Spreadsheets
                                                                                                                  • ☰ 10.3. Importing Data with the Import Tool
                                                                                                                    • ☰ 10.4. Exporting Data to Files
                                                                                                                      • ☰ 10.5. Working with Image and Audio Files
                                                                                                                        • ☰ 10.6. Basic File Management from MATLAB
                                                                                                                        • ☰ 11. Plotting and Visualization
                                                                                                                          • ☰ 11.1. Introduction to 2D Plotting
                                                                                                                            • ☰ 11.2. Customizing Plots and Axes
                                                                                                                              • ☰ 11.3. Multiple Plots in One Figure
                                                                                                                                • ☰ 11.4. Labels, Titles, Legends, and Annotations
                                                                                                                                  • ☰ 11.5. Plotting with Different Line and Marker Styles
                                                                                                                                    • ☰ 11.6. Saving and Exporting Figures
                                                                                                                                    • ☰ 12. Advanced Plotting for Beginners
                                                                                                                                      • ☰ 12.1. Subplots and Tiled Layouts
                                                                                                                                        • ☰ 12.2. Bar, Scatter, and Stem Plots
                                                                                                                                          • ☰ 12.3. Histograms and Density Plots
                                                                                                                                            • ☰ 12.4. 3D Plots and Surface Plots
                                                                                                                                              • ☰ 12.5. Using plottools and Interactive Editing
                                                                                                                                                • ☰ 12.6. Basics of Handle Graphics
                                                                                                                                                • ☰ 13. Working with Scripts and Live Scripts
                                                                                                                                                  • ☰ 13.1. Creating and Running Script Files
                                                                                                                                                    • ☰ 13.2. Sectioning Code and Running Sections
                                                                                                                                                      • ☰ 13.3. Live Scripts and Rich Text
                                                                                                                                                        • ☰ 13.4. Adding Equations, Images, and Outputs
                                                                                                                                                          • ☰ 13.5. Sharing and Exporting Live Scripts
                                                                                                                                                          • ☰ 14. Debugging and Code Quality
                                                                                                                                                            • ☰ 14.1. Finding and Fixing Syntax Errors
                                                                                                                                                              • ☰ 14.2. Setting Breakpoints and Stepping Through Code
                                                                                                                                                                • ☰ 14.3. Inspecting Variable Values During Execution
                                                                                                                                                                  • ☰ 14.4. Using the Editor Debugging Tools
                                                                                                                                                                    • ☰ 14.5. Using Code Analyzer and Warnings
                                                                                                                                                                      • ☰ 14.6. Writing Clean and Maintainable MATLAB Code
                                                                                                                                                                      • ☰ 15. Basic Linear Algebra with MATLAB
                                                                                                                                                                        • ☰ 15.1. Vectors and Norms
                                                                                                                                                                          • ☰ 15.2. Matrix Addition and Multiplication
                                                                                                                                                                            • ☰ 15.3. Solving Linear Systems with Backslash
                                                                                                                                                                              • ☰ 15.4. Inverses, Determinants, and Rank
                                                                                                                                                                                • ☰ 15.5. Eigenvalues and Eigenvectors
                                                                                                                                                                                  • ☰ 15.6. Using Linear Algebra Functions Safely
                                                                                                                                                                                  • ☰ 16. Data Analysis and Statistics Fundamentals
                                                                                                                                                                                    • ☰ 16.1. Descriptive Statistics and Summary Measures
                                                                                                                                                                                      • ☰ 16.2. Sorting, Filtering, and Grouping Data
                                                                                                                                                                                        • ☰ 16.3. Basic Correlation and Regression
                                                                                                                                                                                          • ☰ 16.4. Simple Data Cleaning Techniques
                                                                                                                                                                                            • ☰ 16.5. Basic Time Series Handling
                                                                                                                                                                                            • ☰ 17. Working with Time and Dates
                                                                                                                                                                                              • ☰ 17.1. Representing Dates and Times
                                                                                                                                                                                                • ☰ 17.2. Creating and Formatting datetime Arrays
                                                                                                                                                                                                  • ☰ 17.3. Date Calculations and Differences
                                                                                                                                                                                                    • ☰ 17.4. Plotting Data over Time
                                                                                                                                                                                                    • ☰ 18. Introduction to Signal and Image Handling
                                                                                                                                                                                                      • ☰ 18.1. Basic Signal Representation in MATLAB
                                                                                                                                                                                                        • ☰ 18.2. Simple Filtering and Transformations
                                                                                                                                                                                                          • ☰ 18.3. Reading, Displaying, and Writing Images
                                                                                                                                                                                                            • ☰ 18.4. Basic Image Transformations
                                                                                                                                                                                                            • ☰ 19. Automation and Scripting Tasks
                                                                                                                                                                                                              • ☰ 19.1. Batch Processing of Files
                                                                                                                                                                                                                • ☰ 19.2. Writing Reusable Utility Scripts
                                                                                                                                                                                                                  • ☰ 19.3. Using Relative Paths and the MATLAB Path
                                                                                                                                                                                                                    • ☰ 19.4. Simple User Interaction with input and disp
                                                                                                                                                                                                                    • ☰ 20. Introduction to Toolboxes
                                                                                                                                                                                                                      • ☰ 20.1. What Are Toolboxes and How to Use Them
                                                                                                                                                                                                                        • ☰ 20.2. Exploring Installed Toolboxes
                                                                                                                                                                                                                          • ☰ 20.3. Finding Functions in Toolboxes
                                                                                                                                                                                                                            • ☰ 20.4. Add-Ons and MATLAB File Exchange
                                                                                                                                                                                                                            • ☰ 21. Best Practices and Next Steps
                                                                                                                                                                                                                              • ☰ 21.1. Organizing Projects and Folders
                                                                                                                                                                                                                                • ☰ 21.2. Version Control Basics with MATLAB
                                                                                                                                                                                                                                  • ☰ 21.3. Performance Tips for Beginners
                                                                                                                                                                                                                                    • ☰ 21.4. Learning Resources and Documentation Path
                                                                                                                                                                                                                                      • ☰ 21.5. Suggested Practice Projects for Beginners