Matrices are a very powerful tool in mathematics because they are a convenient and compact way to represent large sets of numbers and with the help of the computer we can easily perform matrix operations. In particular, we can easily use Gaussian Elimination to solve systems of equations of the form where

, and .

In this activity we will learn how to calculate inverses and determinants of matrices so we can solve for the special case of n equations and n unknonws. That is, when A is a square matrix.

Use the MATLAB Live Editor to edit and run this Live Script in your browser or your desktop.

- Read each section carefully.
- Run the code within a section before starting to read the next.
- To run the code from each section, click into the code section and then click on the Run Section button (from the toolstrip) or click on the blue stripe on left side of that section as shown below:

Remark: Run the code of each section from top to bottom, otherwise you may get an error.

- The end of a section is indicated with a thin line, like the next one -

Recall that the inverse of a square matrix A is denoted by such that the following relationship holds

where I is the identity matrix.

In MATLAB we can easily calculate the inverse of a square matrix. For example, consider the 3-by-3 matrix

which can be defined as follows:

A = [2, -3, -1; 1, -2, -3; -2, 2, -5]; % Or A = [2 -3 -1; 1 -2 -3; -2 2 -5];

But before calculating its inverse, we need to check that A is invertible. To do so, we can use the function det() to find the determinant of A. Thus we write:

det(A)

Since det(A) is not zero, A is invertible and we can proceed to find its inverse by simply writing:

A^(-1)

To confirm that A^(-1) is the inverse of A, we can just use the matrix multiplication defined in MATLAB with the operator *. That is:

A * A^(-1)

A^(-1) * A

Note 1: Alternatively, you can write inv(A) to calculate the inverse A. However, if you write A * inv(A), you will get a warning message from MATLAB mentioning that it could be slower or less accurate. Try it yourself! Replace A^(-1) by inv(A) in the previous code to see what happens.

It is worth noting that matrix operations in MATLAB follow the rules of linear algebra and are not compatible with multidimensional arrays. The required size and shape of the inputs in relation to one another depends on the operation.

For example, if you use the matrix right division operator, /, to divide two matrices, the matrices must have the same number of columns. But if you use the matrix multiplication operator, *, to multiply two matrices, then the matrices must have a common inner dimension. That is, the number of columns in the first matrix must be equal to the number of rows in the second matrix. For more details see: Array vs Matrix Operations.

In MATLAB we can also calculate the inverse or determinant of symbolic matrices. For example:

syms a b c d

M = [a b; c d];

det(M)

M^(-1)

Note 2: Matrix computations involving many symbolic variables can be slow. To increase the computational speed, reduce the number of symbolic variables by substituting the given values for some variables.

Consider the system of n equations in n unkowns

which is equivalent to the matrix equation . In this case, we have a matrix A. If A is invertible, we can solve this system by multiplication on the left by to give

.

This implies a unique solution .

This method can be easily implemented in MATLAB by using an operator called mldivide, or the backslash operator \. If A is not invertible, then we need to use Gaussian Elimination.

For example, consider the following system

This is equivalent to solving the matrix equation where

, and .

First, we must initialise the matrices:

A = [2 1 -1; 1 1 -2; 0 2 -1];

b = [1; -1; 3];

x = mldivide(A,b) % You can also write A\b, which is shorter

Confirm this result in your notebook.

Let's practice what we just learned.

Consider the following matrices

and .

Compute each of the pairs of numbers that follow:

- ,
- ,
- ,
- ,
- ,
- ,

In each case, check whether the first number is equal to the second and make some conjectures for the general case. Once you finish analysing the results provided by MATLAB, try to prove or disprove your conjectures in your notebook.

Write your code here:

%{---Define matrices---}%

%{---part 1---}%

%{---part 2---}%

%{---part 3---}%

%{---part 4---}%

%{---part 5---}%

%{---part 6---}%

Calculate the inverse of the matrix

Hint: Don't forget to check first that the determinant is different to zero.

Write your code here:

If the matrix is invertible, confirm your result by using matrix multiplication. Write your code here:

For each matrix A and vector below, use the operator mldivide, or the backslash operator \, to solve the system .

- ,
- ,
- ,

If it is not possible to solve the system, add a comment mentioning why.

Hint: Don't forget to use different variable names to identify each part.

Write your code here:

%{---part 1---}%

%{---part 2---}%

%{---part 3---}%