0% found this document useful (0 votes)
92 views15 pages

Lecture 4, Vectors and Matrices-1

The document discusses basic matrix definitions and operations in MATLAB. It defines matrices and vectors, including special cases like square, diagonal and identity matrices. Vectors can be column or row matrices. The transpose of a matrix or vector is described. Methods for creating vectors using numerical values, variables, expressions and the colon operator are presented, along with examples. Accessing individual elements of a vector using subscript notation is also covered.

Uploaded by

Vishvas Suthar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views15 pages

Lecture 4, Vectors and Matrices-1

The document discusses basic matrix definitions and operations in MATLAB. It defines matrices and vectors, including special cases like square, diagonal and identity matrices. Vectors can be column or row matrices. The transpose of a matrix or vector is described. Methods for creating vectors using numerical values, variables, expressions and the colon operator are presented, along with examples. Accessing individual elements of a vector using subscript notation is also covered.

Uploaded by

Vishvas Suthar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ES106: Programming for Engineers

Lecture 4: Vectors and Matrices


MATLAB is a language whose operating instructions and syntax are based on a set of fundamental matrix
operations and their extensions. Therefore, in order to fully utilize the advantages and compactness of
the MATLAB language, we summarize some basic matrix definitions and symbolism and present many
examples of their usage.

DEFINITIONS OF MATRICES AND VECTORS


An array 𝐴 of 𝑚 rows and 𝑛 columns is called a matrix of order (𝑚 × 𝑛), which consists of a total of 𝑚𝑛
elements arranged in the following rectangular array:
𝑎11 𝑎12 ⋯ 𝑎1𝑛
𝑎21 𝑎22 ⋯ 𝑎2𝑛
𝐴=[ ⋮ ⋮ ⋱ ⋮ ] → (𝑚 × 𝑛)
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛

The elements of the matrix are denoted 𝑎𝑖𝑗 , where 𝑖 indicates the row number and 𝑗 the column number.
In MATLAB, the order of the matrix is referred to as its size. Several special cases of this general matrix
are as follows:

Square Matrix

When 𝑚 = 𝑛, we have square matrix.

Diagonal matrix

When m=n and 𝑎𝑖𝑗 = 0, 𝑖 ≠ 𝑗, we have the diagonal matrix


𝑎11 0 ⋯ 0
0 𝑎22 ⋯ 0
𝐴=[ ] → (𝑛 × 𝑛)
⋮ ⋮ ⋱ ⋮
0 0 ⋯ 𝑎𝑛𝑛
Identity Matrix

In a diagonal matrix, when 𝑎𝑖𝑖 = 1, we have the identity matrix 𝐼, that is


1 0 ⋯ 0
0 1 ⋯ 0
𝐼=[ ]
⋮ ⋮ ⋱ ⋮
0 0 ⋯ 1
Vectors: Column and Row Matrices

When 𝑎𝑖𝑗 = 𝑎𝑖1 , that is, there is only one column, then is called a column matrix or, more commonly, a
vector, that is,

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
𝑎11 𝑎1
𝑎21 𝑎2
𝑎 = [ ⋮ ] = [ ⋮ ] → (𝑚 × 1)
𝑎𝑚1 𝑎𝑚

The quantity 𝑚 is the length of the vector.

When 𝑎𝑖𝑗 = 𝑎1𝑗 , that is, there is only one row, then is called a row matrix or a vector, that is,

𝑎 = [𝑎11 𝑎12 ⋯ 𝑎1𝑛 ] = [𝑎1 𝑎2 ⋯ 𝑎𝑛 ] → (1 × 𝑛)

This is the default definition of a vector in MATLAB. In this case, 𝑛 is the length of the vector. Thus, vector
can be represented by a row or a column matrix.

Transpose of a Matrix and a Vector

The transpose of a matrix is denoted by an (‘) apostrophe , and is defined as follows.

When is A the (𝑚 × 𝑛) matrix,


𝑎11 𝑎12 ⋯ 𝑎1𝑛
𝑎21 𝑎22 ⋯ 𝑎2𝑛
𝐴=[ ⋮ ⋮ ⋱ ⋮ ] → (𝑚 × 𝑛)
𝑎𝑚1 𝑎𝑚2 ⋯ 𝑎𝑚𝑛

Then its transpose 𝑊 = 𝐴’ is the following (𝑛 × 𝑚) matrix:


