MATLAB Lesson 6 - Matrices

Linear Systems

In MATLAB you can solve a linear system A x = b using the backslash operator \. However you need to be aware that the MATLAB's backslash does much, much more.

Square, nonsingular systems

If A is an n by n non-singular matrix (that is the determinant of A is non-zero) then the system of linear equations A x = b has a unique solution x = A-1 b in Rn for any right-hand-side vector b in Rn.

A non-singular linear system can be efficiently solved using the backslash \ operator. This uses Gaussian elimination, based on reducing the matrix to row-echelon form using row operations, as in first year mathematics. However all these details are hidden from you.

In general this is far more efficient than explicitly calculating the inverse A-1.

Define the matrix A = [2 1; -1 3] and check it is non-singular. Solve the linear system for the right-hand-side vector b with all elements equal to 1.

Create the matrix A.

Check A is non-singular using the det function.

Define the right-hand-side column vector of all 1s.

Solve the linear system using Gaussian elimination.

Solve the linear system by explicitly calculating the inverse using the inv function.

>>  A = [2 1; -1 3]
>>  det(A)
>>  b = ones(2,1)
>>  x = A \ b
>>  x = inv(A)*b


Inconsistent linear systems

If the coefficient matrix A is singular, that is det(A) = 0, then you will get a warning message. However MATLAB will still calculate a "solution" which will probably involve Inf or NaN.

Define the matrix A = [2 1; -6 -3] and solve the linear system with right-hand-side vector with all elements equal to 1.

Create the matrix A. The vector b was defined in the previous example.

Solving the linear system using \ gives
Warning: Matrix is singular to working precision.
and x = [-Inf; Inf]

Checking the determinant show that A is singular.

>>  A = [2 1; -6 -3]
>>  x = A \ b
>>  det(A)




Non-square linear systems

When the coefficient matrix A is not square, for example if it has more rows (equations) than columns (variables) then MATLAB calculates the least squares solution.

If you have not seen least squares solutions (yet) then skip the rest of this section, but remember that MATLAB may calculate it, even if you did not (explicitly) ask it to!

When A is not square, it is a good idea to calculate the residual vector r = b - Ax. Then x is a solution to Ax = b if and only if r = 0 (the zero vector).

Define the 3 by 2 matrix A with all elements in the first column equal to 1 and the second column having elements 1, 2, 3. Use MATLAB's backslash operator to "solve" the linear system for the right-hand-side vector b with elements 4, 3, 8.

Create the matrix A by columns, transposing the row vector 1:3 to get a column vector.

Create the right-had-side column vector b.

Solving the linear system using \ gives
x = [1; 2] and no warnings or error messages.

The residual r is not zero, so x does not satisfy Ax = b.

>>  A = [ones(3,1) [1:3]']
>>  b = [4; 3; 8]
>>  x = A \ b
>>  r = b - A*x

 

Warnings

Self-test Exercise

Create the column vector t with elements 0, 1, 2. Create the matrix A with elements Aij = (ti)j-1, i,j = 1,2,3, and column vector y with elements 3, 2, 3. Solve the linear system Ax = y. Calculate the determinant to check A is non-singular, and the residual r = y - Ax to check x does solve Ax = y.

This finds the quadratic passing through (ti, yi) for i = 1,2,3.

Answer:
t = [0:2]'
A = [ones(size(t)) t t.^2]
y = [3; 2; 3]
x = A \ y
det(A)
r = y - A*x
Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

A linear system Ax = b with a square non-singular coefficient matrix A can be efficiently solved using x = A \ b.

If the the coefficient matrix A is not square then this gives the "least squares solution" which does not necessarily satisfy Ax = b.