Kahibaro
Discord Login Register

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: 3

Comments

Please login to add a comment.

Don't have an account? Register now!