𝑎11 𝑎21 ⋯ 𝑎𝑚1
𝑎12 𝑎22 ⋯ 𝑎𝑚2
𝑊 = 𝐴′ = [ ⋮ ⋮ ⋱ ⋮ ] → (𝑛 × 𝑚)
𝑎1𝑛 𝑎2𝑛 ⋯ 𝑎𝑚𝑛

that is, rows and columns are interchanged.

For column and row vectors, we have the following. If


𝑎1
𝑎2
𝑎 = [ ⋮ ] → (𝑚 × 1) then 𝑎′ = [𝑎1 𝑎2 ⋯ 𝑎𝑚 ] → (1 × 𝑚)
𝑎𝑚

and if
𝑎1
𝑎2
𝑎 = [𝑎1 𝑎2 ⋯ 𝑎𝑛 ] → (1 × 𝑛) then 𝑎′ = [ ] → (𝑛 × 1)

𝑎𝑛

CREATION OF VECTORS
Vectors in MATLAB are expressed as either

𝑥 = [𝑎 𝑝 𝑧] 𝑜𝑟 𝑥 = [𝑎, 𝑝, 𝑧]

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
where 𝑎, 𝑝, 𝑧, ⋯ are either variable names, numbers, expressions, or strings. If they are variable names or
expressions, then all variable names and the variable names composing the expressions must be defined
such that a numerical value has been obtained for each of these variable names prior to the execution of
this statement. Variable names, expressions, and numbers can appear in any combination and in any
order. In the form

𝑥 = [𝑎 𝑝 𝑧]

the space (blank) between symbols is required, whereas in the form

𝑥 = [𝑎, 𝑝, 𝑧]
the blank is optional.

Colon Notation

MATLAB gives several additional ways to assign numerical values to the elements of a vector. The first
method uses the colon notation to specify the range of the values and the increment between adjacent
values. The second method specifies the range of the values and the number of values desired. In the first
method, the increment is either important or has been specified, whereas in the second method, the
number of values is important.

The colon notation to create a vector is

𝑥 = 𝑠: 𝑑: 𝑓 𝑜𝑟 𝑥 = (𝑥: 𝑑: 𝑓) 𝑜𝑟 𝑥 = [𝑠: 𝑑: 𝑓]
where, 𝑠 = start or initial value, 𝑑 = increment or decrement, 𝑓 = end or final value. If 𝑑 is not specified
then value 1 is considered as increment. With any of these command, the following row vector 𝑥 is
created.

𝑥 = [𝑠, 𝑠 + 𝑑, 𝑠 + 2𝑑, ⋯ , 𝑠 + 𝑛𝑑]


where, 𝑠 + 𝑛𝑑 ≤ 𝑓. Note that the number of values 𝑛 created for 𝑥 is not specified directly. The quantities
𝑠, 𝑑, and 𝑓 can be any combination of numerical values, variable names, and expressions. The number of
terms (i.e., the length of the vector) that this expression has created is determined from the MATLAB
function.
length(x)
we can create a column vector by taking the transpose of a row vector.

Example:

Command Output (Results)


x=
0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000
x = 0.2:0.1:1
1.0000
n = length(x)
n=
9
x=
x = 1:-0.1:0.2 1.0000 0.9000 0.8000 0.7000 0.6000 0.5000 0.4000 0.3000
0.2000
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
x=
0.2000
0.3000
0.4000
0.5000
x = (0.2:0.1:1)’ 0.6000
n = length(x) 0.7000
0.8000
0.9000
1.0000
n=
9
x=
x=0.2:0.12:1 0.2000 0.3200 0.4400 0.5600 0.6800 0.8000 0.9200
n = length(x) n=
7
x=
x = 1:7 1234567
n = length(x) n=
7
x=
x = 0.5:7 0.5000 1.5000 2.5000 3.5000 4.5000 5.5000 6.5000
n = length(x) n=
7
In the second method, one specifies 𝑛 equally spaced values starting at 𝑠 and ending at 𝑓 as follow:
x = linespace(s,f,n)

where the increment (or decrement) is computed by MATLAB from


𝑓−𝑠
𝑑=
𝑛−1
when 𝑛 is not specified, it is assigned a value of 100.

Command Output (Results)


