Professional Documents
Culture Documents
CHAPTER 2
VECTORS and MATRICES
Chapter 2 Objective
Introduce MATLAB syntax in the context of
vectors and matrices and their manipulation.
Topics
Square Matrix: n = m
a11 0 ... 0
0 a22
A= (n n)
... ...
0 ann
1 0 ... 0
0 1
I=
... ...
0 1
Creation of Vectors
If a, x, b, are either variable names, numbers,
expressions, or strings, then vectors are expressed as
either
f = [a x b ] (Blanks required)
or
f = [a, x, b, ] (Blanks optional)
Example
Create a row vector that goes from 0.2 to 1.0 in
increments of 0.1.
x = 0.2:0.1:1
n = length(x)
When executed, we obtain
x=
0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00
n= x=
0.2000
9 0.3000
0.4000
To create a column vector, we use 0.5000
0.6000
x = (0.2:0.1:1) 0.7000
n = length(x) 0.8000
0.9000
1.0000
n=
9
Examples -
x = linspace(0, 2, 5)
gives
x=
0 0.5000 1.0000 1.5000 2.0000
and
x = logspace(0, 2, 5) % [100, 100.5, 101, 101.5, 102]
gives
x=
1.0000 3.1623 10.0000 31.6228 100.0000
Example
x = [-2, 1, 3, 5, 7, 9, 10];
b = x(4)
c = x(6)
xlast = x(end)
When executed, we obtain
b=
5
c=
9
xlast =
10
Example
Consider the following script
z = [-2, 1, 3, 5, 7, 9, 10];
z(2) = z(2)/2;
z(3:4) = z(3:4)*3-1;
z
When executed, we obtain
z=
-2.00 0.50 8.00 14.00 7.00 9.00 10.00
Example
Consider the script
y = [-1, 6, 15, -7, 31, 2, -4, -5];
x = y(3:5)
whose execution creates the three-element vector
x=
15 -7 31
Example
We shall show that there are several ways to create a
vector x that is composed of the first two and the last two
elements of a vector y. Thus,
y = [-1, 6, 15, -7, 31, 2, -4, -5];
x = [y(1), y(2), y(7), y(8)]
or
y = [-1, 6, 15, -7, 31, 2, -4, -5];
index = [1, 2, 7, 8]; % or [1:2, length(y)-1, length(y)]
x = y(index)
or more compactly
y = [-1, 6, 15, -7, 31, 2, -4, -5];
x = y([1, 2, 7, 8]) % or y([1:2, length(y)-1, length(y)])
Example
y = [-1, 6, 15, -7, 31, 2, -4, -5];
z = [10, 20, 30, 40, 50, 60, 70, 80];
[ynew, indx] = sort(y, 'ascend')
znew = z(indx)
when executed, gives
ynew =
-7 -5 -4 -1 2 6 15 31
indx =
4 8 7 1 6 2 3 5
znew =
40 80 70 10 60 20 30 50
Example
y = [-1, 6, 15, -7, 31, 2, -4, -5];
indxx = find(y<=0)
s = y(indxx)
Upon execution, we obtain
indxx =
1 4 7 8
s=
-1 -7 -4 -5
Example
x = linspace(-pi, pi, 10);
y = sin(x);
[ymax, kmax] = max(y)
[ymin, kmin] = min(y)
Upon execution, we find that
ymax =
0.9848
kmax =
8 y=
-0.0000 -0.6428 -0.9848 -0.8660 -0.3420
ymin =
0.3420 0.8660 0.9848 0.6428 0.0000
-0.9848
kmin =
3
We shall
(a) determine the time at which the minimum
positive value of f(t) occurs
(b) the average value of its negative values
The script is
-0.6237
0.5
tMinValuef =
3.0775
0
-0.5
-1
0 1 2 3 4 5 6
Creation of Matrices
The basic syntax to create a matrix is
A = [a11 a12 a13; a21 a22 a23; a31 a32 a33; a41 a42 a43]
where the semicolons are used to indicate the end of a
row and the aij can be numbers, variable names,
expressions, or strings.
Alternate ways are:
A = [a11 a12 a13; ...
a21 a22 a23; ...
a31 a32 a33; ...
a41 a42 a43]
where the ellipses (...) are required to indicate that the
expression continues on the next line.
Copyright Edward B. Magrab 2009 33
An Engineers Guide to MATLAB Chapter 2
Special Matrices
A matrix of all 1s
ones(r, c) on(1:r,1:c) = 1 on = ones(2, 5)
on =
1 1 1 1 1
1 1 1 1 1
A null matrix
zeros(r, c) zer(1:r,1:c) = 0
zer = zeros(3, 2)
zer =
0 0
0 0
0 0
Diagonal matrix
a = [4, 9, 1];
Create an (nn) diagonal matrix A = diag(a)
whose diagonal elements are a A=
vector a of length n 4 0 0
0 9 0
diag(a)
0 0 1
Extract the diagonal elements of
a square matrix A Ad = diag([11, 12, 13, 14;
diag(A) 21, 22, 23, 24;
31, 32, 33, 34;
41, 42, 43, 44])
Ad =
11
22
33
44
Copyright Edward B. Magrab 2009 38
An Engineers Guide to MATLAB Chapter 2
: means all
B = A(1:3,3:5)
elements of
B=
column 2
7.0000 9.0000 11.0000
20.5000 20.7500 21.0000
1.0000 1.0000 1.0000
Example
We shall set all the diagonal elements of magic(4) to
zero; thus
diag(Z) = diag(diag(Z)) =
Z = magic(4); 16 16 0 0 0
Z = Z-diag(diag(Z)) 11 0 11 0 0
which results in 6 0 0 6 0
1 0 0 0 1
Z=
0 2 3 13
5 0 10 8 magic(4) =
9 7 0 12 16 2 3 13
4 14 15 0 5 11 10 8
9 7 6 12
4 14 15 1
Example
We shall replace all the diagonal elements of magic(4)
with the value 5; thus,
Z = magic(4);
Z = Z-diag(diag(Z))+5*eye(4)
which results in magic(4) =
Z= 16 2 3 13
5 2 3 13 5 11 10 8
5 5 10 8 9 7 6 12
9 7 5 12 4 14 15 1
4 14 15 5
0 1 1 0 2 2 0 3 3
1 0 1 2 0 2 3 0 3
1 1 0 2 2 0 3 3 0
0 4 4 0 5 5 0 6 6
A 4 0 4 5 0 5 6 0 6
4 4 0 5 5 0 6 6 0
0 7 7 0 8 8 0 9 9
7 0 7 8 0 8 9 0 9
7 7 0 8 8 0 9 9 0
The script is
a = ones(3, 3)-eye(3);
A = [a, 2*a, 3*a; 4*a, 5*a, 6*a; 7*a, 8*a, 9*a;]
Upon execution, we obtain
A=
0 1 1 0 2 2 0 3 3
1 0 1 2 0 2 3 0 3
1 1 0 2 2 0 3 3 0
0 4 4 0 5 5 0 6 6
4 0 4 5 0 5 6 0 6
4 4 0 5 5 0 6 6 0
0 7 7 0 8 8 0 9 9
7 0 7 8 0 8 9 0 9
7 7 0 8 8 0 9 9 0
The script is
Matr = [1:9; 10:18; 19:27; 28:36; 37:45; ...
46:54; 55:63; 64:72; 73:81];
Temp1 = Matr(1:3, 1:3);
Matr(1:3, 1:3) = Matr(7:9, 7:9);
Matr(7:9, 7:9) = Temp1;
Temp1 = Matr(1:3, 7:9);
Matr(1:3, 7:9) = Matr(7:9, 1:3);
Matr(7:9, 1:3) = Temp1;
Matr
Manipulation of Arrays
Two functions that create matrices by replicating a
specified number of times either a scalar, a column or row
vector, or a matrix are
repmat
and
meshgrid
which uses repmat. The general form of repmat is
repmat(x, r, c)
where x is either a scalar, vector, or matrix, r is the total
number of rows of x that will be replicated, and c is the
total number of columns of x will be replicated.
Example
We shall produce the following matrix three ways
W=
45.7200 45.7200 45.7200
45.7200 45.7200 45.7200
45.7200 45.7200 45.7200
Thus,
W = repmat(45.72, 3, 3) % Function
or
W = [45.72, 45.72, 45.72; % Hard code
45.72, 45.72, 45.72;
45.72, 45.72, 45.72]
or
W(1:3,1:3) = 45.72 % Subscript colon notation
Example
Consider the vector
s = [a1 a2 a3 a4]
The expression
V = repmat(s, 3, 1)
creates the numerical equivalent of the matrix
a1 a2 a3 a4
V = a1 a2 a3 a4
Replicated
a1 a2 a3 a4 row
whereas
repmat(s, 3, 2)
creates the numerical equivalent of the matrix
a1 a2 a3 a4 a1 a2 a3 a4
V = a1 a2 a3 a4 a1 a2 a3 a4
a1 a2 a3 a4 a1 a2 a3 a4
Replicated
1st row
Replicated
column
a1 a1 a1
a a2 a2
V = 2
a3 a3 a3
a4 a4 a4
Replicated
1st column
and
V = repmat(s', 2, 3)
gives the numerical equivalent of the matrix
a1 a1 a1
a a2 a2
2
a3 a3 a3
a4 a4 a4
V =
a1 a1 a1
a2 a2 a2
a a3 a3
3
a4 a4 a4
meshgrid
If we have two row vectors s and t, then
[U, V] = meshgrid(s, t)
gives the same result as that produced by the two
commands
U = repmat(s, length(t), 1)
V = repmat(t', 1, length(s)) % Notice transpose
In either case, U and V are each matrices of order
(length(t)length(s))
Example
If
s = [s1 s2 s3 s4] % (14)
t = [t1 t2 t3] % (13)
then
[U, V] = meshgrid(s, t) % Sizes of U and V are (34)
produces the numerical equivalent of two (34) matrices
s1 s2 s3 s4 t1 t1 t1 t1
U = s1 s2 s3 s4 V = t 2 t2 t2 t 2
s1 s2 s3 s4 t 3 t3 t3 t 3
Example However,
u = [1, 2, 3, 4];
u = [1, 2, 3, 4]; v = [5, 6, 7];
v = [5, 6, 7]; [V, U] = meshgrid(v, u)
[U, V] = meshgrid(u, v) gives
Upon execution, we obtain V=
U= 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
V= U=
5 5 5 5 1 1 1
6 6 6 6 2 2 2
7 7 7 7 3 3 3
4 4 4
Copyright Edward B. Magrab 2009 61
An Engineers Guide to MATLAB Chapter 2
Then
a a14 a13 a12 a11
fliplr( A) = 15 (2 5)
a25 a24 a23 a22 a21
a a22 a23 a24 a25
flipud( A) = 21 (2 5)
a11 a12 a13 a14 a15
a a a a a
A = 11 12 13 14 15 (25)
and a21 a22 a23 a24 a25
Clarification of Notation
Consider the following two (25) matrices A and B
a a12 a13 a14 a15 b b12 b13 b14 b15
A 11 B 11
a21 a22 a23 a24 a25 b21 b22 b23 b24 b25
Addition or subtraction: C = A B
a11 b11 a12 b12 a13 b13 a14 b14 a15 b15
C (2 5)
a21 b21 a22 b22 a23 b23 a24 b24 a25 b25
Furthermore, if
x = [x1 x2 x3]
y = [y1 y2 y3]
then either x1 y1
Z = [x', y'] or Z = [x; y]' gives Z x2 y2 (2 3)
x3 y3
x1
x
whereas 2
x3
Z = [x'; y'] yields Z (61)
y1
y2
y3
Dot Operations
A means of performing, on matrices of the same order,
arithmetic operations on an element-by-element basis.
Consider the following (34) matrices
x11 x12 x13 x14
X x21 x22 x23 x24
x31 x32 x33 x34
which is equivalent to
(tan(a11 ) - g11 * (b11 /c11 )^ d11 )^ 2 (tan(a12 ) - g12 * (b12 /c12 )^ d12 )^ 2
Z (tan(a21 ) - g21 * (b21 /c21 )^ d 21 )^ 2 (tan(a22 ) - g22 * (b22 /c22 )^ d 22 )^ 2
(tan(a31 ) - g31 * (b31 /c31 )^ d 31 )^ 2 (tan(a32 ) - g32 * (b32 /c32 )^ d 32 )^ 2
Example
Evaluate
sin(b1t c1 )
v e a1t
t c1
for 6 equally-spaced values of t in the interval 0 t 1
when a1 = 0.2, b1 = 0.9, and c1 = /6. The script is
a1 = 0.2; b1 = 0.9; c1 = pi/6;
t = linspace(0, 1, 6);
v = exp(-a1*t).*sin(b1*t+c1)./(t+c1)
Upon execution, we obtain
v=
0.9549 0.8590 0.7726 0.6900 0.6097 0.5316
Example
Create the elements of an (NN) matrix H when each
element is given by
1
hmn m, n 1, 2,...N
m n 1
The script to generate this array for N = 4 is
N = 4;
mm = 1:N; nn = mm;
[n, m] = meshgrid(nn, mm)
h = 1./(m+n-1)
Upon execution, we get h=
1.0000 0.5000 0.3333 0.2500
0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0.1667
0.2500 0.2000 0.1667 0.1429
Copyright Edward B. Magrab 2009 73
An Engineers Guide to MATLAB Chapter 2
s1 * t1 s2 * t1 s3 * t1 s4 * t1
Z s1 * t 2 s2 * t 2 s3 * t 2 s4 * t 2
s1 * t 3 s2 * t 3 s3 * t 3 s4 * t 3
Thus, we have the product of all combinations of the
elements of vectors s and t.
where S a scalar.
If Z is a (34) matrix with elements zij, then
3 3 3
3
S sum ( Z ) zn1 , zn 2 , zn 3 , zn4 (1 4)
n =1 n =1 n =1 n =1
To sum all the elements in an array, we use
3 3 3 3 4 3
S sum (sum ( Z )) zn1 + zn 2 + zn 3 + zn4 zni (11)
n =1 n =1 n =1 n =1 i =1 n =1
Example
Evaluate
4
z mm
m =1
Thus,
m = 1:4;
z = sum(m.^m)
which, when executed, gives
z=
288
Example
Consider the convergence of the series
N=10
1
S
n =1 n 2
N
Thus,
S = cumsum(1./(1:10).^2)
which, upon execution, gives
S=
1.0000 1.2500 1.3611 1.4236 1.4636 1.4914
1.5118 1.5274 1.5398 1.5498
Example
We shall evaluate the following series expression for the
hyperbolic secant for N = 305 and for five equally spaced
values of x from 0 x 2 and compare the results to the
exact values.
N
n(-1)( n-1)/2
sech x 4
n =1,3,5 n + 4 x
2 2
Thus,
nn = 1:2:305; % (1153)
xx = linspace(0, 2, 5); % (15)
[x, n] = meshgrid(xx, nn); % (1535)
s = 4*pi*sum(n.*(-1).^((n-1)/2)./
((pi*n).^2+4*x.^2)); % (15)
se = sech(xx); % (15)
compare = [xx ' s' se' (100*(s-se)./se)'] % (52)
Copyright Edward B. Magrab 2009 80
An Engineers Guide to MATLAB Chapter 2
Multiplication
If we have an (mk) matrix A and a (kn) matrix B, then
c11 c12 c1n
c c22
C AB 21 ( m n)
cm1 cmn
where
k
clp alj b jp
j 1
Matrix Transpose
It can be shown that if C = AB, then its transpose is
C ( AB) BA
If A is an identity matrix (A = I) and m = n, then
C IB BI B
Example
We shall multiply the following two matrices and show
numerically that the transpose can be obtained either
of the two ways indicated previously.
11 12
11 12 13
A B 21 22
21 22 23 31 32
The script is
A = [11, 12, 13; 21, 22, 23];
B = [11, 12; 21, 22; 31, 32];
C = A*B
Ctran1 = C'
Ctran2 = B'*A'
where
k
wij w( xi , y j ) fl ( xi ) gl ( y j )
l 1
b1 l =1 and p = 1
b k k
d ab a1 a2 ... ak a j b j = a j b j (11)
1
... j =1 j =1
bk
and
r1
r
Y r * sin( ) 2 sin1 sin2 ... sinn
...
rm
r1sin1 r1sin2 ... r1sinn
r sin r sin
2 1 2 2
... ...
mr sin 1 ... rm sin n
l=1
p = 1, 2, , n
Then, r = pV gives
m m m
r pV pk hk ( x1 ) pk hk ( x2 ) ... p h
k k ( x n (1 n)
)
k =1 k =1 k =1
hk ( xi ) hk ( i ) cos(2k )
The program is
k = 1:150; % (1150)
tau = linspace(-0.5, 0.5, 100); % (1100)
sk = sin(pi*k/4)./(pi*k/4); % (1150)
cntau = cos(2*pi*k'*tau); % (150100)
f = 0.25*(1+2*sk*cntau); % (1150) (150100) (1100)
plot(tau, f)
Example -
N
1 cos(n )
u( , ) 4 e n sin(n )
n
3
n 1
1 cos n
n 3
en sin n
(1 N ) (1 Ne )
(1 N ) (1 N x )
(1 N ) [Not permissible] [Not permissible]
1 cos n
n 3
en sin n
( N Ne )
(N Nx )
[dot multiplication]
n = (1:25)*pi; % (125)
eta = 0:0.025:1; % (141)
xi = 0:0.05:0.7; % (115)
[X1, temp1] = meshgrid(xi, (1-cos(n))./n.^3); % (2515)
temp2 = exp(-n'*xi); % (2515)
Rnx = temp1.*temp2; % (2515)
temp3 = sin(n'*eta); % (2541)
u = 4*Rnx'*temp3; % (1541)
mesh(eta, xi, u)
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
0.8
0.6 1
0.8
0.4
0.6
0.2 0.4
0.2
0 0
Determinants
A determinant of an array A of order (nn) is represented
symbolically as
a11 a12 ... a1n
a21 a22 ...
A
... ...
a n1 ... ann
For n = 2:
A a11a22 a12 a21
For n = 3:
A a11a22 a33 + a12 a23 a31 + a13 a21a32 a13 a22 a31
a11a23 a32 a12 a21a33
Example
If A is defined as
1 3
A
4 2
then the determinant of A is obtained from
A = [1, 3; 4, 2];
d = det(A)
which, upon execution, gives
d=
-10
Copyright Edward B. Magrab 2009 105
An Engineers Guide to MATLAB Chapter 2
and j = j
A B 0
The program is
K = [50, -30, 0; -30, 70, -40; 0, -40, 50];
M = diag([3, 1.4, 5]);
w = sqrt(eig(K, M))
which, upon execution, gives
w=
1.6734 50 -30 0
3.7772 K -30 70 -40
7.7201 0 -40 50
% rad/s
3 0 0
M 0 1.4 0
0 0 5
Matrix Inverse
The inverse of a square matrix A is an operation
such that
A-1 A = AA-1 = I
provided that A is not singular, that is, its determinant is
not equal to zero (|A| 0).
The expression for obtaining the inverse of matrix A is
either
inv(A)
or
A^-1
Note: 1/A A^-1; 1/A will cause the system to respond
with an error message. However, 1./A is valid, but it is
not the inverse.
Copyright Edward B. Magrab 2009 109
An Engineers Guide to MATLAB Chapter 2
x = linsolve(A, b)
Example
Consider the following system of equations
8 x1 + x2 + 6 x3 = 7.5
3 x1 + 5 x2 + 7 x3 = 4
4 x1 + 9 x2 + 2 x3 = 12
which, in matrix notation, is
8 1 6 x1 7.5
3 5 7 x 4
2
4 9 2 x3 12
We shall determine xk and verify the results.
A = [8, 1, 6; 3, 5, 7; 4, 9, 2];
b = [7.5, 4, 12]'; 8 1 6 x1 7.5
x = A\b 3 5 7 x 4
2
z = A*x 4 9 2 x3 12
which, upon execution, gives
x=
1.2931
0.8972
-0.6236
z=
7.5000
4.0000
12.0000