Unit - Two - Lecture Notes 17.10.2023
Unit - Two - Lecture Notes 17.10.2023
Introduction
Linear algebra is a branch of mathematics concerned with the study of matrices, vectors, vector
spaces (also called linear spaces), linear maps (also called linear transformations), and systems
of linear equations. MATLAB are well suited for studying Linear Algebra because its underlying
structure is based on MATRICES.
There are many situations in which it would be too difficult and/or too tedious to compute or
evaluate matrix operations more especially during processes of numerical differentiation or
integration where large and denser matrices appear. In this unit, we will learn various ways of
defining of matrices, vectors and other linear systems in MATLAB. Specifically, in this unit, we
shall explore ways of basic matrix operations and other related problems in linear algebra.
Unit Objectives
On successful completion of the Unit, students should be able to:
● Describe vectors and matrices in MATLAB
● Define vector and matrix operations in MATLAB
● Load and read data files
● Solve system of linear equations using MATLB
Key Terms
As you go through this unit, ensure that you understand the key terms or phrases used in this unit
as listed below:
● Vector
● Matrix
● Matrix operations
● Eigenvalue
● Coefficient matrix
● Inverse matrix
31
An order or size of any matrix 𝐴 can be obtained via MATLAB command “size(A)”. Thus, other than
defining matrices, MATLAB has specific functions that support reading an entire file and creating
a matrix of the data with one statement. We use a function load to create a matrix of data, and
functions size and length to read the data files. Thus
>> load mydata.dat % loads file into a matrix called mydata
>> size(mydata) % return number of rows and columns for the matrix mydata
>> length(mydata) % return total number of elements in matrix mydata
Matrix functions
We discuss different functions in matrices and describe how each of these is defined in MATLAB.
1) Transpose of a matrix
Just as how we defined transpose of a vector, similarly we describe the transpose of a matrix 𝐴 as a
matrix of 𝑚- rows and 𝑛- columns defined as an 𝑚 × 𝑛 matrix
𝑎11 ⋯ 𝑎1𝑛
𝐴𝑇 = [ ⋮ ⋱ ⋮ ] ∈ ℝ𝑚×𝑛
𝑎𝑚1 ⋯ 𝑎𝑚𝑛
which is defined in MATLAB as
>> A'
ans =
2 1 7
3 2 11
-5 4 3
0 8 6
if A is originally defined as
2 3 −5 0
𝐴 = [1 2 4 8]
7 11 3 6
2) Diagonal matrix
32
2 3 −5 0
1 2 4 8
𝐴=[ ]
7 11 3 6
3 −1 4 5
it’s diagonal matrix is
2
2
𝑑𝑖𝑎𝑔(𝐴) = [ ]
3
5
In MATLAB, we describe such operation as
Let’s look at a second example, a diagonal can still be obtained for non-square matrix such as
2 3 −5 0
𝐴 = [1 2 4 8]
7 11 3 6
whose diagonal is taken as
>> A'
ans =
2 1 7
3 2 11
-5 4 3
0 8 6
>> diag(ans)
ans =
2
2
3
33
Before we describe another form of matrix, triangular matrix, let us have a description of the special
type of diagonal matrix called “Identity” matrix I, defined by
1 0 0 0
0 1 0 0
𝐼=( )
⋮ ⋮ ⋱ 0
0 0 0 1
While in MATLAB, this type of matrix is defined using a special term called “eye”. Thus, “eye(n)”
produces an 𝑛 × 𝑛 diagonal matrix. For example
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
If such a matrix is made of “ones” entry throughout, then we have another special matrix called “ones
matrix” which is generated in MATLAB using a function “ones(n)”. For example with “ones(3)”, we obtain
>> ones(3)
ans =
1 1 1
1 1 1
1 1 1
Alternatively, if the matrix is made of “zero” entry throughout, then we have another special matrix called
“zero matrix” which is generated in MATLAB using a function “zeros(n)”. For example with “zeros(3)”, we
obtain
>> zeros(3)
ans =
0 0 0
0 0 0
0 0 0
3) Triangular matrices
These are another type of special matrices defined as either the upper or lower part of the matrix
including the diagonal.
34
𝑎11 0 0
𝐿=[ ⋮ ⋱ 0 ]
𝑎𝑛1 ⋯ 𝑎𝑛𝑛
b) Upper Triangular matrix 𝑈: is described as
𝑎11 ⋯ 𝑎1𝑛
𝑈=[ 0 ⋱ ⋮ ]
0 0 𝑎𝑛𝑛
In MATLAB, lower triangular matrix is defined as
>> tril(A)
ans =
2 0 0 0
1 2 0 0
7 11 3 0
3 -1 4 5
>> triu(A)
ans =
2 3 -5 0
0 2 4 8
0 0 3 6
0 0 0 5
Other kinds of matrix operations that are critical in numerical operations are matrix addition and
subtraction. Both of these operations require two matrices 𝐴 and 𝐵 to be of the same order 𝑛 × 𝑚 and
𝑝 × 𝑞, respectively where 𝑛 = 𝑝 and 𝑚 = 𝑞. Thus,
a) Matrix addition
Given two matrices 𝐴 ∈ ℝ𝑛×𝑚 and 𝐵 ∈ ℝ𝑛×𝑚 , then 𝐶 = 𝐴 + 𝐵 ∈ ℝ𝑛×𝑚 . Hence addition of two matrices
is done component-wise, such that in MATLAB we have
>> tril(A)+triu(A)
ans =
4 3 -5 0
1 4 4 8
7 11 6 6
3 -1 4 10
35
for the addition of two lower and upper triangular matrices described above.
b) Matrix subtraction
Just as with addition, subtraction of two matrices is done component-wise for the defined resultant matrix
𝐶 = 𝐴 + 𝐵 ∈ ℝ𝑛×𝑚 , whenever 𝐴 ∈ ℝ𝑛×𝑚 and 𝐵 ∈ ℝ𝑛×𝑚 . Thus, using the same two lower and upper
triangular matrices, we have
>> tril(A)-triu(A)
ans =
0 -3 5 0
1 0 -4 -8
7 11 0 -6
3 -1 4 0
5) Matrix multiplication
In matrix multiplication, what is more important is the corresponding number of rows in one given matrix,
corresponding to another matrix. Thus, given two matrices 𝐴 ∈ ℝ𝑛×𝑚 and 𝐵 ∈ ℝ𝑚×𝑝 , then 𝐶 = 𝐴𝐵 ∈
ℝ𝑛×𝑝 . Hence multiplication of two matrices is done such that entries of the resultant matrix 𝐶 are each
defined as
𝑛
Hence, in MATLAB, given any two matrices 𝐴2×3 and 𝐵3×4 , we get 𝐶2×4, that is
One thing we have to remember about matrix multiplication is that 𝐴𝐵 ≠ 𝐵𝐴, that is, matrix
multiplication is never commutative. But, the following associative operations are possible
𝐴(𝐵𝐶) = (𝐴𝐵)𝐶
𝐴(𝐵 + 𝐶) = 𝐴𝐵 + 𝐴𝐶
𝐶(𝐴 + 𝐵) = 𝐶𝐴 + 𝐶𝐵
36
6) Determinant
For square matrices, determinant is a crucial parameter to describe either an invertible or non-invertible
matrix. Given a matrix 𝐴 ∈ ℝ𝑛×𝑛 , then the determinant is given as det(𝐴) = |𝐴| such that for a 2 × 2
matrix
𝑎11 𝑎12 2×2
𝐴 = [𝑎 𝑎22 ] ∈ 𝑅
21
then
Similarly for any matrix of order 𝑛 × 𝑛, the same MATLAB operator can be used to obtain the
determinant. For example
det(𝐴𝑇 ) = det(𝐴
37
For example
7) Inverse
𝐴−1 𝐴 = 𝐴𝐴−1 = 𝐼
Thus, for a 2 × 2 matrix
𝑎11 𝑎12 2×2
𝐴 = [𝑎 𝑎22 ] ∈ 𝑅
21
then
𝑎22 −𝑎12
𝐴−1 = 1⁄det(𝐴) [−𝑎 𝑎11 ] ∈ 𝑅
2×2
21
But the formula is different for any square matrix of order 𝑛, 𝑛 > 2. Nonetheless, the operator for
computing inverse of a square matrix in MATLAB is the same, i.e. “inv(A)”.
38
For example
>> B
B=
2 2
1 -5
>> inv(B)
ans =
0.4167 0.1667
0.0833 -0.1667
Another example
A=
2 3 -5 0
1 2 4 8
7 11 3 6
3 -1 4 5
>> inv(A)
ans =
8) Eigenvalues
Eigenvalues are as well another crucial parameter in describing roots of a characteristic polynomial and
also in classifying dynamical systems. Thus, Given a matrix 𝐴 ∈ ℝ𝑛×𝑛 , then the eigenvalues are given as
eig(A) = |𝐴| such that for a 2 × 2 matrix
𝑎11 𝑎12 2×2
𝐴 = [𝑎 𝑎22 ] ∈ 𝑅
21
then
39
where 𝜆′𝑠 ∈ ℂ are the eigenvalues. For example, in MATLAB
ans =
11.1973 + 0.0000i
4.3970 + 0.0000i
-1.7972 + 5.1562i
-1.7972 - 5.1562i
From the solution, we observe that our eigenvalues are in the form of 𝑎 + 𝑖𝑏 where 𝑎 is the real part while
𝑏 is the imaginary part.
9) Rank
With a MATLAB matrix function “rank”, we are able to compute the number of linearly independent rows
or columns of a given matrix. For example
Lastly, MATLAB provides another set of special matrices (see Table 5). These matrices have interesting
properties that make them useful for constructing examples and for testing algorithms. For more
information, see MATLAB documentation by typing “help_name of a matrix” in the command window.
Table 5: Special matrices
40
Activity 2 b
a) Define any 4 × 4 matrix A and extract a submatrix B consisting of rows 2 and 3 and
columns 1 and 2 of the matrix A.
b) Verify using MATLAB that a matrix
0 2
𝑅=[ ]
0 3
does not have an inverse.
c) Use MATLAB to show that for any given square invertible matrix of order 𝑛, it is not always
true that
𝐴−1 𝐴 = 𝐴𝐴−1 = 𝐼
d) Given any two matrices 𝑆 and 𝑇 of order 𝑛 × 𝑚 and 𝑚 × 𝑝, respectively. Show using
MATLAB that the resultant matrix 𝑆𝑇 is non-singular.
e) From a matrix 𝐴 = [123; 456; 789], define a concatenated matrix 𝐵 = [𝐴10 ∗
𝐴; −𝐴𝑒𝑦𝑒(3)].
f) In Discrete Mathematics module, you learn that 𝐻𝑛×𝑛 is said to be a Hadamard matrix if
and only if 𝐻 = (𝑎𝑖𝑗 )𝑛×𝑛 such that 𝐻𝐻 𝑇 = 𝑛𝐼𝑛 where 𝑎𝑖𝑗 = −1 or 1. Given that
1 1
𝐻=( )
−1 1
a. Demonstrate that 𝐻 is Hadamard matrix.
b. Hence, draw up a concatenated Hadamard matrix
𝐻 𝐻
𝐻∗ = ( )
𝐻 −𝐻
41
Linear system of equations
During applications that involve usage of differential equations, such as in modelling
environmental changes, diseases, interactions, etc. often do land into a system of equations of the
form 𝐴𝑿 = 𝐵 where 𝑋 is a vector of decision variables for the given system of differential
equations, while 𝐴 and 𝐵 are the coefficient matrix and system solution vector, respectively. For
example, a system of the form
2𝑥1 + 𝑥2 − 𝑥3 = 0
𝑥1 + 7𝑥2 + 2𝑥3 = 1
3𝑥1 − 3𝑥2 + 4𝑥3 = 2
𝑿 = 𝐴−1 𝐵
Since we have already looked at how to compute matrix multiplication and also inverse of a matrix, then
easier it will be to compute
𝑿 = 𝐴−1 𝐵
For example, for the equations
𝑥 + 2𝑦 = 5
3𝑥 + 4𝑦 = 6
42
Hence, the solution in MATLAB may be written as
Take note that the inverse syntax “X=inv(A)*B” can as well be written in another form as “X=A\B” where
the latter syntax evaluates even if the matrix A does not have an inverse. But in actual sense, the two
syntaxes should be able to give the same result. For example
>> X=A\B
X=
-4.0000
4.5000
For very large coefficient and ill-conditioned matrix 𝐴, this method of solving the system 𝐴𝑿 = 𝐵 by the
operation
𝑿 = 𝐴−1 𝐵
is not efficient. Thus, other methods exist which we shall discuss them as either iterative or non-iterative
methods, derived based on programming techniques. However, since we have not gone into details yet
of writing MATLAB programming files, in this unit, we only discuss these methods using MATLAB syntaxes.
43
a) LU Factorization method
This method is built on factorization of lower 𝐿 and upper 𝑈 triangular matrices of coefficient matrix 𝐴.
Thus, 𝐿𝑈 factorization of a matrix 𝐴 ∈ ℝ𝑛×𝑛 is given by
𝐴 = 𝐿𝑈
where 𝐿 and 𝑈 are the lower and upper triangular matrices, respectively. The MATLAB syntax for 𝐿𝑈
factorization of matrix 𝐴 ∈ ℝ𝑛×𝑛 is therefore
𝐿𝑈 = 𝑙𝑢(𝐴)
For example
0 0.6667
𝐴 = 𝐿𝑈 = 𝐿𝐷𝑈
where 𝐷 is the diagonal matrix. Hence, the MATLAB syntax for this is
[𝐿, 𝑃, 𝑈] = 𝑙𝑢(𝐴)
44
For example
Hence, in order to solve the system 𝐴𝑿 = 𝐵 by 𝐿𝑈 factorization method, we have 𝐴𝑿 = 𝐿𝑈𝑿 = 𝐵 such
that
𝑿 = (𝐿𝑈)−1 𝐵 = 𝐿−1 𝑈−1 𝐵
𝑈𝑿 = 𝑍 and
𝐿𝒁 = 𝐵
such that the unknowns of vector 𝒁 are determined by Forward substation method, while the
unknowns of vector 𝑿 are determined by backward substitution.
we have
45
>> A=[1 2;3 4]
A=
1 2
3 4
>> [L U]=lu(A)
L=
0.3333 1.0000
1.0000 0
U=
3.0000 4.0000
0 0.6667
>> B=[5;6]
B=
5
6
>> Z=inv(L)*B
Z=
6
3
>> X=inv(U)*Z
X=
-4.0000
4.5000
𝐴 = 𝑈𝑆𝑉 𝑇
where 𝑈 is an orthogonal matrix, 𝑉 is an orthogonal matrix, and 𝑆 is a diagonal singular matrix such that
the MATLAB syntax is defined by
[𝑈, 𝑆, 𝑉] = 𝑠𝑣𝑑(𝐴)
46
For example,
Activity 2 c
a) Consider the following system of linear equations
𝑥 + 2𝑦 + 3𝑧 = 1
{4𝑥 + 5𝑦 + 6𝑧 = 1
7𝑥 + 8𝑦 = 1
𝑥 + 2𝑦 + 3𝑧 = 1
{ 3𝑥 + 3𝑦 + 4𝑧 = 1
2𝑥 + 3𝑦 + 3𝑧 = 1
a. Derive an expression of the form 𝐴𝒙 = 𝒃 in MATLAB
b. Hence, solve for 𝒙 using 𝐿𝑈 factorization method and compare with 𝐴\𝑏 method.
d) From each matrix 𝐴 of problems a) and c), deduce 𝑈, 𝑆, 𝑉via [𝑈, 𝑆, 𝑉] = 𝑠𝑣𝑑(𝐴).
47