x=
x = linspace(-2,6.5,8) -2.0000 -0.7857 0.4286 1.6429 2.8571 4.0714 5.2857 6.5000
n = length(x) n=
8
Accessing Elements of Vectors

Let

𝑏 = [𝑏1 𝑏2 ⋯ 𝑏𝑛 ]

This means that we have created a vector that has one row and columns. In order to access individual
elements of this vector, we use MATLAB’s subscript notation. This notation has the form 𝑏(𝑗), that is 𝑏(𝑗),

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
corresponds to 𝑏𝑗 , where 𝑗 = 1,2, ⋯ 𝑛 is the 𝑗th location in the vector 𝑏. Thus, if we write 𝑏(3), then
MATLAB will return 𝑏3 the numerical value assigned to , the third element of the vector. However,
MATLAB’s interpreter is smart enough to know that the matrix 𝑏 is a (1 × 𝑛) matrix, and in some sense,
it ignores the double subscript requirement. That is, writing 𝑏(3), where is a vector defined above, is the
same as writing it as 𝑏(1,3). However, if one were to either directly or implicitly require 𝑏(3,1), then an
error message would appear because this row (the third row) hasn’t been defined (created).

Conversely, if we let

𝑏 = [𝑏1 𝑏2 ⋯ 𝑏𝑛 ]′

we have created a column vector, that is, a (𝑛 × 1) matrix. If we want to locate the third element of this
vector, then we again write 𝑏(3) and MATLAB returns the numerical value corresponding to 𝑏3 . This is
the same as having written 𝑏(3,1). If one were to either directly or implicitly require 𝑏(1,3), then an error
message would appear because this column (the third column) hasn’t been defined (created).

The elements of a vector can also be accessed using subscript colon notation. In this case, the subscript
colon notation is a shorthand method of accessing a group of elements. For example, for a vector with
elements, one can access a group of them using the notation

𝑏(𝑘: 𝑑: 𝑚)

where 1 ≤ 𝑘 < 𝑚 ≤ 𝑛, 𝑘 and 𝑚 are positive integers and, in this case 𝑑 is a positive integer. Thus if one
has ten elements in a vector 𝑏, the third through seventh elements are selected by using

𝑏(3: 7)

Command Output (Results)


x=
x = [-2,1:2:9, 10]
-2 1 3 5 7 9 10

x = [-2,1:2:9, 10];
xlast=
xlast = x(end)
10
x = [-2,1:2:9, 10]; z=
z = x-1 -3 0 2 4 6 8 9
x = [-2,1:2:9, 10];
x=
x(2) = (2)/2;
x -2.000 0.5000 3.0000 5.0000 7.0000 9.0000 10.0000
y = [-1 6 15 -7 31 2 -
x=
4, -5];
x = y(3:5) 15 -7 31
y = [-1 6 15 -7 31 2 -
x=
4, -5];
x = [y(1) y(2) y(7)] -1 6 -4
y = [-1 6 15 -7 31 2 -
x=
4, -5];
index = [1 2 8]; -1 6 -4

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
x = y(index)
y = [-1 6 15 -7 31 2 -
x=
4, -5];
x = y([1 2 8]) -1 6 -4
x = linespace(-pi, pi, y =
10); -0.0000 -0.6428 -0.8660 -0.3420 0.3420 0.8550 0.9848 0.6428
y = sin(x) 0.0000

CREATION OF MATRICES
Consider the following (4 × 3) matrix:
𝑎11 𝑎12 𝑎13
𝑎21 𝑎22 𝑎23
𝐴 = [𝑎 𝑎32 𝑎33 ] → (4 × 3)
31
𝑎41 𝑎42 𝑎43

This matrix can be created several ways, the basic syntax to create a matric is

𝐴 = [𝑎11 𝑎12 𝑎13 ; 𝑎21 𝑎22 𝑎23 ; 𝑎31 𝑎32 𝑎33 ; 𝑎41 𝑎42 𝑎43 ]

where the semicolons are used to indicate the end of a row. Each row must have the same number of
columns. If a more readable presentation is desired, then one can use the form

𝐴 = [𝑎11 𝑎12 𝑎13 ; ⋯

𝑎21 𝑎22 𝑎23 ; ⋯


𝑎31 𝑎32 𝑎33 ; ⋯
𝑎41 𝑎42 𝑎43 ]

