Skip to main content

Section 2.1 Systems of linear equations

A system of linear equations, such as

\begin{align*} 3x_1 - 2x_2 \amp = 6 \\ 2x_1 + 5x_2 \amp = -3 \end{align*}

can be written in matrix form as \(Ax=b\) where \(A\) is the matrix of coefficients,

\begin{equation*} A = \begin{pmatrix} 3 \amp -2 \\ 2 \amp 5 \end{pmatrix} \end{equation*}

and \(b\) is the column vector on the right-hand side:

\begin{equation*} b = \begin{pmatrix} 6 \\ -3 \end{pmatrix} \end{equation*}

The Matlab command for solving \(Ax=b\) is very short:

x = A\b

The result is the column vector [1.2632; -1.1053], meaning that \(x_1\approx 1.2632\) and \(x_2 \approx -1.1053\text{.}\)

Mathematically, \(x\) could be found as \(x = A^{-1}b\) where \(A^{-1}\) is the inverse matrix. However, this is not a computationally efficient way to solve a linear system. Matlab does not actually compute the inverse matrix when solving a linear system. It chooses one of several algorithms based on the nature of the matrix; usually it is a form of “LU factorization” which is discussed in MAT 532. But even though the formula \(x = A^{-1}b\) is not actually used, it suggests the notation used by Matlab. For two numbers \(a, b\) we can write \(ba^{-1}\) as b/a, meaning \(b\) divided by \(a\text{.}\) If the order of multiplication was important (as it is for matrices), then perhaps \(a^{-1}b\) could be written as a\b, meaning \(b\) divided by \(a\) from the left. We do not actually “divide a vector by a matrix” but the notation A\b can remind us of \(A^{-1}b\text{.}\)

Write a Matlab script which solves the linear system

\begin{align*} 5x_1 + 3x_2 \amp = 2 \\ -2x_1 + x_2 - 6x_3 \amp = 0 \\ 3x_2 + x_3 \amp = 3 \end{align*}

and then plug the solution into the left hand side to check that the solution is correct.

Answer
A = [5 3 0; -2 1 -6; 0 3 1];
b = [2; 0; 3];
x = A\b;
disp(x)
disp(A*x)