where the ellipses (⋯ )are required to indicate that the expression continues on the next line. One can
omit the ellipsis (⋯ ) and instead use the Enter key to indicate the end of a row. In this case, the expression
will look like

𝐴 = [𝑎11 𝑎12 𝑎13 ;


𝑎21 𝑎22 𝑎23 ;
𝑎31 𝑎32 𝑎33 ;
𝑎41 𝑎42 𝑎43 ]

A fourth way is to create four separate row vectors, each with the same number of columns, and then
combine these vectors to form the matrix. In this case, we have

𝑣1 = [𝑎11 𝑎12 𝑎13 ];

𝑣2 = [𝑎21 𝑎22 𝑎23 ];


𝑣3 = [𝑎31 𝑎32 𝑎33 ];

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
𝑣4 = [𝑎41 𝑎42 𝑎43 ];

𝐴 = [𝑣1 ; 𝑣2 ; 𝑣3 ; 𝑣4 ]
where the semicolons in the first four lines are used to suppress display to the command window. The
fourth form is infrequently used.

In all the forms above, the 𝑎𝑖𝑗 are numbers, variable names, expressions, or strings. If they are either
variable names or expressions, then the variable names or the variable names comprising the expressions
must have been assigned numerical values, either by the user or from previously executed expressions,
prior to the execution of this statement. Expressions and numbers can appear in any combination. If they
are strings, then the number of characters in each row must be the same.

Command Output (Results)


A = [11,12,13,14;21,22,23,24;31,32,33,34;41,42,43,44]

A = [11:14;21:24;31:34;41:44]
A=
A = [11,12,13,14;… 11 12 13 14
21,22,23,24;…
21 22 23 24
31,32,33,34;…
41,42,43,44] 31 32 33 34
v1 = [11,12,13,14]; 41 42 43 44
v2 = [21,22,23,24];
v3 = [31,32,33,34];
v4 = [41,42,43,44];
A = [v1; v2; v3; v4];
The order of matrix is determined by

[𝑚, 𝑛] = 𝑠𝑖𝑧𝑒(𝐴)
where 𝑚 is the number of rows and is 𝑛 the number of columns. If one were to use the 𝑙𝑒𝑛𝑔𝑡ℎ function
for a matrix quantity, 𝑙𝑒𝑛𝑔𝑡ℎ would return the number of columns in the array. The transpose of a matrix
is obtained by using the apostrophe (′).

Command Output (Results)


A = m=
[11,12,13,14;21,22,23,24;31,32,33,34;41,42,43,44]; 4
[m, n] = size(A) n=
4
A = [1,2,3,4; 5,6,7,8];
L=
L = length(A)
4
A=
11 21 31 41
A =
12 22 32 42
[11,12,13,14;21,22,23,24;31,32,33,34;41,42,43,44]’
13 23 33 43
14 24 34 44

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
Generation of special matrix

Command Comment Output


on =
on = ones(2,5) % creates matrix with all values one 11111
11111
zer =
11
zer = zeros(3,2) % creates null matrix
11
11
A=
a = [4. 9, 1]; 400
% creates diagonal matrix
A = diag(A) 090
001
d=
100
d = eye(3) % creates identity matrix
010
001
Accessing Matrix Elements

Consider the following matrix

A=

3.0000 5.0000 7.0000 9.0000 11.0000

20.0000 20.2500 20.5000 20.7500 21.0000

1.0000 1.0000 1.0000 1.0000 1.0000

Command Comment Output


% displays the element in first row and first
A(1,1) 3
column
5.0000
A(:,2) % displays all elements of column 2 20.2500
1.0000
A(1,:) % displays all elements of row 1 3 5 7 9 11
7 9 11
% displays all elements row 1 to 3 and column 3
A(1:3,3:5) 20.50 20.75 21.00
to 5 as submatrix
111
If we consider
𝑎11 𝑎12 𝑎13
𝐴 = [𝑎 𝑎22 𝑎23 ]
21

and
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
𝑏11 𝑏12 𝑏13
𝐵=[ ]
𝑏21 𝑏22 𝑏23

Command Comment Output

C=
C = A±B % Addition or Subtraction 𝑎11 ± 𝑏11 𝑎12 ± 𝑏12 𝑎13 ± 𝑏13
𝑎21 ± 𝑏21 𝑎22 ± 𝑏22 𝑎23 ± 𝑏23
C=
𝑎11 𝑎12 𝑎13 𝑏11 𝑏12 𝑏13
C = [A,B] % Column Augmentation 𝑎21 𝑎22 𝑎23 𝑏21 𝑏22 𝑏23

C=
𝑎11 𝑎12 𝑎13
C = [A;B] % Row Augmentation 𝑎21 𝑎22 𝑎23
𝑏11 𝑏12 𝑏13
𝑏21 𝑏22 𝑏23
Dot Operator

(.) followed by any arithmetic operator (+,-,*,/,^) performs element by element arithmetic
operation. If we consider
𝑎11 𝑎12 𝑎13
𝐴 = [𝑎 𝑎22 𝑎23 ]
21

and
𝑏11 𝑏12 𝑏13
𝐵=[ ]
𝑏21 𝑏22 𝑏23

Command Comment Output

C=
C = A.*B % Element multiplication 𝑎11 ∗ 𝑏11 𝑎12 ∗ 𝑏12 𝑎13 ∗ 𝑏13
𝑎21 ∗ 𝑏21 𝑎22 ∗ 𝑏22 𝑎23 ∗ 𝑏23
C=
C = A/B % Element Division 𝑎11 /𝑏11 𝑎12 /𝑏12 𝑎13 /𝑏13
𝑎21 /𝑏21 𝑎22 /𝑏22 𝑎23 /𝑏23
C=
C = A^B % Row Augmentation 𝑎11 ^𝑏11 𝑎12 ^𝑏12 𝑎13 ^𝑏13
𝑎21 ^𝑏21 𝑎22 ^𝑏22 𝑎23 ^𝑏23

MATHEMATICAL OPERATIONS WITH MATRICES


We now define several fundamental matrix operations: addition, subtraction, multiplication, inversion,
determinants, solutions of systems of equations, and roots (eigenvalues).These results are then used to
obtain numerical solutions to classes of engineering problems.
Addition and Subtraction
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
If we have two matrices 𝐴 and 𝐵, each of the order (𝑚 × 𝑛), then
𝑎11 + 𝑏11 𝑎12 + 𝑏12 ⋯ 𝑎1𝑛 + 𝑏1𝑛
𝑎 + 𝑏21 𝑎22 + 𝑏22 ⋯ 𝑎2𝑛 + 𝑏2𝑛
𝐴 ± 𝐵 = [ 21 ] → (𝑚 × 𝑛)
⋮ ⋮ ⋱ ⋮
𝑎𝑚1 + 𝑏𝑚1 𝑎𝑚2 + 𝑏𝑚2 ⋯ 𝑎𝑚𝑛 + 𝑏𝑚𝑛
Multiplication

If we have a matrix 𝐴 in order (𝑚 × 𝑘) and a matrix 𝐵 in order (𝑘 × 𝑛), then


𝑐11 𝑐12 ⋯ 𝑐1𝑛
𝑐21 𝑐22 ⋯ 𝑐2𝑛
𝐶 = 𝐴𝐵 = [ ⋮ ⋮ ⋱ ⋮ ] → (𝑚 × 𝑛)
𝑐𝑚1 𝑐𝑚2 ⋯ 𝑐𝑚𝑛

where
𝑘

𝑐𝑙𝑝 = ∑ 𝑎𝑙𝑗 𝑏𝑗𝑝


𝑗=1

and is of order (𝑚 × 𝑛). Notice that the product of two matrices is defined only when the adjacent
integers of their respective orders are equal; 𝑘 in this case. In other words, (𝑚 × 𝑘)(𝑘 × 𝑛), where the
notation indicates that we have summed terms as indicated in Eq. The MATLAB expression for matrix
multiplication is

𝐶 =𝐴∗𝐵
Example 1:

If we have a vector of radial value 𝑟 = [𝑟1 𝑟2 ⋯ 𝑟𝑚 ] and a vector of angular values =


[𝜃1 𝜃2 ⋯ 𝜃𝑛 ] , then calculate corresponding Cartesian coordinates.

Solution:

The transformation from polar coordinates to Cartesian coordinates can be calculated as shown in Figure
1.

Figure 1 Transformation from polar coordinates to Cartesian coordinates

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
that is

𝑥 = cos 𝜃
𝑦 = sin 𝜃
Hence,
𝑟1
𝑟2
𝑋 = 𝑟′ ∗ cos(𝜃) = [ ⋮ ] [𝜃1 𝜃2 ⋯ 𝜃𝑛 ]
𝑟𝑚
𝑟1 cos 𝜃1 𝑟1 cos 𝜃2 ⋯ 𝑟1 cos 𝜃𝑛
𝑟 cos 𝜃1 𝑟2 cos 𝜃2 ⋯ 𝑟2 cos 𝜃𝑛
=[ 2 ]
⋮ ⋮ ⋱ ⋮
𝑟𝑚 cos 𝜃1 𝑟𝑚 cos 𝜃2 ⋯ 𝑟𝑚 cos 𝜃𝑛
and
𝑟1
𝑟2
𝑋 = 𝑟′ ∗ sin(𝜃) = [ ⋮ ] [𝜃1 𝜃2 ⋯ 𝜃𝑛 ]
𝑟𝑚
𝑟1 sin 𝜃1 𝑟1 sin 𝜃2 ⋯ 𝑟1 sin 𝜃𝑛
𝑟 sin 𝜃1 𝑟2 sin 𝜃2 ⋯ 𝑟2 sin 𝜃𝑛
=[ 2 ]
⋮ ⋮ ⋱ ⋮
𝑟𝑚 sin 𝜃1 𝑟𝑚 sin 𝜃2 ⋯ 𝑟𝑚 sin 𝜃𝑛
Thus, we have mapped the polar coordinates into their Cartesian counterparts. This procedure is very
useful in plotting results.

Determinants

A determinant of a matrix of order (𝑛 × 𝑛) is represented symbolically as


𝑎11 𝑎12 ⋯ 𝑎1𝑛
𝑎21 𝑎22 ⋯ 𝑎2𝑛
|𝐴| = | ⋮ ⋮ ⋱ ⋮ |
𝑎𝑛1 𝑎𝑛2 ⋯ 𝑎𝑛𝑛

For 𝑛 = 2

|𝐴| = 𝑎11 𝑎22 − 𝑎12 𝑎21

For 𝑛 = 3
|𝐴| = 𝑎11 𝑎22 𝑎33 + 𝑎12 𝑎23 𝑎31 + 𝑎13 𝑎21 𝑎32 − 𝑎13 𝑎22 𝑎31 − 𝑎11 𝑎23 𝑎32 − 𝑎12 𝑎21 𝑎33

The MATLAB expression for the determinant is


Det(a)

Command Output (Results)


ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
A = [1,3:4,2];
d=
d = det(A)
-10

Matric Inverse

The inverse of a square matrix 𝐴 is an operation such that

𝐴−1 𝐴 = 𝐴𝐴−1 = 𝐼
provided that 𝐴 is not singular, that is, its determinant is not equal to zero(|𝐴| ≠ 0). The quantity 𝐼 is the
identity matrix. The superscript “-1” denotes the inverse. The expression for obtaining the inverse of
matrix A is either
inv(A)

or
A^-1
1
It is important to note that 𝐴 ≠ 𝐴−1 ; 1/𝐴 will cause the system to respond with an error message.
However, 1./𝐴 is a valid expression, but it is not, in general, equal to 𝐴−1 .

Solution of a System of Equations

Consider the following system of 𝑛 equations and 𝑛 unknowns 𝑥𝑘 , where 𝑘 = 1,2, ⋯ , 𝑛

𝑎11 𝑥1 + 𝑎12 𝑥2 + ⋯ + 𝑎1𝑛 𝑥𝑛 = 𝑏1


𝑎21 𝑥1 + 𝑎22 𝑥2 + ⋯ + 𝑎2𝑛 𝑥𝑛 = 𝑏2

𝑎𝑛1 𝑥1 + 𝑎𝑛2 𝑥2 + ⋯ + 𝑎𝑛𝑛 𝑥𝑛 = 𝑏𝑛
We can rewrite this system of equation in matrix notation as follows:

𝐴𝑥 = 𝑏
where 𝐴 is (𝑛 × 𝑛) matrix
𝑎11 𝑎12 ⋯ 𝑎1𝑛
𝑎21 𝑎22 ⋯ 𝑎2𝑛
𝐴=[ ⋮ ⋮ ⋱ ⋮ ] → (𝑛 × 𝑛)
𝑎𝑛1 𝑎𝑛2 ⋯ 𝑎𝑛𝑛

and 𝑥 and 𝑏 are, respectively, the (𝑛 × 1) column vectors


𝑥1 𝑏1
𝑥2 𝑏2
𝑥 = [ ⋮ ] → (𝑛 × 1) 𝑎𝑛𝑑 𝑏 = [ ] → (𝑛 × 1)

𝑥𝑛 𝑏𝑛
The solution is obtained by premultiplying both sides of the matrix equation by 𝐴−1 . Thus,

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
𝐴−1 𝐴𝑥 = 𝐴−1 𝑏

𝑥 = 𝐴−1 𝑏

since 𝐴−1 𝐴 = 𝐼, the identity matrix, and 𝐼𝑥 = 𝑥. The preferred expression for solving this system of
equations is

x = A\b

or

x = linsolve(A,b)

where the backslash operator indicates matrix division and is referred to by MATLAB as left matrix
divide. Left division uses a procedure that is more numerically stable when compared to the methods
used for either of the following alternative notations:

x = A^-1*b

or

x = inv(A)*b

For large matrices, these two alternative expressions execute considerably slower than when the
backslash operator is used.
Table 1List of function introduced in this note

MATLAB function Description


det Determinant of a square matrix
diag Diagonal of a square matrix, creates a diagonal matirx
dot Dot product of two vectors
end Last index in an array
eye Creates the identity matrix
inv Inverse of a square matrix
length Length of a vector
linsolve Solves linear system of equation
linspace Creates equally spaced elements of a vector
logspace Creates equally spaced elements of a vector on log10 𝑥 scale
magic Creates a square matrix whose sum of each row, each columns, and
diagonals is equal
max Determines the maximum value in an array
min Determines the minimum value in an array
ones Creates an array whose elements equal 1
size Size of an array
sort Sorts elements of an array in ascending order
sum Sums elements of an array
zeros Creates an array whose elements equal 0

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
Exercises:
Write MATLAB code/s for the following:
1. Create two vectors, on whose elements are 𝑎𝑛 = 2𝑛 − 1 and other whose elements are 𝑏𝑛 =
2𝑛 + 1, 𝑛 = 0,1, … , 𝑛. Determine the following:
a. 𝑎 + 𝑏
b. 𝑎 − 𝑏
c. 𝑎’𝑏
d. determinant of 𝑎’𝑏
e. ab’
2.
a. Create a vector of eight values that are equally spaced on a logarithm scale. The first value of the
vector is 6 and the last value is 106.
b. Display the value of the fifth element of the vector created in (a).
c. Create a new vector whose elements are the first, third, fifth, and seventh elements of the vector
created in (a).
3. Let z = magic(5)
a. Perform the following operations to 𝑧 in the order given:
i. Divide column 2 by √3.
ii. Add the elements of the third row to the elements in the fifth row (the third row remains
unchanged).
iii. Multiply the elements of the first column by the corresponding elements of the fourth
column and place the result in the first column.
iv. Set the diagonal elements to 2.
b. If the result obtained in (a) (iv) is denoted as 𝑞, then obtain the diagonal of 𝑞𝑞’.
c. Determine the maximum and minimum values of 𝑞.
4. A matrix is an orthogonal matrix if

𝑋′𝑋 = 𝐼

and, therefore (𝑋 ′ 𝑋)−1 = 𝐼. Show that each of the following matrices is orthogonal

−1 −1 1 −1 −1 1
1 1 −1 1 1 1 −1 −1
𝑤= [ ] 𝑞= [ ]
2 −1 1 2 1 −1 1 −1
1 1 1 1 1 1
5. Given the following matrix:

2 −2 −4
𝐴 = [−1 3 4]
1 −2 −2
Show that 𝐴2 − 𝐴 = 0.

6. If

1 2 2
𝐴 = [2 1 2]
2 2 1
Show that 𝐴2 − 4𝐴 − 5𝐼 = 0, where 𝐼 is the identity matrix.

7. If

1 −1 1 1
𝐴=[ ] 𝑎𝑛𝑑 𝐵 = [ ]
2 −1 4 −1

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
Show that (𝐴 + 𝐵)2 = 𝐴2 + 𝐵2 .

Given the two matrices

1 3 4 11 34 6
𝐴 = [31 67 9] 𝑎𝑛𝑑 𝐵 = [ 7 13 8 ]
7 5 9 43 10 53

ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel

You might also like